Skip to content

Trace id propagation pitfall #4240

@bruno-garcia

Description

@bruno-garcia

Take the following snippet:

  var tran = sentryTrace is not null
             && SentryTraceHeader.Parse(sentryTrace) is { } trace
      ? SentrySdk.StartTransaction("AppStart", "activity.load", trace)
      : SentrySdk.StartTransaction("AppStart", "activity.load");

I marked this issue as a bug because we're crashing apps in case of a very "normal" (albeit not common) looking usage of the SDK.
One issue here is that if the service starting the trace had a DIsabled Sentry SDK, you could get all zeros here.
Repro: SDK starting the trace has a disabled DSN and does span.GetTraceHeader()

public virtual SentryTraceHeader GetTraceHeader() => SentryTraceHeader.Empty;

So sentryTrace is not null or empty. And SentryTraceHeader.Parse successfully parses it.

But trying to start a trace, crashes my app.
Image

So to use this API properly, one has to:

var tran = sentryTrace is not null
           && SentryTraceHeader.Parse(sentryTrace) is { } trace
           && trace.TraceId != SentryId.Empty
    ? SentrySdk.StartTransaction("AppStart", "activity.load", trace)
    : SentrySdk.StartTransaction("AppStart", "activity.load");

Some ideas:

  1. StartTransaction becomes more lenient. It shouldn't crash! So if the id provided is invalid. Just start a new one
  2. SentryTraceHeader.Parse returns null if the parsed trace header is all 0's

I rather we go with 1. And I'd also argue we should change the API to take a trace that is nullable.
So using the API becomes a LOT easier:

var tran = SentrySdk.StartTransaction("AppStart", "activity.load", SentryTraceHeader.Parse(sentryTrace))

Alternatively, we could take a string overload for sentryTrace. But we need to consider that having 3 string parameters in a row and other overloads might make the API confusing. (for example, see this case)

Metadata

Metadata

Assignees

No one assigned

    Projects

    Status

    Done

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions