Skip to content

Commit c7bef72

Browse files
authored
Document role requirement for multi-resource metrics queries (#35121)
1 parent 890071e commit c7bef72

File tree

1 file changed

+39
-36
lines changed

1 file changed

+39
-36
lines changed

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

Lines changed: 39 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -110,10 +110,10 @@ Each set of metric values is a time series with the following characteristics:
110110
## Examples
111111

112112
- [Logs query](#logs-query)
113+
- [Resource-centric logs query](#resource-centric-logs-query)
113114
- [Specify timespan](#specify-timespan)
114115
- [Handle logs query response](#handle-logs-query-response)
115116
- [Batch logs query](#batch-logs-query)
116-
- [Resource logs query](#resource-logs-query)
117117
- [Advanced logs query scenarios](#advanced-logs-query-scenarios)
118118
- [Set logs query timeout](#set-logs-query-timeout)
119119
- [Query multiple workspaces](#query-multiple-workspaces)
@@ -128,6 +128,39 @@ Each set of metric values is a time series with the following characteristics:
128128

129129
This example shows how to query a Log Analytics workspace. To handle the response and view it in a tabular form, the [`pandas`](https://pypi.org/project/pandas/) library is used. See the [samples][samples] if you choose not to use `pandas`.
130130

131+
#### Resource-centric logs query
132+
133+
The following example demonstrates how to query logs directly from an Azure resource without the use of a Log Analytics workspace. Here, the `query_resource` method is used instead of `query_workspace`. Instead of a workspace ID, an Azure resource identifier is passed in. For example, `/subscriptions/{subscription-id}/resourceGroups/{resource-group-name}/providers/{resource-provider}/{resource-type}/{resource-name}`.
134+
135+
```python
136+
import os
137+
import pandas as pd
138+
from datetime import timedelta
139+
from azure.monitor.query import LogsQueryClient, LogsQueryStatus
140+
from azure.core.exceptions import HttpResponseError
141+
from azure.identity import DefaultAzureCredential
142+
143+
credential = DefaultAzureCredential()
144+
client = LogsQueryClient(credential)
145+
146+
query = """AzureActivity | take 5"""
147+
148+
try:
149+
response = client.query_resource(os.environ['LOGS_RESOURCE_ID'], query, timespan=timedelta(days=1))
150+
if response.status == LogsQueryStatus.PARTIAL:
151+
error = response.partial_error
152+
data = response.partial_data
153+
print(error)
154+
elif response.status == LogsQueryStatus.SUCCESS:
155+
data = response.tables
156+
for table in data:
157+
df = pd.DataFrame(data=table.rows, columns=table.columns)
158+
print(df)
159+
except HttpResponseError as err:
160+
print("something fatal happened")
161+
print(err)
162+
```
163+
131164
#### Specify timespan
132165

133166
The `timespan` parameter specifies the time duration for which to query the data. This value can take one of the following forms:
@@ -278,39 +311,6 @@ for res in results:
278311

279312
```
280313

281-
### Resource logs query
282-
283-
The following example demonstrates how to query logs directly from an Azure resource without the use of a Log Analytics workspace. Here, the `query_resource` method is used instead of `query_workspace`. Instead of a workspace ID, an Azure resource identifier is passed in. For example, `/subscriptions/{subscription-id}/resourceGroups/{resource-group-name}/providers/{resource-provider}/{resource-type}/{resource-name}`.
284-
285-
```python
286-
import os
287-
import pandas as pd
288-
from datetime import timedelta
289-
from azure.monitor.query import LogsQueryClient, LogsQueryStatus
290-
from azure.core.exceptions import HttpResponseError
291-
from azure.identity import DefaultAzureCredential
292-
293-
credential = DefaultAzureCredential()
294-
client = LogsQueryClient(credential)
295-
296-
query = """AzureActivity | take 5"""
297-
298-
try:
299-
response = client.query_resource(os.environ['LOGS_RESOURCE_ID'], query, timespan=timedelta(days=1))
300-
if response.status == LogsQueryStatus.PARTIAL:
301-
error = response.partial_error
302-
data = response.partial_data
303-
print(error)
304-
elif response.status == LogsQueryStatus.SUCCESS:
305-
data = response.tables
306-
for table in data:
307-
df = pd.DataFrame(data=table.rows, columns=table.columns)
308-
print(df)
309-
except HttpResponseError as err:
310-
print("something fatal happened")
311-
print(err)
312-
```
313-
314314
### Advanced logs query scenarios
315315

316316
#### Set logs query timeout
@@ -362,7 +362,7 @@ A full sample can be found [here](https://github.com/Azure/azure-sdk-for-python/
362362
To get logs query execution statistics, such as CPU and memory consumption:
363363

364364
1. Set the `include_statistics` parameter to `True`.
365-
2. Access the `statistics` field inside the `LogsQueryResult` object.
365+
1. Access the `statistics` field inside the `LogsQueryResult` object.
366366

367367
The following example prints the query execution time:
368368

@@ -546,7 +546,10 @@ Each Azure resource must reside in:
546546
- The same region as the endpoint specified when creating the client.
547547
- The same Azure subscription.
548548

549-
Furthermore, the metric namespace containing the metrics to be queried must be provided. For a list of metric namespaces, see [Supported metrics and log categories by resource type][metric_namespaces].
549+
Furthermore:
550+
551+
- The user must be authorized to read monitoring data at the Azure subscription level. For example, the [Monitoring Reader role](https://learn.microsoft.com/azure/role-based-access-control/built-in-roles/monitor#monitoring-reader) on the subscription to be queried.
552+
- The metric namespace containing the metrics to be queried must be provided. For a list of metric namespaces, see [Supported metrics and log categories by resource type][metric_namespaces].
550553

551554
```python
552555
from datetime import timedelta

0 commit comments

Comments
 (0)