Skip to content

Commit

Permalink
fix: Overload for ContinueTrace to accept headers as string (#2601)
Browse files Browse the repository at this point in the history
  • Loading branch information
bitsandfoxes committed Sep 18, 2023
1 parent 6de1bbd commit 0ae99b1
Show file tree
Hide file tree
Showing 11 changed files with 123 additions and 2 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@

### Features

- The SDK now provides and overload of `ContinueTrace` that accepts headers as `string` ([#2601](https://github.com/getsentry/sentry-dotnet/pull/2601))
- Sentry tracing middleware now gets configured automatically ([#2602](https://github.com/getsentry/sentry-dotnet/pull/2602))

### Dependencies

- Bump CLI from v2.20.6 to v2.20.7 ([#2604](https://github.com/getsentry/sentry-dotnet/pull/2604))
- [changelog](https://github.com/getsentry/sentry-cli/blob/master/CHANGELOG.md#2207)
- [diff](https://github.com/getsentry/sentry-cli/compare/2.20.6...2.20.7)

## 3.39.1

### Fixes
Expand Down
15 changes: 14 additions & 1 deletion src/Sentry/Extensibility/DisabledHub.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,20 @@ public void BindException(Exception exception, ISpan span)
public BaggageHeader? GetBaggage() => null;

/// <summary>
/// Returns null.
/// Returns sampled out transaction context.
/// </summary>
public TransactionContext ContinueTrace(
string? traceHeader,
string? baggageHeader,
string? name = null,
string? operation = null)
{
// Transactions from DisabledHub are always sampled out
return new TransactionContext( name ?? string.Empty, operation ?? string.Empty, false);
}

/// <summary>
/// Returns sampled out transaction context.
/// </summary>
public TransactionContext ContinueTrace(
SentryTraceHeader? traceHeader,
Expand Down
11 changes: 11 additions & 0 deletions src/Sentry/Extensibility/HubAdapter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,17 @@ public void BindException(Exception exception, ISpan span) =>
public BaggageHeader? GetBaggage()
=> SentrySdk.GetBaggage();

/// <summary>
/// Forwards the call to <see cref="SentrySdk"/>.
/// </summary>
[DebuggerStepThrough]
public TransactionContext ContinueTrace(
string? traceHeader,
string? baggageHeader,
string? name = null,
string? operation = null)
=> SentrySdk.ContinueTrace(traceHeader, baggageHeader, name, operation);

/// <summary>
/// Forwards the call to <see cref="SentrySdk"/>.
/// </summary>
Expand Down
12 changes: 12 additions & 0 deletions src/Sentry/IHub.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,18 @@ ITransaction StartTransaction(
/// </summary>
BaggageHeader? GetBaggage();

/// <summary>
/// Continues a trace based on HTTP header values provided as strings.
/// </summary>
/// <remarks>
/// If no "sentry-trace" header is provided a random trace ID and span ID is created.
/// </remarks>
TransactionContext ContinueTrace(
string? traceHeader,
string? baggageHeader,
string? name = null,
string? operation = null);

/// <summary>
/// Continues a trace based on HTTP header values.
/// </summary>
Expand Down
21 changes: 21 additions & 0 deletions src/Sentry/Internal/Hub.cs
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,27 @@ public BaggageHeader GetBaggage()
return propagationContext.GetOrCreateDynamicSamplingContext(_options).ToBaggageHeader();
}

public TransactionContext ContinueTrace(
string? traceHeader,
string? baggageHeader,
string? name = null,
string? operation = null)
{
SentryTraceHeader? sentryTraceHeader = null;
if (traceHeader is not null)
{
sentryTraceHeader = SentryTraceHeader.Parse(traceHeader);
}

BaggageHeader? sentryBaggageHeader = null;
if (baggageHeader is not null)
{
sentryBaggageHeader = BaggageHeader.TryParse(baggageHeader, onlySentry: true);
}

return ContinueTrace(sentryTraceHeader, sentryBaggageHeader, name, operation);
}

public TransactionContext ContinueTrace(
SentryTraceHeader? traceHeader,
BaggageHeader? baggageHeader,
Expand Down
14 changes: 14 additions & 0 deletions src/Sentry/SentrySdk.cs
Original file line number Diff line number Diff line change
Expand Up @@ -624,6 +624,20 @@ public static void BindException(Exception exception, ISpan span)
public static BaggageHeader? GetBaggage()
=> CurrentHub.GetBaggage();

/// <summary>
/// Continues a trace based on HTTP header values provided as strings.
/// </summary>
/// <remarks>
/// If no "sentry-trace" header is provided a random trace ID and span ID is created.
/// </remarks>
[DebuggerStepThrough]
public static TransactionContext ContinueTrace(
string? traceHeader,
string? baggageHeader,
string? name = null,
string? operation = null)
=> CurrentHub.ContinueTrace(traceHeader, baggageHeader, name, operation);

/// <summary>
/// Continues a trace based on HTTP header values.
/// </summary>
Expand Down
4 changes: 4 additions & 0 deletions test/Sentry.Tests/ApiApprovalTests.Run.Core3_1.verified.txt
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,7 @@ namespace Sentry
void BindException(System.Exception exception, Sentry.ISpan span);
Sentry.SentryId CaptureEvent(Sentry.SentryEvent evt, System.Action<Sentry.Scope> configureScope);
Sentry.TransactionContext ContinueTrace(Sentry.SentryTraceHeader? traceHeader, Sentry.BaggageHeader? baggageHeader, string? name = null, string? operation = null);
Sentry.TransactionContext ContinueTrace(string? traceHeader, string? baggageHeader, string? name = null, string? operation = null);
void EndSession(Sentry.SessionEndStatus status = 0);
Sentry.BaggageHeader? GetBaggage();
Sentry.ISpan? GetSpan();
Expand Down Expand Up @@ -760,6 +761,7 @@ namespace Sentry
public static void ConfigureScope(System.Action<Sentry.Scope> configureScope) { }
public static System.Threading.Tasks.Task ConfigureScopeAsync(System.Func<Sentry.Scope, System.Threading.Tasks.Task> configureScope) { }
public static Sentry.TransactionContext ContinueTrace(Sentry.SentryTraceHeader? traceHeader, Sentry.BaggageHeader? baggageHeader, string? name = null, string? operation = null) { }
public static Sentry.TransactionContext ContinueTrace(string? traceHeader, string? baggageHeader, string? name = null, string? operation = null) { }
public static void EndSession(Sentry.SessionEndStatus status = 0) { }
public static void Flush() { }
public static void Flush(System.TimeSpan timeout) { }
Expand Down Expand Up @@ -1236,6 +1238,7 @@ namespace Sentry.Extensibility
public void ConfigureScope(System.Action<Sentry.Scope> configureScope) { }
public System.Threading.Tasks.Task ConfigureScopeAsync(System.Func<Sentry.Scope, System.Threading.Tasks.Task> configureScope) { }
public Sentry.TransactionContext ContinueTrace(Sentry.SentryTraceHeader? traceHeader, Sentry.BaggageHeader? baggageHeader, string? name = null, string? operation = null) { }
public Sentry.TransactionContext ContinueTrace(string? traceHeader, string? baggageHeader, string? name = null, string? operation = null) { }
public void Dispose() { }
public void EndSession(Sentry.SessionEndStatus status = 0) { }
public System.Threading.Tasks.Task FlushAsync(System.TimeSpan timeout) { }
Expand Down Expand Up @@ -1277,6 +1280,7 @@ namespace Sentry.Extensibility
public void ConfigureScope(System.Action<Sentry.Scope> configureScope) { }
public System.Threading.Tasks.Task ConfigureScopeAsync(System.Func<Sentry.Scope, System.Threading.Tasks.Task> configureScope) { }
public Sentry.TransactionContext ContinueTrace(Sentry.SentryTraceHeader? traceHeader, Sentry.BaggageHeader? baggageHeader, string? name = null, string? operation = null) { }
public Sentry.TransactionContext ContinueTrace(string? traceHeader, string? baggageHeader, string? name = null, string? operation = null) { }
public void EndSession(Sentry.SessionEndStatus status = 0) { }
public System.Threading.Tasks.Task FlushAsync(System.TimeSpan timeout) { }
public Sentry.BaggageHeader? GetBaggage() { }
Expand Down
4 changes: 4 additions & 0 deletions test/Sentry.Tests/ApiApprovalTests.Run.DotNet6_0.verified.txt
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,7 @@ namespace Sentry
void BindException(System.Exception exception, Sentry.ISpan span);
Sentry.SentryId CaptureEvent(Sentry.SentryEvent evt, System.Action<Sentry.Scope> configureScope);
Sentry.TransactionContext ContinueTrace(Sentry.SentryTraceHeader? traceHeader, Sentry.BaggageHeader? baggageHeader, string? name = null, string? operation = null);
Sentry.TransactionContext ContinueTrace(string? traceHeader, string? baggageHeader, string? name = null, string? operation = null);
void EndSession(Sentry.SessionEndStatus status = 0);
Sentry.BaggageHeader? GetBaggage();
Sentry.ISpan? GetSpan();
Expand Down Expand Up @@ -761,6 +762,7 @@ namespace Sentry
public static void ConfigureScope(System.Action<Sentry.Scope> configureScope) { }
public static System.Threading.Tasks.Task ConfigureScopeAsync(System.Func<Sentry.Scope, System.Threading.Tasks.Task> configureScope) { }
public static Sentry.TransactionContext ContinueTrace(Sentry.SentryTraceHeader? traceHeader, Sentry.BaggageHeader? baggageHeader, string? name = null, string? operation = null) { }
public static Sentry.TransactionContext ContinueTrace(string? traceHeader, string? baggageHeader, string? name = null, string? operation = null) { }
public static void EndSession(Sentry.SessionEndStatus status = 0) { }
public static void Flush() { }
public static void Flush(System.TimeSpan timeout) { }
Expand Down Expand Up @@ -1237,6 +1239,7 @@ namespace Sentry.Extensibility
public void ConfigureScope(System.Action<Sentry.Scope> configureScope) { }
public System.Threading.Tasks.Task ConfigureScopeAsync(System.Func<Sentry.Scope, System.Threading.Tasks.Task> configureScope) { }
public Sentry.TransactionContext ContinueTrace(Sentry.SentryTraceHeader? traceHeader, Sentry.BaggageHeader? baggageHeader, string? name = null, string? operation = null) { }
public Sentry.TransactionContext ContinueTrace(string? traceHeader, string? baggageHeader, string? name = null, string? operation = null) { }
public void Dispose() { }
public void EndSession(Sentry.SessionEndStatus status = 0) { }
public System.Threading.Tasks.Task FlushAsync(System.TimeSpan timeout) { }
Expand Down Expand Up @@ -1278,6 +1281,7 @@ namespace Sentry.Extensibility
public void ConfigureScope(System.Action<Sentry.Scope> configureScope) { }
public System.Threading.Tasks.Task ConfigureScopeAsync(System.Func<Sentry.Scope, System.Threading.Tasks.Task> configureScope) { }
public Sentry.TransactionContext ContinueTrace(Sentry.SentryTraceHeader? traceHeader, Sentry.BaggageHeader? baggageHeader, string? name = null, string? operation = null) { }
public Sentry.TransactionContext ContinueTrace(string? traceHeader, string? baggageHeader, string? name = null, string? operation = null) { }
public void EndSession(Sentry.SessionEndStatus status = 0) { }
public System.Threading.Tasks.Task FlushAsync(System.TimeSpan timeout) { }
public Sentry.BaggageHeader? GetBaggage() { }
Expand Down
4 changes: 4 additions & 0 deletions test/Sentry.Tests/ApiApprovalTests.Run.DotNet7_0.verified.txt
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,7 @@ namespace Sentry
void BindException(System.Exception exception, Sentry.ISpan span);
Sentry.SentryId CaptureEvent(Sentry.SentryEvent evt, System.Action<Sentry.Scope> configureScope);
Sentry.TransactionContext ContinueTrace(Sentry.SentryTraceHeader? traceHeader, Sentry.BaggageHeader? baggageHeader, string? name = null, string? operation = null);
Sentry.TransactionContext ContinueTrace(string? traceHeader, string? baggageHeader, string? name = null, string? operation = null);
void EndSession(Sentry.SessionEndStatus status = 0);
Sentry.BaggageHeader? GetBaggage();
Sentry.ISpan? GetSpan();
Expand Down Expand Up @@ -761,6 +762,7 @@ namespace Sentry
public static void ConfigureScope(System.Action<Sentry.Scope> configureScope) { }
public static System.Threading.Tasks.Task ConfigureScopeAsync(System.Func<Sentry.Scope, System.Threading.Tasks.Task> configureScope) { }
public static Sentry.TransactionContext ContinueTrace(Sentry.SentryTraceHeader? traceHeader, Sentry.BaggageHeader? baggageHeader, string? name = null, string? operation = null) { }
public static Sentry.TransactionContext ContinueTrace(string? traceHeader, string? baggageHeader, string? name = null, string? operation = null) { }
public static void EndSession(Sentry.SessionEndStatus status = 0) { }
public static void Flush() { }
public static void Flush(System.TimeSpan timeout) { }
Expand Down Expand Up @@ -1237,6 +1239,7 @@ namespace Sentry.Extensibility
public void ConfigureScope(System.Action<Sentry.Scope> configureScope) { }
public System.Threading.Tasks.Task ConfigureScopeAsync(System.Func<Sentry.Scope, System.Threading.Tasks.Task> configureScope) { }
public Sentry.TransactionContext ContinueTrace(Sentry.SentryTraceHeader? traceHeader, Sentry.BaggageHeader? baggageHeader, string? name = null, string? operation = null) { }
public Sentry.TransactionContext ContinueTrace(string? traceHeader, string? baggageHeader, string? name = null, string? operation = null) { }
public void Dispose() { }
public void EndSession(Sentry.SessionEndStatus status = 0) { }
public System.Threading.Tasks.Task FlushAsync(System.TimeSpan timeout) { }
Expand Down Expand Up @@ -1278,6 +1281,7 @@ namespace Sentry.Extensibility
public void ConfigureScope(System.Action<Sentry.Scope> configureScope) { }
public System.Threading.Tasks.Task ConfigureScopeAsync(System.Func<Sentry.Scope, System.Threading.Tasks.Task> configureScope) { }
public Sentry.TransactionContext ContinueTrace(Sentry.SentryTraceHeader? traceHeader, Sentry.BaggageHeader? baggageHeader, string? name = null, string? operation = null) { }
public Sentry.TransactionContext ContinueTrace(string? traceHeader, string? baggageHeader, string? name = null, string? operation = null) { }
public void EndSession(Sentry.SessionEndStatus status = 0) { }
public System.Threading.Tasks.Task FlushAsync(System.TimeSpan timeout) { }
public Sentry.BaggageHeader? GetBaggage() { }
Expand Down
4 changes: 4 additions & 0 deletions test/Sentry.Tests/ApiApprovalTests.Run.Net4_8.verified.txt
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,7 @@ namespace Sentry
void BindException(System.Exception exception, Sentry.ISpan span);
Sentry.SentryId CaptureEvent(Sentry.SentryEvent evt, System.Action<Sentry.Scope> configureScope);
Sentry.TransactionContext ContinueTrace(Sentry.SentryTraceHeader? traceHeader, Sentry.BaggageHeader? baggageHeader, string? name = null, string? operation = null);
Sentry.TransactionContext ContinueTrace(string? traceHeader, string? baggageHeader, string? name = null, string? operation = null);
void EndSession(Sentry.SessionEndStatus status = 0);
Sentry.BaggageHeader? GetBaggage();
Sentry.ISpan? GetSpan();
Expand Down Expand Up @@ -759,6 +760,7 @@ namespace Sentry
public static void ConfigureScope(System.Action<Sentry.Scope> configureScope) { }
public static System.Threading.Tasks.Task ConfigureScopeAsync(System.Func<Sentry.Scope, System.Threading.Tasks.Task> configureScope) { }
public static Sentry.TransactionContext ContinueTrace(Sentry.SentryTraceHeader? traceHeader, Sentry.BaggageHeader? baggageHeader, string? name = null, string? operation = null) { }
public static Sentry.TransactionContext ContinueTrace(string? traceHeader, string? baggageHeader, string? name = null, string? operation = null) { }
public static void EndSession(Sentry.SessionEndStatus status = 0) { }
public static void Flush() { }
public static void Flush(System.TimeSpan timeout) { }
Expand Down Expand Up @@ -1235,6 +1237,7 @@ namespace Sentry.Extensibility
public void ConfigureScope(System.Action<Sentry.Scope> configureScope) { }
public System.Threading.Tasks.Task ConfigureScopeAsync(System.Func<Sentry.Scope, System.Threading.Tasks.Task> configureScope) { }
public Sentry.TransactionContext ContinueTrace(Sentry.SentryTraceHeader? traceHeader, Sentry.BaggageHeader? baggageHeader, string? name = null, string? operation = null) { }
public Sentry.TransactionContext ContinueTrace(string? traceHeader, string? baggageHeader, string? name = null, string? operation = null) { }
public void Dispose() { }
public void EndSession(Sentry.SessionEndStatus status = 0) { }
public System.Threading.Tasks.Task FlushAsync(System.TimeSpan timeout) { }
Expand Down Expand Up @@ -1276,6 +1279,7 @@ namespace Sentry.Extensibility
public void ConfigureScope(System.Action<Sentry.Scope> configureScope) { }
public System.Threading.Tasks.Task ConfigureScopeAsync(System.Func<Sentry.Scope, System.Threading.Tasks.Task> configureScope) { }
public Sentry.TransactionContext ContinueTrace(Sentry.SentryTraceHeader? traceHeader, Sentry.BaggageHeader? baggageHeader, string? name = null, string? operation = null) { }
public Sentry.TransactionContext ContinueTrace(string? traceHeader, string? baggageHeader, string? name = null, string? operation = null) { }
public void EndSession(Sentry.SessionEndStatus status = 0) { }
public System.Threading.Tasks.Task FlushAsync(System.TimeSpan timeout) { }
public Sentry.BaggageHeader? GetBaggage() { }
Expand Down
34 changes: 33 additions & 1 deletion test/Sentry.Tests/HubTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -915,13 +915,44 @@ public void ContinueTrace_SetsPropagationContextAndReturnsTransactionContext()
var propagationContext = new SentryPropagationContext(
SentryId.Parse("43365712692146d08ee11a729dfbcaca"), SpanId.Parse("1000000000000000"));
hub.ConfigureScope(scope => scope.PropagationContext = propagationContext);

var traceHeader = new SentryTraceHeader(SentryId.Parse("5bd5f6d346b442dd9177dce9302fd737"),
SpanId.Parse("2000000000000000"), null);
var baggageHeader = BaggageHeader.Create(new List<KeyValuePair<string, string>>
{
{"sentry-public_key", "49d0f7386ad645858ae85020e393bef3"}
{"sentry-trace_id", "5bd5f6d346b442dd9177dce9302fd737"},
{"sentry-public_key", "49d0f7386ad645858ae85020e393bef3"},
{"sentry-sample_rate", "1.0"}
});

hub.ConfigureScope(scope => scope.PropagationContext.TraceId.Should().Be("43365712692146d08ee11a729dfbcaca")); // Sanity check

// Act
var transactionContext = hub.ContinueTrace(traceHeader, baggageHeader, "test-name");

// Assert
hub.ConfigureScope(scope =>
{
scope.PropagationContext.TraceId.Should().Be(SentryId.Parse("5bd5f6d346b442dd9177dce9302fd737"));
scope.PropagationContext.ParentSpanId.Should().Be(SpanId.Parse("2000000000000000"));
Assert.NotNull(scope.PropagationContext._dynamicSamplingContext);
});

transactionContext.TraceId.Should().Be(SentryId.Parse("5bd5f6d346b442dd9177dce9302fd737"));
transactionContext.ParentSpanId.Should().Be(SpanId.Parse("2000000000000000"));
}

[Fact]
public void ContinueTrace_ReceivesHeadersAsStrings_SetsPropagationContextAndReturnsTransactionContext()
{
// Arrange
var hub = _fixture.GetSut();
var propagationContext = new SentryPropagationContext(
SentryId.Parse("43365712692146d08ee11a729dfbcaca"), SpanId.Parse("1000000000000000"));
hub.ConfigureScope(scope => scope.PropagationContext = propagationContext);
var traceHeader = "5bd5f6d346b442dd9177dce9302fd737-2000000000000000";
var baggageHeader ="sentry-trace_id=5bd5f6d346b442dd9177dce9302fd737, sentry-public_key=49d0f7386ad645858ae85020e393bef3, sentry-sample_rate=1.0";

hub.ConfigureScope(scope => scope.PropagationContext.TraceId.Should().Be("43365712692146d08ee11a729dfbcaca")); // Sanity check

// Act
Expand All @@ -932,6 +963,7 @@ public void ContinueTrace_SetsPropagationContextAndReturnsTransactionContext()
{
scope.PropagationContext.TraceId.Should().Be(SentryId.Parse("5bd5f6d346b442dd9177dce9302fd737"));
scope.PropagationContext.ParentSpanId.Should().Be(SpanId.Parse("2000000000000000"));
Assert.NotNull(scope.PropagationContext._dynamicSamplingContext);
});

transactionContext.TraceId.Should().Be(SentryId.Parse("5bd5f6d346b442dd9177dce9302fd737"));
Expand Down

0 comments on commit 0ae99b1

Please sign in to comment.