def test_logs_single_query_fatal_exception(): credential = _credential() client = LogsQueryClient(credential) with pytest.raises(HttpResponseError): client.query_workspace('bad_workspace_id', 'AppRequests', timespan=None)
def test_logs_single_query_raises_no_timespan(): credential = _credential() client = LogsQueryClient(credential) query = """AppRequests | where TimeGenerated > ago(12h) | summarize avgRequestDuration=avg(DurationMs) by bin(TimeGenerated, 10m), _ResourceId""" # returns LogsQueryResult with pytest.raises(TypeError): client.query_workspace(os.environ['LOG_WORKSPACE_ID'], query)
def test_logs_single_query_with_non_200(): credential = _credential() client = LogsQueryClient(credential) query = """AppInsights | where TimeGenerated > ago(12h)""" with pytest.raises(HttpResponseError) as e: client.query_workspace(os.environ['LOG_WORKSPACE_ID'], query, timespan=None) assert "SemanticError" in e.value.message
class LogsPerfTest(PerfStressTest): def __init__(self, arguments): super().__init__(arguments) # auth configuration self.workspace_id = self.get_from_env('LOG_WORKSPACE_ID') self.query = "AppRequests | summarize avgRequestDuration=avg(DurationMs) by bin(TimeGenerated, 10m), _ResourceId" # Create clients self.logs_client = SyncLogsQueryClient( credential=SyncDefaultAzureCredential()) self.async_logs_client = AsyncLogsQueryClient( credential=AsyncDefaultAzureCredential()) async def close(self): """This is run after cleanup. Use this to close any open handles or clients. """ await self.async_logs_client.close() await super().close() def run_sync(self): """The synchronous perf test. Try to keep this minimal and focused. Using only a single client API. Avoid putting any ancillary logic (e.g. generating UUIDs), and put this in the setup/init instead so that we're only measuring the client API call. """ start_time = datetime(2021, 7, 25, 0, 0, 0, tzinfo=timezone.utc) end_time = datetime(2021, 7, 26, 0, 0, 0, tzinfo=timezone.utc) self.logs_client.query_workspace(self.workspace_id, self.query, timespan=(start_time, end_time)) async def run_async(self): """The asynchronous perf test. Try to keep this minimal and focused. Using only a single client API. Avoid putting any ancillary logic (e.g. generating UUIDs), and put this in the setup/init instead so that we're only measuring the client API call. """ start_time = datetime(2021, 7, 25, 0, 0, 0, tzinfo=timezone.utc) end_time = datetime(2021, 7, 26, 0, 0, 0, tzinfo=timezone.utc) await self.async_logs_client.query_workspace(self.workspace_id, self.query, timespan=(start_time, end_time))
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 LogsQueryResult client.query_workspace(os.environ['LOG_WORKSPACE_ID'], query, timespan=None)
def test_query_duration_only(): credential = _credential() client = LogsQueryClient(credential) query = "AppRequests | take 5" duration = timedelta(days=3) def callback(request): dic = json.loads(request.http_request.body) assert 'PT259200.0S' in dic.get('timespan') client.query_workspace(os.environ['LOG_WORKSPACE_ID'], query, timespan=duration, raw_request_hook=callback)
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_workspace(os.environ['LOG_WORKSPACE_ID'], query, timespan=(start_time, end_time), raw_request_hook=callback)
def test_logs_single_query_with_render(): credential = _credential() client = LogsQueryClient(credential) query = """AppRequests | take 10""" # returns LogsQueryResult response = client.query_workspace(os.environ['LOG_WORKSPACE_ID'], query, timespan=None, include_visualization=True) assert response.visualization is not None
def test_logs_server_timeout(): client = LogsQueryClient(_credential()) with pytest.raises(HttpResponseError) as e: response = client.query_workspace( os.environ['LOG_WORKSPACE_ID'], "range x from 1 to 1000000000000000 step 1 | count", timespan=None, server_timeout=1, ) assert 'Gateway timeout' in e.value.message
def test_logs_single_query_with_partial_success(): credential = _credential() client = LogsQueryClient(credential) query = """let Weight = 92233720368547758; range x from 1 to 3 step 1 | summarize percentilesw(x, Weight * 100, 50)""" response = client.query_workspace(os.environ['LOG_WORKSPACE_ID'], query, timespan=None) assert response.partial_error is not None assert response.partial_data is not None assert response.__class__ == LogsQueryPartialResult
def test_logs_single_query_with_statistics(): credential = _credential() client = LogsQueryClient(credential) query = """AppRequests""" # returns LogsQueryResult response = client.query_workspace(os.environ['LOG_WORKSPACE_ID'], query, timespan=None, include_statistics=True) assert response.statistics is not None
def test_logs_single_query(): credential = _credential() client = LogsQueryClient(credential) query = """AppRequests | where TimeGenerated > ago(12h) | summarize avgRequestDuration=avg(DurationMs) by bin(TimeGenerated, 10m), _ResourceId""" # returns LogsQueryResult response = client.query_workspace(os.environ['LOG_WORKSPACE_ID'], query, timespan=None) assert response is not None assert response.tables is not None
def test_logs_single_query_additional_workspaces(): credential = _credential() client = LogsQueryClient(credential) query = "union * | where TimeGenerated > ago(100d) | project TenantId | summarize count() by TenantId" # returns LogsQueryResult response = client.query_workspace( os.environ['LOG_WORKSPACE_ID'], query, timespan=None, additional_workspaces=[os.environ["SECONDARY_WORKSPACE_ID"]], ) assert response is not None assert len(response.tables[0].rows) == 2
def test_logs_query_result_row_type(): client = LogsQueryClient(_credential()) query = "AppRequests | take 5" response = client.query_workspace( os.environ['LOG_WORKSPACE_ID'], query, timespan=None, ) ## should iterate over tables for table in response: assert table.__class__ == LogsTable for row in table.rows: assert row.__class__ == LogsTableRow
def test_logs_query_result_iterate_over_tables(): client = LogsQueryClient(_credential()) query = "AppRequests; AppRequests | take 5" response = client.query_workspace(os.environ['LOG_WORKSPACE_ID'], query, timespan=None, include_statistics=True, include_visualization=True) ## should iterate over tables for item in response: assert item.__class__ == LogsTable assert response.statistics is not None assert response.visualization is not None assert len(response.tables) == 2 assert response.__class__ == LogsQueryResult
import os import pandas as pd from datetime import timedelta from azure.monitor.query import LogsQueryClient, LogsQueryStatus from azure.core.exceptions import HttpResponseError from azure.identity import DefaultAzureCredential credential = DefaultAzureCredential() client = LogsQueryClient(credential) query = "range x from 1 to 10000000000 step 1 | count" try: response = client.query_workspace(os.environ['LOGS_WORKSPACE_ID'], query, timespan=timedelta(days=1), server_timeout=3) if response.status == LogsQueryStatus.PARTIAL: error = response.partial_error data = response.partial_data print(error.message) elif response.status == LogsQueryStatus.SUCCESS: data = response.tables for table in data: df = pd.DataFrame(data=table.rows, columns=table.columns) print(df) except HttpResponseError as err: print("something fatal happened") print(err)
For more information on DefaultAzureCredential, see https://docs.microsoft.com/python/api/overview/azure/identity-readme?view=azure-python#defaultazurecredential. **Note** - Although this example uses pandas to print the response, it's optional and isn't a required package for querying. Alternatively, native Python can be used as well. """ import os import pandas as pd from datetime import timedelta from azure.monitor.query import LogsQueryClient from azure.core.exceptions import HttpResponseError from azure.identity import DefaultAzureCredential credential = DefaultAzureCredential() client = LogsQueryClient(credential) query = """AppRequests | take 5""" try: response = client.query_workspace( os.environ['LOGS_WORKSPACE_ID'], query, timespan=timedelta(days=1), additional_workspaces=[os.environ['SECONDARY_WORKSPACE_ID']]) for table in response: df = pd.DataFrame(data=table.rows, columns=table.columns) print(df) except HttpResponseError as err: print("something fatal happened") print(err)