diff --git a/CHANGELOG.md b/CHANGELOG.md index db3b4eb83f4..8bee8bd3cca 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - `opentelemetry-semantic-conventions` Update to semantic conventions v1.6.1 ([#2077](https://github.com/open-telemetry/opentelemetry-python/pull/2077)) +- Fix propagation bug caused by counting skipped entries + ([#2071](https://github.com/open-telemetry/opentelemetry-python/pull/2071)) ## [1.5.0-0.24b0](https://github.com/open-telemetry/opentelemetry-python/releases/tag/v1.5.0-0.24b0) - 2021-08-26 diff --git a/opentelemetry-api/src/opentelemetry/baggage/propagation/__init__.py b/opentelemetry-api/src/opentelemetry/baggage/propagation/__init__.py index b484d9b2e8d..9d170aae9a9 100644 --- a/opentelemetry-api/src/opentelemetry/baggage/propagation/__init__.py +++ b/opentelemetry-api/src/opentelemetry/baggage/propagation/__init__.py @@ -54,9 +54,6 @@ def extract( baggage_entries = header.split(",") total_baggage_entries = self._MAX_PAIRS for entry in baggage_entries: - if total_baggage_entries <= 0: - return context - total_baggage_entries -= 1 if len(entry) > self._MAX_PAIR_LENGTH: continue try: @@ -68,6 +65,9 @@ def extract( unquote_plus(value).strip(), context=context, ) + total_baggage_entries -= 1 + if total_baggage_entries == 0: + break return context diff --git a/opentelemetry-api/tests/baggage/test_baggage_propagation.py b/opentelemetry-api/tests/baggage/test_baggage_propagation.py index 009598db9ff..962075ce60a 100644 --- a/opentelemetry-api/tests/baggage/test_baggage_propagation.py +++ b/opentelemetry-api/tests/baggage/test_baggage_propagation.py @@ -118,6 +118,29 @@ def test_extract_unquote_plus(self): {"key/key": "value/value"}, ) + def test_header_max_entries_skip_invalid_entry(self): + + self.assertEqual( + self._extract( + ",".join( + [ + f"key{index}=value{index}" + if index != 2 + else ( + f"key{index}=" + f"value{'s' * (W3CBaggagePropagator._MAX_PAIR_LENGTH + 1)}" + ) + for index in range(W3CBaggagePropagator._MAX_PAIRS + 1) + ] + ) + ), + { + f"key{index}": f"value{index}" + for index in range(W3CBaggagePropagator._MAX_PAIRS + 1) + if index != 2 + }, + ) + def test_inject_no_baggage_entries(self): values = {} output = self._inject(values)