Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add support for true in x-b3-sampled #1413

Merged
merged 7 commits into from
Oct 29, 2020
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
3 changes: 3 additions & 0 deletions src/OpenTelemetry.Api/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
([#1415](https://github.com/open-telemetry/opentelemetry-dotnet/pull/1415))
* Removed `IsOk` property from `Status` and fixed `StatusCode` enum values
([#1414](https://github.com/open-telemetry/opentelemetry-dotnet/pull/1414))
* `B3Propagator` now supports the value `true` to be passed in for the header
`X-B3-Sampled`.
([#1413](https://github.com/open-telemetry/opentelemetry-dotnet/pull/1413))

## 0.7.0-beta.1

Expand Down
11 changes: 8 additions & 3 deletions src/OpenTelemetry.Api/Context/Propagation/B3Propagator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,19 @@ public sealed class B3Propagator : IPropagator
// ActivityTraceId.SIZE hex characters (8-bytes traceId) in the past.
internal const string UpperTraceId = "0000000000000000";

// Sampled value via the X_B3_SAMPLED header.
// Sampled values via the X_B3_SAMPLED header.
internal const string SampledValue = "1";

// Some old zipkin implementations may send true/false for the sampled header. Only use this for checking incoming values.
internal const string LegacySampledValue = "true";

// "Debug" sampled value.
internal const string FlagsValue = "1";

private static readonly HashSet<string> AllFields = new HashSet<string>() { XB3TraceId, XB3SpanId, XB3ParentSpanId, XB3Sampled, XB3Flags };

private static readonly HashSet<string> SampledValues = new HashSet<string>(StringComparer.Ordinal) { SampledValue, LegacySampledValue };

private readonly bool singleHeader;

/// <summary>
Expand Down Expand Up @@ -180,7 +185,7 @@ private static PropagationContext ExtractFromMultipleHeaders<T>(PropagationConte
}

var traceOptions = ActivityTraceFlags.None;
if (SampledValue.Equals(getter(carrier, XB3Sampled)?.FirstOrDefault(), StringComparison.Ordinal)
if (SampledValues.Contains(getter(carrier, XB3Sampled)?.FirstOrDefault())
|| FlagsValue.Equals(getter(carrier, XB3Flags)?.FirstOrDefault(), StringComparison.Ordinal))
{
traceOptions |= ActivityTraceFlags.Recorded;
Expand Down Expand Up @@ -239,7 +244,7 @@ private static PropagationContext ExtractFromSingleHeader<T>(PropagationContext
if (parts.Length > 2)
{
var traceFlagsStr = parts[2];
if (SampledValue.Equals(traceFlagsStr, StringComparison.Ordinal)
if (SampledValues.Contains(traceFlagsStr)
|| FlagsValue.Equals(traceFlagsStr, StringComparison.Ordinal))
{
traceOptions |= ActivityTraceFlags.Recorded;
Expand Down
17 changes: 11 additions & 6 deletions test/OpenTelemetry.Tests/Trace/Propagation/B3PropagatorTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,23 +81,28 @@ public void ParseMissingSampledAndMissingFlag()
Assert.Equal(new PropagationContext(spanContext, default), this.b3propagator.Extract(default, headersNotSampled, Getter));
}

[Fact]
public void ParseSampled()
[Theory]
[InlineData("1")]
[InlineData("true")]
public void ParseSampled(string sampledValue)
{
var headersSampled = new Dictionary<string, string>
{
{ B3Propagator.XB3TraceId, TraceIdBase16 }, { B3Propagator.XB3SpanId, SpanIdBase16 }, { B3Propagator.XB3Sampled, "1" },
{ B3Propagator.XB3TraceId, TraceIdBase16 }, { B3Propagator.XB3SpanId, SpanIdBase16 }, { B3Propagator.XB3Sampled, sampledValue },
};
var activityContext = new ActivityContext(TraceId, SpanId, TraceOptions, isRemote: true);
Assert.Equal(new PropagationContext(activityContext, default), this.b3propagator.Extract(default, headersSampled, Getter));
}

[Fact]
public void ParseZeroSampled()
[Theory]
[InlineData("0")]
[InlineData("false")]
[InlineData("something_else")]
public void ParseNotSampled(string sampledValue)
{
var headersNotSampled = new Dictionary<string, string>
{
{ B3Propagator.XB3TraceId, TraceIdBase16 }, { B3Propagator.XB3SpanId, SpanIdBase16 }, { B3Propagator.XB3Sampled, "0" },
{ B3Propagator.XB3TraceId, TraceIdBase16 }, { B3Propagator.XB3SpanId, SpanIdBase16 }, { B3Propagator.XB3Sampled, sampledValue },
};
var activityContext = new ActivityContext(TraceId, SpanId, ActivityTraceFlags.None, isRemote: true);
Assert.Equal(new PropagationContext(activityContext, default), this.b3propagator.Extract(default, headersNotSampled, Getter));
Expand Down