Skip to content

Commit de23b45

Browse files
authored
Suppress internal sdkstats HTTP pipeline logs from appearing in user's traces (#45966)
* Suppress internal sdkstats HTTP pipeline logs from appearing in user's traces * Update CHANGELOG * Add test coverage * Update comment * Retrigger CI/CD pipeline
1 parent d9150c7 commit de23b45

3 files changed

Lines changed: 46 additions & 1 deletion

File tree

sdk/monitor/azure-monitor-opentelemetry-exporter/CHANGELOG.md

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

99
### Bugs Fixed
10+
- Suppress internal sdkstats HTTP pipeline logs from appearing in user's logs
11+
([#45966](https://github.com/Azure/azure-sdk-for-python/pull/45966))
1012
- Kubernetes pod name takes precedence when populating `cloud_RoleInstance`
1113
([#45884](https://github.com/Azure/azure-sdk-for-python/pull/45884))
1214

sdk/monitor/azure-monitor-opentelemetry-exporter/azure/monitor/opentelemetry/exporter/export/_base.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,9 +160,13 @@ def __init__(self, **kwargs: Any) -> None:
160160
config.logging_policy,
161161
# Explicitly disabling to avoid infinite loop of Span creation when data is exported
162162
# DistributedTracingPolicy(**kwargs),
163-
config.http_logging_policy or HttpLoggingPolicy(**kwargs),
164163
]
165164

165+
# Exclude HttpLoggingPolicy for the sdkstats exporter so its HTTP
166+
# traffic does not appear in the user's logs.
167+
if not self._is_stats_exporter():
168+
policies.append(config.http_logging_policy or HttpLoggingPolicy(**kwargs))
169+
166170
self.client: AzureMonitorClient = AzureMonitorClient(
167171
host=self._endpoint, connection_timeout=self._timeout, policies=policies, **kwargs
168172
)

sdk/monitor/azure-monitor-opentelemetry-exporter/tests/test_base_exporter.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,45 @@ def test_constructor_disable_offline_storage_with_storage_directory(self, mock_g
310310
self.assertEqual(base._storage_directory, "test/path")
311311
mock_get_temp_dir.assert_not_called()
312312

313+
def test_normal_exporter_includes_http_logging_policy(self):
314+
from azure.core.pipeline.policies import HttpLoggingPolicy
315+
316+
captured_policies = []
317+
original_init = AzureMonitorClient.__init__
318+
319+
def capturing_init(self, *args, **kwargs):
320+
captured_policies.extend(kwargs.get("policies", []))
321+
original_init(self, *args, **kwargs)
322+
323+
with mock.patch.object(AzureMonitorClient, "__init__", capturing_init):
324+
BaseExporter(
325+
connection_string="InstrumentationKey=4321abcd-5678-4efa-8abc-1234567890ab",
326+
)
327+
self.assertTrue(
328+
any(isinstance(p, HttpLoggingPolicy) for p in captured_policies),
329+
"HttpLoggingPolicy should be present in the pipeline for normal exporters",
330+
)
331+
332+
def test_statsbeat_exporter_excludes_http_logging_policy(self):
333+
from azure.core.pipeline.policies import HttpLoggingPolicy
334+
335+
captured_policies = []
336+
original_init = AzureMonitorClient.__init__
337+
338+
def capturing_init(self, *args, **kwargs):
339+
captured_policies.extend(kwargs.get("policies", []))
340+
original_init(self, *args, **kwargs)
341+
342+
with mock.patch.object(AzureMonitorClient, "__init__", capturing_init):
343+
AzureMonitorMetricExporter(
344+
is_sdkstats=True,
345+
disable_offline_storage=True,
346+
)
347+
self.assertFalse(
348+
any(isinstance(p, HttpLoggingPolicy) for p in captured_policies),
349+
"HttpLoggingPolicy must not be present in the pipeline for statsbeat exporters",
350+
)
351+
313352
# ========================================================================
314353
# STORAGE TESTS
315354
# ========================================================================

0 commit comments

Comments
 (0)