Skip to content

Commit 01b5841

Browse files
authored
[Monitor] Fix version in user agent strings (Azure#39866)
In all the Azure Monitor Ingestion and Query clients, the sdk_moniker keyword argument passed to the UserAgentPolicy was always set to "unknown". This update will make it reflect the actual package version. Signed-off-by: Paul Van Eck <[email protected]>
1 parent ba32eba commit 01b5841

20 files changed

+94
-8
lines changed

sdk/monitor/azure-monitor-ingestion/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88

99
### Bugs Fixed
1010

11+
- Fixed an issue where the package version in operation user agent strings was always set to "unknown" instead of the actual package version. ([#39866](https://github.com/Azure/azure-sdk-for-python/pull/39866))
12+
1113
### Other Changes
1214

1315
## 1.0.4 (2024-06-11)

sdk/monitor/azure-monitor-ingestion/azure/monitor/ingestion/_patch.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,14 @@
66
77
Follow our quickstart for examples: https://aka.ms/azsdk/python/dpcodegen/python/customize
88
"""
9+
from typing import Any, TYPE_CHECKING
10+
911
from ._client import LogsIngestionClient as GeneratedClient
1012
from ._models import LogsUploadError
13+
from ._version import SDK_MONIKER
14+
15+
if TYPE_CHECKING:
16+
from azure.core.credentials import TokenCredential
1117

1218

1319
class LogsIngestionClient(GeneratedClient):
@@ -41,6 +47,10 @@ class LogsIngestionClient(GeneratedClient):
4147
:caption: Creating the LogsIngestionClient for use with a sovereign cloud (i.e. non-public cloud).
4248
"""
4349

50+
def __init__(self, endpoint: str, credential: "TokenCredential", **kwargs: Any) -> None:
51+
kwargs.setdefault("sdk_moniker", SDK_MONIKER)
52+
super().__init__(endpoint=endpoint, credential=credential, **kwargs)
53+
4454

4555
__all__ = ["LogsIngestionClient", "LogsUploadError"]
4656

sdk/monitor/azure-monitor-ingestion/azure/monitor/ingestion/_version.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,4 @@
77
# --------------------------------------------------------------------------
88

99
VERSION = "1.0.5"
10+
SDK_MONIKER = f"monitor-ingestion/{VERSION}"

sdk/monitor/azure-monitor-ingestion/azure/monitor/ingestion/aio/_patch.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,12 @@
66
77
Follow our quickstart for examples: https://aka.ms/azsdk/python/dpcodegen/python/customize
88
"""
9-
from typing import List
9+
from typing import List, Any, TYPE_CHECKING
1010
from ._client import LogsIngestionClient as GeneratedClient
11+
from .._version import SDK_MONIKER
12+
13+
if TYPE_CHECKING:
14+
from azure.core.credentials_async import AsyncTokenCredential
1115

1216

1317
class LogsIngestionClient(GeneratedClient):
@@ -41,6 +45,10 @@ class LogsIngestionClient(GeneratedClient):
4145
:caption: Creating the LogsIngestionClient for use with a sovereign cloud (i.e. non-public cloud).
4246
"""
4347

48+
def __init__(self, endpoint: str, credential: "AsyncTokenCredential", **kwargs: Any) -> None:
49+
kwargs.setdefault("sdk_moniker", SDK_MONIKER)
50+
super().__init__(endpoint=endpoint, credential=credential, **kwargs)
51+
4452

4553
__all__: List[str] = ["LogsIngestionClient"]
4654

sdk/monitor/azure-monitor-ingestion/tests/test_logs_ingestion.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
from azure.core.exceptions import HttpResponseError
1515
from azure.monitor.ingestion import LogsIngestionClient
16+
from azure.monitor.ingestion._version import VERSION
1617

1718
from base_testcase import LogsIngestionClientTestCase
1819

@@ -153,3 +154,10 @@ def test_invalid_logs_format(self, monitor_info, logs):
153154

154155
with pytest.raises(ValueError):
155156
client.upload(rule_id="rule", stream_name="stream", logs=logs)
157+
158+
def test_user_agent_version(self, monitor_info):
159+
client = self.get_client(
160+
LogsIngestionClient, self.get_credential(LogsIngestionClient), endpoint=monitor_info["dce"]
161+
)
162+
163+
assert f"monitor-ingestion/{VERSION}" in client._config.user_agent_policy._user_agent

sdk/monitor/azure-monitor-ingestion/tests/test_logs_ingestion_async.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
from azure.core.exceptions import HttpResponseError
1515
from azure.monitor.ingestion.aio import LogsIngestionClient
16+
from azure.monitor.ingestion._version import VERSION
1617

1718
from base_testcase import LogsIngestionClientTestCase
1819

@@ -169,3 +170,12 @@ async def test_invalid_logs_format(self, monitor_info, logs):
169170
with pytest.raises(ValueError):
170171
await client.upload(rule_id="rule", stream_name="stream", logs=logs)
171172
await credential.close()
173+
174+
@pytest.mark.asyncio
175+
async def test_user_agent_version(self, monitor_info):
176+
credential = self.get_credential(LogsIngestionClient, is_async=True)
177+
client = self.get_client(LogsIngestionClient, credential, endpoint=monitor_info["dce"])
178+
179+
async with client:
180+
assert f"azsdk-python-monitor-ingestion/{VERSION}" in client._config.user_agent_policy.user_agent
181+
await credential.close()

sdk/monitor/azure-monitor-query/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88

99
### Bugs Fixed
1010

11+
- Fixed an issue where the package version in operation user agent strings was always set to "unknown" instead of the actual package version. ([#39866](https://github.com/Azure/azure-sdk-for-python/pull/39866))
12+
1113
### Other Changes
1214

1315
## 1.4.1 (2025-01-14)

sdk/monitor/azure-monitor-query/azure/monitor/query/_logs_query_client.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
)
2323
from ._models import LogsBatchQuery, LogsQueryResult, LogsQueryPartialResult
2424
from ._exceptions import LogsQueryError
25+
from ._version import SDK_MONIKER
2526

2627
JSON = MutableMapping[str, Any]
2728

@@ -67,6 +68,7 @@ def __init__(self, credential: TokenCredential, **kwargs: Any) -> None:
6768
audience = kwargs.pop("audience", f"{parsed_endpoint.scheme}://{parsed_endpoint.netloc}")
6869
self._endpoint = endpoint
6970
auth_policy = kwargs.pop("authentication_policy", None)
71+
kwargs.setdefault("sdk_moniker", SDK_MONIKER)
7072
self._client = MonitorQueryClient(
7173
credential=credential,
7274
authentication_policy=auth_policy or get_authentication_policy(credential, audience),
@@ -132,9 +134,7 @@ def query_workspace(
132134

133135
generated_response: JSON = {}
134136
try:
135-
generated_response = self._query_op.execute(
136-
workspace_id=workspace_id, body=body, prefer=prefer, **kwargs
137-
)
137+
generated_response = self._query_op.execute(workspace_id=workspace_id, body=body, prefer=prefer, **kwargs)
138138
except HttpResponseError as err:
139139
process_error(err, LogsQueryError)
140140

sdk/monitor/azure-monitor-query/azure/monitor/query/_metrics_client.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
from ._models import MetricsQueryResult
1616
from ._enums import MetricAggregationType
1717
from ._helpers import get_authentication_policy, get_timespan_iso8601_endpoints, get_subscription_id_from_resource
18+
from ._version import SDK_MONIKER
1819

1920
JSON = MutableMapping[str, Any]
2021

@@ -59,7 +60,7 @@ def __init__(self, endpoint: str, credential: TokenCredential, **kwargs: Any) ->
5960
authentication_policy = kwargs.pop("authentication_policy", None) or get_authentication_policy(
6061
credential, audience
6162
)
62-
63+
kwargs.setdefault("sdk_moniker", SDK_MONIKER)
6364
self._client = MonitorBatchMetricsClient(
6465
credential=credential, endpoint=self._endpoint, authentication_policy=authentication_policy, **kwargs
6566
)

sdk/monitor/azure-monitor-query/azure/monitor/query/_metrics_query_client.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
from ._generated.metrics._client import MonitorMetricsClient
1616
from ._models import MetricsQueryResult, MetricDefinition, MetricNamespace
1717
from ._helpers import get_authentication_policy, construct_iso8601
18+
from ._version import SDK_MONIKER
1819

1920

2021
class MetricsQueryClient(object): # pylint: disable=client-accepts-api-version-keyword
@@ -57,7 +58,7 @@ def __init__(self, credential: TokenCredential, **kwargs: Any) -> None:
5758
authentication_policy = kwargs.pop("authentication_policy", None) or get_authentication_policy(
5859
credential, audience
5960
)
60-
61+
kwargs.setdefault("sdk_moniker", SDK_MONIKER)
6162
self._client = MonitorMetricsClient(
6263
credential=credential, endpoint=self._endpoint, authentication_policy=authentication_policy, **kwargs
6364
)

0 commit comments

Comments
 (0)