Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Workspaces must be a single param #19344

Merged
merged 5 commits into from
Jun 21, 2021
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@

from ._generated._monitor_query_client import MonitorQueryClient

from ._generated.models import BatchRequest
from ._generated.models import BatchRequest, QueryBody as LogsQueryBody
from ._helpers import get_authentication_policy, process_error, construct_iso8601
from ._models import LogsQueryResults, LogsQueryRequest, LogsQueryBody, LogsBatchResults
from ._models import LogsQueryResults, LogsQueryRequest, LogsBatchResults

if TYPE_CHECKING:
from azure.core.credentials import TokenCredential
Expand Down Expand Up @@ -76,14 +76,9 @@ def query(self, workspace_id, query, duration=None, **kwargs):
:keyword bool include_render: In the query language, it is possible to specify different render options.
By default, the API does not return information regarding the type of visualization to show.
If your client requires this information, specify the preference
:keyword workspaces: A list of workspaces that are included in the query.
:paramtype workspaces: list[str]
:keyword qualified_names: A list of qualified workspace names that are included in the query.
:paramtype qualified_names: list[str]
:keyword workspace_ids: A list of workspace IDs that are included in the query.
:paramtype workspace_ids: list[str]
:keyword azure_resource_ids: A list of Azure resource IDs that are included in the query.
:paramtype azure_resource_ids: list[str]
:keyword additional_workspaces: A list of workspaces that are included in the query.
These can be qualified workspace names, workspsce Ids or Azure resource Ids.
:paramtype additional_workspaces: list[str]
:return: QueryResults, or the result of cls(response)
:rtype: ~azure.monitor.query.LogsQueryResults
:raises: ~azure.core.exceptions.HttpResponseError
Expand All @@ -103,6 +98,7 @@ def query(self, workspace_id, query, duration=None, **kwargs):
include_statistics = kwargs.pop("include_statistics", False)
include_render = kwargs.pop("include_render", False)
server_timeout = kwargs.pop("server_timeout", None)
workspaces = kwargs.pop("additional_workspaces", None)

prefer = ""
if server_timeout:
Expand All @@ -119,6 +115,7 @@ def query(self, workspace_id, query, duration=None, **kwargs):
body = LogsQueryBody(
query=query,
timespan=timespan,
workspaces=workspaces,
**kwargs
)

Expand Down
47 changes: 9 additions & 38 deletions sdk/monitor/azure-monitor-query/azure/monitor/query/_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
from ._helpers import order_results, construct_iso8601
from ._generated.models import (
Column as InternalColumn,
QueryBody as InternalQueryBody,
LogQueryRequest as InternalLogQueryRequest,
ErrorDetails as InternalErrorDetails
)
Expand Down Expand Up @@ -151,6 +150,8 @@ class LogsQueryRequest(InternalLogQueryRequest):

Variables are only populated by the server, and will be ignored when sending a request.

:param workspace_id: Workspace Id to be included in the query.
:type workspace_id: str
:param query: The Analytics query. Learn more about the `Analytics query syntax
<https://azure.microsoft.com/documentation/articles/app-insights-analytics-reference/>`_.
:type query: str
Expand All @@ -161,60 +162,30 @@ class LogsQueryRequest(InternalLogQueryRequest):
with either end_time or duration.
:keyword datetime end_time: The end time till which to query the data. This should be accompanied
with either start_time or duration.
:param workspace: Workspace Id to be included in the query.
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no- workspace_id is the primary workspace id and additional_workpspaces is an optional kwarg - the document has both (line 153, line 165)

:type workspace: str
:keyword additional_workspaces: A list of workspaces that are included in the query.
These can be qualified workspace names, workspsce Ids or Azure resource Ids.
:paramtype additional_workspaces: list[str]
:keyword request_id: The error details.
:paramtype request_id: str
:keyword headers: Dictionary of :code:`<string>`.
:paramtype headers: dict[str, str]
"""

def __init__(self, query, workspace, duration=None, **kwargs):
def __init__(self, query, workspace_id, duration=None, **kwargs):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need to add it into changelog?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good catch - yes

# type: (str, str, Optional[str], Any) -> None
super(LogsQueryRequest, self).__init__(**kwargs)
start = kwargs.pop('start_time', None)
end = kwargs.pop('end_time', None)
timespan = construct_iso8601(start, end, duration)
additional_workspaces = kwargs.pop("additional_workspaces", None)
self.id = kwargs.get("request_id", str(uuid.uuid4()))
self.headers = kwargs.get("headers", None)
self.body = {
"query": query, "timespan": timespan
"query": query, "timespan": timespan, "workspaces": additional_workspaces
}
self.workspace = workspace
self.workspace = workspace_id


class LogsQueryBody(InternalQueryBody):
"""The Analytics query. Learn more about the
`Analytics query syntax <https://azure.microsoft.com/documentation/articles/app-insights-analytics-reference/>`_.

