-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Core][OTel] Support OTel schema versioning (#29203)
This also adds support for attribute mapping to ensure that attributes are mapped to the corresponding semantic convention that we are trying to converge on. Missing network attributes were also added. Signed-off-by: Paul Van Eck <paulvaneck@microsoft.com>
- Loading branch information
Showing
5 changed files
with
118 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
...ore/azure-core-tracing-opentelemetry/azure/core/tracing/ext/opentelemetry_span/_schema.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
# ------------------------------------ | ||
# Copyright (c) Microsoft Corporation. | ||
# Licensed under the MIT License. | ||
# ------------------------------------ | ||
from enum import Enum | ||
from typing import Dict | ||
|
||
from azure.core import CaseInsensitiveEnumMeta | ||
|
||
|
||
class OpenTelemetrySchemaVersion( | ||
str, Enum, metaclass=CaseInsensitiveEnumMeta | ||
): # pylint: disable=enum-must-inherit-case-insensitive-enum-meta | ||
|
||
V1_19_0 = "1.19.0" | ||
|
||
|
||
class OpenTelemetrySchema: | ||
|
||
SUPPORTED_VERSIONS = [ | ||
OpenTelemetrySchemaVersion.V1_19_0, | ||
] | ||
|
||
# Mappings of attributes potentially reported by Azure SDKs to corresponding ones that follow | ||
# OpenTelemetry semantic conventions. | ||
_ATTRIBUTE_MAPPINGS = { | ||
OpenTelemetrySchemaVersion.V1_19_0: { | ||
"x-ms-client-request-id": "az.client_request_id", | ||
"x-ms-request-id": "az.service_request_id", | ||
"http.user_agent": "user_agent.original", | ||
"messaging_bus.destination": "messaging.destination.name", | ||
"peer.address": "net.peer.name", | ||
} | ||
} | ||
|
||
@classmethod | ||
def get_latest_version(cls) -> OpenTelemetrySchemaVersion: | ||
return cls.SUPPORTED_VERSIONS[-1] | ||
|
||
@classmethod | ||
def get_attribute_mappings(cls, version: OpenTelemetrySchemaVersion) -> Dict[str, str]: | ||
return cls._ATTRIBUTE_MAPPINGS[version] |
31 changes: 31 additions & 0 deletions
31
sdk/core/azure-core-tracing-opentelemetry/tests/test_schema.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
# ------------------------------------ | ||
# Copyright (c) Microsoft Corporation. | ||
# Licensed under the MIT License. | ||
# ------------------------------------ | ||
import uuid | ||
|
||
from opentelemetry.trace import SpanKind as OpenTelemetrySpanKind | ||
|
||
from azure.core.tracing.ext.opentelemetry_span import OpenTelemetrySpan | ||
from azure.core.tracing.ext.opentelemetry_span._schema import OpenTelemetrySchema | ||
|
||
|
||
class TestOpenTelemetrySchema: | ||
def test_latest_schema_attributes_renamed(self, tracer): | ||
with tracer.start_as_current_span("Root", kind=OpenTelemetrySpanKind.CLIENT) as parent: | ||
wrapped_class = OpenTelemetrySpan(span=parent) | ||
schema_version = OpenTelemetrySchema.get_latest_version() | ||
attribute_mappings = OpenTelemetrySchema.get_attribute_mappings(schema_version) | ||
attribute_values = {} | ||
for key, value in attribute_mappings.items(): | ||
attribute_values[value] = uuid.uuid4().hex | ||
# Add attribute using key that is not following OpenTelemetry semantic conventions. | ||
wrapped_class.add_attribute(key, attribute_values[value]) | ||
|
||
for attribute, expected_value in attribute_values.items(): | ||
# Check that expected renamed attribute is present with the correct value. | ||
assert wrapped_class.span_instance.attributes.get(attribute) == expected_value | ||
|
||
for key in attribute_mappings: | ||
# Check that original attribute is not present. | ||
assert wrapped_class.span_instance.attributes.get(key) is None |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters