Skip to content

Commit 108a0fc

Browse files
committed
fix: traceparent's trace-flags are mandatory
1 parent f14cad5 commit 108a0fc

File tree

3 files changed

+6
-21
lines changed

3 files changed

+6
-21
lines changed

src/Sentry.AspNetCore/Extensions/HttpContextExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ internal static class HttpContextExtensions
6363
options?.LogError(ex, "Invalid Sentry trace header '{0}'.", value);
6464
return null;
6565
}
66-
}
66+
}
6767

6868
public static W3CTraceHeader? TryGetW3CTraceHeader(this HttpContext context, SentryOptions? options)
6969
{

src/Sentry/W3CTraceHeader.cs

Lines changed: 4 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public W3CTraceHeader(SentryTraceHeader source)
3636
/// </value>
3737
public SentryTraceHeader SentryTraceHeader { get; }
3838

39-
/// <summary>
39+
/// <summary>
4040
/// Parses a <see cref="SentryTraceHeader"/> from a string representation of the Sentry trace header.
4141
/// </summary>
4242
/// <param name="value">
@@ -57,7 +57,7 @@ public W3CTraceHeader(SentryTraceHeader source)
5757
}
5858

5959
var components = value.Split('-', StringSplitOptions.RemoveEmptyEntries);
60-
if (components.Length < 2)
60+
if (components.Length < 4)
6161
{
6262
throw new FormatException($"Invalid W3C trace header: {value}.");
6363
}
@@ -71,9 +71,7 @@ public W3CTraceHeader(SentryTraceHeader source)
7171
var traceId = SentryId.Parse(components[1]);
7272
var spanId = SpanId.Parse(components[2]);
7373

74-
var isSampled = components.Length >= 4
75-
? string.Equals(components[3], "01", StringComparison.OrdinalIgnoreCase)
76-
: (bool?)null;
74+
var isSampled = string.Equals(components[3], "01", StringComparison.OrdinalIgnoreCase);
7775

7876
return new W3CTraceHeader(new SentryTraceHeader(traceId, spanId, isSampled));
7977
}
@@ -82,11 +80,6 @@ public W3CTraceHeader(SentryTraceHeader source)
8280
public override string ToString()
8381
{
8482
var traceFlags = ConvertSampledToTraceFlags(SentryTraceHeader.IsSampled);
85-
if (traceFlags is null)
86-
{
87-
return $"{SupportedVersion}-{SentryTraceHeader.TraceId}-{SentryTraceHeader.SpanId}";
88-
}
89-
9083
return $"{SupportedVersion}-{SentryTraceHeader.TraceId}-{SentryTraceHeader.SpanId}-{traceFlags}";
9184
}
9285

@@ -104,13 +97,5 @@ public override bool Equals(object? obj)
10497
/// <inheritdoc/>
10598
public override int GetHashCode() => SentryTraceHeader.GetHashCode();
10699

107-
private static string? ConvertSampledToTraceFlags(bool? isSampled)
108-
{
109-
return isSampled switch
110-
{
111-
true => "01",
112-
false => "00",
113-
null => null
114-
};
115-
}
100+
private static string? ConvertSampledToTraceFlags(bool? isSampled) => isSampled ?? false ? "01" : "00";
116101
}

test/Sentry.Tests/W3CTraceHeaderTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,6 @@ public void ToString_WithoutSampled_ConvertsToW3CFormat()
2929
var result = traceHeader.ToString();
3030

3131
// Assert
32-
result.Should().Be("00-75302ac48a024bde9a3b3734a82e36c8-1000000000000000");
32+
result.Should().Be("00-75302ac48a024bde9a3b3734a82e36c8-1000000000000000-00");
3333
}
3434
}

0 commit comments

Comments
 (0)