Skip to content
Closed
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
### Fixes

- Fixed ArgumentNullException in FormRequestPayloadExtractor when handling invalid form data on ASP.NET ([#3734](https://github.com/getsentry/sentry-dotnet/pull/3734))
- Fixed NullReferenceException in SentryTraceHeader when parsing null or empty values ([#3745](https://github.com/getsentry/sentry-dotnet/pull/3745))

### Dependencies

Expand Down
19 changes: 17 additions & 2 deletions src/Sentry/SentryTraceHeader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,25 @@ public override string ToString() => IsSampled is { } isSampled
: $"{TraceId}-{SpanId}";

/// <summary>
/// Parses <see cref="SentryTraceHeader"/> from string.
/// Parses a <see cref="SentryTraceHeader"/> from a string representation of the Sentry trace header.
/// </summary>
public static SentryTraceHeader Parse(string value)
/// <param name="value">
/// A string containing the Sentry trace header, expected to follow the format "traceId-spanId-sampled",
/// where "sampled" is optional.
/// </param>
/// <returns>
/// A <see cref="SentryTraceHeader"/> object if parsing succeeds, or <c>null</c> if the input string is null, empty, or whitespace.
/// </returns>
/// <exception cref="FormatException">
/// Thrown if the input string does not contain a valid trace header format, specifically if it lacks required trace ID and span ID components.
/// </exception>
public static SentryTraceHeader? Parse(string value)
{
if (string.IsNullOrWhiteSpace(value))
{
return null;
}

var components = value.Split('-', StringSplitOptions.RemoveEmptyEntries);
if (components.Length < 2)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -931,7 +931,7 @@ namespace Sentry
public Sentry.SpanId SpanId { get; }
public Sentry.SentryId TraceId { get; }
public override string ToString() { }
public static Sentry.SentryTraceHeader Parse(string value) { }
public static Sentry.SentryTraceHeader? Parse(string value) { }
}
public class SentryTransaction : Sentry.IEventLike, Sentry.IHasExtra, Sentry.IHasTags, Sentry.ISentryJsonSerializable, Sentry.ISpanData, Sentry.ITransactionContext, Sentry.ITransactionData, Sentry.Protocol.ITraceContext
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -931,7 +931,7 @@ namespace Sentry
public Sentry.SpanId SpanId { get; }
public Sentry.SentryId TraceId { get; }
public override string ToString() { }
public static Sentry.SentryTraceHeader Parse(string value) { }
public static Sentry.SentryTraceHeader? Parse(string value) { }
}
public class SentryTransaction : Sentry.IEventLike, Sentry.IHasExtra, Sentry.IHasTags, Sentry.ISentryJsonSerializable, Sentry.ISpanData, Sentry.ITransactionContext, Sentry.ITransactionData, Sentry.Protocol.ITraceContext
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -933,7 +933,7 @@ namespace Sentry
public Sentry.SpanId SpanId { get; }
public Sentry.SentryId TraceId { get; }
public override string ToString() { }
public static Sentry.SentryTraceHeader Parse(string value) { }
public static Sentry.SentryTraceHeader? Parse(string value) { }
}
public class SentryTransaction : Sentry.IEventLike, Sentry.IHasExtra, Sentry.IHasTags, Sentry.ISentryJsonSerializable, Sentry.ISpanData, Sentry.ITransactionContext, Sentry.ITransactionData, Sentry.Protocol.ITraceContext
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -928,7 +928,7 @@ namespace Sentry
public Sentry.SpanId SpanId { get; }
public Sentry.SentryId TraceId { get; }
public override string ToString() { }
public static Sentry.SentryTraceHeader Parse(string value) { }
public static Sentry.SentryTraceHeader? Parse(string value) { }
}
public class SentryTransaction : Sentry.IEventLike, Sentry.IHasExtra, Sentry.IHasTags, Sentry.ISentryJsonSerializable, Sentry.ISpanData, Sentry.ITransactionContext, Sentry.ITransactionData, Sentry.Protocol.ITraceContext
{
Expand Down
13 changes: 13 additions & 0 deletions test/Sentry.Tests/Protocol/SentryTraceHeaderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,17 @@ public void Parse_WithSampledFalse_Works()
header.SpanId.Should().Be(SpanId.Parse("1000000000000000"));
header.IsSampled.Should().BeFalse();
}

[Theory]
[InlineData(null)]
[InlineData("")]
[InlineData(" ")]
public void Parse_WithoutHeaderValue_ReturnsNull(string headerValue)
{
// Act
var header = SentryTraceHeader.Parse(headerValue);

// Assert
header.Should().BeNull();
}
}
Loading