From 4c00864528571e370ebf0564cbba07bafd0737e9 Mon Sep 17 00:00:00 2001 From: bitsandfoxes Date: Wed, 29 Jan 2025 20:39:00 +0100 Subject: [PATCH 01/16] propagate during startup --- package-dev/Runtime/SentryInitialization.cs | 4 ++++ src/Sentry.Unity.Android/SentryJava.cs | 10 ++++++++++ .../SentryNativeAndroid.cs | 18 ++++++++++++++++++ .../TestSentryJava.cs | 1 + 4 files changed, 33 insertions(+) diff --git a/package-dev/Runtime/SentryInitialization.cs b/package-dev/Runtime/SentryInitialization.cs index 97591140e..d5c1d839a 100644 --- a/package-dev/Runtime/SentryInitialization.cs +++ b/package-dev/Runtime/SentryInitialization.cs @@ -86,6 +86,10 @@ public static void Init() SentryUnity.Init(options); +#if SENTRY_NATIVE_ANDROID + SentryNativeAndroid.SetTraceId(options); +#endif + #if !SENTRY_WEBGL if (options.TracesSampleRate > 0.0f && options.AutoStartupTraces) { diff --git a/src/Sentry.Unity.Android/SentryJava.cs b/src/Sentry.Unity.Android/SentryJava.cs index 679be6d71..5409029d8 100644 --- a/src/Sentry.Unity.Android/SentryJava.cs +++ b/src/Sentry.Unity.Android/SentryJava.cs @@ -13,6 +13,7 @@ internal interface ISentryJava public string? GetInstallationId(IJniExecutor jniExecutor); public bool? CrashedLastRun(IJniExecutor jniExecutor); public void Close(IJniExecutor jniExecutor); + public void ContinueTrace(IJniExecutor jniExecutor, string traceId); public void WriteScope( IJniExecutor jniExecutor, int? GpuId, @@ -170,6 +171,15 @@ public void Close(IJniExecutor jniExecutor) }); } + public void ContinueTrace(IJniExecutor jniExecutor, string traceId) + { + jniExecutor.Run(() => + { + using var sentry = GetSentryJava(); + sentry.CallStatic("continueTrace", traceId, null); + }); + } + public void WriteScope( IJniExecutor jniExecutor, int? GpuId, diff --git a/src/Sentry.Unity.Android/SentryNativeAndroid.cs b/src/Sentry.Unity.Android/SentryNativeAndroid.cs index 1c6b2e705..0152cf67d 100644 --- a/src/Sentry.Unity.Android/SentryNativeAndroid.cs +++ b/src/Sentry.Unity.Android/SentryNativeAndroid.cs @@ -132,6 +132,24 @@ public static void Configure(SentryUnityOptions options, ISentryUnityInfo sentry options.DiagnosticLogger?.LogInfo("Successfully configured the Android SDK"); } + public static void SetTraceId(SentryUnityOptions options) + { + options.DiagnosticLogger?.LogInfo("Setting Trace ID"); + + var traceId = SentrySdk.GetTraceHeader()?.TraceId; + if (traceId is null) + { + options.DiagnosticLogger?.LogInfo("fucking trace id is null"); + } + else + { + options.DiagnosticLogger?.LogInfo("Setting the trace ID on the native layer {0}", traceId); + + JniExecutor ??= new JniExecutor(options.DiagnosticLogger); + SentryJava.ContinueTrace(JniExecutor, traceId.ToString()); + } + } + /// /// Closes the native Android support. /// diff --git a/test/Sentry.Unity.Android.Tests/TestSentryJava.cs b/test/Sentry.Unity.Android.Tests/TestSentryJava.cs index 16cb0fd97..678e5e371 100644 --- a/test/Sentry.Unity.Android.Tests/TestSentryJava.cs +++ b/test/Sentry.Unity.Android.Tests/TestSentryJava.cs @@ -19,6 +19,7 @@ public void Init(IJniExecutor jniExecutor, SentryUnityOptions options, TimeSpan public bool? CrashedLastRun(IJniExecutor jniExecutor) => IsCrashedLastRun; public void Close(IJniExecutor jniExecutor) { } + public void ContinueTrace(IJniExecutor jniExecutor, string traceId) { } public void WriteScope( IJniExecutor jniExecutor, From 3dc1d67ebafea313c25ea2f456f93fb17034d291 Mon Sep 17 00:00:00 2001 From: bitsandfoxes Date: Fri, 7 Feb 2025 11:27:41 +0100 Subject: [PATCH 02/16] setting trace in java --- modules/sentry-java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/sentry-java b/modules/sentry-java index 2de45ebb0..c8e5c3fef 160000 --- a/modules/sentry-java +++ b/modules/sentry-java @@ -1 +1 @@ -Subproject commit 2de45ebb088ed5802dcac00781cbaca7e54ed9d9 +Subproject commit c8e5c3fef077cdb0b4b2fa4344497d691a1bd2d4 From 59976fedfee3e54589542be6d0388d6581d32bb3 Mon Sep 17 00:00:00 2001 From: bitsandfoxes Date: Thu, 27 Feb 2025 11:22:00 +0100 Subject: [PATCH 03/16] bump --- modules/sentry-java | 2 +- src/Sentry.Unity.Android/SentryJava.cs | 9 +++++---- .../SentryNativeAndroid.cs | 18 ++++++++++++------ 3 files changed, 18 insertions(+), 11 deletions(-) diff --git a/modules/sentry-java b/modules/sentry-java index af5d4aa36..d9d43ee7f 160000 --- a/modules/sentry-java +++ b/modules/sentry-java @@ -1 +1 @@ -Subproject commit af5d4aa3646ebbe8b85ab4d6993af1f919a7f280 +Subproject commit d9d43ee7f987f62f5e00106a50a8595fdab2b817 diff --git a/src/Sentry.Unity.Android/SentryJava.cs b/src/Sentry.Unity.Android/SentryJava.cs index 5409029d8..3f4ba638f 100644 --- a/src/Sentry.Unity.Android/SentryJava.cs +++ b/src/Sentry.Unity.Android/SentryJava.cs @@ -13,7 +13,7 @@ internal interface ISentryJava public string? GetInstallationId(IJniExecutor jniExecutor); public bool? CrashedLastRun(IJniExecutor jniExecutor); public void Close(IJniExecutor jniExecutor); - public void ContinueTrace(IJniExecutor jniExecutor, string traceId); + public void SetTrace(IJniExecutor jniExecutor, string traceId, string spanId); public void WriteScope( IJniExecutor jniExecutor, int? GpuId, @@ -45,6 +45,7 @@ public void WriteScope( internal class SentryJava : ISentryJava { private static AndroidJavaObject GetSentryJava() => new AndroidJavaClass("io.sentry.Sentry"); + private static AndroidJavaObject GetInternalSentryJava() => new AndroidJavaClass("io.sentry.InternalSentrySdk"); public bool IsEnabled(IJniExecutor jniExecutor, TimeSpan timeout) { @@ -171,12 +172,12 @@ public void Close(IJniExecutor jniExecutor) }); } - public void ContinueTrace(IJniExecutor jniExecutor, string traceId) + public void SetTrace(IJniExecutor jniExecutor, string traceId, string spanId) { jniExecutor.Run(() => { - using var sentry = GetSentryJava(); - sentry.CallStatic("continueTrace", traceId, null); + using var sentry = GetInternalSentryJava(); + sentry.CallStatic("setTrace", traceId, spanId); }); } diff --git a/src/Sentry.Unity.Android/SentryNativeAndroid.cs b/src/Sentry.Unity.Android/SentryNativeAndroid.cs index 0152cf67d..a9306be98 100644 --- a/src/Sentry.Unity.Android/SentryNativeAndroid.cs +++ b/src/Sentry.Unity.Android/SentryNativeAndroid.cs @@ -137,17 +137,23 @@ public static void SetTraceId(SentryUnityOptions options) options.DiagnosticLogger?.LogInfo("Setting Trace ID"); var traceId = SentrySdk.GetTraceHeader()?.TraceId; + var spanId = SentrySdk.GetTraceHeader()?.SpanId; if (traceId is null) { - options.DiagnosticLogger?.LogInfo("fucking trace id is null"); + options.DiagnosticLogger?.LogError("trace id is null"); + return; } - else - { - options.DiagnosticLogger?.LogInfo("Setting the trace ID on the native layer {0}", traceId); - JniExecutor ??= new JniExecutor(options.DiagnosticLogger); - SentryJava.ContinueTrace(JniExecutor, traceId.ToString()); + if (spanId is null) + { + options.DiagnosticLogger?.LogError("span id is null"); + return; } + + options.DiagnosticLogger?.LogInfo("Setting the trace ID on the native layer {0}", traceId); + + JniExecutor ??= new JniExecutor(options.DiagnosticLogger); + SentryJava.SetTrace(JniExecutor, traceId.ToString(), spanId.ToString()); } /// From 43b1486308cf6ca778d3d54c1075af1939cd3d76 Mon Sep 17 00:00:00 2001 From: bitsandfoxes Date: Thu, 27 Feb 2025 11:22:18 +0100 Subject: [PATCH 04/16] test --- test/Sentry.Unity.Android.Tests/TestSentryJava.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/Sentry.Unity.Android.Tests/TestSentryJava.cs b/test/Sentry.Unity.Android.Tests/TestSentryJava.cs index 678e5e371..be1139782 100644 --- a/test/Sentry.Unity.Android.Tests/TestSentryJava.cs +++ b/test/Sentry.Unity.Android.Tests/TestSentryJava.cs @@ -19,7 +19,7 @@ public void Init(IJniExecutor jniExecutor, SentryUnityOptions options, TimeSpan public bool? CrashedLastRun(IJniExecutor jniExecutor) => IsCrashedLastRun; public void Close(IJniExecutor jniExecutor) { } - public void ContinueTrace(IJniExecutor jniExecutor, string traceId) { } + public void SetTrace(IJniExecutor jniExecutor, string traceId, string spanId) { } public void WriteScope( IJniExecutor jniExecutor, From 63d5a25652c712a9e8f3aa18511aed21f8685082 Mon Sep 17 00:00:00 2001 From: bitsandfoxes Date: Fri, 28 Feb 2025 12:38:47 +0100 Subject: [PATCH 05/16] bumped java --- modules/sentry-java | 2 +- src/Sentry.Unity.Android/SentryJava.cs | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/modules/sentry-java b/modules/sentry-java index d9d43ee7f..f8f097aca 160000 --- a/modules/sentry-java +++ b/modules/sentry-java @@ -1 +1 @@ -Subproject commit d9d43ee7f987f62f5e00106a50a8595fdab2b817 +Subproject commit f8f097aca547c58d02def2a357131d7ce7262ace diff --git a/src/Sentry.Unity.Android/SentryJava.cs b/src/Sentry.Unity.Android/SentryJava.cs index 3f4ba638f..2601b1a01 100644 --- a/src/Sentry.Unity.Android/SentryJava.cs +++ b/src/Sentry.Unity.Android/SentryJava.cs @@ -45,7 +45,7 @@ public void WriteScope( internal class SentryJava : ISentryJava { private static AndroidJavaObject GetSentryJava() => new AndroidJavaClass("io.sentry.Sentry"); - private static AndroidJavaObject GetInternalSentryJava() => new AndroidJavaClass("io.sentry.InternalSentrySdk"); + private static AndroidJavaObject GetInternalSentryJava() => new AndroidJavaClass("io.sentry.android.core.InternalSentrySdk"); public bool IsEnabled(IJniExecutor jniExecutor, TimeSpan timeout) { @@ -177,7 +177,7 @@ public void SetTrace(IJniExecutor jniExecutor, string traceId, string spanId) jniExecutor.Run(() => { using var sentry = GetInternalSentryJava(); - sentry.CallStatic("setTrace", traceId, spanId); + sentry.CallStatic("setTrace", traceId, spanId, (Double?)null, (Double?)null); }); } From 18c23cc8d7c0a72b2293f9ddea378571d3527a1d Mon Sep 17 00:00:00 2001 From: bitsandfoxes Date: Mon, 3 Mar 2025 14:37:01 +0100 Subject: [PATCH 06/16] bumped java --- modules/sentry-java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/sentry-java b/modules/sentry-java index f8f097aca..158d6889e 160000 --- a/modules/sentry-java +++ b/modules/sentry-java @@ -1 +1 @@ -Subproject commit f8f097aca547c58d02def2a357131d7ce7262ace +Subproject commit 158d6889e5a1251c2dd5dc566869594f12c80004 From 85bf6575e7f0e75987637006acfe2e0c3cdb4a13 Mon Sep 17 00:00:00 2001 From: bitsandfoxes Date: Wed, 5 Mar 2025 17:46:01 +0100 Subject: [PATCH 07/16] use scope observer instead --- .../AndroidJavaScopeObserver.cs | 11 ++++++++++ src/Sentry.Unity.Android/SentryJava.cs | 11 ---------- .../SentryNativeAndroid.cs | 22 ++----------------- .../NativeScopeObserver.cs | 4 ++++ src/Sentry.Unity.iOS/NativeScopeObserver.cs | 4 ++++ src/Sentry.Unity/ScopeObserver.cs | 7 +++++- src/sentry-dotnet | 2 +- 7 files changed, 28 insertions(+), 33 deletions(-) diff --git a/src/Sentry.Unity.Android/AndroidJavaScopeObserver.cs b/src/Sentry.Unity.Android/AndroidJavaScopeObserver.cs index ef54bb937..93381d6ac 100644 --- a/src/Sentry.Unity.Android/AndroidJavaScopeObserver.cs +++ b/src/Sentry.Unity.Android/AndroidJavaScopeObserver.cs @@ -1,3 +1,4 @@ +using System; using UnityEngine; namespace Sentry.Unity.Android; @@ -16,6 +17,7 @@ public AndroidJavaScopeObserver(SentryOptions options, IJniExecutor jniExecutor) } private AndroidJavaObject GetSentryJava() => new AndroidJavaClass("io.sentry.Sentry"); + private static AndroidJavaObject GetInternalSentryJava() => new AndroidJavaClass("io.sentry.android.core.InternalSentrySdk"); public override void AddBreadcrumbImpl(Breadcrumb breadcrumb) { @@ -88,4 +90,13 @@ public override void UnsetUserImpl() sentry.CallStatic("setUser", null); }); } + + public override void SetTraceImpl(SentryId traceId, SpanId parentSpanId) + { + _jniExecutor.Run(() => + { + using var sentry = GetInternalSentryJava(); + sentry.CallStatic("setTrace", traceId.ToString(), parentSpanId.ToString(), (Double?)null, (Double?)null); + }); + } } diff --git a/src/Sentry.Unity.Android/SentryJava.cs b/src/Sentry.Unity.Android/SentryJava.cs index 2601b1a01..679be6d71 100644 --- a/src/Sentry.Unity.Android/SentryJava.cs +++ b/src/Sentry.Unity.Android/SentryJava.cs @@ -13,7 +13,6 @@ internal interface ISentryJava public string? GetInstallationId(IJniExecutor jniExecutor); public bool? CrashedLastRun(IJniExecutor jniExecutor); public void Close(IJniExecutor jniExecutor); - public void SetTrace(IJniExecutor jniExecutor, string traceId, string spanId); public void WriteScope( IJniExecutor jniExecutor, int? GpuId, @@ -45,7 +44,6 @@ public void WriteScope( internal class SentryJava : ISentryJava { private static AndroidJavaObject GetSentryJava() => new AndroidJavaClass("io.sentry.Sentry"); - private static AndroidJavaObject GetInternalSentryJava() => new AndroidJavaClass("io.sentry.android.core.InternalSentrySdk"); public bool IsEnabled(IJniExecutor jniExecutor, TimeSpan timeout) { @@ -172,15 +170,6 @@ public void Close(IJniExecutor jniExecutor) }); } - public void SetTrace(IJniExecutor jniExecutor, string traceId, string spanId) - { - jniExecutor.Run(() => - { - using var sentry = GetInternalSentryJava(); - sentry.CallStatic("setTrace", traceId, spanId, (Double?)null, (Double?)null); - }); - } - public void WriteScope( IJniExecutor jniExecutor, int? GpuId, diff --git a/src/Sentry.Unity.Android/SentryNativeAndroid.cs b/src/Sentry.Unity.Android/SentryNativeAndroid.cs index a9306be98..c4c1c7a10 100644 --- a/src/Sentry.Unity.Android/SentryNativeAndroid.cs +++ b/src/Sentry.Unity.Android/SentryNativeAndroid.cs @@ -134,26 +134,8 @@ public static void Configure(SentryUnityOptions options, ISentryUnityInfo sentry public static void SetTraceId(SentryUnityOptions options) { - options.DiagnosticLogger?.LogInfo("Setting Trace ID"); - - var traceId = SentrySdk.GetTraceHeader()?.TraceId; - var spanId = SentrySdk.GetTraceHeader()?.SpanId; - if (traceId is null) - { - options.DiagnosticLogger?.LogError("trace id is null"); - return; - } - - if (spanId is null) - { - options.DiagnosticLogger?.LogError("span id is null"); - return; - } - - options.DiagnosticLogger?.LogInfo("Setting the trace ID on the native layer {0}", traceId); - - JniExecutor ??= new JniExecutor(options.DiagnosticLogger); - SentryJava.SetTrace(JniExecutor, traceId.ToString(), spanId.ToString()); + options.DiagnosticLogger?.LogInfo("Setting new Trace"); + SentrySdk.ContinueTrace(SentryId.Create().ToString(), null); } /// diff --git a/src/Sentry.Unity.Native/NativeScopeObserver.cs b/src/Sentry.Unity.Native/NativeScopeObserver.cs index 5075febca..8a3ef4306 100644 --- a/src/Sentry.Unity.Native/NativeScopeObserver.cs +++ b/src/Sentry.Unity.Native/NativeScopeObserver.cs @@ -40,6 +40,10 @@ public override void SetUserImpl(SentryUser user) } public override void UnsetUserImpl() => C.sentry_remove_user(); + public override void SetTraceImpl(SentryId traceId, SpanId parentSpanId) + { + throw new NotImplementedException(); + } private static string GetTimestamp(DateTimeOffset timestamp) => // "o": Using ISO 8601 to make sure the timestamp makes it to the bridge correctly. diff --git a/src/Sentry.Unity.iOS/NativeScopeObserver.cs b/src/Sentry.Unity.iOS/NativeScopeObserver.cs index f46d69bdf..36de7fe3e 100644 --- a/src/Sentry.Unity.iOS/NativeScopeObserver.cs +++ b/src/Sentry.Unity.iOS/NativeScopeObserver.cs @@ -25,6 +25,10 @@ public override void SetUserImpl(SentryUser user) => SentryCocoaBridgeProxy.SetUser(user.Email, user.Id, user.IpAddress, user.Username); public override void UnsetUserImpl() => SentryCocoaBridgeProxy.UnsetUser(); + public override void SetTraceImpl(SentryId traceId, SpanId parentSpanId) + { + throw new NotImplementedException(); + } internal static string GetTimestamp(DateTimeOffset timestamp) => // "o": Using ISO 8601 to make sure the timestamp makes it to the bridge correctly. diff --git a/src/Sentry.Unity/ScopeObserver.cs b/src/Sentry.Unity/ScopeObserver.cs index 1a7f0996b..b19c66e97 100644 --- a/src/Sentry.Unity/ScopeObserver.cs +++ b/src/Sentry.Unity/ScopeObserver.cs @@ -3,7 +3,7 @@ namespace Sentry.Unity; /// -/// Sentry Unity Scope Observer wrapper for the common behaviour accross platforms. +/// Sentry Unity Scope Observer wrapper for the common behaviour across platforms. /// public abstract class ScopeObserver : IScopeObserver { @@ -84,4 +84,9 @@ public void SetUser(SentryUser? user) public abstract void SetUserImpl(SentryUser user); public abstract void UnsetUserImpl(); + + public abstract void SetTraceImpl(SentryId traceId, SpanId parentSpanId); + + public void SetTrace(SentryId traceId, SpanId parentSpanId) + => SetTraceImpl(traceId, parentSpanId); } diff --git a/src/sentry-dotnet b/src/sentry-dotnet index 05ac853ae..9e7c42325 160000 --- a/src/sentry-dotnet +++ b/src/sentry-dotnet @@ -1 +1 @@ -Subproject commit 05ac853ae29c10bd76db0e382777a7122bd6b171 +Subproject commit 9e7c42325254da768ed288a7be07efb3a27c09d5 From 882eed93a4d7800265959064b428be83e3981ab4 Mon Sep 17 00:00:00 2001 From: bitsandfoxes Date: Fri, 28 Mar 2025 23:58:01 +0100 Subject: [PATCH 08/16] updated trace generation and sync --- src/Sentry.Unity.Android/AndroidJavaScopeObserver.cs | 1 + src/Sentry.Unity.Android/SentryNativeAndroid.cs | 6 ------ .../Android/AndroidManifestConfiguration.cs | 4 ++++ src/Sentry.Unity/ScopeObserver.cs | 11 ++++++++--- src/Sentry.Unity/SentryUnitySDK.cs | 5 +++++ src/sentry-dotnet | 2 +- 6 files changed, 19 insertions(+), 10 deletions(-) diff --git a/src/Sentry.Unity.Android/AndroidJavaScopeObserver.cs b/src/Sentry.Unity.Android/AndroidJavaScopeObserver.cs index 93381d6ac..f56b7060d 100644 --- a/src/Sentry.Unity.Android/AndroidJavaScopeObserver.cs +++ b/src/Sentry.Unity.Android/AndroidJavaScopeObserver.cs @@ -96,6 +96,7 @@ public override void SetTraceImpl(SentryId traceId, SpanId parentSpanId) _jniExecutor.Run(() => { using var sentry = GetInternalSentryJava(); + // We have to explicitly cast to `(Double?)` sentry.CallStatic("setTrace", traceId.ToString(), parentSpanId.ToString(), (Double?)null, (Double?)null); }); } diff --git a/src/Sentry.Unity.Android/SentryNativeAndroid.cs b/src/Sentry.Unity.Android/SentryNativeAndroid.cs index 4734c7acf..1d2122939 100644 --- a/src/Sentry.Unity.Android/SentryNativeAndroid.cs +++ b/src/Sentry.Unity.Android/SentryNativeAndroid.cs @@ -131,12 +131,6 @@ public static void Configure(SentryUnityOptions options, ISentryUnityInfo sentry options.DiagnosticLogger?.LogInfo("Successfully configured the Android SDK"); } - public static void SetTraceId(SentryUnityOptions options) - { - options.DiagnosticLogger?.LogInfo("Setting new Trace"); - SentrySdk.ContinueTrace(SentryId.Create().ToString(), null); - } - /// /// Closes the native Android support. /// diff --git a/src/Sentry.Unity.Editor/Android/AndroidManifestConfiguration.cs b/src/Sentry.Unity.Editor/Android/AndroidManifestConfiguration.cs index c61de02b9..5042f1026 100644 --- a/src/Sentry.Unity.Editor/Android/AndroidManifestConfiguration.cs +++ b/src/Sentry.Unity.Editor/Android/AndroidManifestConfiguration.cs @@ -206,6 +206,7 @@ internal void ModifyManifest(string basePath) // Disabling the native in favor of the C# layer for now androidManifest.SetNdkEnabled(_options.NdkIntegrationEnabled); androidManifest.SetNdkScopeSync(_options.NdkScopeSyncEnabled); + androidManifest.SetAutoTraceIdGeneration(false); androidManifest.SetAutoSessionTracking(false); androidManifest.SetAutoAppLifecycleBreadcrumbs(false); androidManifest.SetAnr(false); @@ -483,6 +484,9 @@ internal void SetNdkEnabled(bool enableNdk) internal void SetNdkScopeSync(bool enableNdkScopeSync) => SetMetaData($"{SentryPrefix}.ndk.scope-sync.enable", enableNdkScopeSync.ToString()); + public void SetAutoTraceIdGeneration(bool enableAutoTraceIdGeneration) + => SetMetaData($"{SentryPrefix}.traces.enable-auto-id-generation", enableAutoTraceIdGeneration.ToString()); + internal void SetDebug(bool debug) => SetMetaData($"{SentryPrefix}.debug", debug ? "true" : "false"); // https://github.com/getsentry/sentry-java/blob/db4dfc92f202b1cefc48d019fdabe24d487db923/sentry/src/main/java/io/sentry/SentryLevel.java#L4-L9 diff --git a/src/Sentry.Unity/ScopeObserver.cs b/src/Sentry.Unity/ScopeObserver.cs index b19c66e97..0a416fcdb 100644 --- a/src/Sentry.Unity/ScopeObserver.cs +++ b/src/Sentry.Unity/ScopeObserver.cs @@ -85,8 +85,13 @@ public void SetUser(SentryUser? user) public abstract void UnsetUserImpl(); - public abstract void SetTraceImpl(SentryId traceId, SpanId parentSpanId); - public void SetTrace(SentryId traceId, SpanId parentSpanId) - => SetTraceImpl(traceId, parentSpanId); + { + _options.DiagnosticLogger?.Log( + SentryLevel.Debug,"{0} Scope Sync - Setting Trace traceId:{1} parentSpanId:{2}", null, + _name, traceId, parentSpanId); + SetTraceImpl(traceId, parentSpanId); + } + + public abstract void SetTraceImpl(SentryId traceId, SpanId parentSpanId); } diff --git a/src/Sentry.Unity/SentryUnitySDK.cs b/src/Sentry.Unity/SentryUnitySDK.cs index 419a8e8d0..3f692ba60 100644 --- a/src/Sentry.Unity/SentryUnitySDK.cs +++ b/src/Sentry.Unity/SentryUnitySDK.cs @@ -51,6 +51,11 @@ private SentryUnitySdk(SentryUnityOptions options) unitySdk._dotnetSdk = SentrySdk.Init(options); + // For now, we're creating a new trace after initializing to be able to tie errors and crashes together on all + // layers. To be able to regenerate new traces based on some mechanism, this will move into some sort of + // integration i.e. scene manager. + InternalSentrySdk.SetTrace(SentryId.Create(), SpanId.Create()); + if (options.NativeContextWriter is { } contextWriter) { SentrySdk.ConfigureScope((scope) => diff --git a/src/sentry-dotnet b/src/sentry-dotnet index 65facbeeb..ba8eebd26 160000 --- a/src/sentry-dotnet +++ b/src/sentry-dotnet @@ -1 +1 @@ -Subproject commit 65facbeeb16dcd0b329de269d9c191a334164728 +Subproject commit ba8eebd26a701f30e8bc1ff0ae0b56c82c7977f5 From 84d971c4b2edb2b74b13e08fa0d5e2996fa60293 Mon Sep 17 00:00:00 2001 From: bitsandfoxes Date: Mon, 31 Mar 2025 11:27:56 +0200 Subject: [PATCH 09/16] removed redundant android trace start --- package-dev/Runtime/SentryInitialization.cs | 3 --- 1 file changed, 3 deletions(-) diff --git a/package-dev/Runtime/SentryInitialization.cs b/package-dev/Runtime/SentryInitialization.cs index f7d62ef5f..229e68fac 100644 --- a/package-dev/Runtime/SentryInitialization.cs +++ b/package-dev/Runtime/SentryInitialization.cs @@ -68,9 +68,6 @@ public static void Init() SetupNativeSdk(options, unityInfo); SentryUnity.Init(options); -#if SENTRY_NATIVE_ANDROID - SentryNativeAndroid.SetTraceId(options); -#endif SetupStartupTracing(options); } else From 559deab4f0a4cea15b8032b460431d5ae67e1ad9 Mon Sep 17 00:00:00 2001 From: bitsandfoxes Date: Mon, 31 Mar 2025 11:29:00 +0200 Subject: [PATCH 10/16] . --- package-dev/Runtime/SentryInitialization.cs | 1 - 1 file changed, 1 deletion(-) diff --git a/package-dev/Runtime/SentryInitialization.cs b/package-dev/Runtime/SentryInitialization.cs index 229e68fac..918cc2165 100644 --- a/package-dev/Runtime/SentryInitialization.cs +++ b/package-dev/Runtime/SentryInitialization.cs @@ -67,7 +67,6 @@ public static void Init() // Configures scope sync and (by default) initializes the native SDK. SetupNativeSdk(options, unityInfo); SentryUnity.Init(options); - SetupStartupTracing(options); } else From e708ade37ee0c0116053dd6369d88c50c8286763 Mon Sep 17 00:00:00 2001 From: bitsandfoxes Date: Mon, 31 Mar 2025 11:30:27 +0200 Subject: [PATCH 11/16] native and ios scope observers are not there yet --- src/Sentry.Unity.Native/NativeScopeObserver.cs | 2 +- src/Sentry.Unity.iOS/NativeScopeObserver.cs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Sentry.Unity.Native/NativeScopeObserver.cs b/src/Sentry.Unity.Native/NativeScopeObserver.cs index 8a3ef4306..e9c15797e 100644 --- a/src/Sentry.Unity.Native/NativeScopeObserver.cs +++ b/src/Sentry.Unity.Native/NativeScopeObserver.cs @@ -42,7 +42,7 @@ public override void SetUserImpl(SentryUser user) public override void UnsetUserImpl() => C.sentry_remove_user(); public override void SetTraceImpl(SentryId traceId, SpanId parentSpanId) { - throw new NotImplementedException(); + // TODO: Needs to be implemented } private static string GetTimestamp(DateTimeOffset timestamp) => diff --git a/src/Sentry.Unity.iOS/NativeScopeObserver.cs b/src/Sentry.Unity.iOS/NativeScopeObserver.cs index 36de7fe3e..862eb8ef8 100644 --- a/src/Sentry.Unity.iOS/NativeScopeObserver.cs +++ b/src/Sentry.Unity.iOS/NativeScopeObserver.cs @@ -27,7 +27,7 @@ public override void SetUserImpl(SentryUser user) => public override void UnsetUserImpl() => SentryCocoaBridgeProxy.UnsetUser(); public override void SetTraceImpl(SentryId traceId, SpanId parentSpanId) { - throw new NotImplementedException(); + // TODO: Needs to be implemented } internal static string GetTimestamp(DateTimeOffset timestamp) => From 165b425751351d3f2a137b4cfec8090e3bce6e9f Mon Sep 17 00:00:00 2001 From: Sentry Github Bot Date: Mon, 31 Mar 2025 09:33:36 +0000 Subject: [PATCH 12/16] Format code --- src/Sentry.Unity/ScopeObserver.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Sentry.Unity/ScopeObserver.cs b/src/Sentry.Unity/ScopeObserver.cs index 0a416fcdb..fb5e155d0 100644 --- a/src/Sentry.Unity/ScopeObserver.cs +++ b/src/Sentry.Unity/ScopeObserver.cs @@ -88,7 +88,7 @@ public void SetUser(SentryUser? user) public void SetTrace(SentryId traceId, SpanId parentSpanId) { _options.DiagnosticLogger?.Log( - SentryLevel.Debug,"{0} Scope Sync - Setting Trace traceId:{1} parentSpanId:{2}", null, + SentryLevel.Debug, "{0} Scope Sync - Setting Trace traceId:{1} parentSpanId:{2}", null, _name, traceId, parentSpanId); SetTraceImpl(traceId, parentSpanId); } From 9aeb52ebd70259691da42ba72d2c9d93cc0daabb Mon Sep 17 00:00:00 2001 From: bitsandfoxes Date: Mon, 31 Mar 2025 11:55:10 +0200 Subject: [PATCH 13/16] Updated CHANGELOG.md --- CHANGELOG.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 710930bd0..58121096b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,10 @@ ## Unreleased +### Features + +- When running on Android, the SDK now links errors and events originating on different layers (managed, native errors) via `trace ID` ([#1997](https://github.com/getsentry/sentry-unity/pull/1997)) + ### Dependencies - Bump Cocoa SDK from v8.45.0 to v8.48.0 ([#2063](https://github.com/getsentry/sentry-unity/pull/2063), [#2071](https://github.com/getsentry/sentry-unity/pull/2071)) From f2cfb1a3051376d5bd1359eabb3e4a02baa6bada Mon Sep 17 00:00:00 2001 From: bitsandfoxes Date: Wed, 2 Apr 2025 15:06:58 +0200 Subject: [PATCH 14/16] use internal API --- src/Sentry.Unity/SentryUnitySDK.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Sentry.Unity/SentryUnitySDK.cs b/src/Sentry.Unity/SentryUnitySDK.cs index 3f692ba60..fb17efe57 100644 --- a/src/Sentry.Unity/SentryUnitySDK.cs +++ b/src/Sentry.Unity/SentryUnitySDK.cs @@ -54,7 +54,7 @@ private SentryUnitySdk(SentryUnityOptions options) // For now, we're creating a new trace after initializing to be able to tie errors and crashes together on all // layers. To be able to regenerate new traces based on some mechanism, this will move into some sort of // integration i.e. scene manager. - InternalSentrySdk.SetTrace(SentryId.Create(), SpanId.Create()); + SentrySdk.SetTrace(SentryId.Create(), SpanId.Create()); if (options.NativeContextWriter is { } contextWriter) { From 6013e11b4d2dd401b2f513ec88ce8e31a24dbf77 Mon Sep 17 00:00:00 2001 From: bitsandfoxes Date: Wed, 9 Apr 2025 12:01:16 +0200 Subject: [PATCH 15/16] . --- src/Sentry.Unity.Android/AndroidJavaScopeObserver.cs | 4 ++-- src/Sentry.Unity.Native/NativeScopeObserver.cs | 2 +- src/Sentry.Unity.iOS/NativeScopeObserver.cs | 2 +- src/Sentry.Unity/ScopeObserver.cs | 10 +++++----- 4 files changed, 9 insertions(+), 9 deletions(-) diff --git a/src/Sentry.Unity.Android/AndroidJavaScopeObserver.cs b/src/Sentry.Unity.Android/AndroidJavaScopeObserver.cs index f56b7060d..ac9f38087 100644 --- a/src/Sentry.Unity.Android/AndroidJavaScopeObserver.cs +++ b/src/Sentry.Unity.Android/AndroidJavaScopeObserver.cs @@ -91,13 +91,13 @@ public override void UnsetUserImpl() }); } - public override void SetTraceImpl(SentryId traceId, SpanId parentSpanId) + public override void SetTraceImpl(SentryId traceId, SpanId spanId) { _jniExecutor.Run(() => { using var sentry = GetInternalSentryJava(); // We have to explicitly cast to `(Double?)` - sentry.CallStatic("setTrace", traceId.ToString(), parentSpanId.ToString(), (Double?)null, (Double?)null); + sentry.CallStatic("setTrace", traceId.ToString(), spanId.ToString(), (Double?)null, (Double?)null); }); } } diff --git a/src/Sentry.Unity.Native/NativeScopeObserver.cs b/src/Sentry.Unity.Native/NativeScopeObserver.cs index e9c15797e..3499e38d8 100644 --- a/src/Sentry.Unity.Native/NativeScopeObserver.cs +++ b/src/Sentry.Unity.Native/NativeScopeObserver.cs @@ -40,7 +40,7 @@ public override void SetUserImpl(SentryUser user) } public override void UnsetUserImpl() => C.sentry_remove_user(); - public override void SetTraceImpl(SentryId traceId, SpanId parentSpanId) + public override void SetTraceImpl(SentryId traceId, SpanId spanId) { // TODO: Needs to be implemented } diff --git a/src/Sentry.Unity.iOS/NativeScopeObserver.cs b/src/Sentry.Unity.iOS/NativeScopeObserver.cs index 862eb8ef8..836a8532e 100644 --- a/src/Sentry.Unity.iOS/NativeScopeObserver.cs +++ b/src/Sentry.Unity.iOS/NativeScopeObserver.cs @@ -25,7 +25,7 @@ public override void SetUserImpl(SentryUser user) => SentryCocoaBridgeProxy.SetUser(user.Email, user.Id, user.IpAddress, user.Username); public override void UnsetUserImpl() => SentryCocoaBridgeProxy.UnsetUser(); - public override void SetTraceImpl(SentryId traceId, SpanId parentSpanId) + public override void SetTraceImpl(SentryId traceId, SpanId spanId) { // TODO: Needs to be implemented } diff --git a/src/Sentry.Unity/ScopeObserver.cs b/src/Sentry.Unity/ScopeObserver.cs index fb5e155d0..9055ff55e 100644 --- a/src/Sentry.Unity/ScopeObserver.cs +++ b/src/Sentry.Unity/ScopeObserver.cs @@ -85,13 +85,13 @@ public void SetUser(SentryUser? user) public abstract void UnsetUserImpl(); - public void SetTrace(SentryId traceId, SpanId parentSpanId) + public void SetTrace(SentryId traceId, SpanId spanId) { _options.DiagnosticLogger?.Log( - SentryLevel.Debug, "{0} Scope Sync - Setting Trace traceId:{1} parentSpanId:{2}", null, - _name, traceId, parentSpanId); - SetTraceImpl(traceId, parentSpanId); + SentryLevel.Debug, "{0} Scope Sync - Setting Trace traceId:{1} spanId:{2}", null, + _name, traceId, spanId); + SetTraceImpl(traceId, spanId); } - public abstract void SetTraceImpl(SentryId traceId, SpanId parentSpanId); + public abstract void SetTraceImpl(SentryId traceId, SpanId spanId); } From c053dac297ef58810e4aaa076032f46173871546 Mon Sep 17 00:00:00 2001 From: bitsandfoxes Date: Wed, 9 Apr 2025 21:40:08 +0200 Subject: [PATCH 16/16] review --- src/Sentry.Unity.Android/AndroidJavaScopeObserver.cs | 2 +- src/Sentry.Unity.Editor/Android/AndroidManifestConfiguration.cs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Sentry.Unity.Android/AndroidJavaScopeObserver.cs b/src/Sentry.Unity.Android/AndroidJavaScopeObserver.cs index ac9f38087..0f80acfd8 100644 --- a/src/Sentry.Unity.Android/AndroidJavaScopeObserver.cs +++ b/src/Sentry.Unity.Android/AndroidJavaScopeObserver.cs @@ -16,7 +16,7 @@ public AndroidJavaScopeObserver(SentryOptions options, IJniExecutor jniExecutor) _jniExecutor = jniExecutor; } - private AndroidJavaObject GetSentryJava() => new AndroidJavaClass("io.sentry.Sentry"); + private static AndroidJavaObject GetSentryJava() => new AndroidJavaClass("io.sentry.Sentry"); private static AndroidJavaObject GetInternalSentryJava() => new AndroidJavaClass("io.sentry.android.core.InternalSentrySdk"); public override void AddBreadcrumbImpl(Breadcrumb breadcrumb) diff --git a/src/Sentry.Unity.Editor/Android/AndroidManifestConfiguration.cs b/src/Sentry.Unity.Editor/Android/AndroidManifestConfiguration.cs index 5042f1026..fc63611d7 100644 --- a/src/Sentry.Unity.Editor/Android/AndroidManifestConfiguration.cs +++ b/src/Sentry.Unity.Editor/Android/AndroidManifestConfiguration.cs @@ -484,7 +484,7 @@ internal void SetNdkEnabled(bool enableNdk) internal void SetNdkScopeSync(bool enableNdkScopeSync) => SetMetaData($"{SentryPrefix}.ndk.scope-sync.enable", enableNdkScopeSync.ToString()); - public void SetAutoTraceIdGeneration(bool enableAutoTraceIdGeneration) + internal void SetAutoTraceIdGeneration(bool enableAutoTraceIdGeneration) => SetMetaData($"{SentryPrefix}.traces.enable-auto-id-generation", enableAutoTraceIdGeneration.ToString()); internal void SetDebug(bool debug) => SetMetaData($"{SentryPrefix}.debug", debug ? "true" : "false");