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

fix: Overload for ContinueTrace to accept headers as string #2601

Merged
merged 9 commits into from
Sep 18, 2023
Merged
Show file tree
Hide file tree
Changes from 5 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 @@ -4,6 +4,7 @@

### 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
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 != null)
bitsandfoxes marked this conversation as resolved.
Show resolved Hide resolved
{
sentryTraceHeader = SentryTraceHeader.Parse(traceHeader);
}

BaggageHeader? sentryBaggageHeader = null;
if (baggageHeader != null)
bitsandfoxes marked this conversation as resolved.
Show resolved Hide resolved
{
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
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
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