From cc59075e7b186ff0048a5ad248872a98f4705631 Mon Sep 17 00:00:00 2001 From: Rachel Yang Date: Mon, 22 Sep 2025 16:32:42 -0400 Subject: [PATCH 1/2] WIP: (chore): propagate baggage tags to all spans --- ddtrace/internal/utils/__init__.py | 3 ++- tests/contrib/fastapi/test_fastapi.py | 19 +++++++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/ddtrace/internal/utils/__init__.py b/ddtrace/internal/utils/__init__.py index 1d7ee493953..3881cf7c9af 100644 --- a/ddtrace/internal/utils/__init__.py +++ b/ddtrace/internal/utils/__init__.py @@ -4,6 +4,7 @@ from typing import Optional # noqa:F401 from typing import Tuple # noqa:F401 from typing import Union # noqa:F401 +from ddtrace.internal.constants import BAGGAGE_TAG_PREFIX class ArgumentError(Exception): @@ -77,7 +78,7 @@ def _get_metas_to_propagate(context): metas_to_propagate = [] # copying context._meta.items() to avoid RuntimeError: dictionary changed size during iteration for k, v in list(context._meta.items()): - if isinstance(k, str) and k.startswith("_dd.p."): + if isinstance(k, str) and (k.startswith("_dd.p.") or k.startswith(BAGGAGE_TAG_PREFIX)): metas_to_propagate.append((k, v)) return metas_to_propagate diff --git a/tests/contrib/fastapi/test_fastapi.py b/tests/contrib/fastapi/test_fastapi.py index 15c29246314..53beefb66d8 100644 --- a/tests/contrib/fastapi/test_fastapi.py +++ b/tests/contrib/fastapi/test_fastapi.py @@ -944,6 +944,25 @@ def test_baggage_span_tagging_default(client, tracer, test_spans): assert request_span.get_tag("baggage.region") is None +def test_baggage_span_tags_propagate_to_child_spans(client, tracer, test_spans): + # Baggage header contains keys where user.id/account.id are in the default allowlist, + # and region is not. We expect the allowed baggage tags to appear on all spans. + response = client.get("/", headers={"baggage": "user.id=123,account.id=456,region=us-west"}) + + assert response.status_code == 200 + + traces = test_spans.pop_traces() + assert len(traces) >= 1 + spans = traces[0] + assert len(spans) >= 1 + + for span in spans: + assert span.get_tag("baggage.user.id") == "123" + assert span.get_tag("baggage.account.id") == "456" + # Since "region" is not in the default list, its baggage tag should not be present. + assert span.get_tag("baggage.region") is None + + def test_baggage_span_tagging_no_headers(client, tracer, test_spans): response = client.get("/", headers={}) assert response.status_code == 200 From 3a36181226c06e868a3e8aece058bc127090a6a8 Mon Sep 17 00:00:00 2001 From: Rachel Yang Date: Mon, 22 Sep 2025 16:40:24 -0400 Subject: [PATCH 2/2] linting --- ddtrace/internal/utils/__init__.py | 1 + 1 file changed, 1 insertion(+) diff --git a/ddtrace/internal/utils/__init__.py b/ddtrace/internal/utils/__init__.py index 3881cf7c9af..9c0d2abced2 100644 --- a/ddtrace/internal/utils/__init__.py +++ b/ddtrace/internal/utils/__init__.py @@ -4,6 +4,7 @@ from typing import Optional # noqa:F401 from typing import Tuple # noqa:F401 from typing import Union # noqa:F401 + from ddtrace.internal.constants import BAGGAGE_TAG_PREFIX