Skip to content

Commit d32cf36

Browse files
authored
feat: Propagate traceId to the Cocoa SDK (#2106)
1 parent 87b331f commit d32cf36

File tree

7 files changed

+66
-9
lines changed

7 files changed

+66
-9
lines changed

CHANGELOG.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,16 @@
88

99
### Features
1010

11+
- The SDK now links errors and events (managed and native errors) via `trace ID`. This allows you to correlate events captured from different layers of your game ([#1997](https://github.com/getsentry/sentry-unity/pull/1997), [#2089](https://github.com/getsentry/sentry-unity/pull/2089), [#2106](https://github.com/getsentry/sentry-unity/pull/2106))
1112
- Drastically improved performance of scope sync when targeting Android ([#2107](https://github.com/getsentry/sentry-unity/pull/2107))
12-
- When running on Android, Windows or Linux, 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), [#2089](https://github.com/getsentry/sentry-unity/pull/2089))
1313
- The SDK now reports the game's name as part of the app context ([2083](https://github.com/getsentry/sentry-unity/pull/2083))
1414
- The SDK now reports the active scene's name as part of the `Unity Context` ([2084](https://github.com/getsentry/sentry-unity/pull/2084))
1515

1616
### Dependencies
1717

18-
- Bump Cocoa SDK from v8.45.0 to v8.49.0 ([#2063](https://github.com/getsentry/sentry-unity/pull/2063), [#2071](https://github.com/getsentry/sentry-unity/pull/2071), [#2105](https://github.com/getsentry/sentry-unity/pull/2105))
19-
- [changelog](https://github.com/getsentry/sentry-cocoa/blob/main/CHANGELOG.md#8490)
20-
- [diff](https://github.com/getsentry/sentry-cocoa/compare/8.45.0...8.49.0)
18+
- Bump Cocoa SDK from v8.45.0 to v8.49.1 ([#2063](https://github.com/getsentry/sentry-unity/pull/2063), [#2071](https://github.com/getsentry/sentry-unity/pull/2071), [#2105](https://github.com/getsentry/sentry-unity/pull/2105), [#2106](https://github.com/getsentry/sentry-unity/pull/2106))
19+
- [changelog](https://github.com/getsentry/sentry-cocoa/blob/main/CHANGELOG.md#8491)
20+
- [diff](https://github.com/getsentry/sentry-cocoa/compare/8.45.0...8.49.1)
2121
- Bump Java SDK from v8.3.0 to v8.8.0 ([#2066](https://github.com/getsentry/sentry-unity/pull/2066), [#2075](https://github.com/getsentry/sentry-unity/pull/2075), [#2092](https://github.com/getsentry/sentry-unity/pull/2092), [#2103](https://github.com/getsentry/sentry-unity/pull/2103), [#2111](https://github.com/getsentry/sentry-unity/pull/2111))
2222
- [changelog](https://github.com/getsentry/sentry-java/blob/main/CHANGELOG.md#880)
2323
- [diff](https://github.com/getsentry/sentry-java/compare/8.3.0...8.8.0)

modules/sentry-cocoa.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
version = 8.49.0
1+
version = 8.49.1
22
repo = https://github.com/getsentry/sentry-cocoa

package-dev/Plugins/iOS/SentryNativeBridge.m

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,29 @@ void SentryNativeBridgeUnsetUser()
169169
return cString;
170170
}
171171

172+
void SentryNativeBridgeSetTrace(const char *traceId, const char *spanId)
173+
{
174+
if (traceId == NULL || spanId == NULL) {
175+
return;
176+
}
177+
178+
NSString *traceIdStr = [NSString stringWithUTF8String:traceId];
179+
NSString *spanIdStr = [NSString stringWithUTF8String:spanId];
180+
181+
// This is a workaround to deal with SentryId living inside the Swift header
182+
Class sentryIdClass = NSClassFromString(@"_TtC6Sentry8SentryId");
183+
Class sentrySpanIdClass = NSClassFromString(@"SentrySpanId");
184+
185+
if (sentryIdClass && sentrySpanIdClass) {
186+
id sentryTraceId = [[sentryIdClass alloc] initWithUUIDString:traceIdStr];
187+
id sentrySpanId = [[sentrySpanIdClass alloc] initWithValue:spanIdStr];
188+
189+
if (sentryTraceId && sentrySpanId) {
190+
[PrivateSentrySDKOnly setTrace:sentryTraceId spanId:sentrySpanId];
191+
}
192+
}
193+
}
194+
172195
static inline NSString *_NSStringOrNil(const char *value)
173196
{
174197
return value ? [NSString stringWithUTF8String:value] : nil;

package-dev/Plugins/iOS/SentryNativeBridgeNoOp.m

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ void SentryNativeBridgeUnsetUser() { }
2828

2929
char *SentryNativeBridgeGetInstallationId() { return NULL; }
3030

31+
void SentryNativeBridgeSetTrace(const char *traceId, const char *spanId) { }
32+
3133
void SentryNativeBridgeWriteScope( // clang-format off
3234
// // const char *AppStartTime,
3335
// const char *AppBuildType,

package-dev/Plugins/macOS/SentryNativeBridge.m

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
static Class SentryBreadcrumb;
99
static Class SentryUser;
1010
static Class SentryOptions;
11+
static Class SentryId;
12+
static Class SentrySpanId;
1113
static Class PrivateSentrySDKOnly;
1214

1315
#define LOAD_CLASS_OR_BREAK(name) \
@@ -17,6 +19,13 @@
1719
break; \
1820
}
1921

22+
#define LOAD_SWIFT_CLASS_OR_BREAK(name, mangled_name) \
23+
name = (__bridge Class)dlsym(dylib, "OBJC_CLASS_$_" #mangled_name); \
24+
if (!name) { \
25+
NSLog(@"Sentry (bridge): Couldn't load class '" #name "' from the dynamic library"); \
26+
break; \
27+
}
28+
2029
// Returns (bool): 0 on failure, 1 on success
2130
// WARNING: you may only call other Sentry* functions AFTER calling this AND only if it returned "1"
2231
int SentryNativeBridgeLoadLibrary()
@@ -40,6 +49,8 @@ int SentryNativeBridgeLoadLibrary()
4049
LOAD_CLASS_OR_BREAK(SentryBreadcrumb)
4150
LOAD_CLASS_OR_BREAK(SentryUser)
4251
LOAD_CLASS_OR_BREAK(SentryOptions)
52+
LOAD_SWIFT_CLASS_OR_BREAK(SentryId, _TtC6Sentry8SentryId)
53+
LOAD_CLASS_OR_BREAK(SentrySpanId)
4354
LOAD_CLASS_OR_BREAK(PrivateSentrySDKOnly)
4455

4556
// everything above passed - mark as successfully loaded
@@ -263,6 +274,26 @@ void SentryNativeBridgeUnsetUser()
263274
return cString;
264275
}
265276

277+
void SentryNativeBridgeSetTrace(const char *traceId, const char *spanId)
278+
{
279+
if (traceId == NULL || spanId == NULL) {
280+
return;
281+
}
282+
283+
id sentryTraceId = [[SentryId alloc]
284+
performSelector:@selector(initWithUUIDString:)
285+
withObject:[NSString stringWithUTF8String:traceId]];
286+
287+
id sentrySpanId = [[SentrySpanId alloc]
288+
performSelector:@selector(initWithValue:)
289+
withObject:[NSString stringWithUTF8String:spanId]];
290+
291+
[PrivateSentrySDKOnly
292+
performSelector:@selector(setTrace:spanId:)
293+
withObject:sentryTraceId
294+
withObject:sentrySpanId];
295+
}
296+
266297
static inline NSString *_NSStringOrNil(const char *value)
267298
{
268299
return value ? [NSString stringWithUTF8String:value] : nil;

src/Sentry.Unity.iOS/NativeScopeObserver.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,8 @@ public override void SetUserImpl(SentryUser user) =>
2626

2727
public override void UnsetUserImpl() => SentryCocoaBridgeProxy.UnsetUser();
2828

29-
public override void SetTraceImpl(SentryId traceId, SpanId spanId)
30-
{
31-
// TODO: Needs to be implemented
32-
}
29+
public override void SetTraceImpl(SentryId traceId, SpanId spanId) =>
30+
SentryCocoaBridgeProxy.SetTrace(traceId.ToString(), spanId.ToString());
3331

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

src/Sentry.Unity.iOS/SentryCocoaBridgeProxy.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,4 +108,7 @@ public static bool Init(SentryUnityOptions options)
108108

109109
[DllImport("__Internal", EntryPoint = "SentryNativeBridgeGetInstallationId")]
110110
public static extern string GetInstallationId();
111+
112+
[DllImport("__Internal", EntryPoint = "SentryNativeBridgeSetTrace")]
113+
public static extern void SetTrace(string traceId, string spanId);
111114
}

0 commit comments

Comments
 (0)