Example #1
0
def test_logs_query_batch_additional_workspaces():
    client = LogsQueryClient(_credential())
    query = "union * | where TimeGenerated > ago(100d) | project TenantId | summarize count() by TenantId"

    requests = [
        LogsBatchQuery(
            os.environ['LOG_WORKSPACE_ID'],
            query,
            timespan=timedelta(hours=1),
            additional_workspaces=[os.environ['SECONDARY_WORKSPACE_ID']]),
        LogsBatchQuery(
            os.environ['LOG_WORKSPACE_ID'],
            query,
            timespan=timedelta(hours=1),
            additional_workspaces=[os.environ['SECONDARY_WORKSPACE_ID']]),
        LogsBatchQuery(
            os.environ['LOG_WORKSPACE_ID'],
            query,
            timespan=timedelta(hours=1),
            additional_workspaces=[os.environ['SECONDARY_WORKSPACE_ID']]),
    ]
    response = client.query_batch(requests)

    for resp in response:
        assert len(resp.tables[0].rows) == 2
Example #2
0
def test_logs_query_batch_default():
    client = LogsQueryClient(_credential())

    requests = [
        LogsBatchQuery(query="AzureActivity | summarize count()",
                       timespan=timedelta(hours=1),
                       workspace_id=os.environ['LOG_WORKSPACE_ID']),
        LogsBatchQuery(query="""AppRequests | take 10  |
                summarize avgRequestDuration=avg(DurationMs) by bin(TimeGenerated, 10m), _ResourceId""",
                       timespan=timedelta(hours=1),
                       workspace_id=os.environ['LOG_WORKSPACE_ID']),
        LogsBatchQuery(query="Wrong query | take 2",
                       workspace_id=os.environ['LOG_WORKSPACE_ID'],
                       timespan=None),
    ]
    response = client.query_batch(requests)

    assert len(response) == 3

    r0 = response[0]
    assert r0.tables[0].columns == ['count_']
    r1 = response[1]
    assert r1.tables[0].columns[0] == 'TimeGenerated'
    assert r1.tables[0].columns[1] == '_ResourceId'
    assert r1.tables[0].columns[2] == 'avgRequestDuration'
    r2 = response[2]
    assert r2.__class__ == LogsQueryError
Example #3
0
def test_logs_query_batch_with_statistics_in_some():
    client = LogsQueryClient(_credential())

    requests = [
        LogsBatchQuery(
            query="AzureActivity | summarize count()",
            timespan=timedelta(hours=1),
            workspace_id= os.environ['LOG_WORKSPACE_ID']
        ),
        LogsBatchQuery(
            query= """AppRequests|
                summarize avgRequestDuration=avg(DurationMs) by bin(TimeGenerated, 10m), _ResourceId""",
            timespan=timedelta(hours=1),
            workspace_id= os.environ['LOG_WORKSPACE_ID'],
            include_statistics=True
        ),
        LogsBatchQuery(
            query= "AppRequests",
            workspace_id= os.environ['LOG_WORKSPACE_ID'],
            timespan=None,
            include_statistics=True
        ),
    ]
    response = client.query_batch(requests)

    assert len(response) == 3
    assert response[0].statistics is None
    assert response[2].statistics is not None
Example #4
0
def test_logs_batch_query_non_fatal_exception():
    credential = _credential()
    client = LogsQueryClient(credential)
    requests = [
        LogsBatchQuery(query="AzureActivity | summarize count()",
                       timespan=timedelta(hours=1),
                       workspace_id=os.environ['LOG_WORKSPACE_ID']),
        LogsBatchQuery(query="""AppRequests | take 10""",
                       timespan=(datetime(2021, 6, 2), timedelta(days=1)),
                       workspace_id=os.environ['LOG_WORKSPACE_ID']),
        LogsBatchQuery(query="""Bad Query""",
                       workspace_id=os.environ['LOG_WORKSPACE_ID'],
                       timespan=(datetime(2021, 6, 2), datetime(2021, 6, 3)),
                       include_statistics=True),
    ]
    responses = client.query_batch(requests)
    r1, r2, r3 = responses[0], responses[1], responses[2]
    assert r1.__class__ == LogsQueryResult
    assert r2.__class__ == LogsQueryResult
    assert r3.__class__ == LogsQueryError
