diff --git a/sdk/core/azure-core/azure/core/tracing/ext/opencensus_span.py b/sdk/core/azure-core/azure/core/tracing/ext/opencensus_span.py index ceb8e5a017e5..7aeb79c77e36 100644 --- a/sdk/core/azure-core/azure/core/tracing/ext/opencensus_span.py +++ b/sdk/core/azure-core/azure/core/tracing/ext/opencensus_span.py @@ -15,7 +15,7 @@ TYPE_CHECKING = False if TYPE_CHECKING: - from typing import Dict, Optional, Union, TypeVar + from typing import Dict, Optional, Union from azure.core.pipeline.transport import HttpRequest, HttpResponse @@ -83,7 +83,10 @@ def to_header(self): temp_headers = {} if tracer_from_context is not None: ctx = tracer_from_context.span_context - temp_headers = tracer_from_context.propagator.to_headers(ctx) + try: + temp_headers = tracer_from_context.propagator.to_headers(ctx) + except AttributeError: + pass return temp_headers def add_attribute(self, key, value): diff --git a/sdk/core/azure-core/tests/test_tracing_context.py b/sdk/core/azure-core/tests/test_tracing_context.py index a537d4200179..59aa2c8a8b60 100644 --- a/sdk/core/azure-core/tests/test_tracing_context.py +++ b/sdk/core/azure-core/tests/test_tracing_context.py @@ -22,6 +22,7 @@ def __init__(self, environ={}, tracer_to_use=None): def __enter__(self): self.orig_sdk_context_span = tracing_context.current_span.get() + tracing_context.current_span.clear() settings.tracing_implementation.set_value(self.tracer_to_use) self.os_env.start() return self diff --git a/sdk/core/azure-core/tests/tracing_common.py b/sdk/core/azure-core/tests/tracing_common.py index caa8605762df..a1dfabdca8e8 100644 --- a/sdk/core/azure-core/tests/tracing_common.py +++ b/sdk/core/azure-core/tests/tracing_common.py @@ -19,6 +19,7 @@ from opencensus.trace.samplers import AlwaysOnSampler from opencensus.trace.base_exporter import Exporter from collections import defaultdict +from opencensus.trace import execution_context try: from unittest import mock @@ -39,6 +40,8 @@ def __enter__(self): self.orig_tracer = OpenCensusSpan.get_current_tracer() self.orig_current_span = OpenCensusSpan.get_current_span() self.orig_sdk_context_span = tracing_context.current_span.get() + execution_context.clear() + tracing_context.current_span.clear() if self.tracer_to_use is not None: settings.tracing_implementation.set_value(self.tracer_to_use) if self.should_only_propagate is not None: @@ -81,7 +84,4 @@ def export(self, span_datas): def build_tree(self): for node in self._all_nodes: if node.span_data.span_id in self.parent_dict: - node.children = sorted( - self.parent_dict[node.span_data.span_id], - key=lambda x: x.span_data.start_time, - ) + node.children = sorted(self.parent_dict[node.span_data.span_id], key=lambda x: x.span_data.start_time)