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

Set the scope transaction for Otel transactions #3072

Merged
merged 3 commits into from
Jan 23, 2024
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ If you have conflicts, you can opt-out by adding the following to your `csproj`:

- Moved the binding to MAUI events for breadcrumb creation from `WillFinishLaunching` to `FinishedLaunching`. This delays the initial instantiation of `app`. ([#3057](https://github.com/getsentry/sentry-dotnet/pull/3057))
- The SDK no longer adds the `WinUIUnhandledExceptionIntegration` on non Windows platforms ([#3055](https://github.com/getsentry/sentry-dotnet/pull/3055))
- The scope transaction is now correctly set for Otel transactions ([#3072](https://github.com/getsentry/sentry-dotnet/pull/3072))

### Dependencies

Expand Down
1 change: 1 addition & 0 deletions src/Sentry.OpenTelemetry/SentrySpanProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ public override void OnStart(Activity data)
transactionContext, new Dictionary<string, object?>(), dynamicSamplingContext
);
transaction.StartTimestamp = data.StartTimeUtc;
_hub.ConfigureScope(scope => scope.Transaction = transaction);
_map[data.SpanId] = transaction;
}
}
Expand Down
8 changes: 0 additions & 8 deletions src/Sentry/TransactionTracer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -275,14 +275,6 @@ internal TransactionTracer(IHub hub, ITransactionContext context, TimeSpan? idle
internal ISpan StartChild(SpanId? spanId, SpanId parentSpanId, string operation,
Instrumenter instrumenter = Instrumenter.Sentry)
{
if (instrumenter != _instrumenter)
Copy link
Contributor

Choose a reason for hiding this comment

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

Why is this no longer needed?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I'm not sure if it was ever needed to be honest.

If the instrumenter is OpenTelemetry then we don't want our own Asp.NET Core integration also trying to instrument the same requests... so I can see why we might throw if an attempt is made to create a new transaction using the Sentry Hub.

I can't see any reason to prevent adding children to transactions that have already been created however.

I think this code was Matt's originally (I took it over part way through the PR)... so I'm not sure what his original reasoning was (I'm just guessing at it).

The alternative would be for calling methods in our own code to check what the current instrumenter is and pass that in as a parameter so as to sidestep the above check... that seemed like a weird solution. Those spans aren't actually coming from OpenTelemetry (they're coming from a Sentry integration). It seemed more honest just to admit that actually we're going to have Sentry child spans of OpenTelemetry transaction spans.

{
_options?.LogWarning(
"Attempted to create a span via {0} instrumentation to a span or transaction" +
" originating from {1} instrumentation. The span will not be created.", instrumenter, _instrumenter);
return NoOpSpan.Instance;
}

var span = new SpanTracer(_hub, this, parentSpanId, TraceId, operation);
if (spanId is { } id)
{
Expand Down
2 changes: 2 additions & 0 deletions test/Sentry.OpenTelemetry.Tests/SentrySpanProcessorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,7 @@ public void OnStart_WithoutParentSpanId_StartsNewTransaction()
{
// Arrange
_fixture.Options.Instrumenter = Instrumenter.OpenTelemetry;
_fixture.ScopeManager = Substitute.For<IInternalScopeManager>();
var sut = _fixture.GetSut();

var data = Tracer.StartActivity("test op");
Expand All @@ -212,6 +213,7 @@ public void OnStart_WithoutParentSpanId_StartsNewTransaction()
transaction.Description.Should().Be(data.DisplayName);
transaction.Status.Should().BeNull();
transaction.StartTimestamp.Should().Be(data.StartTimeUtc);
_fixture.ScopeManager.Received(1).ConfigureScope(Arg.Any<Action<Scope>>());
}
}

Expand Down