Skip to content
Merged
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.sentry.android.ndk;

import io.sentry.Attachment;
import io.sentry.Breadcrumb;
import io.sentry.DateUtils;
import io.sentry.IScope;
Expand Down Expand Up @@ -145,4 +146,31 @@ public void setTrace(@Nullable SpanContext spanContext, @NotNull IScope scope) {
options.getLogger().log(SentryLevel.ERROR, e, "Scope sync setTrace failed.");
}
}

@Override
public void addAttachment(final @NotNull Attachment attachment) {
final String pathname = attachment.getPathname();
if (pathname == null) {
// Only file-path attachments are getting synced to the native layer right now.
options
.getLogger()
.log(SentryLevel.DEBUG, "Scope sync addAttachment skips non-file attachment.");
return;
}
Comment thread
sentry[bot] marked this conversation as resolved.

try {
options.getExecutorService().submit(() -> nativeScope.addAttachment(pathname));
} catch (Throwable e) {
options.getLogger().log(SentryLevel.ERROR, e, "Scope sync addAttachment has an error.");
}
}
Comment thread
bitsandfoxes marked this conversation as resolved.

@Override
public void clearAttachments() {
try {
options.getExecutorService().submit(() -> nativeScope.clearAttachments());
} catch (Throwable e) {
options.getLogger().log(SentryLevel.ERROR, e, "Scope sync clearAttachments has an error.");
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.sentry.android.ndk

import io.sentry.Attachment
import io.sentry.Breadcrumb
import io.sentry.DateUtils
import io.sentry.JsonSerializer
Expand Down Expand Up @@ -153,4 +154,33 @@ class NdkScopeObserverTest {
verify(fixture.nativeScope)
.addBreadcrumb(anyOrNull(), anyOrNull(), anyOrNull(), anyOrNull(), anyOrNull(), anyOrNull())
}

@Test
fun `add file-path attachment syncs to native scope`() {
val sut = fixture.getSut()

val attachment = Attachment("/data/data/com.example/files/log.txt")
sut.addAttachment(attachment)

verify(fixture.nativeScope).addAttachment("/data/data/com.example/files/log.txt")
}

@Test
fun `add byte attachment does not sync to native scope`() {
val sut = fixture.getSut()

val attachment = Attachment(byteArrayOf(1, 2, 3), "data.bin")
sut.addAttachment(attachment)

verify(fixture.nativeScope, never()).addAttachment(any())
}

@Test
fun `clear attachments forwards call to native scope`() {
val sut = fixture.getSut()

sut.clearAttachments()

verify(fixture.nativeScope).clearAttachments()
}
}
4 changes: 4 additions & 0 deletions sentry/src/main/java/io/sentry/IScopeObserver.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,8 @@ public interface IScopeObserver {
void setTrace(@Nullable SpanContext spanContext, @NotNull IScope scope);

void setReplayId(@NotNull SentryId replayId);

void addAttachment(@NotNull Attachment attachment) {}

void clearAttachments() {}
}
8 changes: 8 additions & 0 deletions sentry/src/main/java/io/sentry/Scope.java
Original file line number Diff line number Diff line change
Expand Up @@ -924,12 +924,20 @@ public List<Attachment> getAttachments() {
@Override
public void addAttachment(final @NotNull Attachment attachment) {
attachments.add(attachment);

for (final IScopeObserver observer : options.getScopeObservers()) {
observer.addAttachment(attachment);
}
}

/** Clear all attachments. */
@Override
public void clearAttachments() {
attachments.clear();

for (final IScopeObserver observer : options.getScopeObservers()) {
observer.clearAttachments();
}
}

/**
Expand Down
6 changes: 6 additions & 0 deletions sentry/src/main/java/io/sentry/ScopeObserverAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,10 @@ public void setTrace(@Nullable SpanContext spanContext, @NotNull IScope scope) {

@Override
public void setReplayId(@NotNull SentryId replayId) {}

@Override
public void addAttachment(@NotNull Attachment attachment) {}

@Override
public void clearAttachments() {}
}