From 87532da8eddf1bd23ecab235bbc38f5afca4c22e Mon Sep 17 00:00:00 2001 From: ZStriker19 Date: Tue, 7 Jan 2025 14:52:42 -0500 Subject: [PATCH] don't drop extract contexts lacking trace_id --- ddtrace/contrib/trace_utils.py | 4 ++-- tests/tracer/test_propagation.py | 16 ++++++++-------- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/ddtrace/contrib/trace_utils.py b/ddtrace/contrib/trace_utils.py index db8509d8c35..740ae9288b7 100644 --- a/ddtrace/contrib/trace_utils.py +++ b/ddtrace/contrib/trace_utils.py @@ -565,7 +565,7 @@ def activate_distributed_headers(tracer, int_config=None, request_headers=None, context = HTTPPropagator.extract(request_headers) # Only need to activate the new context if something was propagated - if not context.trace_id: + if not context.trace_id or context._baggage: return None # Do not reactivate a context with the same trace id @@ -577,7 +577,7 @@ def activate_distributed_headers(tracer, int_config=None, request_headers=None, # app = Flask(__name__) # Traced via Flask instrumentation # app = DDWSGIMiddleware(app) # Extra layer on top for WSGI current_context = tracer.current_trace_context() - if current_context and current_context.trace_id == context.trace_id: + if current_context and current_context.trace_id and current_context.trace_id == context.trace_id: log.debug( "will not activate extracted Context(trace_id=%r, span_id=%r), a context with that trace id is already active", # noqa: E501 context.trace_id, diff --git a/tests/tracer/test_propagation.py b/tests/tracer/test_propagation.py index 4e330ccb886..b2bd438032e 100644 --- a/tests/tracer/test_propagation.py +++ b/tests/tracer/test_propagation.py @@ -2494,14 +2494,14 @@ def test_propagation_extract_w_config( overrides["_propagation_style_extract"] = styles if extract_behavior is not None: overrides["_propagation_behavior_extract"] = extract_behavior - with override_global_config(overrides): - context = HTTPPropagator.extract(headers) - if not expected_context.get("tracestate"): - assert context == Context(**expected_context) - else: - copied_expectation = expected_context.copy() - tracestate = copied_expectation.pop("tracestate") - assert context == Context(**copied_expectation, meta={"tracestate": tracestate}) + with override_global_config(overrides): + context = HTTPPropagator.extract(headers) + if not expected_context.get("tracestate"): + assert context == Context(**expected_context) + else: + copied_expectation = expected_context.copy() + tracestate = copied_expectation.pop("tracestate") + assert context == Context(**copied_expectation, meta={"tracestate": tracestate}) EXTRACT_OVERRIDE_FIXTURES = [