getsentry / sentry-dotnet

Sentry SDK for .NET
https://docs.sentry.io/platforms/dotnet
MIT License
605 stars 206 forks source link

SentrySdk.GetSpan() returns null #3165

Open vaind opened 9 months ago

vaind commented 9 months ago

Package

Sentry

.NET Flavor

.NET

.NET Version

any

OS

Any (not platform specific)

SDK Version

4.1.2

Self-Hosted Sentry Version

No response

Steps to Reproduce

        var transaction = SentrySdk.StartTransaction("test", "test");
        var transactionFromScope = SentrySdk.GetSpan();
        transactionFromScope.Should().BeSameAs(transaction);

Expected Result

test pass

See docs with the example suggesting GetSpan() should work:

Actual Result

test fails

vaind commented 9 months ago

As an additional issue docs explicitly say:

In cases where you want to attach Spans to an already ongoing Transaction you can use SentrySdk.GetSpan(). If there is a running Transaction or Span currently on the scope, this method will return a SentryTransaction or Span; otherwise, it returns null.

and then in another part they call

var span = SentrySdk.GetSpan();  // or hub.GetSpan()
if (span != null)
{
    var transaction = span.GetTransaction();

However, that doesn't seem to be right because SentryTransction doesn't have such a method.

jamescrosswell commented 9 months ago

However, that doesn't seem to be right because SentryTransction doesn't have such a method.

I take it this is related to the comment re extension methods vs class methods.

jamescrosswell commented 9 months ago

@vaind I think you might have bumped into this:

I hit that when I first started working with Sentry as well. Still not sure what the implications of setting scope.Transaction automatically would be... At the very least, I guess we could try it out and see if it breaks any of our unit tests. It's hard to say whether such a change might break any of our customers' code though.