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 CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@
- Added support for `.NET 9` (preview) ([#3699](https://github.com/getsentry/sentry-dotnet/pull/3699))
- libsentrysupplemental.so now supports 16 KB page sizes on Android ([#3723](https://github.com/getsentry/sentry-dotnet/pull/3723))

### Fixes

- Fixed NullReferenceException in SentryTraceHeader when parsing null or empty values ([#3757](https://github.com/getsentry/sentry-dotnet/pull/3757))

## Unreleased

### Fixes
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 @@ -911,7 +911,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 @@ -911,7 +911,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 @@ -913,7 +913,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 @@ -913,7 +913,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
2 changes: 1 addition & 1 deletion test/Sentry.Tests/ApiApprovalTests.Run.Net4_8.verified.txt
Original file line number Diff line number Diff line change
Expand Up @@ -908,7 +908,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