Skip to content

Commit

Permalink
fix(propagation): make baggage header extract case insensitive (#11859)
Browse files Browse the repository at this point in the history
Following the WSGI spec, headers shouldn't be case sensitive, therefore
baggage, like the other headers we extract, should not be case
sensitive.
https://peps.python.org/pep-3333/#environ-variables: "(A reminder for
server/gateway authors: HTTP header names are case-insensitive, so be
sure to take that into consideration when examining application-supplied
headers!)"

## 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)
  • Loading branch information
ZStriker19 authored Jan 9, 2025
1 parent d676233 commit 68fc8f0
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 9 deletions.
3 changes: 2 additions & 1 deletion ddtrace/propagation/http.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ def _possible_header(header):
_POSSIBLE_HTTP_HEADER_B3_FLAGS = _possible_header(_HTTP_HEADER_B3_FLAGS)
_POSSIBLE_HTTP_HEADER_TRACEPARENT = _possible_header(_HTTP_HEADER_TRACEPARENT)
_POSSIBLE_HTTP_HEADER_TRACESTATE = _possible_header(_HTTP_HEADER_TRACESTATE)
_POSSIBLE_HTTP_BAGGAGE_HEADER = _possible_header(_HTTP_HEADER_BAGGAGE)


# https://www.w3.org/TR/trace-context/#traceparent-header-field-values
Expand Down Expand Up @@ -937,7 +938,7 @@ def _inject(span_context: Context, headers: Dict[str, str]) -> None:

@staticmethod
def _extract(headers: Dict[str, str]) -> Context:
header_value = headers.get(_HTTP_HEADER_BAGGAGE)
header_value = _extract_header_value(_POSSIBLE_HTTP_BAGGAGE_HEADER, headers)

if not header_value:
return Context(baggage={})
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
fixes:
- |
tracer: This fix resolves an issue where baggage header extraction was case sensitive and didn't accept the header prepended with HTTP.
Now the baggage header will be extracted regardless of casing and the HTTP format.
24 changes: 16 additions & 8 deletions tests/tracer/test_propagation.py
Original file line number Diff line number Diff line change
Expand Up @@ -1888,6 +1888,14 @@ def test_extract_tracecontext(headers, expected_context):
B3_SINGLE_HEADERS_VALID,
CONTEXT_EMPTY,
),
(
"baggage_case_insensitive",
None,
{"BAgGage": "key1=val1,key2=val2"},
{
"baggage": {"key1": "val1", "key2": "val2"},
},
),
# All valid headers
(
"valid_all_headers_default_style",
Expand Down Expand Up @@ -2278,14 +2286,14 @@ def test_propagation_extract_w_config(name, styles, headers, expected_context, r
overrides = {}
if styles is not None:
overrides["_propagation_style_extract"] = styles
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 = [
Expand Down

0 comments on commit 68fc8f0

Please sign in to comment.