Skip to content

feat: Propagate traceId on platforms supported by sentry-native #2089

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 28 commits into from
Apr 9, 2025
Merged
Show file tree
Hide file tree
Changes from 27 commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
4c00864
propagate during startup
bitsandfoxes Jan 29, 2025
3dc1d67
setting trace in java
bitsandfoxes Feb 7, 2025
c3c8ff8
merged main
bitsandfoxes Feb 25, 2025
59976fe
bump
bitsandfoxes Feb 27, 2025
43b1486
test
bitsandfoxes Feb 27, 2025
63d5a25
bumped java
bitsandfoxes Feb 28, 2025
18c23cc
bumped java
bitsandfoxes Mar 3, 2025
85bf657
use scope observer instead
bitsandfoxes Mar 5, 2025
ddd5619
merged all kinds of things
bitsandfoxes Mar 28, 2025
882eed9
updated trace generation and sync
bitsandfoxes Mar 28, 2025
84d971c
removed redundant android trace start
bitsandfoxes Mar 31, 2025
559deab
.
bitsandfoxes Mar 31, 2025
e708ade
native and ios scope observers are not there yet
bitsandfoxes Mar 31, 2025
6d8e9af
Merge branch 'main' into feat/propagate-traceid-android
bitsandfoxes Mar 31, 2025
165b425
Format code
getsentry-bot Mar 31, 2025
256a721
bump .NET to get the scope observer
bitsandfoxes Mar 31, 2025
929073c
starting a trace
bitsandfoxes Mar 31, 2025
3203e9c
merged
bitsandfoxes Mar 31, 2025
de43fd5
Updated CHANGELOG.md
bitsandfoxes Mar 31, 2025
9aeb52e
Updated CHANGELOG.md
bitsandfoxes Mar 31, 2025
f2cfb1a
use internal API
bitsandfoxes Apr 2, 2025
fa68abf
Merge branch 'main' into feat/propagate-traceid-android
bitsandfoxes Apr 2, 2025
f16e148
merged main
bitsandfoxes Apr 3, 2025
8894c78
merged
bitsandfoxes Apr 3, 2025
db260c7
Merge branch 'main' into feat/propagate-traceid-android
bitsandfoxes Apr 9, 2025
6013e11
.
bitsandfoxes Apr 9, 2025
6265ac0
bump
bitsandfoxes Apr 9, 2025
0060e5c
Merge branch 'main' into feat/propagate-traceid-desktop
bitsandfoxes Apr 9, 2025
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: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

### Features

- When running on Windows or Linux, the SDK now links errors and events originating on different layers (managed, native errors) via `trace ID` ([#2089](https://github.com/getsentry/sentry-unity/pull/2089))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

question: Could we merge both entries into a single entry linking both PRs, or do we prefer to have one entry per PR?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

True, we could just append the links to the PRs to the entry!

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

Expand Down
12 changes: 12 additions & 0 deletions 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;
Expand All @@ -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)
{
Expand Down Expand Up @@ -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
Expand Up @@ -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);
Expand Down Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions src/Sentry.Unity.Native/NativeScopeObserver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ public override void SetUserImpl(SentryUser user)

public override void UnsetUserImpl() => C.sentry_remove_user();

public override void SetTraceImpl(SentryId traceId, SpanId spanId) =>
C.sentry_set_trace(traceId.ToString(), spanId.ToString());

private static string GetTimestamp(DateTimeOffset timestamp) =>
// "o": Using ISO 8601 to make sure the timestamp makes it to the bridge correctly.
// https://docs.microsoft.com/en-gb/dotnet/standard/base-types/standard-date-and-time-format-strings#Roundtrip
Expand Down
5 changes: 5 additions & 0 deletions src/Sentry.Unity.iOS/NativeScopeObserver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ public override void SetUserImpl(SentryUser user) =>

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.
// https://docs.microsoft.com/en-gb/dotnet/standard/base-types/standard-date-and-time-format-strings#Roundtrip
Expand Down
3 changes: 3 additions & 0 deletions src/Sentry.Unity/NativeUtils/CFunctions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,9 @@ internal static void SetValueIfNotNull(sentry_value_t obj, string key, double? v
[DllImport("sentry")]
internal static extern void sentry_remove_extra(string key);

[DllImport("sentry")]
internal static extern void sentry_set_trace(string traceId, string parentSpanId);

internal static readonly Lazy<IEnumerable<DebugImage>> DebugImages = new(LoadDebugImages);

private static IEnumerable<DebugImage> LoadDebugImages()
Expand Down
15 changes: 11 additions & 4 deletions src/Sentry.Unity/ScopeObserver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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
Expand Up @@ -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) =>
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 SetTrace(IJniExecutor jniExecutor, string traceId, string spanId) { }

public void WriteScope(
IJniExecutor jniExecutor,
Expand Down
Loading