Skip to content

Commit 4bbd687

Browse files
authored
[Monitor Query] Update docstrings/samples (#30811)
This cleans up the samples and adds a new sample for async batch queries. New authentication samples were added to cleanly organize code snippets for class docstrings. Signed-off-by: Paul Van Eck <[email protected]>
1 parent e016b9b commit 4bbd687

26 files changed

+553
-286
lines changed

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

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -31,21 +31,21 @@ class LogsQueryClient(object): # pylint: disable=client-accepts-api-version-key
3131
apps can be consolidated into a single Azure Log Analytics workspace.
3232
3333
The various data types can be analyzed together using the
34-
[Kusto Query Language](https://docs.microsoft.com/azure/data-explorer/kusto/query/)
35-
36-
.. admonition:: Example:
37-
38-
.. literalinclude:: ../samples/sample_single_logs_query.py
39-
:start-after: [START client_auth_with_token_cred]
40-
:end-before: [END client_auth_with_token_cred]
41-
:language: python
42-
:dedent: 0
43-
:caption: Creating the LogsQueryClient with a TokenCredential.
34+
[Kusto Query Language](https://learn.microsoft.com/azure/data-explorer/kusto/query/)
4435
4536
:param credential: The credential to authenticate the client.
4637
:type credential: ~azure.core.credentials.TokenCredential
4738
:keyword endpoint: The endpoint to connect to. Defaults to 'https://api.loganalytics.io/v1'.
4839
:paramtype endpoint: Optional[str]
40+
41+
.. admonition:: Example:
42+
43+
.. literalinclude:: ../samples/sample_authentication.py
44+
:start-after: [START create_logs_query_client]
45+
:end-before: [END create_logs_query_client]
46+
:language: python
47+
:dedent: 4
48+
:caption: Creating the LogsQueryClient with a TokenCredential.
4949
"""
5050

5151
def __init__(self, credential: TokenCredential, **kwargs: Any) -> None:
@@ -81,7 +81,7 @@ def query_workspace(
8181
Azure portal.
8282
:type workspace_id: str
8383
:param query: The Kusto query. Learn more about the `Kusto query syntax
84-
<https://docs.microsoft.com/azure/data-explorer/kusto/query/>`_.
84+
<https://learn.microsoft.com/azure/data-explorer/kusto/query/>`_.
8585
:type query: str
8686
:keyword timespan: Required. The timespan for which to query the data. This can be a timedelta,
8787
a timedelta and a start datetime, or a start datetime/end datetime. Set to None to not constrain
@@ -103,12 +103,12 @@ def query_workspace(
103103
104104
.. admonition:: Example:
105105
106-
.. literalinclude:: ../samples/sample_single_logs_query.py
107-
:start-after: [START send_logs_query]
108-
:end-before: [END send_logs_query]
109-
:language: python
110-
:dedent: 0
111-
:caption: Get a response for a single Log Query
106+
.. literalinclude:: ../samples/sample_logs_single_query.py
107+
:start-after: [START send_logs_query]
108+
:end-before: [END send_logs_query]
109+
:language: python
110+
:dedent: 0
111+
:caption: Get a response for a single log query.
112112
"""
113113
timespan_iso = construct_iso8601(timespan)
114114
include_statistics = kwargs.pop("include_statistics", False)
@@ -157,12 +157,12 @@ def query_batch(
157157
158158
.. admonition:: Example:
159159
160-
.. literalinclude:: ../samples/sample_batch_query.py
161-
:start-after: [START send_query_batch]
162-
:end-before: [END send_query_batch]
163-
:language: python
164-
:dedent: 0
165-
:caption: Get a response for multiple Log Queries.
160+
.. literalinclude:: ../samples/sample_batch_query.py
161+
:start-after: [START send_query_batch]
162+
:end-before: [END send_query_batch]
163+
:language: python
164+
:dedent: 0
165+
:caption: Execute multiple queries in a batch.
166166
"""
167167
try:
168168
queries = [LogsBatchQuery(**cast(Dict, q)) for q in queries]
@@ -199,7 +199,7 @@ def query_resource(
199199
'/subscriptions/<sid>/resourceGroups/<rg>/providers/<providerName>/<resourceType>/<resourceName>'.
200200
:type resource_id: str
201201
:param query: The Kusto query. Learn more about the `Kusto query syntax
202-
<https://docs.microsoft.com/azure/data-explorer/kusto/query/>`_.
202+
<https://learn.microsoft.com/azure/data-explorer/kusto/query/>`_.
203203
:type query: str
204204
:keyword timespan: Required. The timespan for which to query the data. This can be a timedelta,
205205
a timedelta and a start datetime, or a start datetime/end datetime. Set to None to not constrain
@@ -221,12 +221,12 @@ def query_resource(
221221
222222
.. admonition:: Example:
223223
224-
.. literalinclude:: ../samples/sample_resource_logs_query.py
225-
:start-after: [START resource_logs_query]
226-
:end-before: [END resource_logs_query]
227-
:language: python
228-
:dedent: 0
229-
:caption: Get a response for a single query on a resource's logs.
224+
.. literalinclude:: ../samples/sample_resource_logs_query.py
225+
:start-after: [START resource_logs_query]
226+
:end-before: [END resource_logs_query]
227+
:language: python
228+
:dedent: 0
229+
:caption: Get a response for a single query on a resource's logs.
230230
"""
231231
timespan_iso = construct_iso8601(timespan)
232232
include_statistics = kwargs.pop("include_statistics", False)

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

Lines changed: 33 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -24,19 +24,19 @@ class MetricsQueryClient(object): # pylint: disable=client-accepts-api-version-
2424
supporting near real-time scenarios, making them particularly useful for alerting and
2525
fast detection of issues.
2626
27-
.. admonition:: Example:
28-
29-
.. literalinclude:: ../samples/sample_metrics_query.py
30-
:start-after: [START metrics_client_auth_with_token_cred]
31-
:end-before: [END metrics_client_auth_with_token_cred]
32-
:language: python
33-
:dedent: 0
34-
:caption: Creating the MetricsQueryClient with a TokenCredential.
35-
3627
:param credential: The credential to authenticate the client.
3728
:type credential: ~azure.core.credentials.TokenCredential
3829
:keyword endpoint: The endpoint to connect to. Defaults to 'https://management.azure.com'.
3930
:paramtype endpoint: Optional[str]
31+
32+
.. admonition:: Example:
33+
34+
.. literalinclude:: ../samples/sample_authentication.py
35+
:start-after: [START create_metrics_query_client]
36+
:end-before: [END create_metrics_query_client]
37+
:language: python
38+
:dedent: 4
39+
:caption: Creating the MetricsQueryClient with a TokenCredential.
4040
"""
4141

4242
def __init__(self, credential: TokenCredential, **kwargs: Any) -> None:
@@ -102,12 +102,12 @@ def query_resource(self, resource_uri: str, metric_names: List[str], **kwargs: A
102102
103103
.. admonition:: Example:
104104
105-
.. literalinclude:: ../samples/sample_metrics_query_client.py
106-
:start-after: [START send_metrics_query]
107-
:end-before: [END send_metrics_query]
108-
:language: python
109-
:dedent: 0
110-
:caption: Get a response for a single Metrics Query
105+
.. literalinclude:: ../samples/sample_metrics_query.py
106+
:start-after: [START send_metrics_query]
107+
:end-before: [END send_metrics_query]
108+
:language: python
109+
:dedent: 0
110+
:caption: Get a response for a single metrics query.
111111
"""
112112

113113
aggregations = kwargs.pop("aggregations", None)
@@ -137,6 +137,15 @@ def list_metric_namespaces(self, resource_uri: str, **kwargs: Any) -> ItemPaged[
137137
:return: An iterator like instance of either MetricNamespace or the result of cls(response)
138138
:rtype: ~azure.core.paging.ItemPaged[~azure.monitor.query.MetricNamespace]
139139
:raises: ~azure.core.exceptions.HttpResponseError
140+
141+
.. admonition:: Example:
142+
143+
.. literalinclude:: ../samples/sample_metric_namespaces.py
144+
:start-after: [START send_metric_namespaces_query]
145+
:end-before: [END send_metric_namespaces_query]
146+
:language: python
147+
:dedent: 0
148+
:caption: Get a response for a single metric namespaces query.
140149
"""
141150
start_time = kwargs.pop("start_time", None)
142151
if start_time:
@@ -163,6 +172,15 @@ def list_metric_definitions(self, resource_uri: str, **kwargs: Any) -> ItemPaged
163172
:return: An iterator like instance of either MetricDefinition or the result of cls(response)
164173
:rtype: ~azure.core.paging.ItemPaged[~azure.monitor.query.MetricDefinition]
165174
:raises: ~azure.core.exceptions.HttpResponseError
175+
176+
.. admonition:: Example:
177+
178+
.. literalinclude:: ../samples/sample_metric_definitions.py
179+
:start-after: [START send_metric_definitions_query]
180+
:end-before: [END send_metric_definitions_query]
181+
:language: python
182+
:dedent: 0
183+
:caption: Get a response for a single metric definitions query.
166184
"""
167185
metric_namespace = kwargs.pop("namespace", None)
168186
res = self._definitions_op.list(

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

Lines changed: 36 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,21 @@ class LogsQueryClient(object): # pylint: disable=client-accepts-api-version-key
2626
apps can be consolidated into a single Azure Log Analytics workspace.
2727
2828
The various data types can be analyzed together using the
29-
[Kusto Query Language](https://docs.microsoft.com/azure/data-explorer/kusto/query/)
29+
[Kusto Query Language](https://learn.microsoft.com/azure/data-explorer/kusto/query/)
3030
3131
:param credential: The credential to authenticate the client
3232
:type credential: ~azure.core.credentials_async.AsyncTokenCredential
3333
:keyword endpoint: The endpoint to connect to. Defaults to 'https://api.loganalytics.io/v1'.
3434
:paramtype endpoint: Optional[str]
35+
36+
.. admonition:: Example:
37+
38+
.. literalinclude:: ../samples/async_samples/sample_authentication_async.py
39+
:start-after: [START create_logs_query_client_async]
40+
:end-before: [END create_logs_query_client_async]
41+
:language: python
42+
:dedent: 4
43+
:caption: Creating the asynchronous LogsQueryClient with a TokenCredential.
3544
"""
3645

3746
def __init__(self, credential: AsyncTokenCredential, **kwargs: Any) -> None:
@@ -68,7 +77,7 @@ async def query_workspace(
6877
Azure portal.
6978
:type workspace_id: str
7079
:param query: The Kusto query. Learn more about the `Kusto query syntax
71-
<https://docs.microsoft.com/azure/data-explorer/kusto/query/>`_.
80+
<https://learn.microsoft.com/azure/data-explorer/kusto/query/>`_.
7281
:type query: str
7382
:keyword timespan: Required. The timespan for which to query the data. This can be a timedelta,
7483
a timedelta and a start datetime, or a start datetime/end datetime. Set to None to not constrain
@@ -87,6 +96,15 @@ async def query_workspace(
8796
:return: LogsQueryResult if there is a success or LogsQueryPartialResult when there is a partial success.
8897
:rtype: ~azure.monitor.query.LogsQueryResult or ~azure.monitor.query.LogsQueryPartialResult
8998
:raises: ~azure.core.exceptions.HttpResponseError
99+
100+
.. admonition:: Example:
101+
102+
.. literalinclude:: ../samples/async_samples/sample_log_query_async.py
103+
:start-after: [START send_logs_query_async]
104+
:end-before: [END send_logs_query_async]
105+
:language: python
106+
:dedent: 0
107+
:caption: Get a response for a single log query.
90108
"""
91109
timespan_iso = construct_iso8601(timespan)
92110
include_statistics = kwargs.pop("include_statistics", False)
@@ -131,6 +149,15 @@ async def query_batch(
131149
:rtype: list[~azure.monitor.query.LogsQueryResult or ~azure.monitor.query.LogsQueryPartialResult
132150
or ~azure.monitor.query.LogsQueryError]
133151
:raises: ~azure.core.exceptions.HttpResponseError
152+
153+
.. admonition:: Example:
154+
155+
.. literalinclude:: ../samples/async_samples/sample_batch_query_async.py
156+
:start-after: [START send_query_batch_async]
157+
:end-before: [END send_query_batch_async]
158+
:language: python
159+
:dedent: 0
160+
:caption: Execute multiple queries in a batch.
134161
"""
135162
try:
136163
queries = [LogsBatchQuery(**cast(Dict, q)) for q in queries]
@@ -167,7 +194,7 @@ async def query_resource(
167194
'/subscriptions/<sid>/resourceGroups/<rg>/providers/<providerName>/<resourceType>/<resourceName>'.
168195
:type resource_id: str
169196
:param query: The Kusto query. Learn more about the `Kusto query syntax
170-
<https://docs.microsoft.com/azure/data-explorer/kusto/query/>`_.
197+
<https://learn.microsoft.com/azure/data-explorer/kusto/query/>`_.
171198
:type query: str
172199
:keyword timespan: Required. The timespan for which to query the data. This can be a timedelta,
173200
a timedelta and a start datetime, or a start datetime/end datetime. Set to None to not constrain
@@ -189,12 +216,12 @@ async def query_resource(
189216
190217
.. admonition:: Example:
191218
192-
.. literalinclude:: ../samples/async_samples/sample_resource_logs_query_async.py
193-
:start-after: [START resource_logs_query_async]
194-
:end-before: [END resource_logs_query_async]
195-
:language: python
196-
:dedent: 0
197-
:caption: Get a response for a single query on a resource's logs.
219+
.. literalinclude:: ../samples/async_samples/sample_resource_logs_query_async.py
220+
:start-after: [START resource_logs_query_async]
221+
:end-before: [END resource_logs_query_async]
222+
:language: python
223+
:dedent: 0
224+
:caption: Get a response for a single query on a resource's logs.
198225
"""
199226
timespan_iso = construct_iso8601(timespan)
200227
include_statistics = kwargs.pop("include_statistics", False)

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

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,15 @@ class MetricsQueryClient(object): # pylint: disable=client-accepts-api-version-
2727
:type credential: ~azure.core.credentials_async.AsyncTokenCredential
2828
:keyword endpoint: The endpoint to connect to. Defaults to 'https://management.azure.com'.
2929
:paramtype endpoint: Optional[str]
30+
31+
.. admonition:: Example:
32+
33+
.. literalinclude:: ../samples/async_samples/sample_authentication_async.py
34+
:start-after: [START create_metrics_query_client_async]
35+
:end-before: [END create_metrics_query_client_async]
36+
:language: python
37+
:dedent: 4
38+
:caption: Creating the asynchronous MetricsQueryClient with a TokenCredential.
3039
"""
3140

3241
def __init__(self, credential: AsyncTokenCredential, **kwargs: Any) -> None:
@@ -92,6 +101,15 @@ async def query_resource(self, resource_uri: str, metric_names: List[str], **kwa
92101
:return: A MetricsQueryResult object.
93102
:rtype: ~azure.monitor.query.MetricsQueryResult
94103
:raises: ~azure.core.exceptions.HttpResponseError
104+
105+
.. admonition:: Example:
106+
107+
.. literalinclude:: ../samples/async_samples/sample_metrics_query_async.py
108+
:start-after: [START send_metrics_query_async]
109+
:end-before: [END send_metrics_query_async]
110+
:language: python
111+
:dedent: 0
112+
:caption: Get a response for a single metrics query.
95113
"""
96114
timespan = construct_iso8601(kwargs.pop("timespan", None))
97115
# Metric names with commas need to be encoded.
@@ -120,6 +138,15 @@ def list_metric_namespaces(self, resource_uri: str, **kwargs: Any) -> AsyncItemP
120138
:return: An iterator like instance of either MetricNamespace or the result of cls(response)
121139
:rtype: ~azure.core.paging.AsyncItemPaged[:class: `~azure.monitor.query.MetricNamespace`]
122140
:raises: ~azure.core.exceptions.HttpResponseError
141+
142+
.. admonition:: Example:
143+
144+
.. literalinclude:: ../samples/async_samples/sample_metric_namespaces_async.py
145+
:start-after: [START send_metric_namespaces_query_async]
146+
:end-before: [END send_metric_namespaces_query_async]
147+
:language: python
148+
:dedent: 0
149+
:caption: Get a response for a single metric namespaces query.
123150
"""
124151
start_time = kwargs.pop("start_time", None)
125152
if start_time:
@@ -146,6 +173,15 @@ def list_metric_definitions(self, resource_uri: str, **kwargs: Any) -> AsyncItem
146173
:return: An iterator like instance of either MetricDefinition or the result of cls(response)
147174
:rtype: ~azure.core.paging.AsyncItemPaged[:class: `~azure.monitor.query.MetricDefinition`]
148175
:raises: ~azure.core.exceptions.HttpResponseError
176+
177+
.. admonition:: Example:
178+
179+
.. literalinclude:: ../samples/async_samples/sample_metric_definitions_async.py
180+
:start-after: [START send_metric_definitions_query_async]
181+
:end-before: [END send_metric_definitions_query_async]
182+
:language: python
183+
:dedent: 0
184+
:caption: Get a response for a single metric definitions query.
149185
"""
150186
metric_namespace = kwargs.pop("namespace", None)
151187
res = self._definitions_op.list(

0 commit comments

Comments
 (0)