-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Rakshith Bhyravabhotla
authored
Jun 18, 2021
1 parent
c8291ac
commit f3bceec
Showing
4 changed files
with
175 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
sdk/monitor/azure-monitor-query/samples/sample_log_query_client_without_pandas.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
# Copyright (c) Microsoft Corporation. All rights reserved. | ||
# Licensed under the MIT License. | ||
|
||
import os | ||
from datetime import datetime | ||
from msrest.serialization import UTC | ||
from azure.monitor.query import LogsQueryClient | ||
from azure.identity import ClientSecretCredential | ||
|
||
# [START client_auth_with_token_cred] | ||
credential = ClientSecretCredential( | ||
client_id = os.environ['AZURE_CLIENT_ID'], | ||
client_secret = os.environ['AZURE_CLIENT_SECRET'], | ||
tenant_id = os.environ['AZURE_TENANT_ID'] | ||
) | ||
|
||
client = LogsQueryClient(credential) | ||
# [END client_auth_with_token_cred] | ||
|
||
# Response time trend | ||
# request duration over the last 12 hours. | ||
# [START send_logs_query] | ||
query = """AppRequests | | ||
summarize avgRequestDuration=avg(DurationMs) by bin(TimeGenerated, 10m), _ResourceId""" | ||
|
||
end_time = datetime.now(UTC()) | ||
|
||
# returns LogsQueryResults | ||
response = client.query(os.environ['LOG_WORKSPACE_ID'], query, duration='PT1H', end_time=end_time) | ||
|
||
if not response.tables: | ||
print("No results for the query") | ||
|
||
#response.tables is a LogsQueryResultTable | ||
for table in response.tables: | ||
for col in table.columns: #LogsQueryResultColumn | ||
print(col.name + "/"+ col.type + " | ", end="") | ||
print("\n") | ||
for row in table.rows: | ||
for item in row: | ||
print(item + " | ", end="") | ||
print("\n") | ||
|
||
|
||
""" | ||
TimeGenerated/datetime | _ResourceId/string | avgRequestDuration/real | | ||
2021-05-11T08:20:00Z | /subscriptions/<subscription id>/resourcegroups/cobey-azuresdkshinydashboardgrp/providers/microsoft.insights/components/cobey-willthisbestatic | 10.8915 | | ||
2021-05-11T08:30:00Z | /subscriptions/<subscription id>/resourcegroups/cobey-azuresdkshinydashboardgrp/providers/microsoft.insights/components/cobey-willthisbestatic | 33.23276666666667 | | ||
2021-05-11T08:40:00Z | /subscriptions/<subscription id>/resourcegroups/cobey-azuresdkshinydashboardgrp/providers/microsoft.insights/components/cobey-willthisbestatic | 21.83535 | | ||
2021-05-11T08:50:00Z | /subscriptions/<subscription id>/resourcegroups/cobey-azuresdkshinydashboardgrp/providers/microsoft.insights/components/cobey-willthisbestatic | 11.028649999999999 | | ||
""" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
92 changes: 92 additions & 0 deletions
92
sdk/monitor/azure-monitor-query/tests/test_logs_timespans.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
from datetime import datetime, time, timedelta | ||
import pytest | ||
import json | ||
import os | ||
from msrest.serialization import UTC | ||
|
||
from azure.identity import ClientSecretCredential | ||
from azure.core.exceptions import HttpResponseError | ||
from azure.monitor.query import LogsQueryClient, LogsQueryRequest | ||
|
||
def _credential(): | ||
credential = ClientSecretCredential( | ||
client_id = os.environ['AZURE_CLIENT_ID'], | ||
client_secret = os.environ['AZURE_CLIENT_SECRET'], | ||
tenant_id = os.environ['AZURE_TENANT_ID'] | ||
) | ||
return credential | ||
|
||
@pytest.mark.live_test_only | ||
def test_query_no_duration(): | ||
credential = _credential() | ||
client = LogsQueryClient(credential) | ||
query = """AppRequests | | ||
where TimeGenerated > ago(12h) | | ||
summarize avgRequestDuration=avg(DurationMs) by bin(TimeGenerated, 10m), _ResourceId""" | ||
|
||
def callback(request): | ||
dic = json.loads(request.http_request.body) | ||
assert dic.get('timespan') is None | ||
# returns LogsQueryResults | ||
client.query(os.environ['LOG_WORKSPACE_ID'], query) | ||
|
||
@pytest.mark.live_test_only | ||
def test_query_start_and_end_time(): | ||
credential = _credential() | ||
client = LogsQueryClient(credential) | ||
query = "AppRequests | take 5" | ||
|
||
end_time = datetime.now(UTC()) | ||
start_time = end_time - timedelta(days=3) | ||
|
||
def callback(request): | ||
dic = json.loads(request.http_request.body) | ||
assert dic.get('timespan') is not None | ||
|
||
client.query(os.environ['LOG_WORKSPACE_ID'], query, start_time=start_time, end_time=end_time, raw_request_hook=callback) | ||
|
||
@pytest.mark.live_test_only | ||
def test_query_duration_and_end_time(): | ||
credential = _credential() | ||
client = LogsQueryClient(credential) | ||
query = "AppRequests | take 5" | ||
|
||
end_time = datetime.now(UTC()) | ||
duration = 'P3D' | ||
|
||
def callback(request): | ||
dic = json.loads(request.http_request.body) | ||
assert 'P3D/' in dic.get('timespan') | ||
|
||
client.query(os.environ['LOG_WORKSPACE_ID'], query, duration=duration, end_time=end_time, raw_request_hook=callback) | ||
|
||
@pytest.mark.live_test_only | ||
def test_query_duration_and_start_time(): | ||
credential = _credential() | ||
client = LogsQueryClient(credential) | ||
query = "AppRequests | take 5" | ||
|
||
end_time = datetime.now(UTC()) | ||
start_time = end_time - timedelta(days=3) | ||
duration = 'P3D' | ||
|
||
def callback(request): | ||
dic = json.loads(request.http_request.body) | ||
assert '/P3D' in dic.get('timespan') | ||
|
||
client.query(os.environ['LOG_WORKSPACE_ID'], query, duration=duration, start_time=start_time, raw_request_hook=callback) | ||
|
||
|
||
@pytest.mark.live_test_only | ||
def test_query_duration_only(): | ||
credential = _credential() | ||
client = LogsQueryClient(credential) | ||
query = "AppRequests | take 5" | ||
|
||
duration = 'P3D' | ||
|
||
def callback(request): | ||
dic = json.loads(request.http_request.body) | ||
assert 'P3D' in dic.get('timespan') | ||
|
||
client.query(os.environ['LOG_WORKSPACE_ID'], query, duration=duration, raw_request_hook=callback) |