Skip to content

Commit d0502db

Browse files
feat: Add addToTransactions to Attachment (#1191)
Add extra parameter addToTransaction to attachment, which specifies if the SDK adds it to transactions or not. The default is false. Fixes GH-1185
1 parent 9c6af50 commit d0502db

File tree

6 files changed

+112
-7
lines changed

6 files changed

+112
-7
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# vNext
22

3+
* Feat: Add addToTransactions to Attachment (#1191)
34
* Enhancement: Support SENTRY_TRACES_SAMPLE_RATE conf. via env variables (#1171)
45
* Enhancement: Pass request to CustomSamplingContext in Spring integration (#1172)
56
* Ref: Set SpanContext on SentryTransaction to avoid potential NPE (#1173)

sentry/api/sentry.api

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,10 @@ public final class io/sentry/Attachment {
77
public fun <init> (Ljava/lang/String;)V
88
public fun <init> (Ljava/lang/String;Ljava/lang/String;)V
99
public fun <init> (Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)V
10+
public fun <init> (Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Z)V
1011
public fun <init> ([BLjava/lang/String;)V
1112
public fun <init> ([BLjava/lang/String;Ljava/lang/String;)V
13+
public fun <init> ([BLjava/lang/String;Ljava/lang/String;Z)V
1214
public fun getBytes ()[B
1315
public fun getContentType ()Ljava/lang/String;
1416
public fun getFilename ()Ljava/lang/String;

sentry/src/main/java/io/sentry/Attachment.java

Lines changed: 60 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ public final class Attachment {
1313
private @Nullable String pathname;
1414
private final @NotNull String filename;
1515
private final @NotNull String contentType;
16+
private final boolean addToTransactions;
1617

1718
/**
1819
* We could use Files.probeContentType(path) to determine the content type of the filename. This
@@ -24,7 +25,8 @@ public final class Attachment {
2425
private static final String DEFAULT_CONTENT_TYPE = "application/octet-stream";
2526

2627
/**
27-
* Initializes an Attachment with bytes and a filename.
28+
* Initializes an Attachment with bytes and a filename. Sets addToTransaction to <code>false
29+
* </code>.
2830
*
2931
* @param bytes The bytes of file.
3032
* @param filename The name of the attachment to display in Sentry.
@@ -34,7 +36,8 @@ public Attachment(final @NotNull byte[] bytes, final @NotNull String filename) {
3436
}
3537

3638
/**
37-
* Initializes an Attachment with bytes, filename and content type.
39+
* Initializes an Attachment with bytes, a filename, and a content type. Sets addToTransaction to
40+
* <code>false</code>.
3841
*
3942
* @param bytes The bytes of file.
4043
* @param filename The name of the attachment to display in Sentry.
@@ -44,13 +47,32 @@ public Attachment(
4447
final @NotNull byte[] bytes,
4548
final @NotNull String filename,
4649
final @NotNull String contentType) {
50+
this(bytes, filename, contentType, false);
51+
}
52+
53+
/**
54+
* Initializes an Attachment with bytes, a filename, a content type, and addToTransactions.
55+
*
56+
* @param bytes The bytes of file.
57+
* @param filename The name of the attachment to display in Sentry.
58+
* @param contentType The content type of the attachment.
59+
* @param addToTransactions <code>true</code> if the SDK should add this attachment to every
60+
* {@link ITransaction} or set to <code>false</code> if it shouldn't.
61+
*/
62+
public Attachment(
63+
final @NotNull byte[] bytes,
64+
final @NotNull String filename,
65+
final @NotNull String contentType,
66+
final boolean addToTransactions) {
4767
this.bytes = bytes;
4868
this.filename = filename;
4969
this.contentType = contentType;
70+
this.addToTransactions = addToTransactions;
5071
}
5172

5273
/**
5374
* Initializes an Attachment with a path. The filename of the file located at the path is used.
75+
* Sets addToTransaction to <code>false</code>.
5476
*
5577
* <p>The file located at the pathname is read lazily when the SDK captures an event or
5678
* transaction not when the attachment is initialized. The pathname string is converted into an
@@ -63,7 +85,8 @@ public Attachment(final @NotNull String pathname) {
6385
}
6486

6587
/**
66-
* Initializes an Attachment with a path and a filename.
88+
* Initializes an Attachment with a path and a filename. Sets addToTransaction to <code>false
89+
* </code>.
6790
*
6891
* <p>The file located at the pathname is read lazily when the SDK captures an event or
6992
* transaction not when the attachment is initialized. The pathname string is converted into an
@@ -77,7 +100,8 @@ public Attachment(final @NotNull String pathname, final @NotNull String filename
77100
}
78101

79102
/**
80-
* Initializes an Attachment with a path, a filename and a content type.
103+
* Initializes an Attachment with a path, a filename, and a content type. Sets addToTransaction to
104+
* <code>false</code>.
81105
*
82106
* <p>The file located at the pathname is read lazily when the SDK captures an event or
83107
* transaction not when the attachment is initialized. The pathname string is converted into an
@@ -91,9 +115,31 @@ public Attachment(
91115
final @NotNull String pathname,
92116
final @NotNull String filename,
93117
final @NotNull String contentType) {
118+
this(pathname, filename, contentType, false);
119+
}
120+
121+
/**
122+
* Initializes an Attachment with a path, a filename, a content type, and addToTransactions.
123+
*
124+
* <p>The file located at the pathname is read lazily when the SDK captures an event or
125+
* transaction not when the attachment is initialized. The pathname string is converted into an
126+
* abstract pathname before reading the file.
127+
*
128+
* @param pathname The pathname string of the file to upload as an attachment.
129+
* @param filename The name of the attachment to display in Sentry.
130+
* @param contentType The content type of the attachment.
131+
* @param addToTransactions <code>true</code> if the SDK should add this attachment to every
132+
* {@link ITransaction} or set to <code>false</code> if it shouldn't.
133+
*/
134+
public Attachment(
135+
final @NotNull String pathname,
136+
final @NotNull String filename,
137+
final @NotNull String contentType,
138+
final boolean addToTransactions) {
94139
this.pathname = pathname;
95140
this.filename = filename;
96141
this.contentType = contentType;
142+
this.addToTransactions = addToTransactions;
97143
}
98144

99145
/**
@@ -131,4 +177,14 @@ public Attachment(
131177
public @NotNull String getContentType() {
132178
return contentType;
133179
}
180+
181+
/**
182+
* Returns <code>true</code> if the SDK should add this attachment to every {@link ITransaction}
183+
* and <code>false</code> if it shouldn't. Default is <code>false</code>.
184+
*
185+
* @return <code>true</code> if attachment should be added to every {@link ITransaction}.
186+
*/
187+
boolean isAddToTransactions() {
188+
return addToTransactions;
189+
}
134190
}

sentry/src/main/java/io/sentry/SentryClient.java

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -357,7 +357,7 @@ public void captureSession(final @NotNull Session session, final @Nullable Objec
357357
processTransaction((SentryTransaction) transaction);
358358
try {
359359
final SentryEnvelope envelope =
360-
buildEnvelope(sentryTransaction, getAttachmentsFromScope(scope));
360+
buildEnvelope(sentryTransaction, filterForTransaction(getAttachmentsFromScope(scope)));
361361
if (envelope != null) {
362362
transport.send(envelope, hint);
363363
} else {
@@ -377,6 +377,21 @@ public void captureSession(final @NotNull Session session, final @Nullable Objec
377377
return sentryId;
378378
}
379379

380+
private @Nullable List<Attachment> filterForTransaction(@Nullable List<Attachment> attachments) {
381+
if (attachments == null) {
382+
return null;
383+
}
384+
385+
List<Attachment> attachmentsToSend = new ArrayList<>();
386+
for (Attachment attachment : attachments) {
387+
if (attachment.isAddToTransactions()) {
388+
attachmentsToSend.add(attachment);
389+
}
390+
}
391+
392+
return attachmentsToSend;
393+
}
394+
380395
private @NotNull SentryTransaction processTransaction(
381396
final @NotNull SentryTransaction transaction) {
382397
if (transaction.getRelease() == null) {

sentry/src/test/java/io/sentry/AttachmentTest.kt

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ package io.sentry
22

33
import kotlin.test.Test
44
import kotlin.test.assertEquals
5+
import kotlin.test.assertFalse
56
import kotlin.test.assertNull
7+
import kotlin.test.assertTrue
68

79
class AttachmentTest {
810

@@ -71,4 +73,22 @@ class AttachmentTest {
7173
val byteAttachment = Attachment(fixture.bytes, fixture.filename, fixture.contentType)
7274
assertEquals(fixture.contentType, byteAttachment.contentType)
7375
}
76+
77+
@Test
78+
fun `default of addToTransactions is false`() {
79+
val fileAttachment = Attachment(fixture.filename)
80+
assertFalse(fileAttachment.isAddToTransactions)
81+
82+
val byteAttachment = Attachment(fixture.bytes, fixture.filename)
83+
assertFalse(byteAttachment.isAddToTransactions)
84+
}
85+
86+
@Test
87+
fun `set addToTransactions`() {
88+
val fileAttachment = Attachment(fixture.pathname, fixture.filename, fixture.contentType, true)
89+
assertTrue(fileAttachment.isAddToTransactions)
90+
91+
val byteAttachment = Attachment(fixture.bytes, fixture.filename, fixture.contentType, true)
92+
assertTrue(byteAttachment.isAddToTransactions)
93+
}
7494
}

sentry/src/test/java/io/sentry/SentryClientTest.kt

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ class SentryClientTest {
6969
whenever(factory.create(any(), any())).thenReturn(transport)
7070
}
7171

72-
var attachment = Attachment("hello".toByteArray(), "hello.txt")
72+
var attachment = Attachment("hello".toByteArray(), "hello.txt", "text/plain", true)
7373

7474
fun getSut() = SentryClient(sentryOptions)
7575
}
@@ -763,6 +763,17 @@ class SentryClientTest {
763763
verifyAttachmentsInEnvelope(transaction.eventId)
764764
}
765765

766+
@Test
767+
fun `when captureTransaction with attachments not added to transaction`() {
768+
val transaction = SentryTransaction("a-transaction")
769+
770+
val scope = createScopeWithAttachments()
771+
scope.addAttachment(Attachment("hello".toByteArray(), "application/octet-stream"))
772+
fixture.getSut().captureTransaction(transaction, scope, null)
773+
774+
verifyAttachmentsInEnvelope(transaction.eventId)
775+
}
776+
766777
@Test
767778
fun `when scope's active span is a transaction, transaction context is applied to an event`() {
768779
val event = SentryEvent()
@@ -834,7 +845,7 @@ class SentryClientTest {
834845
addAttachment(fixture.attachment)
835846

836847
val bytesTooBig = ByteArray((fixture.maxAttachmentSize + 1).toInt()) { 0 }
837-
addAttachment(Attachment(bytesTooBig, "will_get_dropped.txt"))
848+
addAttachment(Attachment(bytesTooBig, "will_get_dropped.txt", "application/octet-stream", true))
838849
}
839850
}
840851

0 commit comments

Comments
 (0)