Skip to content

Commit a4783a4

Browse files
Rakshith Bhyravabhotlayunhaoling
andauthored
More Renaming in query (Azure#20303)
* More Reanaming in query * changelog * commit 2 * some changes * remove errror * Update sdk/monitor/azure-monitor-query/azure/monitor/query/_models.py * Apply suggestions from code review Co-authored-by: Adam Ling (MSFT) <[email protected]> * Update sdk/monitor/azure-monitor-query/CHANGELOG.md Co-authored-by: Adam Ling (MSFT) <[email protected]>
1 parent 3d14c47 commit a4783a4

File tree

10 files changed

+74
-26
lines changed

10 files changed

+74
-26
lines changed

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
### Features Added
66

7+
- Added additional `display_description` attribute to the `Metric` type.
8+
79
### Breaking Changes
810

911
- Rename `batch_query` to `query_batch`.
@@ -15,7 +17,8 @@
1517
- `top` is renamed to `max_results` in the metric's `query` API.
1618
- `metric_namespace_name` is renamed to `fully_qualified_namespace`
1719
- `is_dimension_required` is renamed to `dimension_required`
18-
- `time_grain` is renamed to `granularity`
20+
- `interval` and `time_grain` are renamed to `granularity`
21+
- `orderby` is renamed to `order_by`
1922
- `LogsQueryResult` now returns `datetime` objects for a time values.
2023

2124
### Bugs Fixed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -289,11 +289,11 @@ for metric in response.metrics:
289289

290290
### Handle metrics response
291291

292-
The metrics query API returns a `MetricsResult` object. The `MetricsResult` object contains properties such as a list of `Metric`-typed objects, `interval`, `namespace`, and `timespan`. The `Metric` objects list can be accessed using the `metrics` param. Each `Metric` object in this list contains a list of `TimeSeriesElement` objects. Each `TimeSeriesElement` contains `data` and `metadata_values` properties. In visual form, the object hierarchy of the response resembles the following structure:
292+
The metrics query API returns a `MetricsResult` object. The `MetricsResult` object contains properties such as a list of `Metric`-typed objects, `granularity`, `namespace`, and `timespan`. The `Metric` objects list can be accessed using the `metrics` param. Each `Metric` object in this list contains a list of `TimeSeriesElement` objects. Each `TimeSeriesElement` contains `data` and `metadata_values` properties. In visual form, the object hierarchy of the response resembles the following structure:
293293

294294
```
295295
MetricsResult
296-
|---interval
296+
|---granularity
297297
|---timespan
298298
|---cost
299299
|---namespace

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,19 +68,19 @@ def query(self, resource_uri, metric_names, **kwargs):
6868
a timedelta and a start datetime, or a start datetime/end datetime.
6969
:paramtype timespan: ~datetime.timedelta or tuple[~datetime.datetime, ~datetime.timedelta]
7070
or tuple[~datetime.datetime, ~datetime.datetime]
71-
:keyword interval: The interval (i.e. timegrain) of the query.
72-
:paramtype interval: ~datetime.timedelta
71+
:keyword granularity: The granularity (i.e. timegrain) of the query.
72+
:paramtype granularity: ~datetime.timedelta
7373
:keyword aggregations: The list of aggregation types to retrieve. Use `azure.monitor.query.AggregationType`
7474
enum to get each aggregation type.
7575
:paramtype aggregations: list[str]
7676
:keyword max_results: The maximum number of records to retrieve.
7777
Valid only if $filter is specified.
7878
Defaults to 10.
7979
:paramtype max_results: int
80-
:keyword orderby: The aggregation to use for sorting results and the direction of the sort.
80+
:keyword order_by: The aggregation to use for sorting results and the direction of the sort.
8181
Only one order can be specified.
8282
Examples: sum asc.
83-
:paramtype orderby: str
83+
:paramtype order_by: str
8484
:keyword filter: The **$filter** is used to reduce the set of metric data
8585
returned.:code:`<br>`Example::code:`<br>`Metric contains metadata A, B and C.:code:`<br>`-
8686
Return all time series of C where A = a1 and B = b1 or b2:code:`<br>`\ **$filter=A eq ‘a1’ and
@@ -117,6 +117,8 @@ def query(self, resource_uri, metric_names, **kwargs):
117117
kwargs.setdefault("metricnames", ",".join(metric_names))
118118
kwargs.setdefault("timespan", timespan)
119119
kwargs.setdefault("top", kwargs.pop("max_results", None))
120+
kwargs.setdefault("interval", kwargs.pop("granularity", None))
121+
kwargs.setdefault("orderby", kwargs.pop("order_by", None))
120122
generated = self._metrics_op.list(resource_uri, connection_verify=False, **kwargs)
121123
return MetricsResult._from_generated(generated) # pylint: disable=protected-access
122124

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

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -115,10 +115,10 @@ class MetricsResult(object):
115115
two datetimes concatenated, separated by '/'. This may be adjusted in the future and returned
116116
back from what was originally requested.
117117
:vartype timespan: str
118-
:ivar interval: The interval (window size) for which the metric data was returned in. This
118+
:ivar granularity: The granularity (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-
:vartype interval: ~datetime.timedelta
121+
:vartype granularity: ~datetime.timedelta
122122
:ivar namespace: The namespace of the metrics that has been queried.
123123
:vartype namespace: str
124124
:ivar resource_region: The region of the resource that has been queried for metrics.
@@ -130,7 +130,7 @@ def __init__(self, **kwargs):
130130
# type: (Any) -> None
131131
self.cost = kwargs.get("cost", None)
132132
self.timespan = kwargs["timespan"]
133-
self.interval = kwargs.get("interval", None)
133+
self.granularity = kwargs.get("granularity", None)
134134
self.namespace = kwargs.get("namespace", None)
135135
self.resource_region = kwargs.get("resource_region", None)
136136
self.metrics = kwargs["metrics"]
@@ -142,7 +142,7 @@ def _from_generated(cls, generated):
142142
return cls(
143143
cost=generated.cost,
144144
timespan=generated.timespan,
145-
interval=generated.interval,
145+
granularity=generated.interval,
146146
namespace=generated.namespace,
147147
resource_region=generated.resourceregion,
148148
metrics=[Metric._from_generated(m) for m in generated.value] # pylint: disable=protected-access
@@ -459,6 +459,8 @@ class Metric(object):
459459
:vartype unit: str
460460
:ivar timeseries: Required. The time series returned when a data query is performed.
461461
:vartype timeseries: list[~monitor_query_client.models.TimeSeriesElement]
462+
:ivar display_description: Detailed description of this metric.
463+
:vartype display_description: str
462464
"""
463465
def __init__(
464466
self,
@@ -470,6 +472,7 @@ def __init__(
470472
self.name = kwargs['name']
471473
self.unit = kwargs['unit']
472474
self.timeseries = kwargs['timeseries']
475+
self.display_description = kwargs['display_description']
473476

474477
@classmethod
475478
def _from_generated(cls, generated):
@@ -482,7 +485,8 @@ def _from_generated(cls, generated):
482485
unit=generated.unit,
483486
timeseries=[
484487
TimeSeriesElement._from_generated(t) for t in generated.timeseries # pylint: disable=protected-access
485-
]
488+
],
489+
display_description=generated.display_description,
486490
)
487491

488492

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,19 +62,19 @@ async def query(
6262
a timedelta and a start datetime, or a start datetime/end datetime.
6363
:paramtype timespan: ~datetime.timedelta or tuple[~datetime.datetime, ~datetime.timedelta]
6464
or tuple[~datetime.datetime, ~datetime.datetime]
65-
:keyword interval: The interval (i.e. timegrain) of the query.
66-
:paramtype interval: ~datetime.timedelta
65+
:keyword granularity: The interval (i.e. timegrain) of the query.
66+
:paramtype granularity: ~datetime.timedelta
6767
:keyword aggregations: The list of aggregation types to retrieve. Use `azure.monitor.query.AggregationType`
6868
enum to get each aggregation type.
6969
:paramtype aggregations: list[str]
7070
:keyword max_results: The maximum number of records to retrieve.
7171
Valid only if $filter is specified.
7272
Defaults to 10.
7373
:paramtype max_results: int
74-
:keyword orderby: The aggregation to use for sorting results and the direction of the sort.
74+
:keyword order_by: The aggregation to use for sorting results and the direction of the sort.
7575
Only one order can be specified.
7676
Examples: sum asc.
77-
:paramtype orderby: str
77+
:paramtype order_by: str
7878
:keyword filter: The **$filter** is used to reduce the set of metric data
7979
returned.:code:`<br>`Example::code:`<br>`Metric contains metadata A, B and C.:code:`<br>`-
8080
Return all time series of C where A = a1 and B = b1 or b2:code:`<br>`\ **$filter=A eq ‘a1’ and
@@ -98,6 +98,8 @@ async def query(
9898
kwargs.setdefault("metricnames", ",".join(metric_names))
9999
kwargs.setdefault("timespan", timespan)
100100
kwargs.setdefault("top", kwargs.pop("max_results", None))
101+
kwargs.setdefault("interval", kwargs.pop("granularity", None))
102+
kwargs.setdefault("orderby", kwargs.pop("order_by", None))
101103
aggregations = kwargs.pop("aggregations", None)
102104
if aggregations:
103105
kwargs.setdefault("aggregation", ",".join(aggregations))

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

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,18 +20,26 @@
2020
query = """AppRequests |
2121
summarize avgRequestDuration=avg(DurationMs) by bin(TimeGenerated, 10m), _ResourceId"""
2222

23+
query = """
24+
AppRequests
25+
| where TimeGenerated > ago(1h)
26+
| fork
27+
( summarize avgRequestDuration=avg(DurationMs) by bin(TimeGenerated, 10m), _ResourceId )
28+
"""
29+
2330
# returns LogsQueryResult
2431
response = client.query(os.environ['LOG_WORKSPACE_ID'], query, timespan=timedelta(days=1))
2532

2633
if not response.tables:
2734
print("No results for the query")
2835

29-
try:
30-
table = response.tables[0]
31-
df = pd.DataFrame(table.rows, columns=[col.name for col in table.columns])
32-
print(df)
33-
except TypeError:
34-
print(response.error)
36+
for table in response.tables:
37+
try:
38+
print ([col.name for col in table.columns])
39+
df = pd.DataFrame(table.rows, columns=[col.name for col in table.columns])
40+
print(df)
41+
except TypeError:
42+
print(response.error)
3543
# [END send_logs_query]
3644
"""
3745
TimeGenerated _ResourceId avgRequestDuration

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,5 @@
1313
response = client.list_metric_namespaces(metrics_uri)
1414

1515
for item in response:
16-
print(item.metric_namespace_name)
16+
print(item.fully_qualified_namespace)
1717
print(item.type)

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,8 @@
1919
metrics_uri = os.environ['METRICS_RESOURCE_URI']
2020
response = client.query(
2121
metrics_uri,
22-
metric_names=["MatchedEventCount"],
23-
start_time=datetime(2021, 6, 21),
24-
duration=timedelta(days=1),
22+
metric_names=["MatchedEventCount", "DeliverySuccesssCount"],
23+
timespan=timedelta(days=1),
2524
aggregations=[AggregationType.COUNT]
2625
)
2726

sdk/monitor/azure-monitor-query/tests/async/test_metrics_client_async.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ def _credential():
1414
return credential
1515

1616
@pytest.mark.live_test_only
17+
@pytest.mark.asyncio
1718
async def test_metrics_auth():
1819
credential = _credential()
1920
client = MetricsQueryClient(credential)
@@ -26,6 +27,21 @@ async def test_metrics_auth():
2627
assert response
2728
assert response.metrics
2829

30+
@pytest.mark.live_test_only
31+
@pytest.mark.asyncio
32+
async def test_metrics_granularity():
33+
credential = _credential()
34+
client = MetricsQueryClient(credential)
35+
response = await client.query(
36+
os.environ['METRICS_RESOURCE_URI'],
37+
metric_names=["MatchedEventCount"],
38+
timespan=timedelta(days=1),
39+
granularity=timedelta(minutes=5),
40+
aggregations=[AggregationType.COUNT]
41+
)
42+
assert response
43+
assert response.granularity == timedelta(minutes=5)
44+
2945
@pytest.mark.live_test_only
3046
async def test_metrics_namespaces():
3147
client = MetricsQueryClient(_credential())

sdk/monitor/azure-monitor-query/tests/test_metrics_client.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,20 @@ def test_metrics_auth():
2525
assert response
2626
assert response.metrics
2727

28+
@pytest.mark.live_test_only
29+
def test_metrics_granularity():
30+
credential = _credential()
31+
client = MetricsQueryClient(credential)
32+
response = client.query(
33+
os.environ['METRICS_RESOURCE_URI'],
34+
metric_names=["MatchedEventCount"],
35+
timespan=timedelta(days=1),
36+
granularity=timedelta(minutes=5),
37+
aggregations=[AggregationType.COUNT]
38+
)
39+
assert response
40+
assert response.granularity == timedelta(minutes=5)
41+
2842
@pytest.mark.live_test_only
2943
def test_metrics_namespaces():
3044
client = MetricsQueryClient(_credential())

0 commit comments

Comments
 (0)