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

feat: Tracing without performance #2493

Merged
merged 49 commits into from
Aug 3, 2023
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
Show all changes
49 commits
Select commit Hold shift + click to select a range
d486409
wip
bitsandfoxes Jul 18, 2023
3ec17d7
added propagation context
bitsandfoxes Jul 18, 2023
60dabd0
baggage fetching
bitsandfoxes Jul 19, 2023
8c90704
scope creates propagationcontext
bitsandfoxes Jul 19, 2023
3f88d89
http handler retrieves baggage from hub
bitsandfoxes Jul 19, 2023
56f14d9
keeping things internal
bitsandfoxes Jul 19, 2023
6fecb32
properly propagating things with tracing disabled
bitsandfoxes Jul 19, 2023
84971ed
wip
bitsandfoxes Jul 20, 2023
6c7aff1
simplifying
bitsandfoxes Jul 20, 2023
717b37e
caching parsed DSN
bitsandfoxes Jul 24, 2023
16494c6
reviewpoints
bitsandfoxes Jul 25, 2023
0a37731
continuing trace in ASP.NET Core
bitsandfoxes Jul 25, 2023
c6d632c
simplified
bitsandfoxes Jul 25, 2023
9f94934
simplified 2
bitsandfoxes Jul 25, 2023
b421c88
merged main
bitsandfoxes Jul 25, 2023
f4a6b04
cleanup
bitsandfoxes Jul 25, 2023
848af4e
fixed tracing middleware
bitsandfoxes Jul 25, 2023
dc0e022
wip
bitsandfoxes Jul 26, 2023
b973357
tests
bitsandfoxes Jul 28, 2023
522a752
Apply suggestions from code review
bitsandfoxes Jul 31, 2023
cb54916
objects as keys
bitsandfoxes Jul 31, 2023
7b54760
merge
bitsandfoxes Jul 31, 2023
d87e4d5
fixed using
bitsandfoxes Jul 31, 2023
cd02bce
logging the name of the missing key
bitsandfoxes Jul 31, 2023
d27622d
cleanup
bitsandfoxes Jul 31, 2023
6bfd515
comments
bitsandfoxes Jul 31, 2023
ffc633a
merged main
bitsandfoxes Jul 31, 2023
91b3bd1
renamed internal 'IsTracingEnabled' to 'IsPerformanceMonitoringEnabled'
bitsandfoxes Jul 31, 2023
3ed5a8f
test
bitsandfoxes Jul 31, 2023
dfc4274
istracingenabled
bitsandfoxes Jul 31, 2023
b812630
removed unused method
bitsandfoxes Jul 31, 2023
9640278
fixed parsing dsn test
bitsandfoxes Jul 31, 2023
57ff2f5
last one? maybe?
bitsandfoxes Aug 1, 2023
dd1ed93
added release to options in verify
bitsandfoxes Aug 2, 2023
48ba10a
updated verify
bitsandfoxes Aug 2, 2023
75916f0
simplified
bitsandfoxes Aug 2, 2023
fde8865
reverted submodule bump
bitsandfoxes Aug 2, 2023
ee2a0c8
disabled hub
bitsandfoxes Aug 2, 2023
ced245b
Updated CHANGELOG.md
bitsandfoxes Aug 2, 2023
5c3b22e
setting release
bitsandfoxes Aug 2, 2023
cc049ad
missed a release
bitsandfoxes Aug 2, 2023
f072aad
trying to fix the verified
bitsandfoxes Aug 2, 2023
3cb0af4
line numbers
bitsandfoxes Aug 2, 2023
3dd2265
last round of verified?
bitsandfoxes Aug 2, 2023
60d1f1c
reset
bitsandfoxes Aug 2, 2023
b5afd16
dear lord.. please accept these verified update
bitsandfoxes Aug 2, 2023
88ba20c
added missing mono
bitsandfoxes Aug 2, 2023
bde03c6
whitespace?
bitsandfoxes Aug 2, 2023
139579a
Apply suggestions from code review
bitsandfoxes Aug 3, 2023
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
2 changes: 1 addition & 1 deletion src/Sentry/BaggageHeader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public override string ToString()
/// <returns>
/// An object representing the members baggage header, or <c>null</c> if there are no members parsed.
/// </returns>
public static BaggageHeader? TryParse(string baggage, bool onlySentry = false)
internal static BaggageHeader? TryParse(string baggage, bool onlySentry = false)
{
// Example from W3C baggage spec:
// "key1=value1;property1;property2, key2 = value2, key3=value3; propertyKey=propertyValue"
Expand Down
8 changes: 8 additions & 0 deletions src/Sentry/DynamicSamplingContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,14 @@ public static DynamicSamplingContext CreateFromTransaction(TransactionTracer tra
userSegment,
transactionName);
}