All required parameters must be populated in order to send to Azure.

:param query: Required. The query to execute.
:type query: str
:keyword timespan: Optional. The timespan over which to query data. This is an ISO8601 time
period value. This timespan is applied in addition to any that are specified in the query
expression.
:paramtype timespan: str
:keyword workspaces: A list of workspaces that are included in the query.
:paramtype workspaces: list[str]
:keyword qualified_names: A list of qualified workspace names that are included in the query.
:paramtype qualified_names: list[str]
:keyword workspace_ids: A list of workspace IDs that are included in the query.
:paramtype workspace_ids: list[str]
:keyword azure_resource_ids: A list of Azure resource IDs that are included in the query.
:paramtype azure_resource_ids: list[str]
"""

def __init__(self, query, timespan=None, **kwargs):
# type: (str, Optional[str], Any) -> None
kwargs.setdefault("query", query)
kwargs.setdefault("timespan", timespan)
super(LogsQueryBody, self).__init__(**kwargs)
self.workspaces = kwargs.get("workspaces", None)
self.qualified_names = kwargs.get("qualified_names", None)
self.workspace_ids = kwargs.get("workspace_ids", None)
self.azure_resource_ids = kwargs.get("azure_resource_ids", None)

class LogsQueryResult(object):
"""The LogsQueryResult.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
from azure.core.exceptions import HttpResponseError
from .._generated.aio._monitor_query_client import MonitorQueryClient

from .._generated.models import BatchRequest
from .._generated.models import BatchRequest, QueryBody as LogsQueryBody
from .._helpers import process_error, construct_iso8601
from .._models import LogsQueryResults, LogsQueryRequest, LogsQueryBody, LogsBatchResults
from .._models import LogsQueryResults, LogsQueryRequest, LogsBatchResults
from ._helpers_asyc import get_authentication_policy

