Azure Monitor Query client library Python samples
Samples
The following code samples show common scenarios with the Azure Monitor Query client library.
For examples on authenticating with the Azure Monitor service, see sample_authentication.py and sample_authentication_async.py.
Logs query samples
- Send a single workspace query with LogsQueryClient and handle the response as a table (async sample)
- Send a single workspace query with LogsQueryClient and handle the response in key-value form
- Send a single workspace query with LogsQueryClient without pandas
- Send a single workspace query with LogsQueryClient across multiple workspaces
- Send multiple workspace queries with LogsQueryClient (async sample)
- Send a single workspace query with LogsQueryClient using server timeout
- Send a single resource query with LogsQueryClient (async sample)
Notebook samples
- Split a large query into multiple smaller queries to avoid hitting service limits
- Detect anomalies in Azure Monitor log data using machine learning techniques
Prerequisites
- Python 3.9 or later
- An Azure subscription
- To query Logs, you need an Azure Log Analytics workspace.
Setup
- Install the latest version of the Azure Monitor Query library:
pip install azure-monitor-query
- Clone or download this sample repository.
- Open the samples folder in Visual Studio Code or your IDE of choice.
- To run most of this samples, you need
azure-identityandpandas. Although, those dependencies are optional and can be replaced.
pip install azure-identity pandas
- To run the async samples, you need an asynchronous HTTP framework like
aiohttp:
pip install aiohttp
Run the samples
- Open a terminal window and
cdto the directory that the samples are saved in. - Set the environment variables specified in the sample file you wish to run.
- Follow the usage described in the file. For example,
python sample_logs_single_query.py.
Next steps
To learn more about Azure Monitor, see the Azure Monitor service documentation.