public static DynamicSamplingContext CreateFromPropagationContext(SentryPropagationContext propagationContext, SentryOptions options)
{
var publicKey = Dsn.Parse(options.Dsn!).PublicKey;
bitsandfoxes marked this conversation as resolved.
Show resolved Hide resolved
var traceId = propagationContext.TraceId;

return new DynamicSamplingContext(traceId, publicKey, 1.0f);
}
bitsandfoxes marked this conversation as resolved.
Show resolved Hide resolved
}

internal static class DynamicSamplingContextExtensions
Expand Down
72 changes: 46 additions & 26 deletions src/Sentry/Internal/Hub.cs
Original file line number Diff line number Diff line change
Expand Up @@ -197,26 +197,39 @@ public void BindException(Exception exception, ISpan span)

public SentryTraceHeader? GetTraceHeader() => GetTraceParent();
bitsandfoxes marked this conversation as resolved.
Show resolved Hide resolved

public SentryTraceHeader? GetTraceParent()
public SentryTraceHeader GetTraceParent()
{
var traceHeader = GetSpan()?.GetTraceHeader();
if (traceHeader is not null)
if (_options.IsTracingEnabled)
{
return traceHeader;
var traceHeader = GetSpan()?.GetTraceHeader();
if (traceHeader is not null)
{
return traceHeader;
}
}
bitsandfoxes marked this conversation as resolved.
Show resolved Hide resolved

var propagationContext = ScopeManager.GetCurrent().Key.PropagationContext;
if (propagationContext is not null)
{
return new SentryTraceHeader(propagationContext.TraceId, propagationContext.SpanId, null);
}

return null;
return new SentryTraceHeader(propagationContext.TraceId, propagationContext.SpanId, null);
bitsandfoxes marked this conversation as resolved.
Show resolved Hide resolved
}

public BaggageHeader? GetBaggage()
{
throw new NotImplementedException();
if (_options.IsTracingEnabled)
{
var transaction = GetSpan();
if (transaction is TransactionTracer {DynamicSamplingContext: {IsEmpty: false} dsc})
{
return dsc.ToBaggageHeader();
}
}
bitsandfoxes marked this conversation as resolved.
Show resolved Hide resolved

var propagationContext = ScopeManager.GetCurrent().Key.PropagationContext;
if (propagationContext?.DynamicSamplingContext is not null)
{
return propagationContext.DynamicSamplingContext.ToBaggageHeader();
}

return null;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I havent' spotted any usage of Logging. There are a few conditions happening here:

  • tracing enabled
  • transation of type X with prop Y having value Z
  • DSC not being null and returning the result of another method call (that could also be null?

Is this easy to track down what's going on in debug mode?

}

public TransactionContext? ContinueTrace(string? sentryTrace, string? baggageHeaders)
Expand All @@ -228,10 +241,8 @@ public void BindException(Exception exception, ISpan span)
{
return new TransactionContext(propagationContext.SpanId,propagationContext.ParentSpanId, propagationContext.TraceId, "", "", "", null, null, null);
}
else
{
return null;
}

return null;
}

public void StartSession()
Expand Down Expand Up @@ -375,14 +386,6 @@ SentryId IHubEx.CaptureEventInternal(SentryEvent evt, Hint? hint, Scope? scope)
var currentScope = ScopeManager.GetCurrent();
var actualScope = scope ?? currentScope.Key;

// Inject trace information from a linked span
if (GetLinkedSpan(evt, actualScope) is { } linkedSpan)
{
evt.Contexts.Trace.SpanId = linkedSpan.SpanId;
evt.Contexts.Trace.TraceId = linkedSpan.TraceId;
evt.Contexts.Trace.ParentSpanId = linkedSpan.ParentSpanId;
}

var hasTerminalException = evt.HasTerminalException();
if (hasTerminalException)
{
Expand All @@ -397,9 +400,26 @@ SentryId IHubEx.CaptureEventInternal(SentryEvent evt, Hint? hint, Scope? scope)
actualScope.SessionUpdate = _sessionManager.ReportError();
}

