Skip to content

Commit 3113eaf

Browse files
author
Rakshith Bhyravabhotla
authored
readme + changelog (Azure#19173)
* readme + changelog * Prepare for release * codeowners * Update sdk/monitor/azure-monitor-query/README.md
1 parent e7cf320 commit 3113eaf

File tree

10 files changed

+139
-20
lines changed

10 files changed

+139
-20
lines changed

.github/CODEOWNERS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,9 @@
4949
# PRLabel: %Monitor - Exporter
5050
/sdk/monitor/azure-monitor-opentelemetry-exporter @rakshith91 @lmazuel @lzchen
5151

52+
# PRLabel: %Monitor - Log
53+
/sdk/monitor/azure-monitor-query @rakshith91
54+
5255
# PRLabel: %Consumption
5356
/sdk/consumption/ @sandeepnl
5457

eng/.docsettings.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ known_content_issues:
7070
- ['sdk/core/azure-servicemanagement-legacy/README.md', '#4554']
7171
- ['sdk/eventgrid/azure-eventgrid/README.md', '#4554']
7272
- ['sdk/monitor/azure-monitor-query/README.md', '#4554']
73+
- ['sdk/monitor/azure-monitor-query/samples/README.md', '#4554']
7374
- ['sdk/graphrbac/azure-graphrbac/README.md', '#4554']
7475
- ['sdk/loganalytics/azure-loganalytics/README.md', '#4554']
7576
- ['sdk/servicebus/azure-servicebus/README.md', '#4554']
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
# Release History
22

3-
## 1.0.0b1 (Unreleased)
3+
## 1.0.0b1 (2021-06-10)
44

55
**Features**
66
- Version (1.0.0b1) is the first preview of our efforts to create a user-friendly and Pythonic client library for Azure Monitor Query.
77
For more information about this, and preview releases of other Azure SDK libraries, please visit https://azure.github.io/azure-sdk/releases/latest/python.html.
8-
- Added `LogsQueryClient` to query log analytics.
9-
- Implements the `MetricsQueryClient` for querying metrics, listing namespaces and metric definitions.
8+
- Added `~azure.monitor.query.LogsQueryClient` to query log analytics along with `~azure.monitor.query.aio.LogsQueryClient`.
9+
- Implements the `~azure.monitor.query.MetricsQueryClient` for querying metrics, listing namespaces and metric definitions along with `~azure.monitor.query.aio.MetricsQueryClient`.

sdk/monitor/azure-monitor-query/README.md

Lines changed: 36 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,8 @@ time-stamped data. Each set of metric values is a time series with the following
108108

109109
### Get logs for a query
110110

111+
This sample shows getting a log query. to handle the response and view it in a tabular form, the [pandas](https://pypi.org/project/pandas/) library is used. Please look at the samples if you don't want to use the pandas library.
112+
111113
```Python
112114
import os
113115
import pandas as pd
@@ -130,7 +132,12 @@ where TimeGenerated > ago(12h) |
130132
summarize avgRequestDuration=avg(DurationMs) by bin(TimeGenerated, 10m), _ResourceId"""
131133

132134
# returns LogsQueryResults
133-
response = client.query(os.environ['LOG_WORKSPACE_ID'], query)
135+
response = client.query(
136+
os.environ['LOG_WORKSPACE_ID'],
137+
query,
138+
start_time=datetime(2021, 6, 2),
139+
end_time=datetime.now()
140+
)
134141

135142
if not response.tables:
136143
print("No results for the query")
@@ -142,6 +149,8 @@ for table in response.tables:
142149

143150
### Get Logs for multiple queries
144151

152+
This sample shows sending multiple queries at the same time using batch query API. For each query, a `LogQueryRequest` object can be used. Alternatively, a dictionary can be used as well.
153+
145154
```Python
146155
import os
147156
import pandas as pd
@@ -160,13 +169,14 @@ client = LogsQueryClient(credential)
160169
requests = [
161170
LogsQueryRequest(
162171
query="AzureActivity | summarize count()",
163-
timespan="PT1H",
172+
duration="PT1H",
164173
workspace= os.environ['LOG_WORKSPACE_ID']
165174
),
166175
LogsQueryRequest(
167176
query= """AppRequests | take 10 |
168177
summarize avgRequestDuration=avg(DurationMs) by bin(TimeGenerated, 10m), _ResourceId""",
169-
timespan="PT1H",
178+
duration="PT1H",
179+
start_time=datetime(2021, 6, 2),
170180
workspace= os.environ['LOG_WORKSPACE_ID']
171181
),
172182
LogsQueryRequest(
@@ -188,6 +198,8 @@ for response in response.responses:
188198

189199
### Get logs with server timeout
190200

201+
This sample shows setting a server timeout in seconds. A GateWay timeout is raised if the query takes more time than the mentioned timeout. The default is 180 seconds and can be set uptio 10 minutes (600 seconds).
202+
191203
```Python
192204
import os
193205
import pandas as pd
@@ -205,17 +217,15 @@ client = LogsQueryClient(credential)
205217

206218
response = client.query(
207219
os.environ['LOG_WORKSPACE_ID'],
208-
"Perf | summarize count() by bin(TimeGenerated, 4h) | render barchart title='24H Perf events'",
209-
server_timeout=10,
220+
"range x from 1 to 10000000000 step 1 | count",
221+
server_timeout=1,
210222
)
211-
212-
for table in response.tables:
213-
df = pd.DataFrame(table.rows, columns=[col.name for col in table.columns])
214-
print(df)
215223
```
216224

217225
### Get Metrics
218226

227+
This example shows getting the metrics for an EventGrid subscription. The resource URI is that of an eventgrid topic.
228+
219229
```Python
220230
import os
221231
from azure.monitor.query import MetricsQueryClient
@@ -229,7 +239,20 @@ credential = ClientSecretCredential(
229239
)
230240

231241
client = MetricsQueryClient(credential)
232-
response = client.query(os.environ['METRICS_RESOURCE_URI'], metric_names=["Microsoft.CognitiveServices/accounts"])
242+
243+
metrics_uri = os.environ['METRICS_RESOURCE_URI']
244+
response = client.query(
245+
metrics_uri,
246+
metric_names=["PublishSuccessCount"],
247+
start_time=datetime(2021, 5, 25),
248+
duration='P1D'
249+
)
250+
251+
for metric in response.metrics:
252+
print(metric.name)
253+
for time_series_element in metric.timeseries:
254+
for metric_value in time_series_element.data:
255+
print(metric_value.time_stamp)
233256
```
234257

235258
## Troubleshooting
@@ -268,9 +291,9 @@ This project has adopted the [Microsoft Open Source Code of Conduct][code_of_con
268291

269292
[azure_cli_link]: https://pypi.org/project/azure-cli/
270293
[python-query-src]: https://github.com/Azure/azure-sdk-for-python/blob/master/sdk/monitor/azure-monitor-query/
271-
[python-query-pypi]: https://github.com/Azure/azure-sdk-for-python/blob/master/sdk/monitor/azure-monitor-query/
272-
[python-query-product-docs]: https://docs.microsoft.com/rest/api/monitor/metrics
273-
[python-query-ref-docs]: https://github.com/Azure/azure-sdk-for-python/blob/master/sdk/monitor/azure-monitor-query/
294+
[python-query-pypi]: https://aka.ms/azsdk-python-monitor-query-pypi
295+
[python-query-product-docs]: https://docs.microsoft.com/azure/azure-monitor/
296+
[python-query-ref-docs]: https://docs.microsoft.com/python/api/overview/azure/?view=azure-python
274297
[python-query-samples]: https://github.com/Azure/azure-sdk-for-python/tree/master/sdk/monitor/azure-monitor-query/samples
275298
[python-query-changelog]: https://github.com/Azure/azure-sdk-for-python/tree/master/sdk/monitor/azure-monitor-query/CHANGELOG.md
276299
[pip]: https://pypi.org/project/pip/

sdk/monitor/azure-monitor-query/azure/monitor/query/_log_query_client.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,15 @@ class LogsQueryClient(object):
2525
:type credential: ~azure.core.credentials.TokenCredential
2626
:keyword endpoint: The endpoint to connect to. Defaults to 'https://api.loganalytics.io'.
2727
:paramtype endpoint: str
28+
29+
.. admonition:: Example:
30+
31+
.. literalinclude:: ../samples/sample_log_query_client.py
32+
:start-after: [START client_auth_with_token_cred]
33+
:end-before: [END client_auth_with_token_cred]
34+
:language: python
35+
:dedent: 0
36+
:caption: Creating the LogsQueryClient with a TokenCredential.
2837
"""
2938

3039
def __init__(self, credential, **kwargs):
@@ -78,6 +87,15 @@ def query(self, workspace_id, query, duration=None, **kwargs):
7887
:return: QueryResults, or the result of cls(response)
7988
:rtype: ~azure.monitor.query.LogsQueryResults
8089
:raises: ~azure.core.exceptions.HttpResponseError
90+
91+
.. admonition:: Example:
92+
93+
.. literalinclude:: ../samples/sample_log_query_client.py
94+
:start-after: [START send_logs_query]
95+
:end-before: [END send_logs_query]
96+
:language: python
97+
:dedent: 0
98+
:caption: Get a response for a single Log Query
8199
"""
82100
start = kwargs.pop('start_time', None)
83101
end = kwargs.pop('end_time', None)
@@ -126,6 +144,15 @@ def batch_query(self, queries, **kwargs):
126144
:return: BatchResponse, or the result of cls(response)
127145
:rtype: ~azure.monitor.query.LogsBatchResults
128146
:raises: ~azure.core.exceptions.HttpResponseError
147+
148+
.. admonition:: Example:
149+
150+
.. literalinclude:: ../samples/sample_batch_query.py
151+
:start-after: [START send_batch_query]
152+
:end-before: [END send_batch_query]
153+
:language: python
154+
:dedent: 0
155+
:caption: Get a response for multiple Log Queries.
129156
"""
130157
try:
131158
queries = [LogsQueryRequest(**q) for q in queries]

sdk/monitor/azure-monitor-query/azure/monitor/query/_metrics_query_client.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,15 @@ class MetricsQueryClient(object):
2828
:type credential: ~azure.core.credentials.TokenCredential
2929
:keyword endpoint: The endpoint to connect to. Defaults to 'https://management.azure.com'.
3030
:paramtype endpoint: str
31+
32+
.. admonition:: Example:
33+
34+
.. literalinclude:: ../samples/sample_metrics_query_client.py
35+
:start-after: [START metrics_client_auth_with_token_cred]
36+
:end-before: [END metrics_client_auth_with_token_cred]
37+
:language: python
38+
:dedent: 0
39+
:caption: Creating the MetricsQueryClient with a TokenCredential.
3140
"""
3241

3342
def __init__(self, credential, **kwargs):
@@ -91,6 +100,15 @@ def query(self, resource_uri, metric_names, duration=None, **kwargs):
91100
:return: Response, or the result of cls(response)
92101
:rtype: ~azure.monitor.query.MetricsResult
93102
:raises: ~azure.core.exceptions.HttpResponseError
103+
104+
.. admonition:: Example:
105+
106+
.. literalinclude:: ../samples/sample_metrics_query_client.py
107+
:start-after: [START send_metrics_query]
108+
:end-before: [END send_metrics_query]
109+
:language: python
110+
:dedent: 0
111+
:caption: Get a response for a single Metrics Query
94112
"""
95113
start = kwargs.pop('start_time', None)
96114
end = kwargs.pop('end_time', None)
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
---
2+
page_type: sample
3+
languages:
4+
- python
5+
products:
6+
- azure
7+
- azure-monitor
8+
urlFragment: query-azuremonitor-samples
9+
---
10+
11+
# Azure Monitor Query Client Library Python Samples
12+
13+
## Sync samples
14+
These code samples show common champion scenario operations with the Azure Monitor Query client library.
15+
16+
* Send a single query with LogsQueryClient: [sample_log_query_client.py](https://github.com/Azure/azure-sdk-for-python/blob/master/sdk/monitor/azure-monitor-query/samples/sample_log_query_client.py)
17+
18+
* Send multiple queries with LogsQueryClient: [sample_batch_query.py](https://github.com/Azure/azure-sdk-for-python/blob/master/sdk/monitor/azure-monitor-query/samples/sample_batch_query.py)
19+
20+
* Send multiple queries with LogsQueryClient as a dictionary: [sample_batch_query_serialized.py](https://github.com/Azure/azure-sdk-for-python/blob/master/sdk/monitor/azure-monitor-query/samples/sample_batch_query_serialized.py)
21+
22+
* Send a single query with LogsQueryClient using server timeout: [sample_server_timeout.py](https://github.com/Azure/azure-sdk-for-python/blob/master/sdk/monitor/azure-monitor-query/samples/sample_server_timeout.py)
23+
24+
* Send a query using MetricsQueryClient: [sample_metrics_query_client.py](https://github.com/Azure/azure-sdk-for-python/blob/master/sdk/monitor/azure-monitor-query/samples/sample_metrics_query_client.py)
25+
26+
* Get a list of metric namespaces: [sample_metric_namespaces.py](https://github.com/Azure/azure-sdk-for-python/blob/master/sdk/monitor/azure-monitor-query/samples/sample_metric_namespaces.py)
27+
28+
* Get a list of metric definitions: [sample_metric_definitions.py](https://github.com/Azure/azure-sdk-for-python/blob/master/sdk/monitor/azure-monitor-query/samples/sample_metric_definitions.py)
29+
30+
## Async samples
31+
These code samples show common champion scenario operations with the Azure Monitor Query client library using the async client.
32+
33+
* Send a single query with LogsQueryClient: [sample_log_query_client_async.py](https://github.com/Azure/azure-sdk-for-python/blob/master/sdk/monitor/azure-monitor-query/samples/async_samples/sample_log_query_client_async.py)
34+
35+
* Send a query using MetricsQueryClient: [sample_metrics_query_client_async.py](https://github.com/Azure/azure-sdk-for-python/blob/master/sdk/monitor/azure-monitor-query/samples/async_samples/sample_metrics_query_client_async.py)
36+
37+
* Get a list of metric namespaces: [sample_metric_namespaces_async.py](https://github.com/Azure/azure-sdk-for-python/blob/master/sdk/monitor/azure-monitor-query/samples/async_samples/sample_metric_namespaces_async.py)
38+
39+
* Get a list of metric definitions: [sample_metric_definitions_async.py](https://github.com/Azure/azure-sdk-for-python/blob/master/sdk/monitor/azure-monitor-query/samples/async_samples/sample_metric_definitions_async.py)

sdk/monitor/azure-monitor-query/samples/sample_batch_query.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
client = LogsQueryClient(credential)
1818

19+
# [START send_batch_query]
1920
requests = [
2021
LogsQueryRequest(
2122
query="AzureActivity | summarize count()",
@@ -44,4 +45,6 @@
4445
else:
4546
for table in body.tables:
4647
df = pd.DataFrame(table.rows, columns=[col.name for col in table.columns])
47-
print(df)
48+
print(df)
49+
50+
# [END send_batch_query]

sdk/monitor/azure-monitor-query/samples/sample_log_query_client.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,19 @@
77
from azure.monitor.query import LogsQueryClient
88
from azure.identity import ClientSecretCredential
99

10-
10+
# [START client_auth_with_token_cred]
1111
credential = ClientSecretCredential(
1212
client_id = os.environ['AZURE_CLIENT_ID'],
1313
client_secret = os.environ['AZURE_CLIENT_SECRET'],
1414
tenant_id = os.environ['AZURE_TENANT_ID']
1515
)
1616

1717
client = LogsQueryClient(credential)
18+
# [END client_auth_with_token_cred]
1819

1920
# Response time trend
2021
# request duration over the last 12 hours.
22+
# [START send_logs_query]
2123
query = """AppRequests |
2224
where TimeGenerated > ago(12h) |
2325
summarize avgRequestDuration=avg(DurationMs) by bin(TimeGenerated, 10m), _ResourceId"""
@@ -31,7 +33,7 @@
3133
for table in response.tables:
3234
df = pd.DataFrame(table.rows, columns=[col.name for col in table.columns])
3335
print(df)
34-
36+
# [END send_logs_query]
3537
"""
3638
TimeGenerated _ResourceId avgRequestDuration
3739
0 2021-05-27T08:40:00Z /subscriptions/faa080af-c1d8-40ad-9cce-e1a450c... 27.307699999999997

sdk/monitor/azure-monitor-query/samples/sample_metrics_query_client.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,17 @@
99

1010
urllib3.disable_warnings()
1111

12+
# [START metrics_client_auth_with_token_cred]
1213
credential = ClientSecretCredential(
1314
client_id = os.environ['AZURE_CLIENT_ID'],
1415
client_secret = os.environ['AZURE_CLIENT_SECRET'],
1516
tenant_id = os.environ['AZURE_TENANT_ID']
1617
)
1718

1819
client = MetricsQueryClient(credential)
20+
# [END metrics_client_auth_with_token_cred]
1921

22+
# [START send_metrics_query]
2023
metrics_uri = os.environ['METRICS_RESOURCE_URI']
2124
response = client.query(
2225
metrics_uri,
@@ -30,4 +33,4 @@
3033
for time_series_element in metric.timeseries:
3134
for metric_value in time_series_element.data:
3235
print(metric_value.time_stamp)
33-
36+
# [END send_metrics_query]

0 commit comments

Comments
 (0)