Skip to content

Commit a3da89d

Browse files
committed
observe and sync file attachments
1 parent 80672fe commit a3da89d

5 files changed

Lines changed: 74 additions & 0 deletions

File tree

sentry-android-ndk/src/main/java/io/sentry/android/ndk/NdkScopeObserver.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package io.sentry.android.ndk;
22

3+
import io.sentry.Attachment;
34
import io.sentry.Breadcrumb;
45
import io.sentry.DateUtils;
56
import io.sentry.IScope;
@@ -145,4 +146,29 @@ public void setTrace(@Nullable SpanContext spanContext, @NotNull IScope scope) {
145146
options.getLogger().log(SentryLevel.ERROR, e, "Scope sync setTrace failed.");
146147
}
147148
}
149+
150+
@Override
151+
public void addAttachment(final @NotNull Attachment attachment) {
152+
final String pathname = attachment.getPathname();
153+
if (pathname == null) {
154+
// Only file-path attachments are getting synced to the native layer right now.
155+
options.getLogger().log(SentryLevel.DEBUG, "Scope sync addAttachment skips non-file attachment.");
156+
return;
157+
}
158+
159+
try {
160+
options.getExecutorService().submit(() -> nativeScope.addAttachment(pathname));
161+
} catch (Throwable e) {
162+
options.getLogger().log(SentryLevel.ERROR, e, "Scope sync addAttachment has an error.");
163+
}
164+
}
165+
166+
@Override
167+
public void clearAttachments() {
168+
try {
169+
options.getExecutorService().submit(() -> nativeScope.clearAttachments());
170+
} catch (Throwable e) {
171+
options.getLogger().log(SentryLevel.ERROR, e, "Scope sync clearAttachments has an error.");
172+
}
173+
}
148174
}

sentry-android-ndk/src/test/java/io/sentry/android/ndk/NdkScopeObserverTest.kt

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package io.sentry.android.ndk
22

3+
import io.sentry.Attachment
34
import io.sentry.Breadcrumb
45
import io.sentry.DateUtils
56
import io.sentry.JsonSerializer
@@ -153,4 +154,33 @@ class NdkScopeObserverTest {
153154
verify(fixture.nativeScope)
154155
.addBreadcrumb(anyOrNull(), anyOrNull(), anyOrNull(), anyOrNull(), anyOrNull(), anyOrNull())
155156
}
157+
158+
@Test
159+
fun `add file-path attachment syncs to native scope`() {
160+
val sut = fixture.getSut()
161+
162+
val attachment = Attachment("/data/data/com.example/files/log.txt")
163+
sut.addAttachment(attachment)
164+
165+
verify(fixture.nativeScope).addAttachment("/data/data/com.example/files/log.txt")
166+
}
167+
168+
@Test
169+
fun `add byte attachment does not sync to native scope`() {
170+
val sut = fixture.getSut()
171+
172+
val attachment = Attachment(byteArrayOf(1, 2, 3), "data.bin")
173+
sut.addAttachment(attachment)
174+
175+
verify(fixture.nativeScope, never()).addAttachment(any())
176+
}
177+
178+
@Test
179+
fun `clear attachments forwards call to native scope`() {
180+
val sut = fixture.getSut()
181+
182+
sut.clearAttachments()
183+
184+
verify(fixture.nativeScope).clearAttachments()
185+
}
156186
}

sentry/src/main/java/io/sentry/IScopeObserver.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,4 +45,8 @@ public interface IScopeObserver {
4545
void setTrace(@Nullable SpanContext spanContext, @NotNull IScope scope);
4646

4747
void setReplayId(@NotNull SentryId replayId);
48+
49+
void addAttachment(@NotNull Attachment attachment) {}
50+
51+
void clearAttachments() {}
4852
}

sentry/src/main/java/io/sentry/Scope.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -924,12 +924,20 @@ public List<Attachment> getAttachments() {
924924
@Override
925925
public void addAttachment(final @NotNull Attachment attachment) {
926926
attachments.add(attachment);
927+
928+
for (final IScopeObserver observer : options.getScopeObservers()) {
929+
observer.addAttachment(attachment);
930+
}
927931
}
928932

929933
/** Clear all attachments. */
930934
@Override
931935
public void clearAttachments() {
932936
attachments.clear();
937+
938+
for (final IScopeObserver observer : options.getScopeObservers()) {
939+
observer.clearAttachments();
940+
}
933941
}
934942

935943
/**

sentry/src/main/java/io/sentry/ScopeObserverAdapter.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,4 +57,10 @@ public void setTrace(@Nullable SpanContext spanContext, @NotNull IScope scope) {
5757

5858
@Override
5959
public void setReplayId(@NotNull SentryId replayId) {}
60+
61+
@Override
62+
public void addAttachment(@NotNull Attachment attachment) {}
63+
64+
@Override
65+
public void clearAttachments() {}
6066
}

0 commit comments

Comments
 (0)