// When a transaction is present, copy its DSC to the event.
var transaction = actualScope.Transaction as TransactionTracer;
evt.DynamicSamplingContext = transaction?.DynamicSamplingContext;
TransactionTracer? transaction = null;
if(_options.IsTracingEnabled)
bitsandfoxes marked this conversation as resolved.
Show resolved Hide resolved
{
// Inject trace information from a linked span
if (GetLinkedSpan(evt, actualScope) is { } linkedSpan)
{
evt.Contexts.Trace.SpanId = linkedSpan.SpanId;
evt.Contexts.Trace.TraceId = linkedSpan.TraceId;
evt.Contexts.Trace.ParentSpanId = linkedSpan.ParentSpanId;
}

// When a transaction is present, copy its DSC to the event.
bitsandfoxes marked this conversation as resolved.
Show resolved Hide resolved
transaction = actualScope.Transaction as TransactionTracer;
evt.DynamicSamplingContext = transaction?.DynamicSamplingContext;
bitsandfoxes marked this conversation as resolved.
Show resolved Hide resolved
}
else
{
actualScope.PropagationContext.DynamicSamplingContext ??= DynamicSamplingContext.CreateFromPropagationContext(actualScope.PropagationContext, _options);
evt.DynamicSamplingContext = actualScope.PropagationContext.DynamicSamplingContext;
}

// Now capture the event with the Sentry client on the current scope.
var sentryClient = currentScope.Value;
Expand Down
14 changes: 12 additions & 2 deletions src/Sentry/Scope.cs
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ public ITransaction? Transaction
set => _transaction = value;
}

internal SentryPropagationContext? PropagationContext { get; set; }
internal SentryPropagationContext PropagationContext { get; set; }

internal SessionUpdate? SessionUpdate { get; set; }

Expand Down Expand Up @@ -235,8 +235,14 @@ public ITransaction? Transaction
/// Creates a scope with the specified options.
/// </summary>
public Scope(SentryOptions? options)
: this(options, null)
{
}

internal Scope(SentryOptions? options, SentryPropagationContext? propagationContext)
{
Options = options ?? new SentryOptions();
PropagationContext = propagationContext ?? new SentryPropagationContext();
}

// For testing. Should explicitly require SentryOptions.
Expand Down Expand Up @@ -443,6 +449,10 @@ public void Apply(IEventLike other)
{
other.Sdk.AddPackage(package);
}

other.Contexts.Trace.SpanId = PropagationContext.SpanId;
other.Contexts.Trace.TraceId = PropagationContext.TraceId;
other.Contexts.Trace.ParentSpanId = PropagationContext.ParentSpanId;
}

/// <summary>
Expand Down Expand Up @@ -478,7 +488,7 @@ public void Apply(Scope other)
/// </summary>
public Scope Clone()
{
var clone = new Scope(Options)
var clone = new Scope(Options, PropagationContext)
{
OnEvaluating = OnEvaluating
};
Expand Down
6 changes: 2 additions & 4 deletions src/Sentry/SentryHttpMessageHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -156,14 +156,12 @@ private void AddSentryTraceHeader(HttpRequestMessage request)

private void AddBaggageHeader(HttpRequestMessage request)
{
var transaction = _hub.GetSpan();
if (transaction is not TransactionTracer {DynamicSamplingContext: {IsEmpty: false} dsc})
var baggage = _hub.GetBaggage();
if (baggage is null)
{
return;
}

var baggage = dsc.ToBaggageHeader();

if (request.Headers.TryGetValues(BaggageHeader.HttpHeaderName, out var baggageHeaders))
{
var headers = baggageHeaders.ToList();
Expand Down
12 changes: 6 additions & 6 deletions src/Sentry/SentryPropagationContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,23 @@ internal class SentryPropagationContext
public SentryId TraceId { get; }
public SpanId SpanId { get; }
public SpanId? ParentSpanId { get; }
public DynamicSamplingContext? DynamicSamplingContext { get; }
public DynamicSamplingContext? DynamicSamplingContext { get; set; }

private SentryPropagationContext(
SentryTraceHeader traceHeader,
SpanId? parentSpanId,
DynamicSamplingContext? dynamicSamplingContext)
{
TraceId = traceHeader.TraceId;
SpanId = traceHeader.SpanId;
ParentSpanId = parentSpanId;
SpanId = SpanId.Create();
ParentSpanId = parentSpanId ?? traceHeader.SpanId;
DynamicSamplingContext = dynamicSamplingContext;
}

private SentryPropagationContext()
public SentryPropagationContext()
{
TraceId = new SentryId();
SpanId = new SpanId();
TraceId = SentryId.Create();
SpanId = SpanId.Create();
}

public static SentryPropagationContext CreateFromHeaders(string? sentryTraceHeader, string? baggageHeadersString)
Expand Down