Skip to content

Commit 8a20de4

Browse files
authored
[Monitor][Query] Add resource centric query support (#29365)
This regenerates with autorest and provides an API for users to query Azure Monitor logs directly from a resource instead of going through the context of a Log Analytics workspace. Signed-off-by: Paul Van Eck <[email protected]>
1 parent 4197d67 commit 8a20de4

File tree

17 files changed

+2771
-508
lines changed

17 files changed

+2771
-508
lines changed

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

+2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
### Features Added
66

7+
- Add the `query_resource` method to `LogsQueryClient` to allow users to query Azure resources directly without the context of a workspace. ([#29365](https://github.com/Azure/azure-sdk-for-python/pull/29365))
8+
79
### Breaking Changes
810

911
### Bugs Fixed

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

+39-5
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ Each set of metric values is a time series with the following characteristics:
9292
- [Specify timespan](#specify-timespan)
9393
- [Handle logs query response](#handle-logs-query-response)
9494
- [Batch logs query](#batch-logs-query)
95+
- [Resource logs query](#resource-logs-query)
9596
- [Advanced logs query scenarios](#advanced-logs-query-scenarios)
9697
- [Set logs query timeout](#set-logs-query-timeout)
9798
- [Query multiple workspaces](#query-multiple-workspaces)
@@ -103,7 +104,7 @@ Each set of metric values is a time series with the following characteristics:
103104

104105
### Logs query
105106

106-
This example shows getting a logs query. 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.
107+
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.
107108

108109
#### Specify timespan
109110

@@ -148,7 +149,7 @@ try:
148149
print(df)
149150
except HttpResponseError as err:
150151
print("something fatal happened")
151-
print (err)
152+
print(err)
152153
```
153154

154155
#### Handle logs query response
@@ -219,18 +220,18 @@ requests = [
219220
LogsBatchQuery(
220221
query="AzureActivity | summarize count()",
221222
timespan=timedelta(hours=1),
222-
workspace_id= os.environ['LOG_WORKSPACE_ID']
223+
workspace_id=os.environ['LOG_WORKSPACE_ID']
223224
),
224225
LogsBatchQuery(
225226
query= """bad query""",
226227
timespan=timedelta(days=1),
227-
workspace_id= os.environ['LOG_WORKSPACE_ID']
228+
workspace_id=os.environ['LOG_WORKSPACE_ID']
228229
),
229230
LogsBatchQuery(
230231
query= """let Weight = 92233720368547758;
231232
range x from 1 to 3 step 1
232233
| summarize percentilesw(x, Weight * 100, 50)""",
233-
workspace_id= os.environ['LOG_WORKSPACE_ID'],
234+
workspace_id=os.environ['LOG_WORKSPACE_ID'],
234235
timespan=(datetime(2021, 6, 2, tzinfo=timezone.utc), datetime(2021, 6, 5, tzinfo=timezone.utc)), # (start, end)
235236
include_statistics=True
236237
),
@@ -255,6 +256,39 @@ for res in results:
255256

256257
```
257258

259+
### Resource logs query
260+
261+
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`, and instead of a workspace ID, an Azure resource identifier is passed in (e.g. `/subscriptions/{subscription-id}/resourceGroups/{resource-group-name}/providers/{resource-provider}/{resource-type}/{resource-name}`).
262+
263+
```python
264+
import os
265+
import pandas as pd
266+
from datetime import timedelta
267+
from azure.monitor.query import LogsQueryClient, LogsQueryStatus
268+
from azure.core.exceptions import HttpResponseError
269+
from azure.identity import DefaultAzureCredential
270+
271+
credential = DefaultAzureCredential()
272+
client = LogsQueryClient(credential)
273+
274+
query = """AzureActivity | take 5"""
275+
276+
try:
277+
response = client.query_resource(os.environ['LOGS_RESOURCE_ID'], query, timespan=timedelta(days=1))
278+
if response.status == LogsQueryStatus.PARTIAL:
279+
error = response.partial_error
280+
data = response.partial_data
281+
print(error)
282+
elif response.status == LogsQueryStatus.SUCCESS:
283+
data = response.tables
284+
for table in data:
285+
df = pd.DataFrame(data=table.rows, columns=table.columns)
286+
print(df)
287+
except HttpResponseError as err:
288+
print("something fatal happened")
289+
print(err)
290+
```
291+
258292
### Advanced logs query scenarios
259293

260294
#### Set logs query timeout

sdk/monitor/azure-monitor-query/assets.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@
22
"AssetsRepo": "Azure/azure-sdk-assets",
33
"AssetsRepoPrefixPath": "python",
44
"TagPrefix": "python/monitor/azure-monitor-query",
5-
"Tag": "python/monitor/azure-monitor-query_86a8ec0380"
5+
"Tag": "python/monitor/azure-monitor-query_8b3197a327"
66
}

0 commit comments

Comments
 (0)