Skip to content

Incorrect tracestate key validation in W3CPropagator (.NET 10 RC1) #119767

@gjuttla

Description

@gjuttla

Description

It appears that the W3CPropagator incorrectly detects digits in the tracestate keys as invalid characters and therefore skips calling the setter delegate for the tracestate header.
Based on the TraceContext spec tracestate keys can contain digits [1].

Digits missing in

private const string TraceStateKeyValidChars = "*-/@_abcdefghijklmnopqrstuvwxyz";

key = simple-key / multi-tenant-key
simple-key = lcalpha 0*255( lcalpha / DIGIT / "_" / "-"/ "*" / "/" )
multi-tenant-key = tenant-id "@" system-id
tenant-id = ( lcalpha / DIGIT ) 0*240( lcalpha / DIGIT / "_" / "-"/ "*" / "/" )
system-id = lcalpha 0*13( lcalpha / DIGIT / "_" / "-"/ "*" / "/" )
lcalpha    = %x61-7A ; a-z

[1] https://www.w3.org/TR/trace-context/#key

cc @tarekgh

Reproduction Steps

app.cs

using System.Diagnostics;

using var listener = new ActivityListener
{
    ShouldListenTo = _ => true,
    Sample = (ref ActivityCreationOptions<ActivityContext> _) => ActivitySamplingResult.AllDataAndRecorded,
    ActivityStarted = activity => { }
};

ActivitySource.AddActivityListener(listener);

var activitySource = new ActivitySource("myapp");

using (var activity = activitySource.StartActivity("myactivity"))
{
    activity!.TraceStateString = "f0o=b4r";
    DistributedContextPropagator.CreatePreW3CPropagator().Inject(activity, null, HeaderSetter);
    Console.WriteLine("----");
    DistributedContextPropagator.CreateW3CPropagator().Inject(activity, null, HeaderSetter);
}

void HeaderSetter(object? carrier, string fieldName, string fieldValue)
{
    Console.WriteLine($"{fieldName} = {fieldValue}");
}
traceparent = 00-6be559b2c9a18d5161ab496103927731-86bc8f6b5595b4af-01
tracestate = f0o=b4r
----
traceparent = 00-6be559b2c9a18d5161ab496103927731-86bc8f6b5595b4af-01

Expected behavior

Setter delegate being called for tracestate header when using the W3CPropagator.

Actual behavior

Setter delegate not called for tracestate header when using the W3CPropagator.

Regression?

Yes, works with the LegacyPropagator.

Known Workarounds

Using the LegacyPropagator.

DistributedContextPropagator.Current = DistributedContextPropagator.CreatePreW3CPropagator();

Configuration

.NET 10.0.0-rc.1.25451.107

Other information

No response

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions