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
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
2 changes: 1 addition & 1 deletion modules/sentry-java
Submodule sentry-java updated 896 files
4 changes: 4 additions & 0 deletions package-dev/Runtime/SentryInitialization.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down
10 changes: 10 additions & 0 deletions src/Sentry.Unity.Android/SentryJava.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -170,6 +171,15 @@ public void Close(IJniExecutor jniExecutor)
});
}

public void ContinueTrace(IJniExecutor jniExecutor, string traceId)
{
jniExecutor.Run(() =>
{
using var sentry = GetSentryJava();
sentry.CallStatic<AndroidJavaObject>("continueTrace", traceId, null);
});
}

public void WriteScope(
IJniExecutor jniExecutor,
int? GpuId,
Expand Down
18 changes: 18 additions & 0 deletions src/Sentry.Unity.Android/SentryNativeAndroid.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}

/// <summary>
/// Closes the native Android support.
/// </summary>
Expand Down
1 change: 1 addition & 0 deletions test/Sentry.Unity.Android.Tests/TestSentryJava.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Loading