Example #5
0
def test_logs_batch_query_partial_exception():
    credential = _credential()
    client = LogsQueryClient(credential)
    requests = [
        LogsBatchQuery(query="AzureActivity | summarize count()",
                       timespan=timedelta(hours=1),
                       workspace_id=os.environ['LOG_WORKSPACE_ID']),
        LogsBatchQuery(query="""AppRequests | take 10""",
                       timespan=(datetime(2021, 6, 2), timedelta(days=1)),
                       workspace_id=os.environ['LOG_WORKSPACE_ID']),
        LogsBatchQuery(query="""let Weight = 92233720368547758;
            range x from 1 to 3 step 1
            | summarize percentilesw(x, Weight * 100, 50)""",
                       workspace_id=os.environ['LOG_WORKSPACE_ID'],
                       timespan=(datetime(2021, 6, 2), datetime(2021, 6, 3)),
                       include_statistics=True),
    ]
    responses = client.query_batch(requests)
    r1, r2, r3 = responses[0], responses[1], responses[2]
    assert r1.__class__ == LogsQueryResult
    assert r2.__class__ == LogsQueryResult
    assert r3.__class__ == LogsQueryPartialResult
Example #6
0
def test_logs_batch_query_fatal_exception():
    credential = ClientSecretCredential(
        client_id=os.environ['AZURE_CLIENT_ID'],
        client_secret='bad_secret',
        tenant_id=os.environ['AZURE_TENANT_ID'])
    client = LogsQueryClient(credential)
    requests = [
        LogsBatchQuery(query="AzureActivity | summarize count()",
                       timespan=timedelta(hours=1),
                       workspace_id=os.environ['LOG_WORKSPACE_ID']),
        LogsBatchQuery(query="""AppRequestsss | take 10""",
                       timespan=(datetime(2021, 6, 2), timedelta(days=1)),
                       workspace_id=os.environ['LOG_WORKSPACE_ID']),
        LogsBatchQuery(query="""let Weight = 92233720368547758;
            range x from 1 to 3 step 1
            | summarize percentilesw(x, Weight * 100, 50)""",
                       workspace_id=os.environ['LOG_WORKSPACE_ID'],
                       timespan=(datetime(2021, 6, 2), datetime(2021, 6, 3)),
                       include_statistics=True),
    ]
    with pytest.raises(HttpResponseError):
        responses = client.query_batch(requests)
async def test_logs_query_batch_raises_on_no_timespan():
    with pytest.raises(TypeError):
        LogsBatchQuery(query="AzureActivity | summarize count()",
                       workspace_id=os.environ['LOG_WORKSPACE_ID'])
Example #8
0
from datetime import datetime, timedelta
import os
import pandas as pd
from azure.monitor.query import LogsQueryClient, LogsBatchQuery
from azure.identity import DefaultAzureCredential


credential  = DefaultAzureCredential()

client = LogsQueryClient(credential)

# [START send_query_batch]
requests = [
    LogsBatchQuery(
        query="AzureActivity | summarize count()",
        timespan=timedelta(hours=1),
        workspace_id= os.environ['LOG_WORKSPACE_ID']
    ),
    LogsBatchQuery(
        query= """AppRequests | take 10  |
            summarize avgRequestDuration=avg(DurationMs) by bin(TimeGenerated, 10m), _ResourceId""",
        timespan=(datetime(2021, 6, 2), timedelta(hours=1)),
        workspace_id= os.environ['LOG_WORKSPACE_ID']
    ),
    LogsBatchQuery(
        query= "AppRequests | take 5",
        workspace_id= os.environ['LOG_WORKSPACE_ID'],
        timespan=(datetime(2021, 6, 2), datetime(2021, 6, 3)),
        include_statistics=True
    ),
]
"""

from datetime import datetime, timedelta, timezone
import os
import pandas as pd
from azure.monitor.query import LogsQueryClient, LogsBatchQuery, LogsQueryStatus
from azure.identity import DefaultAzureCredential

credential = DefaultAzureCredential()

client = LogsQueryClient(credential)

# [START send_query_batch]
requests = [
    LogsBatchQuery(query="AzureActivity | summarize count()",
                   timespan=timedelta(hours=1),
                   workspace_id=os.environ['LOGS_WORKSPACE_ID']),
    LogsBatchQuery(query="""bad query""",
                   timespan=timedelta(days=1),
                   workspace_id=os.environ['LOGS_WORKSPACE_ID']),
    LogsBatchQuery(
        query="""let Weight = 92233720368547758;
        range x from 1 to 3 step 1
        | summarize percentilesw(x, Weight * 100, 50)""",
        workspace_id=os.environ['LOGS_WORKSPACE_ID'],
        timespan=(datetime(2021, 6, 2, tzinfo=timezone.utc),
                  datetime(2021, 6, 5, tzinfo=timezone.utc)),  # (start, end)
        include_statistics=True),
]
results = client.query_batch(requests)