Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/OpenTelemetry.Api/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ Notes](../../RELEASENOTES.md).
normalize to the requested key.
([#7410](https://github.com/open-telemetry/opentelemetry-dotnet/pull/7410))

* **Experimental (pre-release builds only):** Updated `EnvironmentVariableCarrier`
key normalization to replace an empty key with a single underscore (`_`).
([#7424](https://github.com/open-telemetry/opentelemetry-dotnet/pull/7424))

## 1.16.0

Released 2026-Jun-10
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ namespace OpenTelemetry.Context.Propagation;
/// normalization follows the OpenTelemetry environment carrier specification by
/// uppercasing ASCII letters, replacing non-ASCII letters, non-digits, and
/// non-underscore characters with underscores, prefixing an underscore when
/// a normalized key would otherwise start with a digit.
/// a key would otherwise start with a digit, and replacing an empty
/// key with a single underscore.
/// </remarks>
[System.Diagnostics.CodeAnalysis.Experimental(DiagnosticDefinitions.EnvironmentVariableContextPropagationExperimentalApi, UrlFormat = DiagnosticDefinitions.ExperimentalApiUrlFormat)]
public
Expand Down Expand Up @@ -167,9 +168,10 @@ public static string NormalizeKey(string key)

private static bool IsAlreadyNormalized(string key)
{
// An empty key is non-normalized and normalizes to a single underscore.
if (key.Length == 0 || char.IsAsciiDigit(key[0]))
{
return key.Length == 0;
return false;
}

foreach (var ch in key)
Expand All @@ -185,6 +187,11 @@ private static bool IsAlreadyNormalized(string key)

private static string CreateNormalizedKey(string key)
{
if (key.Length == 0)
{
return "_";
}

var prefixLength = char.IsAsciiDigit(key[0]) ? 1 : 0;
int length = key.Length + prefixLength;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public class EnvironmentVariableCarrierTests
[InlineData("otel-baggage-key", "OTEL_BAGGAGE_KEY")]
[InlineData("123trace", "_123TRACE")]
[InlineData("M\u00f6j Baga\u017c", "M_J_BAGA_")]
[InlineData("", "_")]
public void NormalizeKey_CompliesWithSpecification(string key, string expected)
=> Assert.Equal(expected, EnvironmentVariableCarrier.NormalizeKey(key));

Expand Down