Skip to content

Commit ea27de2

Browse files
chore(tracer): handle None in activate_distributed_headers (#13329)
The function `activate_distributed_headers` unconditionally calls `context = HTTPPropagator.extract(request_headers)` and then immediately does` context.trace_id`, but under some circumstances (e.g. no distributed-trace headers present) `extract()` can return `None`. This leads to an uncaught `AttributeError: 'NoneType' object has no attribute 'trace_id'`. Fixes issue: #13277 ## Checklist - [x] PR author has checked that all the criteria below are met - The PR description includes an overview of the change - The PR description articulates the motivation for the change - The change includes tests OR the PR description describes a testing strategy - The PR description notes risks associated with the change, if any - Newly-added code is easy to change - The change follows the [library release note guidelines](https://ddtrace.readthedocs.io/en/stable/releasenotes.html) - The change includes or references documentation updates if necessary - Backport labels are set (if [applicable](https://ddtrace.readthedocs.io/en/latest/contributing.html#backporting)) ## Reviewer Checklist - [x] Reviewer has checked that all the criteria below are met - Title is accurate - All changes are related to the pull request's stated goal - Avoids breaking [API](https://ddtrace.readthedocs.io/en/stable/versioning.html#interfaces) changes - Testing strategy adequately addresses listed risks - Newly-added code is easy to change - Release note makes sense to a user of the library - If necessary, author has acknowledged and discussed the performance implications of this PR as reported in the benchmarks PR comment - Backport labels are set in a manner that is consistent with the [release branch maintenance policy](https://ddtrace.readthedocs.io/en/latest/contributing.html#backporting)
1 parent aa52c00 commit ea27de2

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

ddtrace/contrib/internal/trace_utils.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -510,7 +510,9 @@ def activate_distributed_headers(tracer, int_config=None, request_headers=None,
510510

511511
if override or (int_config and distributed_tracing_enabled(int_config)):
512512
context = HTTPPropagator.extract(request_headers)
513-
513+
# bail out if no context was extracted
514+
if context is None:
515+
return None
514516
# Only need to activate the new context if something was propagated
515517
# The new context must have one of these values in order for it to be activated
516518
if not context.trace_id and not context._baggage and not context._span_links:

tests/tracer/test_trace_utils.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
from ddtrace.internal.compat import ensure_text
2323
from ddtrace.propagation.http import HTTP_HEADER_PARENT_ID
2424
from ddtrace.propagation.http import HTTP_HEADER_TRACE_ID
25+
from ddtrace.propagation.http import HTTPPropagator
2526
from ddtrace.settings import IntegrationConfig
2627
from ddtrace.settings._config import Config
2728
from ddtrace.trace import Context
@@ -974,6 +975,15 @@ def test_activate_distributed_headers_no_headers(int_config, tracer):
974975
assert tracer.context_provider.active() is None
975976

976977

978+
@mock.patch.object(HTTPPropagator, "extract", return_value=None)
979+
def test_activate_distributed_headers_missing_headers(int_config, tracer):
980+
# Verify that when HTTPPropagator.extract returns None (no incoming headers),
981+
# activate_distributed_headers returns early without error and leaves the tracer’s context empty.
982+
int_config.myint["distributed_tracing_enabled"] = True
983+
trace_utils.activate_distributed_headers(tracer, int_config=int_config.myint, request_headers=None, override=True)
984+
assert tracer.context_provider.active() is None
985+
986+
977987
def test_activate_distributed_headers_override_true(int_config, tracer):
978988
int_config.myint["distributed_tracing_enabled"] = False
979989
headers = {

0 commit comments

Comments
 (0)