Skip to content
Open
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
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)
{
foreach (var tag in options.DefaultTags)
{
observer.SetTag(tag.Key, tag.Value);
}
}

// 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";

// Act
SentrySdk.InitHub(options);

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

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