Skip to content

Commit 937f5f2

Browse files
authored
feat: Enable java scope sync for attachments (#1584)
1 parent a04841a commit 937f5f2

5 files changed

Lines changed: 95 additions & 1 deletion

File tree

CHANGELOG.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
## Unreleased:
44

5+
**Features**:
6+
7+
- Add `addAttachment` and `clearAttachments` to the NDK `NativeScope` API for managing file and byte attachments via JNI. ([#1584](https://github.com/getsentry/sentry-native/pull/1584))
8+
59
**Fixes**:
610

711
- inproc: only the handling thread cleans up after the crash. ([#1579](https://github.com/getsentry/sentry-native/pull/1579))
@@ -118,7 +122,6 @@
118122
- Add logs flush on `sentry_flush()`. ([#1434](https://github.com/getsentry/sentry-native/pull/1434))
119123
- Add global attributes API. These are added to all `sentry_log_X` calls. ([#1450](https://github.com/getsentry/sentry-native/pull/1450))
120124

121-
122125
## 0.12.1
123126

124127
**Fixes**:

ndk/lib/api/sentry-native-ndk.api

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,10 @@ public final class io/sentry/ndk/DebugImage {
2929
}
3030

3131
public abstract interface class io/sentry/ndk/INativeScope {
32+
public abstract fun addAttachment (Ljava/lang/String;)V
33+
public abstract fun addAttachmentBytes ([BLjava/lang/String;)V
3234
public abstract fun addBreadcrumb (Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)V
35+
public abstract fun clearAttachments ()V
3336
public abstract fun removeExtra (Ljava/lang/String;)V
3437
public abstract fun removeTag (Ljava/lang/String;)V
3538
public abstract fun removeUser ()V
@@ -49,8 +52,14 @@ public final class io/sentry/ndk/NativeModuleListLoader {
4952

5053
public final class io/sentry/ndk/NativeScope : io/sentry/ndk/INativeScope {
5154
public fun <init> ()V
55+
public fun addAttachment (Ljava/lang/String;)V
56+
public fun addAttachmentBytes ([BLjava/lang/String;)V
5257
public fun addBreadcrumb (Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)V
58+
public fun clearAttachments ()V
59+
public static fun nativeAddAttachment (Ljava/lang/String;)V
60+
public static fun nativeAddAttachmentBytes ([BLjava/lang/String;)V
5361
public static fun nativeAddBreadcrumb (Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)V
62+
public static fun nativeClearAttachments ()V
5463
public static fun nativeRemoveExtra (Ljava/lang/String;)V
5564
public static fun nativeRemoveTag (Ljava/lang/String;)V
5665
public static fun nativeRemoveUser ()V

ndk/lib/src/main/java/io/sentry/ndk/INativeScope.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,10 @@ void addBreadcrumb(
1717
String level, String message, String category, String type, String timestamp, String data);
1818

1919
void setTrace(String traceId, String parentSpanId);
20+
21+
void addAttachment(String path);
22+
23+
void addAttachmentBytes(byte[] data, String filename);
24+
25+
void clearAttachments();
2026
}

ndk/lib/src/main/java/io/sentry/ndk/NativeScope.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,12 @@ public static native void nativeAddBreadcrumb(
1919

2020
public static native void nativeSetTrace(String traceId, String parentSpanId);
2121

22+
public static native void nativeAddAttachment(String path);
23+
24+
public static native void nativeAddAttachmentBytes(byte[] data, String filename);
25+
26+
public static native void nativeClearAttachments();
27+
2228
@Override
2329
public void setTag(String key, String value) {
2430
nativeSetTag(key, value);
@@ -63,4 +69,19 @@ public void addBreadcrumb(
6369
public void setTrace(String traceId, String parentSpanId) {
6470
nativeSetTrace(traceId, parentSpanId);
6571
}
72+
73+
@Override
74+
public void addAttachment(String path) {
75+
nativeAddAttachment(path);
76+
}
77+
78+
@Override
79+
public void addAttachmentBytes(byte[] data, String filename) {
80+
nativeAddAttachmentBytes(data, filename);
81+
}
82+
83+
@Override
84+
public void clearAttachments() {
85+
nativeClearAttachments();
86+
}
6687
}

ndk/lib/src/main/jni/sentry.c

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,61 @@ Java_io_sentry_ndk_NativeScope_nativeAddBreadcrumb(
227227
sentry_add_breadcrumb(crumb);
228228
}
229229

230+
JNIEXPORT void JNICALL
231+
Java_io_sentry_ndk_NativeScope_nativeAddAttachment(
232+
JNIEnv *env,
233+
jclass cls,
234+
jstring path) {
235+
if (!path) {
236+
return;
237+
}
238+
const char *charPath = (*env)->GetStringUTFChars(env, path, 0);
239+
if (!charPath) {
240+
return;
241+
}
242+
243+
// The returned sentry_attachment_t* is intentionally discarded.
244+
// We are not tracking it across the JNI boundary for individual removals.
245+
// Use sentry_clear_attachments() for bulk removal.
246+
sentry_attach_file(charPath);
247+
248+
(*env)->ReleaseStringUTFChars(env, path, charPath);
249+
}
250+
251+
JNIEXPORT void JNICALL
252+
Java_io_sentry_ndk_NativeScope_nativeAddAttachmentBytes(
253+
JNIEnv *env,
254+
jclass cls,
255+
jbyteArray data,
256+
jstring filename) {
257+
if (!data || !filename) {
258+
return;
259+
}
260+
jsize bufLen = (*env)->GetArrayLength(env, data);
261+
jbyte *buf = (*env)->GetByteArrayElements(env, data, 0);
262+
if (!buf) {
263+
return;
264+
}
265+
const char *charFilename = (*env)->GetStringUTFChars(env, filename, 0);
266+
if (!charFilename) {
267+
(*env)->ReleaseByteArrayElements(env, data, buf, JNI_ABORT);
268+
return;
269+
}
270+
271+
// The returned sentry_attachment_t* is intentionally discarded.
272+
// We are not tracking it across the JNI boundary for individual removals.
273+
// Use sentry_clear_attachments() for bulk removal.
274+
sentry_attach_bytes((const char *)buf, (size_t)bufLen, charFilename);
275+
276+
(*env)->ReleaseStringUTFChars(env, filename, charFilename);
277+
(*env)->ReleaseByteArrayElements(env, data, buf, JNI_ABORT);
278+
}
279+
280+
JNIEXPORT void JNICALL
281+
Java_io_sentry_ndk_NativeScope_nativeClearAttachments(JNIEnv *env, jclass cls) {
282+
sentry_clear_attachments();
283+
}
284+
230285
static void send_envelope(sentry_envelope_t *envelope, void *data) {
231286
const char *outbox_path = (const char *) data;
232287
char envelope_id_str[40];

0 commit comments

Comments
 (0)