Azure Monitor Ingestion client library Python samples
This library allows you to send data from virtually any source to supported built-in tables or to custom tables that you create in Log Analytics workspaces. The following code samples show common scenarios with the Azure Monitor Ingestion client library.
File Name | Description |
---|---|
sample_authentication.py and sample_authentication_async.py | Authenticate a client with the public cloud and a sovereign cloud. |
sample_send_small_logs.py and sample_send_small_logs_async.py | Send a small number of logs to a Log Analytics workspace. |
sample_custom_error_callback.py and sample_custom_error_callback_async.py | Use error callbacks to customize how errors are handled during upload. |
sample_upload_file_contents.py and sample_upload_file_contents_async.py | Upload the contents of a file to a Log Analytics workspace. |
sample_upload_pandas_dataframe.py and sample_upload_pandas_dataframe_async.py | Upload data in a pandas DataFrame to a Log Analytics workspace. |
Prerequisites
- Python 3.7 or later
- An Azure subscription
- An Azure Log Analytics workspace
- A Data Collection Endpoint (DCE)
- A Data Collection Rule (DCR)
How to run the samples
Install the dependencies
To run the samples, you need to install the following dependencies:
pip install azure-monitor-ingestion azure-identity pandas
To run the async samples, you need an asynchronous HTTP framework like aiohttp
:
pip install aiohttp
Set up authentication
We use azure-identity's DefaultAzureCredential to authenticate. Ensure that your service principal or managed identity has the Monitoring Metrics Publisher
role assigned on the Data Collection Rule resource. If you are using a service principal, set the following environment variables:
AZURE_TENANT_ID="your Azure AD tenant (directory) ID"
AZURE_CLIENT_ID="your Azure AD client (application) ID"
AZURE_CLIENT_SECRET="your Azure AD client secret"
Set up additional environment variables
Change and set the following environment variables to match your configuration:
DATA_COLLECTION_ENDPOINT="your data collection endpoint"
LOGS_DCR_RULE_ID="your data collection rule immutable ID"
LOGS_DCR_STREAM_NAME="your data collection rule stream name"
Run the samples
Navigate to the directory that the samples are saved in, and follow the usage described in the file. For example, python sample_send_small_logs.py
.
Next steps
To learn more about Azure Monitor, see the Azure Monitor service documentation and the Logs Ingestion API overview.