Skip to content

Commit cf2c60c

Browse files
committed
Modularize fetching appid logic and fix format
1 parent 8ed73de commit cf2c60c

3 files changed

Lines changed: 8 additions & 5 deletions

File tree

sdk/monitor/azure-monitor-opentelemetry-exporter/azure/monitor/opentelemetry/exporter/_connection_string_parser.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
INSTRUMENTATION_KEY = "instrumentationkey"
1010
# cspell:disable-next-line
1111
AAD_AUDIENCE = "aadaudience"
12-
APPLICATION_ID = "applicationid"
12+
APPLICATION_ID = "applicationid" # cspell:disable-line
1313

1414
# Validate UUID format
1515
# Specs taken from https://tools.ietf.org/html/rfc4122

sdk/monitor/azure-monitor-opentelemetry-exporter/azure/monitor/opentelemetry/exporter/_utils.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
from azure.core.pipeline.policies import BearerTokenCredentialPolicy
2121
from azure.monitor.opentelemetry.exporter._generated.models import ContextTagKeys, TelemetryItem
2222
from azure.monitor.opentelemetry.exporter._version import VERSION as ext_version
23+
from azure.monitor.opentelemetry.exporter._connection_string_parser import ConnectionStringParser
2324
from azure.monitor.opentelemetry.exporter._constants import (
2425
_AKS_ARM_NAMESPACE_ID,
2526
_DEFAULT_AAD_SCOPE,
@@ -431,3 +432,7 @@ def get_compute_type():
431432

432433
def _get_sha256_hash(input_str: str) -> str:
433434
return hashlib.sha256(input_str.encode("utf-8")).hexdigest()
435+
436+
def _get_application_id(connection_string: Optional[str]) -> Optional[str]:
437+
parsed_connection_string = ConnectionStringParser(connection_string)
438+
return parsed_connection_string.application_id

sdk/monitor/azure-monitor-opentelemetry-exporter/azure/monitor/opentelemetry/exporter/export/trace/_exporter.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@
3434
_APPLICATION_ID_RESOURCE_KEY,
3535
)
3636
from azure.monitor.opentelemetry.exporter import _utils
37-
from azure.monitor.opentelemetry.exporter._connection_string_parser import ConnectionStringParser
3837
from azure.monitor.opentelemetry.exporter._generated.models import (
3938
ContextTagKeys,
4039
MessageData,
@@ -108,6 +107,7 @@ class AzureMonitorTraceExporter(BaseExporter, SpanExporter):
108107
def __init__(self, **kwargs: Any):
109108
self._tracer_provider = kwargs.pop("tracer_provider", None)
110109
super().__init__(**kwargs)
110+
self.application_id = _utils._get_application_id(self._connection_string)
111111

112112
def export(
113113
self, spans: Sequence[ReadableSpan], **kwargs: Any # pylint: disable=unused-argument
@@ -120,15 +120,13 @@ def export(
120120
:rtype: ~opentelemetry.sdk.trace.export.SpanExportResult
121121
"""
122122
envelopes = []
123-
parsed_connection_string = ConnectionStringParser(self._connection_string)
124-
application_id = parsed_connection_string.application_id
125123

126124
if spans and self._should_collect_otel_resource_metric():
127125
resource = None
128126
try:
129127
tracer_provider = self._tracer_provider or get_tracer_provider()
130128
resource = tracer_provider.resource # type: ignore
131-
envelopes.append(self._get_otel_resource_envelope(resource, application_id))
129+
envelopes.append(self._get_otel_resource_envelope(resource, self.application_id))
132130
except AttributeError as e:
133131
_logger.exception("Failed to derive Resource from Tracer Provider: %s", e) # pylint: disable=C4769
134132
for span in spans:

0 commit comments

Comments
 (0)