Skip to content
Open
Show file tree
Hide file tree
Changes from 2 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
5 changes: 3 additions & 2 deletions src/Sentry/Platforms/Android/SentrySdk.cs
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,9 @@ private static void InitSentryAndroidSdk(SentryOptions options)
o.CacheDirPath = Path.Combine(cacheDirectoryPath, "android");
}

// NOTE: Tags in options.DefaultTags should not be passed down, because we already call SetTag on each
// one when sending events, which is relayed through the scope observer.
// NOTE: options.DefaultTags are forwarded to the scope observer in SentrySdk.InitHub so the
// Android SDK attaches them to native crashes. The Enricher continues to apply them to
// managed events at send time.

if (options.HttpProxy is System.Net.WebProxy proxy)
{
Expand Down
5 changes: 3 additions & 2 deletions src/Sentry/Platforms/Cocoa/SentrySdk.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,9 @@ private static void InitSentryCocoaSdk(SentryOptions options)
// NOTE: options.CacheDirectoryPath - No option for this in Sentry Cocoa, but caching is still enabled
// https://github.com/getsentry/sentry-cocoa/issues/1051

// NOTE: Tags in options.DefaultTags should not be passed down, because we already call SetTag on each
// one when sending events, which is relayed through the scope observer.
// NOTE: options.DefaultTags are forwarded to the scope observer in SentrySdk.InitHub so the
// Cocoa SDK attaches them to native crashes. The Enricher continues to apply them to
// managed events at send time.

if (options.BeforeBreadcrumbInternal is { } beforeBreadcrumb)
{
Expand Down
12 changes: 12 additions & 0 deletions src/Sentry/SentrySdk.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,18 @@ internal static IHub InitHub(SentryOptions options)
}
options.PostInitCallbacks.Clear();

// Default tags are applied per-event by the Enricher to events going through the .NET pipeline,
// but native crashes are captured and uploaded by the native SDK without going through that pipeline.
// Forward them to the scope observer so the native layer attaches them to crash reports.
// Bypassing the .NET scope keeps scope.Tags identical between native and non-native apps.
if (options is { EnableScopeSync: true, ScopeObserver: { } observer } && options.DefaultTags.Count > 0)
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

It looks like this would work in Global mode (where there is only one scope).

I'm wondering what would happen for an Asp.Net Core application that's compiled AOT. I believe we enable Sentry Native for AOT compiled applications - but I imagine Sentry Native doesn't have any notion of an 'AsyncLocal' scope stack so we effectively end up with Global scopes (and no scope observer in any case) for native events in Asp.Net Core apps compiled AOT. So that would (probably) work as expected as well (i.e. anything you put in the Default tags would end up on all native events in Asp.Net Core AOT apps).

It might be worth testing that scenario just to check (or getting an LLM to build a little test app to check that - can't really add it to our test suite as we have no easy way to test AOT yet - so it would just be a sanity check).

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Can DefaultTags.Count > 0 be merged into the pattern match to simplify this?

{
foreach (var tag in options.DefaultTags)
{
observer.SetTag(tag.Key, tag.Value);
Comment thread
jamescrosswell marked this conversation as resolved.
}
}

// Platform specific check for profiler misconfiguration.
#if __IOS__
// No user-facing warning necessary - the integration is part of InitSentryCocoaSdk().
Expand Down
46 changes: 46 additions & 0 deletions test/Sentry.Tests/SentrySdkTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -967,6 +967,52 @@ public void InitHub_GlobalModeOff_NoWarningOrErrorLogged()
}
#endif

[Fact]
public void InitHub_DefaultTagsWithScopeSync_RelayedToScopeObserver()
{
// Arrange
var observer = Substitute.For<IScopeObserver>();
var options = new SentryOptions
{
Dsn = ValidDsn,
ScopeObserver = observer,
EnableScopeSync = true,
BackgroundWorker = Substitute.For<IBackgroundWorker>(),
InitNativeSdks = false,
};
options.DefaultTags["env"] = "production";
options.DefaultTags["region"] = "us-east-1";

// Act
SentrySdk.InitHub(options);

// Assert
observer.Received(1).SetTag("env", "production");
observer.Received(1).SetTag("region", "us-east-1");
}

[Fact]
public void InitHub_DefaultTagsWithoutScopeSync_NotRelayedToScopeObserver()
{
// Arrange
var observer = Substitute.For<IScopeObserver>();
var options = new SentryOptions
{
Dsn = ValidDsn,
ScopeObserver = observer,
EnableScopeSync = false,
BackgroundWorker = Substitute.For<IBackgroundWorker>(),
InitNativeSdks = false,
};
options.DefaultTags["env"] = "production";
Comment thread
bitsandfoxes marked this conversation as resolved.

// Act
SentrySdk.InitHub(options);

// Assert
observer.DidNotReceive().SetTag(Arg.Any<string>(), Arg.Any<string>());
}

[Fact]
public void InitHub_DebugEnabled_DebugLogsLogged()
{
Expand Down
Loading