if TYPE_CHECKING:
Expand Down Expand Up @@ -69,14 +69,9 @@ async def query(
:keyword bool include_render: In the query language, it is possible to specify different render options.
By default, the API does not return information regarding the type of visualization to show.
If your client requires this information, specify the preference
:keyword workspaces: A list of workspaces that are included in the query.
:paramtype workspaces: list[str]
:keyword qualified_names: A list of qualified workspace names that are included in the query.
:paramtype qualified_names: list[str]
:keyword workspace_ids: A list of workspace IDs that are included in the query.
:paramtype workspace_ids: list[str]
:keyword azure_resource_ids: A list of Azure resource IDs that are included in the query.
:paramtype azure_resource_ids: list[str]
:keyword additional_workspaces: A list of workspaces that are included in the query.
These can be qualified workspace names, workspsce Ids or Azure resource Ids.
:paramtype additional_workspaces: list[str]
:return: QueryResults, or the result of cls(response)
:rtype: ~azure.monitor.query.LogsQueryResults
:raises: ~azure.core.exceptions.HttpResponseError
Expand All @@ -87,6 +82,7 @@ async def query(
include_statistics = kwargs.pop("include_statistics", False)
include_render = kwargs.pop("include_render", False)
server_timeout = kwargs.pop("server_timeout", None)
additional_workspaces = kwargs.pop("additional_workspaces", None)

prefer = ""
if server_timeout:
Expand All @@ -103,6 +99,7 @@ async def query(
body = LogsQueryBody(
query=query,
timespan=timespan,
workspaces=additional_workspaces,
**kwargs
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,16 @@
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"""

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.

import os
import pandas as pd
from datetime import datetime
from msrest.serialization import UTC
from azure.monitor.query import LogsQueryClient
from azure.identity import ClientSecretCredential

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)

# Response time trend
# request duration over the last 12 hours.
query = "union * | where TimeGenerated > ago(100d) | project TenantId | summarize count() by TenantId"

end_time = datetime.now(UTC())

# returns LogsQueryResults
response = client.query(
os.environ['LOG_WORKSPACE_ID'],
query,
additional_workspaces=[os.environ["SECONDARY_WORKSPACE_ID"]],
)

if not response.tables:
print("No results for the query")

for table in response.tables:
df = pd.DataFrame(table.rows, columns=[col.name for col in table.columns])
print(df)

Original file line number Diff line number Diff line change
Expand Up @@ -47,20 +47,68 @@ async def test_logs_batch_query():
LogsQueryRequest(
query="AzureActivity | summarize count()",
timespan="PT1H",
workspace= os.environ['LOG_WORKSPACE_ID']
workspace_id= os.environ['LOG_WORKSPACE_ID']
),
LogsQueryRequest(
query= """AppRequests | take 10 |
summarize avgRequestDuration=avg(DurationMs) by bin(TimeGenerated, 10m), _ResourceId""",
timespan="PT1H",
workspace= os.environ['LOG_WORKSPACE_ID']
workspace_id= os.environ['LOG_WORKSPACE_ID']
),
LogsQueryRequest(
query= "AppRequests | take 2",
workspace= os.environ['LOG_WORKSPACE_ID']
workspace_id= os.environ['LOG_WORKSPACE_ID']
),
]
response = await client.batch_query(requests)

assert len(response.responses) == 3

@pytest.mark.live_test_only
@pytest.mark.asyncio
async def test_logs_single_query_additional_workspaces_async():
credential = _credential()
client = LogsQueryClient(credential)
query = "union * | where TimeGenerated > ago(100d) | project TenantId | summarize count() by TenantId"

# returns LogsQueryResults
response = await client.query(
os.environ['LOG_WORKSPACE_ID'],
query,
additional_workspaces=[os.environ["SECONDARY_WORKSPACE_ID"]],
)

assert response is not None
rakshith91 marked this conversation as resolved.
Show resolved Hide resolved
assert len(response.tables[0].rows) == 2

@pytest.mark.live_test_only
@pytest.mark.asyncio
async def test_logs_batch_query_additional_workspaces():
client = LogsQueryClient(_credential())
query = "union * | where TimeGenerated > ago(100d) | project TenantId | summarize count() by TenantId"

requests = [
LogsQueryRequest(
query,
timespan="PT1H",
workspace_id= os.environ['LOG_WORKSPACE_ID'],
additional_workspaces=[os.environ['SECONDARY_WORKSPACE_ID']]
),
LogsQueryRequest(
query,
timespan="PT1H",
workspace_id= os.environ['LOG_WORKSPACE_ID'],
additional_workspaces=[os.environ['SECONDARY_WORKSPACE_ID']]
),
LogsQueryRequest(
query,
workspace_id= os.environ['LOG_WORKSPACE_ID'],
additional_workspaces=[os.environ['SECONDARY_WORKSPACE_ID']]
),
]
response = await client.batch_query(requests)

assert len(response.responses) == 3

for resp in response.responses:
assert len(resp.body.tables[0].rows) == 2
52 changes: 49 additions & 3 deletions sdk/monitor/azure-monitor-query/tests/test_logs_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,20 +68,66 @@ def test_logs_batch_query():
LogsQueryRequest(
query="AzureActivity | summarize count()",
timespan="PT1H",
workspace= os.environ['LOG_WORKSPACE_ID']
workspace_id= os.environ['LOG_WORKSPACE_ID']
),
LogsQueryRequest(
query= """AppRequests | take 10 |
summarize avgRequestDuration=avg(DurationMs) by bin(TimeGenerated, 10m), _ResourceId""",
timespan="PT1H",
workspace= os.environ['LOG_WORKSPACE_ID']
workspace_id= os.environ['LOG_WORKSPACE_ID']
),
LogsQueryRequest(
query= "AppRequests | take 2",
workspace= os.environ['LOG_WORKSPACE_ID']
workspace_id= os.environ['LOG_WORKSPACE_ID']
),
]
response = client.batch_query(requests)

assert len(response.responses) == 3

@pytest.mark.live_test_only
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 LogsQueryResults
response = client.query(
os.environ['LOG_WORKSPACE_ID'],
query,
additional_workspaces=[os.environ["SECONDARY_WORKSPACE_ID"]],
)

assert response is not None
assert len(response.tables[0].rows) == 2

@pytest.mark.live_test_only
def test_logs_batch_query_additional_workspaces():
client = LogsQueryClient(_credential())
query = "union * | where TimeGenerated > ago(100d) | project TenantId | summarize count() by TenantId"

requests = [
LogsQueryRequest(
query,
timespan="PT1H",
workspace_id= os.environ['LOG_WORKSPACE_ID'],
additional_workspaces=[os.environ['SECONDARY_WORKSPACE_ID']]
),
LogsQueryRequest(
query,
timespan="PT1H",
workspace_id= os.environ['LOG_WORKSPACE_ID'],
additional_workspaces=[os.environ['SECONDARY_WORKSPACE_ID']]
),
LogsQueryRequest(
query,
workspace_id= os.environ['LOG_WORKSPACE_ID'],
additional_workspaces=[os.environ['SECONDARY_WORKSPACE_ID']]
),
]
response = client.batch_query(requests)

assert len(response.responses) == 3

for resp in response.responses:
assert len(resp.body.tables[0].rows) == 2
1 change: 1 addition & 0 deletions sdk/monitor/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,6 @@ stages:
AZURE_CLIENT_ID: $(aad-azure-sdk-test-client-id)
AZURE_CLIENT_SECRET: $(aad-azure-sdk-test-client-secret)
LOG_WORKSPACE_ID: $(azure-monitor-query-log-workspace)
SECONDARY_WORKSPACE_ID: $(python-query-secondary-workspace-id)
METRICS_RESOURCE_URI: $(azure-monitor-query-metrics-uri)
AZURE_TEST_RUN_LIVE: 'true'