Skip to content

[CONTEXT] Fix env carrier key normalization to be spec compliant#4141

Open
pranitaurlam wants to merge 1 commit into
open-telemetry:mainfrom
pranitaurlam:fix/env-carrier-spec-compliance
Open

[CONTEXT] Fix env carrier key normalization to be spec compliant#4141
pranitaurlam wants to merge 1 commit into
open-telemetry:mainfrom
pranitaurlam:fix/env-carrier-spec-compliance

Conversation

@pranitaurlam
Copy link
Copy Markdown

Fixes #4061

Changes

  • ASCII-only normalization: Replace std::isalnum (locale-dependent) with explicit ASCII range checks (a-z, A-Z, 0-9)
  • Digit prefix: Prepend _ when normalized key starts with a digit, per the spec
  • Normalized cache key: Get() now caches by the normalized key, so Get("x-b3-traceid") and Get("X_B3_TRACEID") hit the same cache entry and map to the same env var X_B3_TRACEID
  • Remove unused includes: Drop <algorithm> and <cctype> since std::transform/std::isalnum are no longer used
  • Tests: Add cases for hyphen/dot replacement, digit prefix, normalized cache key lookup

Spec reference

https://opentelemetry.io/docs/specs/otel/context/env-carriers/#key-name-normalization

CC @marcalff @lalitb @ThomsonTan

- Use ASCII-only normalization (was locale-dependent via std::isalnum)
- Prefix key with '_' when it starts with a digit
- Cache by normalized key so equivalent forms hit the same entry

Fixes open-telemetry#4061
@pranitaurlam pranitaurlam requested a review from a team as a code owner June 7, 2026 19:39
@pranitaurlam
Copy link
Copy Markdown
Author

Hi @marcalff @lalitb @ThomsonTan
could you please review and approve PR #4141? It fixes environment variable and carrier key normalization to be spec compliment (issue #4061). Thanks

@codecov
Copy link
Copy Markdown

codecov Bot commented Jun 7, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 82.03%. Comparing base (4352a63) to head (7861ce3).

Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##             main    #4141      +/-   ##
==========================================
+ Coverage   82.01%   82.03%   +0.02%     
==========================================
  Files         385      385              
  Lines       16093    16098       +5     
==========================================
+ Hits        13197    13204       +7     
+ Misses       2896     2894       -2     
Files with missing lines Coverage Δ
...elemetry/context/propagation/environment_carrier.h 100.00% <100.00%> (ø)

... and 1 file with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

}

// Read from environment
const char *value = std::getenv(env_name.c_str());
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This only looks up the normalized requested name. The spec says Get should also normalize key names already present in the carrier before matching, so this would not match an existing env key like x-b3-traceid. Should we snapshot/list the environment and match normalized names there?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Make environment variable propagation spec compliant

2 participants