Baggage is ignored by W3CBaggagePropagator and erronously causes warning 'Invalid baggage entry'

Open Beginner friendly
#5,678 0 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
82/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
python

Research direction

Start in opentelemetry-api/src/opentelemetry/baggage/propagation/init.py at W3CBaggagePropagator.extract(), then inspect how _is_valid_pair() is called and how the baggage value is decoded. Run the existing baggage propagation tests or the reproduction, and consider the issue done when whitespace around the equals sign is accepted without the warning and the baggage is returned.

Written by the indexing model from the issue text.

Description

bug
Describe your environment

main branch and branch release/v1.44.x-0.65bx

What happened?

When calling an API method from C# that is instrumented via OpenTelemetry nuget packages, then baggage header values are sent as "name = value", i.e. with a space around the = sign.
This is valid, because according to https://www.w3.org/TR/baggage/ a baggage header property has the form: key OWS "=" OWS value, i.e. there can be optional whitespace around the = sign.
However, the Python instrumentation ignores the baggage and logs the warning "Invalid baggage entry".

Steps to Reproduce

import logging
from opentelemetry.baggage import get_all
from opentelemetry.baggage.propagation import W3CBaggagePropagator

logging.basicConfig(
level=logging.WARNING,
format="%(levelname)s:%(name)s:%(message)s",
)

propagator = W3CBaggagePropagator()

""" print warning and ignore baggage """
carrier = {"baggage": "some_name = some_value"}
context = propagator.extract(carrier)
print(dict(get_all(context)))

""" extract the baggage """
carrier = {"baggage": "some_name_no_space=some_value_no_space"}
context = propagator.extract(carrier)
print(dict(get_all(context)))

Expected Result

Whitespace around the = sign in a baggage header value should be accepted and ignored.

Actual Result

The baggage is ignored and the warning "Invalid baggage entry" is logged.

Additional context

In opentelemetry-api/src/opentelemetry/baggage/propagation/init.py in the extract() method of the W3CBaggagePropagator class, whitespace around the = sign leads to the warning "Invalid baggage entry" and the baggage is ignored.
This is because the property is first split by the = sign into name and value.
Then _is_valid_pair(name, value) fails, because of the trailing whitespace in name or the leading whitespace in value.
Only after this check strip() is applied.
The fix is to apply strip() also before calling _is_valid_pair().
It also should be called after unquote_plus(), because spaces could arise from it. But maybe those spaces are intentional and no strip() should be called.

Would you like to implement a fix?

None

Tip

React with 👍 to help prioritize this issue. Please use comments to provide useful context, avoiding +1 or me too, to help us triage it. Learn more here.

Dominant language
Python
Stars
2.6k
Forks
1k
Avg merge
5d 29m
Merged PRs (30d)
17

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from open-telemetry/opentelemetry-python

All issues in open-telemetry/opentelemetry-python

Similar issues

More Python issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.