Skip to content

Commit 824cc36

Browse files
authored
Fix HTTP suppression in AI SDKs (Azure#41341)
* change_context with abstract span
1 parent 0bc642d commit 824cc36

File tree

2 files changed

+22
-4
lines changed

2 files changed

+22
-4
lines changed

sdk/ai/azure-ai-inference/azure/ai/inference/tracing.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -611,7 +611,7 @@ def inner(*args, **kwargs):
611611
try:
612612
# tracing events not supported in azure-core-tracing-opentelemetry
613613
# so need to access the span instance directly
614-
with span_impl_type.change_context(span.span_instance):
614+
with span_impl_type.change_context(span):
615615
last_event_timestamp_ns = self._add_request_details(span, args, kwargs)
616616
result = function(*args, **kwargs)
617617
if kwargs.get("stream") is True:
@@ -685,7 +685,7 @@ async def inner(*args, **kwargs):
685685
try:
686686
# tracing events not supported in azure-core-tracing-opentelemetry
687687
# so need to access the span instance directly
688-
with span_impl_type.change_context(span.span_instance):
688+
with span_impl_type.change_context(span):
689689
last_event_timestamp_ns = self._add_request_details(span, args, kwargs)
690690
result = await function(*args, **kwargs)
691691
if kwargs.get("stream") is True:

sdk/core/azure-core-tracing-opentelemetry/tests/test_tracing_implementations.py

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,17 +129,35 @@ def test_nested_span_suppression_with_multiple_outer_spans(self, tracing_helper)
129129
spans_names_list = [span.name for span in tracing_helper.exporter.get_finished_spans()]
130130
assert spans_names_list == ["outer-span-1", "outer-span-2", "Root"]
131131

132+
def test_suppress_http_under_change_context(self, tracing_helper):
133+
from opentelemetry.context import get_value, _SUPPRESS_HTTP_INSTRUMENTATION_KEY
134+
135+
span1 = OpenTelemetrySpan(name="span1", kind=SpanKind.INTERNAL)
136+
137+
with OpenTelemetrySpan.change_context(span1.span_instance):
138+
# this won't suppress HTTP since it's an arbitrary span
139+
assert get_value(_SUPPRESS_HTTP_INSTRUMENTATION_KEY) is None
140+
141+
with OpenTelemetrySpan.change_context(span1):
142+
# this works - we can guarantee suppression under our spans
143+
assert get_value(_SUPPRESS_HTTP_INSTRUMENTATION_KEY)
144+
132145
def test_suppress_http_auto_instrumentation_policy(self, tracing_helper):
133146
from azure.core.rest import HttpRequest
134147
from azure.core.pipeline.transport import RequestsTransport
135-
from azure.core.pipeline.policies import DistributedTracingPolicy
148+
from azure.core.pipeline.policies import DistributedTracingPolicy, SansIOHTTPPolicy
136149
from azure.core.settings import settings
150+
from opentelemetry.context import get_value, _SUPPRESS_HTTP_INSTRUMENTATION_KEY
137151

138152
settings.tracing_implementation = "opentelemetry"
139153

154+
class ContextValidator(SansIOHTTPPolicy):
155+
def on_request(self, request):
156+
assert get_value(_SUPPRESS_HTTP_INSTRUMENTATION_KEY)
157+
140158
class FooClient:
141159
def __init__(self, endpoint: str):
142-
policies = [DistributedTracingPolicy()]
160+
policies = [DistributedTracingPolicy(), ContextValidator()]
143161
self._client = PipelineClient(endpoint, policies=policies, transport=RequestsTransport())
144162

145163
@distributed_trace

0 commit comments

Comments
 (0)