Skip to content

Commit f3bceec

Browse files
author
Rakshith Bhyravabhotla
authored
query - add more tests (Azure#19316)
1 parent c8291ac commit f3bceec

File tree

4 files changed

+175
-29
lines changed

4 files changed

+175
-29
lines changed

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

Lines changed: 5 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import os
55
import pandas as pd
66
from datetime import datetime
7+
from msrest.serialization import UTC
78
from azure.monitor.query import LogsQueryClient
89
from azure.identity import ClientSecretCredential
910

@@ -20,12 +21,13 @@
2021
# Response time trend
2122
# request duration over the last 12 hours.
2223
# [START send_logs_query]
23-
query = """AppRequests |
24-
where TimeGenerated > ago(12h) |
24+
query = """AppRequests |
2525
summarize avgRequestDuration=avg(DurationMs) by bin(TimeGenerated, 10m), _ResourceId"""
2626

27+
end_time = datetime.now(UTC())
28+
2729
# returns LogsQueryResults
28-
response = client.query(os.environ['LOG_WORKSPACE_ID'], query, start_time=datetime(2021, 6, 2), end_time=datetime.now())
30+
response = client.query(os.environ['LOG_WORKSPACE_ID'], query, duration='PT1H', end_time=end_time)
2931

3032
if not response.tables:
3133
print("No results for the query")
@@ -40,28 +42,3 @@
4042
1 2021-05-27T08:50:00Z /subscriptions/faa080af-c1d8-40ad-9cce-e1a450c... 18.11655
4143
2 2021-05-27T09:00:00Z /subscriptions/faa080af-c1d8-40ad-9cce-e1a450c... 24.5271
4244
"""
43-
44-
# if you dont want to use pandas - here's how you can process it.
45-
46-
#response.tables is a LogsQueryResultTable
47-
for table in response.tables:
48-
for col in table.columns: #LogsQueryResultColumn
49-
print(col.name + "/"+ col.type + " | ", end="")
50-
print("\n")
51-
for row in table.rows:
52-
for item in row:
53-
print(item + " | ", end="")
54-
print("\n")
55-
56-
57-
"""
58-
TimeGenerated/datetime | _ResourceId/string | avgRequestDuration/real |
59-
60-
2021-05-11T08:20:00Z | /subscriptions/<subscription id>/resourcegroups/cobey-azuresdkshinydashboardgrp/providers/microsoft.insights/components/cobey-willthisbestatic | 10.8915 |
61-
62-
2021-05-11T08:30:00Z | /subscriptions/<subscription id>/resourcegroups/cobey-azuresdkshinydashboardgrp/providers/microsoft.insights/components/cobey-willthisbestatic | 33.23276666666667 |
63-
64-
2021-05-11T08:40:00Z | /subscriptions/<subscription id>/resourcegroups/cobey-azuresdkshinydashboardgrp/providers/microsoft.insights/components/cobey-willthisbestatic | 21.83535 |
65-
66-
2021-05-11T08:50:00Z | /subscriptions/<subscription id>/resourcegroups/cobey-azuresdkshinydashboardgrp/providers/microsoft.insights/components/cobey-willthisbestatic | 11.028649999999999 |
67-
"""
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
# Copyright (c) Microsoft Corporation. All rights reserved.
2+
# Licensed under the MIT License.
3+
4+
import os
5+
from datetime import datetime
6+
from msrest.serialization import UTC
7+
from azure.monitor.query import LogsQueryClient
8+
from azure.identity import ClientSecretCredential
9+
10+
# [START client_auth_with_token_cred]
11+
credential = ClientSecretCredential(
12+
client_id = os.environ['AZURE_CLIENT_ID'],
13+
client_secret = os.environ['AZURE_CLIENT_SECRET'],
14+
tenant_id = os.environ['AZURE_TENANT_ID']
15+
)
16+
17+
client = LogsQueryClient(credential)
18+
# [END client_auth_with_token_cred]
19+
20+
# Response time trend
21+
# request duration over the last 12 hours.
22+
# [START send_logs_query]
23+
query = """AppRequests |
24+
summarize avgRequestDuration=avg(DurationMs) by bin(TimeGenerated, 10m), _ResourceId"""
25+
26+
end_time = datetime.now(UTC())
27+
28+
# returns LogsQueryResults
29+
response = client.query(os.environ['LOG_WORKSPACE_ID'], query, duration='PT1H', end_time=end_time)
30+
31+
if not response.tables:
32+
print("No results for the query")
33+
34+
#response.tables is a LogsQueryResultTable
35+
for table in response.tables:
36+
for col in table.columns: #LogsQueryResultColumn
37+
print(col.name + "/"+ col.type + " | ", end="")
38+
print("\n")
39+
for row in table.rows:
40+
for item in row:
41+
print(item + " | ", end="")
42+
print("\n")
43+
44+
45+
"""
46+
TimeGenerated/datetime | _ResourceId/string | avgRequestDuration/real |
47+
48+
2021-05-11T08:20:00Z | /subscriptions/<subscription id>/resourcegroups/cobey-azuresdkshinydashboardgrp/providers/microsoft.insights/components/cobey-willthisbestatic | 10.8915 |
49+
50+
2021-05-11T08:30:00Z | /subscriptions/<subscription id>/resourcegroups/cobey-azuresdkshinydashboardgrp/providers/microsoft.insights/components/cobey-willthisbestatic | 33.23276666666667 |
51+
52+
2021-05-11T08:40:00Z | /subscriptions/<subscription id>/resourcegroups/cobey-azuresdkshinydashboardgrp/providers/microsoft.insights/components/cobey-willthisbestatic | 21.83535 |
53+
54+
2021-05-11T08:50:00Z | /subscriptions/<subscription id>/resourcegroups/cobey-azuresdkshinydashboardgrp/providers/microsoft.insights/components/cobey-willthisbestatic | 11.028649999999999 |
55+
"""

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

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ def _credential():
1313
return credential
1414

1515
@pytest.mark.live_test_only
16-
def test_logs_auth():
16+
def test_logs_single_query():
1717
credential = _credential()
1818
client = LogsQueryClient(credential)
1919
query = """AppRequests |
@@ -26,6 +26,28 @@ def test_logs_auth():
2626
assert response is not None
2727
assert response.tables is not None
2828

29+
@pytest.mark.live_test_only
30+
def test_logs_single_query_with_non_200():
31+
credential = _credential()
32+
client = LogsQueryClient(credential)
33+
query = """AppInsights |
34+
where TimeGenerated > ago(12h)"""
35+
36+
with pytest.raises(HttpResponseError) as e:
37+
client.query(os.environ['LOG_WORKSPACE_ID'], query)
38+
39+
assert "SemanticError" in e.value.message
40+
41+
@pytest.mark.live_test_only
42+
def test_logs_single_query_with_partial_success():
43+
credential = _credential()
44+
client = LogsQueryClient(credential)
45+
query = "set truncationmaxrecords=1; union * | project TimeGenerated | take 10"
46+
47+
response = client.query(os.environ['LOG_WORKSPACE_ID'], query)
48+
49+
assert response is not None
50+
2951
@pytest.mark.live_test_only
3052
def test_logs_server_timeout():
3153
client = LogsQueryClient(_credential())
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
from datetime import datetime, time, timedelta
2+
import pytest
3+
import json
4+
import os
5+
from msrest.serialization import UTC
6+
7+
from azure.identity import ClientSecretCredential
8+
from azure.core.exceptions import HttpResponseError
9+
from azure.monitor.query import LogsQueryClient, LogsQueryRequest
10+
11+
def _credential():
12+
credential = ClientSecretCredential(
13+
client_id = os.environ['AZURE_CLIENT_ID'],
14+
client_secret = os.environ['AZURE_CLIENT_SECRET'],
15+
tenant_id = os.environ['AZURE_TENANT_ID']
16+
)
17+
return credential
18+
19+
@pytest.mark.live_test_only
20+
def test_query_no_duration():
21+
credential = _credential()
22+
client = LogsQueryClient(credential)
23+
query = """AppRequests |
24+
where TimeGenerated > ago(12h) |
25+
summarize avgRequestDuration=avg(DurationMs) by bin(TimeGenerated, 10m), _ResourceId"""
26+
27+
def callback(request):
28+
dic = json.loads(request.http_request.body)
29+
assert dic.get('timespan') is None
30+
# returns LogsQueryResults
31+
client.query(os.environ['LOG_WORKSPACE_ID'], query)
32+
33+
@pytest.mark.live_test_only
34+
def test_query_start_and_end_time():
35+
credential = _credential()
36+
client = LogsQueryClient(credential)
37+
query = "AppRequests | take 5"
38+
39+
end_time = datetime.now(UTC())
40+
start_time = end_time - timedelta(days=3)
41+
42+
def callback(request):
43+
dic = json.loads(request.http_request.body)
44+
assert dic.get('timespan') is not None
45+
46+
client.query(os.environ['LOG_WORKSPACE_ID'], query, start_time=start_time, end_time=end_time, raw_request_hook=callback)
47+
48+
@pytest.mark.live_test_only
49+
def test_query_duration_and_end_time():
50+
credential = _credential()
51+
client = LogsQueryClient(credential)
52+
query = "AppRequests | take 5"
53+
54+
end_time = datetime.now(UTC())
55+
duration = 'P3D'
56+
57+
def callback(request):
58+
dic = json.loads(request.http_request.body)
59+
assert 'P3D/' in dic.get('timespan')
60+
61+
client.query(os.environ['LOG_WORKSPACE_ID'], query, duration=duration, end_time=end_time, raw_request_hook=callback)
62+
63+
@pytest.mark.live_test_only
64+
def test_query_duration_and_start_time():
65+
credential = _credential()
66+
client = LogsQueryClient(credential)
67+
query = "AppRequests | take 5"
68+
69+
end_time = datetime.now(UTC())
70+
start_time = end_time - timedelta(days=3)
71+
duration = 'P3D'
72+
73+
def callback(request):
74+
dic = json.loads(request.http_request.body)
75+
assert '/P3D' in dic.get('timespan')
76+
77+
client.query(os.environ['LOG_WORKSPACE_ID'], query, duration=duration, start_time=start_time, raw_request_hook=callback)
78+
79+
80+
@pytest.mark.live_test_only
81+
def test_query_duration_only():
82+
credential = _credential()
83+
client = LogsQueryClient(credential)
84+
query = "AppRequests | take 5"
85+
86+
duration = 'P3D'
87+
88+
def callback(request):
89+
dic = json.loads(request.http_request.body)
90+
assert 'P3D' in dic.get('timespan')
91+
92+
client.query(os.environ['LOG_WORKSPACE_ID'], query, duration=duration, raw_request_hook=callback)

0 commit comments

Comments
 (0)