Skip to content

Commit 80b6678

Browse files
Rakshith Bhyravabhotlaswathipil
andauthored
Imrprove LogsQuery Request (Azure#20132)
* Imrprove docstrings * more docs changes * Apply suggestions from code review Co-authored-by: swathipil <76007337+swathipil@users.noreply.github.com> Co-authored-by: swathipil <76007337+swathipil@users.noreply.github.com>
1 parent 168ac5b commit 80b6678

File tree

5 files changed

+114
-66
lines changed

5 files changed

+114
-66
lines changed

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

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,44 @@ for rsp in response:
212212
print(df)
213213
```
214214

215+
#### Handling the response for Logs Query
216+
217+
The `query` API returns the `LogsQueryResult` while the `batch_query` API returns the `LogsBatchQueryResult`.
218+
219+
Here is a heirarchy of the response:
220+
221+
```
222+
LogsQueryResult / LogsBatchQueryResult
223+
|---id (this exists in `LogsBatchQueryResult` object only)
224+
|---status (this exists in `LogsBatchQueryResult` object only)
225+
|---statistics
226+
|---render
227+
|---error
228+
|---tables (list of `LogsQueryResultTable` objects)
229+
|---name
230+
|---rows
231+
|---columns (list of `LogsQueryResultColumn` objects)
232+
|---name
233+
|---type
234+
```
235+
236+
So, to handle a response with tables and display it using pandas,
237+
238+
```python
239+
table = response.tables[0]
240+
df = pd.DataFrame(table.rows, columns=[col.name for col in table.columns])
241+
```
242+
A full sample can be found [here](https://github.com/Azure/azure-sdk-for-python/blob/main/sdk/monitor/azure-monitor-query/samples/sample_log_query_client.py).
243+
244+
In a very similar fashion, to handle a batch response,
245+
246+
```python
247+
for result in response:
248+
table = result.tables[0]
249+
df = pd.DataFrame(table.rows, columns=[col.name for col in table.columns])
250+
```
251+
A full sample can be found [here](https://github.com/Azure/azure-sdk-for-python/blob/main/sdk/monitor/azure-monitor-query/samples/sample_batch_query.py).
252+
215253
### Query metrics
216254

217255
The following example gets metrics for an Event Grid subscription. The resource URI is that of an event grid topic.

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

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,6 @@
2222
class LogsQueryClient(object):
2323
"""LogsQueryClient
2424
25-
:param credential: The credential to authenticate the client
26-
:type credential: ~azure.core.credentials.TokenCredential
27-
:keyword endpoint: The endpoint to connect to. Defaults to 'https://api.loganalytics.io'.
28-
:paramtype endpoint: str
29-
3025
.. admonition:: Example:
3126
3227
.. literalinclude:: ../samples/sample_log_query_client.py
@@ -35,6 +30,11 @@ class LogsQueryClient(object):
3530
:language: python
3631
:dedent: 0
3732
:caption: Creating the LogsQueryClient with a TokenCredential.
33+
34+
:param credential: The credential to authenticate the client.
35+
:type credential: ~azure.core.credentials.TokenCredential
36+
:keyword endpoint: The endpoint to connect to. Defaults to 'https://api.loganalytics.io'.
37+
:paramtype endpoint: str
3838
"""
3939

4040
def __init__(self, credential, **kwargs):
@@ -139,7 +139,7 @@ def batch_query(self, queries, **kwargs):
139139
140140
:param queries: The list of queries that should be processed
141141
:type queries: list[dict] or list[~azure.monitor.query.LogsBatchQueryRequest]
142-
:return: BatchResponse, or the result of cls(response)
142+
:return: List of LogsBatchQueryResult, or the result of cls(response)
143143
:rtype: ~list[~azure.monitor.query.LogsBatchQueryResult]
144144
:raises: ~azure.core.exceptions.HttpResponseError
145145
@@ -156,6 +156,7 @@ def batch_query(self, queries, **kwargs):
156156
queries = [LogsBatchQueryRequest(**q) for q in queries]
157157
except (KeyError, TypeError):
158158
pass
159+
queries = [q._to_generated() for q in queries] # pylint: disable=protected-access
159160
try:
160161
request_order = [req.id for req in queries]
161162
except AttributeError:

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,6 @@
2525
class MetricsQueryClient(object):
2626
"""MetricsQueryClient
2727
28-
:param credential: The credential to authenticate the client
29-
:type credential: ~azure.core.credentials.TokenCredential
30-
:keyword endpoint: The endpoint to connect to. Defaults to 'https://management.azure.com'.
31-
:paramtype endpoint: str
32-
3328
.. admonition:: Example:
3429
3530
.. literalinclude:: ../samples/sample_metrics_query_client.py
@@ -38,6 +33,11 @@ class MetricsQueryClient(object):
3833
:language: python
3934
:dedent: 0
4035
:caption: Creating the MetricsQueryClient with a TokenCredential.
36+
37+
:param credential: The credential to authenticate the client.
38+
:type credential: ~azure.core.credentials.TokenCredential
39+
:keyword endpoint: The endpoint to connect to. Defaults to 'https://management.azure.com'.
40+
:paramtype endpoint: str
4141
"""
4242

4343
def __init__(self, credential, **kwargs):

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

Lines changed: 63 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ class LogsQueryResultTable(object):
2525
:type name: str
2626
:param columns: Required. The list of columns in this table.
2727
:type columns: list[~azure.monitor.query.LogsQueryResultColumn]
28-
:keyword rows: Required. The resulting rows from this query.
29-
:paramtype rows: list[list[str]]
28+
:param rows: Required. The resulting rows from this query.
29+
:type rows: list[list[str]]
3030
"""
3131
def __init__(self, name, columns, rows):
3232
# type: (str, List[LogsQueryResultColumn], List[List[str]]) -> None
@@ -46,10 +46,10 @@ def _from_generated(cls, generated):
4646
class LogsQueryResultColumn(InternalColumn):
4747
"""A column in a table.
4848
49-
:keyword name: The name of this column.
50-
:paramtype name: str
51-
:keyword type: The data type of this column.
52-
:paramtype type: str
49+
:ivar name: The name of this column.
50+
:vartype name: str
51+
:ivar type: The data type of this column.
52+
:vartype type: str
5353
"""
5454

5555
_attribute_map = {
@@ -109,22 +109,22 @@ class MetricsResult(object):
109109
110110
All required parameters must be populated in order to send to Azure.
111111
112-
:keyword cost: The integer value representing the cost of the query, for data case.
113-
:paramtype cost: int
114-
:keyword timespan: Required. The timespan for which the data was retrieved. Its value consists of
112+
:ivar cost: The integer value representing the cost of the query, for data case.
113+
:vartype cost: int
114+
:ivar timespan: Required. The timespan for which the data was retrieved. Its value consists of
115115
two datetimes concatenated, separated by '/'. This may be adjusted in the future and returned
116116
back from what was originally requested.
117-
:paramtype timespan: str
118-
:keyword interval: The interval (window size) for which the metric data was returned in. This
117+
:vartype timespan: str
118+
:ivar interval: The interval (window size) for which the metric data was returned in. This
119119
may be adjusted in the future and returned back from what was originally requested. This is
120120
not present if a metadata request was made.
121-
:paramtype interval: ~datetime.timedelta
122-
:keyword namespace: The namespace of the metrics been queried.
123-
:paramtype namespace: str
124-
:keyword resourceregion: The region of the resource been queried for metrics.
125-
:paramtype resourceregion: str
126-
:keyword metrics: Required. the value of the collection.
127-
:paramtype metrics: list[~monitor_query_client.models.Metric]
121+
:vartype interval: ~datetime.timedelta
122+
:ivar namespace: The namespace of the metrics that has been queried.
123+
:vartype namespace: str
124+
:ivar resourceregion: The region of the resource that has been queried for metrics.
125+
:vartype resourceregion: str
126+
:ivar metrics: Required. The value of the collection.
127+
:vartype metrics: list[~monitor_query_client.models.Metric]
128128
"""
129129
def __init__(self, **kwargs):
130130
# type: (Any) -> None
@@ -148,7 +148,7 @@ def _from_generated(cls, generated):
148148
metrics=[Metric._from_generated(m) for m in generated.value] # pylint: disable=protected-access
149149
)
150150

151-
class LogsBatchQueryRequest(InternalLogQueryRequest):
151+
class LogsBatchQueryRequest(object):
152152
"""A single request in a batch.
153153
154154
Variables are only populated by the server, and will be ignored when sending a request.
@@ -212,6 +212,14 @@ def __init__(self, query, workspace_id, duration=None, **kwargs): #pylint: disab
212212
self.headers = headers
213213
self.workspace = workspace_id
214214

215+
def _to_generated(self):
216+
return InternalLogQueryRequest(
217+
id=self.id,
218+
body=self.body,
219+
headers=self.headers,
220+
workspace=self.workspace
221+
)
222+
215223
class LogsBatchQueryResult(object):
216224
"""The LogsBatchQueryResult.
217225
@@ -265,12 +273,12 @@ def _from_generated(cls, generated):
265273
class LogsBatchResultError(object):
266274
"""Error response for a batch request.
267275
268-
:param message: The error message describing the cause of the error.
269-
:type message: str
276+
:ivar message: The error message describing the cause of the error.
277+
:vartype message: str
270278
:param code: The error code.
271-
:type code: str
279+
:vartype code: str
272280
:param details: The details of the error.
273-
:type inner_error: list[~azure.monitor.query.ErrorDetails]
281+
:vartype inner_error: list[~azure.monitor.query.ErrorDetails]
274282
"""
275283
def __init__(self, **kwargs):
276284
# type: (Any) -> None
@@ -299,7 +307,7 @@ class MetricNamespace(object):
299307
:keyword name: The name of the namespace.
300308
:paramtype name: str
301309
:keyword metric_namespace_name: The fully qualified namespace name.
302-
:paramtype properties: str
310+
:paramtype metric_namespace_name: str
303311
"""
304312
def __init__(
305313
self,
@@ -399,19 +407,19 @@ class MetricValue(object):
399407
400408
All required parameters must be populated in order to send to Azure.
401409
402-
:keyword time_stamp: Required. the timestamp for the metric value in ISO 8601 format.
403-
:paramtype time_stamp: ~datetime.datetime
404-
:keyword average: the average value in the time range.
405-
:paramtype average: float
406-
:keyword minimum: the least value in the time range.
407-
:paramtype minimum: float
408-
:keyword maximum: the greatest value in the time range.
409-
:paramtype maximum: float
410-
:keyword total: the sum of all of the values in the time range.
411-
:paramtype total: float
412-
:keyword count: the number of samples in the time range. Can be used to determine the number of
410+
:ivar time_stamp: Required. The timestamp for the metric value in ISO 8601 format.
411+
:vartype time_stamp: ~datetime.datetime
412+
:ivar average: The average value in the time range.
413+
:vartype average: float
414+
:ivar minimum: The least value in the time range.
415+
:vartype minimum: float
416+
:ivar maximum: The greatest value in the time range.
417+
:vartype maximum: float
418+
:ivar total: The sum of all of the values in the time range.
419+
:vartype total: float
420+
:ivar count: The number of samples in the time range. Can be used to determine the number of
413421
values that contributed to the average value.
414-
:paramtype count: float
422+
:vartype count: float
415423
"""
416424
def __init__(
417425
self,
@@ -443,18 +451,18 @@ class Metric(object):
443451
444452
All required parameters must be populated in order to send to Azure.
445453
446-
:keyword id: Required. the metric Id.
447-
:paramtype id: str
448-
:keyword type: Required. the resource type of the metric resource.
449-
:paramtype type: str
450-
:keyword name: Required. the name of the metric.
451-
:paramtype name: str
452-
:keyword unit: Required. the unit of the metric. Possible values include: "Count", "Bytes",
454+
:ivar id: Required. The metric Id.
455+
:vartype id: str
456+
:ivar type: Required. The resource type of the metric resource.
457+
:vartype type: str
458+
:ivar name: Required. The name of the metric.
459+
:vartype name: str
460+
:ivar unit: Required. The unit of the metric. Possible values include: "Count", "Bytes",
453461
"Seconds", "CountPerSecond", "BytesPerSecond", "Percent", "MilliSeconds", "ByteSeconds",
454462
"Unspecified", "Cores", "MilliCores", "NanoCores", "BitsPerSecond".
455-
:paramtype unit: str
456-
:keyword timeseries: Required. the time series returned when a data query is performed.
457-
:paramtype timeseries: list[~monitor_query_client.models.TimeSeriesElement]
463+
:vartype unit: str
464+
:ivar timeseries: Required. The time series returned when a data query is performed.
465+
:vartype timeseries: list[~monitor_query_client.models.TimeSeriesElement]
458466
"""
459467
def __init__(
460468
self,
@@ -485,11 +493,11 @@ def _from_generated(cls, generated):
485493
class TimeSeriesElement(object):
486494
"""A time series result type. The discriminator value is always TimeSeries in this case.
487495
488-
:keyword metadata_values: the metadata values returned if $filter was specified in the call.
489-
:paramtype metadata_values: list[~monitor_query_client.models.MetadataValue]
490-
:keyword data: An array of data points representing the metric values. This is only returned if
496+
:ivar metadata_values: The metadata values returned if $filter was specified in the call.
497+
:vartype metadata_values: list[~monitor_query_client.models.MetadataValue]
498+
:ivar data: An array of data points representing the metric values. This is only returned if
491499
a result type of data is specified.
492-
:paramtype data: list[~monitor_query_client.models.MetricValue]
500+
:vartype data: list[~monitor_query_client.models.MetricValue]
493501
"""
494502

495503
_attribute_map = {
@@ -521,10 +529,10 @@ def _from_generated(cls, generated):
521529
class MetricsMetadataValue(object):
522530
"""Represents a metric metadata value.
523531
524-
:keyword name: the name of the metadata.
525-
:paramtype name: str
526-
:keyword value: the value of the metadata.
527-
:paramtype value: str
532+
:ivar name: The name of the metadata.
533+
:vartype name: str
534+
:ivar value: The value of the metadata.
535+
:vartype value: str
528536
"""
529537
def __init__(
530538
self,
@@ -574,7 +582,7 @@ def _from_generated(cls, generated):
574582

575583

576584
class AggregationType(str, Enum):
577-
"""the aggregation type of the metric.
585+
"""The aggregation type of the metric.
578586
"""
579587

580588
NONE = "None"

sdk/monitor/azure-monitor-query/azure/monitor/query/aio/_logs_query_client_async.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@ async def batch_query(
134134
queries = [LogsBatchQueryRequest(**q) for q in queries]
135135
except (KeyError, TypeError):
136136
pass
137+
queries = [q._to_generated() for q in queries] # pylint: disable=protected-access
137138
try:
138139
request_order = [req.id for req in queries]
139140
except AttributeError:

0 commit comments

Comments
 (0)