Skip to content

feat: Propagate traceId to the Android SDK #1997

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 22 commits into from
Apr 9, 2025
Merged
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -4,6 +4,7 @@

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

14 changes: 13 additions & 1 deletion src/Sentry.Unity.Android/AndroidJavaScopeObserver.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using UnityEngine;

namespace Sentry.Unity.Android;
@@ -15,7 +16,8 @@ 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)
{
@@ -88,4 +90,14 @@ public override void UnsetUserImpl()
sentry.CallStatic("setUser", null);
});
}

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(), spanId.ToString(), (Double?)null, (Double?)null);
});
}
}
Original file line number Diff line number Diff line change
@@ -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());

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

// https://github.com/getsentry/sentry-java/blob/db4dfc92f202b1cefc48d019fdabe24d487db923/sentry/src/main/java/io/sentry/SentryLevel.java#L4-L9
4 changes: 4 additions & 0 deletions src/Sentry.Unity.Native/NativeScopeObserver.cs
Original file line number Diff line number Diff line change
@@ -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 spanId)
{
// TODO: Needs to be implemented
}

private static string GetTimestamp(DateTimeOffset timestamp) =>
// "o": Using ISO 8601 to make sure the timestamp makes it to the bridge correctly.
4 changes: 4 additions & 0 deletions src/Sentry.Unity.iOS/NativeScopeObserver.cs
Original file line number Diff line number Diff line change
@@ -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 spanId)
{
// TODO: Needs to be implemented
}

internal static string GetTimestamp(DateTimeOffset timestamp) =>
// "o": Using ISO 8601 to make sure the timestamp makes it to the bridge correctly.
15 changes: 11 additions & 4 deletions src/Sentry.Unity/ScopeObserver.cs
Original file line number Diff line number Diff line change
@@ -3,7 +3,7 @@
namespace Sentry.Unity;

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

public void SetTrace(SentryId traceId, SpanId parentSpanId)
{ }

public abstract void SetUserImpl(SentryUser user);

public abstract void UnsetUserImpl();

public void SetTrace(SentryId traceId, SpanId spanId)
{
_options.DiagnosticLogger?.Log(
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 spanId);
}
5 changes: 5 additions & 0 deletions src/Sentry.Unity/SentryUnitySDK.cs
Original file line number Diff line number Diff line change
@@ -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.
SentrySdk.SetTrace(SentryId.Create(), SpanId.Create());

if (options.NativeContextWriter is { } contextWriter)
{
SentrySdk.ConfigureScope((scope) =>
1 change: 1 addition & 0 deletions test/Sentry.Unity.Android.Tests/TestSentryJava.cs
Original file line number Diff line number Diff line change
@@ -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 SetTrace(IJniExecutor jniExecutor, string traceId, string spanId) { }

public void WriteScope(
IJniExecutor jniExecutor,
Loading