Skip to content

Commit 0bd2b41

Browse files
authored
fix: Sync breadcrumb data to native layer (#2720)
1 parent 3f711e2 commit 0bd2b41

12 files changed

Lines changed: 121 additions & 13 deletions

File tree

CHANGELOG.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
### Fixes
66

7+
- The SDK now also syncs breadcrumb data to the native layer so they are available on events coming from the native SDKs ([#2720](https://github.com/getsentry/sentry-unity/pull/2720))
78
- Removed `Lumin` and `Stadia` platforms from the package's assembly definition to resolve compilation errors. ([#2716](https://github.com/getsentry/sentry-unity/pull/2716))
89

910
### Features
@@ -12,9 +13,9 @@
1213

1314
### Dependencies
1415

15-
- Bump Java SDK from v8.43.0 to v8.44.0 ([#2706](https://github.com/getsentry/sentry-unity/pull/2706), [#2711](https://github.com/getsentry/sentry-unity/pull/2711), [#2723](https://github.com/getsentry/sentry-unity/pull/2723))
16-
- [changelog](https://github.com/getsentry/sentry-java/blob/main/CHANGELOG.md#8440)
17-
- [diff](https://github.com/getsentry/sentry-java/compare/8.43.0...8.44.0)
16+
- Bump Java SDK from v8.43.0 to v8.44.1 ([#2706](https://github.com/getsentry/sentry-unity/pull/2706), [#2711](https://github.com/getsentry/sentry-unity/pull/2711), [#2723](https://github.com/getsentry/sentry-unity/pull/2723), [#2720](https://github.com/getsentry/sentry-unity/pull/2720))
17+
- [changelog](https://github.com/getsentry/sentry-java/blob/main/CHANGELOG.md#8441)
18+
- [diff](https://github.com/getsentry/sentry-java/compare/8.43.0...8.44.1)
1819
- Bump Cocoa SDK from v9.15.0 to v9.18.0 ([#2705](https://github.com/getsentry/sentry-unity/pull/2705), [#2713](https://github.com/getsentry/sentry-unity/pull/2713), [#2724](https://github.com/getsentry/sentry-unity/pull/2724))
1920
- [changelog](https://github.com/getsentry/sentry-cocoa/blob/main/CHANGELOG.md#9180)
2021
- [diff](https://github.com/getsentry/sentry-cocoa/compare/9.15.0...9.18.0)

modules/sentry-java

Submodule sentry-java updated 46 files

package-dev/Plugins/iOS/SentryNativeBridge.m

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,8 +107,8 @@ void SentryNativeBridgeSetSdkName()
107107

108108
void SentryNativeBridgeClose() { [SentrySDK close]; }
109109

110-
void SentryNativeBridgeAddBreadcrumb(
111-
const char *timestamp, const char *message, const char *type, const char *category, int level)
110+
void SentryNativeBridgeAddBreadcrumb(const char *timestamp, const char *message, const char *type,
111+
const char *category, int level, const char **dataKeys, const char **dataValues, int dataCount)
112112
{
113113
if (timestamp == NULL && message == NULL && type == NULL && category == NULL) {
114114
return;
@@ -139,6 +139,18 @@ void SentryNativeBridgeAddBreadcrumb(
139139
breadcrumb.type = typeString;
140140
}
141141

142+
if (dataCount > 0 && dataKeys != NULL && dataValues != NULL) {
143+
NSMutableDictionary *data = [NSMutableDictionary dictionaryWithCapacity:dataCount];
144+
for (int i = 0; i < dataCount; i++) {
145+
NSString *key = _NSStringOrNil(dataKeys[i]);
146+
NSString *value = _NSStringOrNil(dataValues[i]);
147+
if (key != nil && value != nil) {
148+
data[key] = value;
149+
}
150+
}
151+
breadcrumb.data = data;
152+
}
153+
142154
[scope addBreadcrumb:breadcrumb];
143155
}];
144156
}

package-dev/Plugins/iOS/SentryNativeBridgeNoOp.m

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ void SentryNativeBridgeSetSdkName() { }
1616

1717
void SentryNativeBridgeClose() { }
1818

19-
void SentryNativeBridgeAddBreadcrumb(
20-
const char *timestamp, const char *message, const char *type, const char *category, int level) { }
19+
void SentryNativeBridgeAddBreadcrumb(const char *timestamp, const char *message, const char *type,
20+
const char *category, int level, const char **dataKeys, const char **dataValues, int dataCount) { }
2121

2222
void SentryNativeBridgeSetExtra(const char *key, const char *value) { }
2323

package-dev/Plugins/macOS/SentryNativeBridge.m

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -208,8 +208,8 @@ void SentryNativeBridgeClose()
208208
}
209209
}
210210

211-
void SentryNativeBridgeAddBreadcrumb(
212-
const char *timestamp, const char *message, const char *type, const char *category, int level)
211+
void SentryNativeBridgeAddBreadcrumb(const char *timestamp, const char *message, const char *type,
212+
const char *category, int level, const char **dataKeys, const char **dataValues, int dataCount)
213213
{
214214
if (timestamp == NULL && message == NULL && type == NULL && category == NULL) {
215215
return;
@@ -244,6 +244,18 @@ void SentryNativeBridgeAddBreadcrumb(
244244

245245
[breadcrumb setValue:[NSNumber numberWithInt:level] forKey:@"level"];
246246

247+
if (dataCount > 0 && dataKeys != NULL && dataValues != NULL) {
248+
NSMutableDictionary *data = [NSMutableDictionary dictionaryWithCapacity:dataCount];
249+
for (int i = 0; i < dataCount; i++) {
250+
NSString *key = _NSStringOrNil(dataKeys[i]);
251+
NSString *value = _NSStringOrNil(dataValues[i]);
252+
if (key != nil && value != nil) {
253+
data[key] = value;
254+
}
255+
}
256+
[breadcrumb setValue:data forKey:@"data"];
257+
}
258+
247259
[scope performSelector:@selector(addBreadcrumb:) withObject:breadcrumb];
248260
});
249261
}

src/Sentry.Unity.Android/SentryJava.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,13 @@ public void AddBreadcrumb(Breadcrumb breadcrumb)
304304
javaBreadcrumb.Set("category", breadcrumb.Category);
305305
using var javaLevel = breadcrumb.Level.ToJavaSentryLevel();
306306
javaBreadcrumb.Set("level", javaLevel);
307+
if (breadcrumb.Data is { Count: > 0 })
308+
{
309+
foreach (var kvp in breadcrumb.Data)
310+
{
311+
javaBreadcrumb.Call("setData", kvp.Key, kvp.Value);
312+
}
313+
}
307314
sentry.CallStatic("addBreadcrumb", javaBreadcrumb, null);
308315
});
309316
}

src/Sentry.Unity.Native/NativeScopeObserver.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,15 @@ public override void AddBreadcrumbImpl(Breadcrumb breadcrumb)
1717
C.sentry_value_set_by_key(crumb, "level", C.sentry_value_new_string(breadcrumb.Level.ToString().ToLower()));
1818
C.sentry_value_set_by_key(crumb, "timestamp", C.sentry_value_new_string(GetTimestamp(breadcrumb.Timestamp)));
1919
C.SetValueIfNotNull(crumb, "category", breadcrumb.Category);
20+
if (breadcrumb.Data is { Count: > 0 })
21+
{
22+
var data = C.sentry_value_new_object();
23+
foreach (var kvp in breadcrumb.Data)
24+
{
25+
C.SetValueIfNotNull(data, kvp.Key, kvp.Value);
26+
}
27+
_ = C.sentry_value_set_by_key(crumb, "data", data);
28+
}
2029
C.sentry_add_breadcrumb(crumb);
2130
}
2231

src/Sentry.Unity.iOS/NativeScopeObserver.cs

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,33 @@ public override void AddBreadcrumbImpl(Breadcrumb breadcrumb)
1111
var level = GetBreadcrumbLevel(breadcrumb.Level);
1212
var timestamp = GetTimestamp(breadcrumb.Timestamp);
1313

14-
SentryCocoaBridgeProxy.AddBreadcrumb(timestamp, breadcrumb.Message, breadcrumb.Type, breadcrumb.Category, level);
14+
// Pass breadcrumb.Data as two parallel key/value arrays for the __Internal P/Invoke boundary.
15+
var dataCount = GetBreadcrumbData(breadcrumb, out var dataKeys, out var dataValues);
16+
17+
SentryCocoaBridgeProxy.AddBreadcrumb(timestamp, breadcrumb.Message, breadcrumb.Type, breadcrumb.Category, level,
18+
dataKeys, dataValues, dataCount);
19+
}
20+
21+
internal static int GetBreadcrumbData(Breadcrumb breadcrumb, out string[]? keys, out string[]? values)
22+
{
23+
if (breadcrumb.Data is not { Count: > 0 } data)
24+
{
25+
keys = null;
26+
values = null;
27+
return 0;
28+
}
29+
30+
keys = new string[data.Count];
31+
values = new string[data.Count];
32+
var i = 0;
33+
foreach (var kvp in data)
34+
{
35+
keys[i] = kvp.Key;
36+
values[i] = kvp.Value;
37+
i++;
38+
}
39+
40+
return data.Count;
1541
}
1642

1743
public override void SetExtraImpl(string key, string? value) =>

src/Sentry.Unity.iOS/SentryCocoaBridgeProxy.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,8 @@ public static bool Init(SentryUnityOptions options)
128128
public static extern void Close();
129129

130130
[DllImport("__Internal", EntryPoint = "SentryNativeBridgeAddBreadcrumb")]
131-
public static extern void AddBreadcrumb(string timestamp, string? message, string? type, string? category, int level);
131+
public static extern void AddBreadcrumb(string timestamp, string? message, string? type, string? category, int level,
132+
string[]? dataKeys, string[]? dataValues, int dataCount);
132133

133134
[DllImport("__Internal", EntryPoint = "SentryNativeBridgeSetExtra")]
134135
public static extern void SetExtra(string key, string? value);

test/IntegrationTest/CommonTestCases.ps1

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,10 @@ $CommonTestCases = @(
7272
$SentryEvent.breadcrumbs.values | Should -Not -BeNullOrEmpty
7373
$SentryEvent.breadcrumbs.values | Where-Object { $_.message -eq "Integration test started" } | Should -Not -BeNullOrEmpty
7474
$SentryEvent.breadcrumbs.values | Where-Object { $_.message -eq "Context configuration finished" } | Should -Not -BeNullOrEmpty
75+
76+
$dataCrumb = $SentryEvent.breadcrumbs.values | Where-Object { $_.message -eq "Context configuration finished" }
77+
$dataCrumb | Should -Not -BeNullOrEmpty
78+
$dataCrumb.data.integration_test_key | Should -Be "integration_test_value"
7579
}
7680
}
7781
@{ Name = "Contains SDK information"; TestBlock = {

0 commit comments

Comments
 (0)