Skip to content

Commit 98a2e87

Browse files
authored
feat: Propagate traceId to the Android SDK (#1997)
1 parent 1ac130c commit 98a2e87

File tree

8 files changed

+43
-5
lines changed

8 files changed

+43
-5
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
### Features
66

7+
- 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))
78
- The SDK now reports the game's name as part of the app context ([2083](https://github.com/getsentry/sentry-unity/pull/2083))
89
- The SDK now reports the active scene's name as part of the `Unity Context` ([2084](https://github.com/getsentry/sentry-unity/pull/2084))
910

src/Sentry.Unity.Android/AndroidJavaScopeObserver.cs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System;
12
using UnityEngine;
23

34
namespace Sentry.Unity.Android;
@@ -15,7 +16,8 @@ public AndroidJavaScopeObserver(SentryOptions options, IJniExecutor jniExecutor)
1516
_jniExecutor = jniExecutor;
1617
}
1718

18-
private AndroidJavaObject GetSentryJava() => new AndroidJavaClass("io.sentry.Sentry");
19+
private static AndroidJavaObject GetSentryJava() => new AndroidJavaClass("io.sentry.Sentry");
20+
private static AndroidJavaObject GetInternalSentryJava() => new AndroidJavaClass("io.sentry.android.core.InternalSentrySdk");
1921

2022
public override void AddBreadcrumbImpl(Breadcrumb breadcrumb)
2123
{
@@ -88,4 +90,14 @@ public override void UnsetUserImpl()
8890
sentry.CallStatic("setUser", null);
8991
});
9092
}
93+
94+
public override void SetTraceImpl(SentryId traceId, SpanId spanId)
95+
{
96+
_jniExecutor.Run(() =>
97+
{
98+
using var sentry = GetInternalSentryJava();
99+
// We have to explicitly cast to `(Double?)`
100+
sentry.CallStatic("setTrace", traceId.ToString(), spanId.ToString(), (Double?)null, (Double?)null);
101+
});
102+
}
91103
}

src/Sentry.Unity.Editor/Android/AndroidManifestConfiguration.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,7 @@ internal void ModifyManifest(string basePath)
206206
// Disabling the native in favor of the C# layer for now
207207
androidManifest.SetNdkEnabled(_options.NdkIntegrationEnabled);
208208
androidManifest.SetNdkScopeSync(_options.NdkScopeSyncEnabled);
209+
androidManifest.SetAutoTraceIdGeneration(false);
209210
androidManifest.SetAutoSessionTracking(false);
210211
androidManifest.SetAutoAppLifecycleBreadcrumbs(false);
211212
androidManifest.SetAnr(false);
@@ -483,6 +484,9 @@ internal void SetNdkEnabled(bool enableNdk)
483484
internal void SetNdkScopeSync(bool enableNdkScopeSync)
484485
=> SetMetaData($"{SentryPrefix}.ndk.scope-sync.enable", enableNdkScopeSync.ToString());
485486

487+
internal void SetAutoTraceIdGeneration(bool enableAutoTraceIdGeneration)
488+
=> SetMetaData($"{SentryPrefix}.traces.enable-auto-id-generation", enableAutoTraceIdGeneration.ToString());
489+
486490
internal void SetDebug(bool debug) => SetMetaData($"{SentryPrefix}.debug", debug ? "true" : "false");
487491

488492
// https://github.com/getsentry/sentry-java/blob/db4dfc92f202b1cefc48d019fdabe24d487db923/sentry/src/main/java/io/sentry/SentryLevel.java#L4-L9

src/Sentry.Unity.Native/NativeScopeObserver.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,10 @@ public override void SetUserImpl(SentryUser user)
4040
}
4141

4242
public override void UnsetUserImpl() => C.sentry_remove_user();
43+
public override void SetTraceImpl(SentryId traceId, SpanId spanId)
44+
{
45+
// TODO: Needs to be implemented
46+
}
4347

4448
private static string GetTimestamp(DateTimeOffset timestamp) =>
4549
// "o": Using ISO 8601 to make sure the timestamp makes it to the bridge correctly.

src/Sentry.Unity.iOS/NativeScopeObserver.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ public override void SetUserImpl(SentryUser user) =>
2525
SentryCocoaBridgeProxy.SetUser(user.Email, user.Id, user.IpAddress, user.Username);
2626

2727
public override void UnsetUserImpl() => SentryCocoaBridgeProxy.UnsetUser();
28+
public override void SetTraceImpl(SentryId traceId, SpanId spanId)
29+
{
30+
// TODO: Needs to be implemented
31+
}
2832

2933
internal static string GetTimestamp(DateTimeOffset timestamp) =>
3034
// "o": Using ISO 8601 to make sure the timestamp makes it to the bridge correctly.

src/Sentry.Unity/ScopeObserver.cs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
namespace Sentry.Unity;
44

55
/// <summary>
6-
/// Sentry Unity Scope Observer wrapper for the common behaviour accross platforms.
6+
/// Sentry Unity Scope Observer wrapper for the common behaviour across platforms.
77
/// </summary>
88
public abstract class ScopeObserver : IScopeObserver
99
{
@@ -81,10 +81,17 @@ public void SetUser(SentryUser? user)
8181
}
8282
}
8383

84-
public void SetTrace(SentryId traceId, SpanId parentSpanId)
85-
{ }
86-
8784
public abstract void SetUserImpl(SentryUser user);
8885

8986
public abstract void UnsetUserImpl();
87+
88+
public void SetTrace(SentryId traceId, SpanId spanId)
89+
{
90+
_options.DiagnosticLogger?.Log(
91+
SentryLevel.Debug, "{0} Scope Sync - Setting Trace traceId:{1} spanId:{2}", null,
92+
_name, traceId, spanId);
93+
SetTraceImpl(traceId, spanId);
94+
}
95+
96+
public abstract void SetTraceImpl(SentryId traceId, SpanId spanId);
9097
}

src/Sentry.Unity/SentryUnitySDK.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,11 @@ private SentryUnitySdk(SentryUnityOptions options)
5151

5252
unitySdk._dotnetSdk = SentrySdk.Init(options);
5353

54+
// For now, we're creating a new trace after initializing to be able to tie errors and crashes together on all
55+
// layers. To be able to regenerate new traces based on some mechanism, this will move into some sort of
56+
// integration i.e. scene manager.
57+
SentrySdk.SetTrace(SentryId.Create(), SpanId.Create());
58+
5459
if (options.NativeContextWriter is { } contextWriter)
5560
{
5661
SentrySdk.ConfigureScope((scope) =>

test/Sentry.Unity.Android.Tests/TestSentryJava.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ public void Init(IJniExecutor jniExecutor, SentryUnityOptions options, TimeSpan
1919
public bool? CrashedLastRun(IJniExecutor jniExecutor) => IsCrashedLastRun;
2020

2121
public void Close(IJniExecutor jniExecutor) { }
22+
public void SetTrace(IJniExecutor jniExecutor, string traceId, string spanId) { }
2223

2324
public void WriteScope(
2425
IJniExecutor jniExecutor,

0 commit comments

Comments
 (0)