Skip to content

Commit 0852e8f

Browse files
adinauerimatwawana
andauthored
Document how to manipulate attachments via hints (#5070)
* Document how to manipulate attachments via hints * Apply suggestions from code review Co-authored-by: Isabel <[email protected]> * Update src/includes/enriching-events/add-attachment/java.mdx Co-authored-by: Isabel <[email protected]> * Apply suggestions from code review Co-authored-by: Isabel <[email protected]>
1 parent 197422e commit 0852e8f

File tree

2 files changed

+111
-1
lines changed
  • src
    • includes/enriching-events/add-attachment
    • platforms/common/enriching-events/attachments

2 files changed

+111
-1
lines changed
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
To add an attachment, you can either add it to the scope, pass it to any of the `capture` methods, or manipulate the list of attachments in an `EventProcessor` or `beforeSend`.
2+
3+
### Passing Attachments to Capture
4+
5+
You may pass attachments to any of the `capture` methods, for example, when capturing an exception:
6+
7+
```java
8+
import io.sentry.Attachment;
9+
import io.sentry.Hint;
10+
import io.sentry.Sentry;
11+
12+
Sentry.captureException(new IllegalStateException(), Hint.withAttachment("/path/to/file.txt"))
13+
```
14+
15+
```kotlin
16+
import io.sentry.Attachment;
17+
import io.sentry.Hint;
18+
import io.sentry.Sentry;
19+
20+
Sentry.captureException(IllegalStateException(), Hint.withAttachment("/path/to/file.txt"))
21+
```
22+
23+
### Adding Attachments in `EventProcessor`
24+
25+
You may also manipulate attachments in `EventProcessor`:
26+
27+
```java
28+
import io.sentry.Attachment;
29+
import io.sentry.EventProcessor;
30+
import io.sentry.Hint;
31+
import io.sentry.Sentry;
32+
import io.sentry.SentryEvent;
33+
34+
class AttachmentManipulationEventProcessor implements EventProcessor {
35+
@Override
36+
public SentryEvent process(SentryEvent event, Hint hint) {
37+
hint.addAttachment(new Attachment("/path/to/file.txt"))
38+
return event;
39+
}
40+
}
41+
42+
// Register the AttachmentManipulationEventProcessor using SentryOptions#addEventProcessor or Scope#addEventProcessor
43+
44+
// Send an event to Sentry. During construction of the event
45+
// the attachment will be added by the event processor.
46+
Sentry.captureMessage("Hello, world!");
47+
```
48+
49+
```kotlin
50+
import io.sentry.Attachment;
51+
import io.sentry.EventProcessor;
52+
import io.sentry.Hint;
53+
import io.sentry.Sentry
54+
import io.sentry.SentryEvent;
55+
import io.sentry.protocol.Message
56+
57+
class AttachmentManipulationEventProcessor: EventProcessor {
58+
override fun process(event: SentryEvent, hint: Hint): SentryEvent? {
59+
hint.addAttachment(Attachment("/path/to/file.txt"))
60+
return event
61+
}
62+
}
63+
64+
// Register the AttachmentManipulationEventProcessor using SentryOptions#addEventProcessor or Scope#addEventProcessor
65+
66+
// Send an event to Sentry. During construction of the event
67+
// the attachment will be added by the event processor.
68+
Sentry.captureMessage("Hello, world!")
69+
```
70+
71+
<Note>
72+
73+
Instead of adding attachments, you can also remove them, by setting a different (or empty) list of attachments using `Hint#setAttachments()`.
74+
75+
</Note>
76+
77+
### Adding Attachments in `beforeSend`
78+
79+
Another way of adding attachments is using `beforeSend`:
80+
81+
```java
82+
import io.sentry.Attachment;
83+
import io.sentry.Hint;
84+
import io.sentry.Sentry;
85+
86+
options.setBeforeSend((event, hint) -> {
87+
hint.addAttachment(new Attachment("/path/to/file.txt"))
88+
return event;
89+
});
90+
```
91+
92+
```kotlin
93+
import io.sentry.Attachment;
94+
import io.sentry.Hint;
95+
import io.sentry.Sentry;
96+
import io.sentry.SentryOptions.BeforeSendCallback
97+
98+
options.beforeSend = BeforeSendCallback { event, hint ->
99+
hint.addAttachment(Attachment("/path/to/file.txt"))
100+
event
101+
}
102+
```
103+
104+
<Note>
105+
106+
Instead of adding attachments, you can also remove them, by setting a different (or empty) list of attachments using `Hint#setAttachments()`.
107+
108+
</Note>
109+
110+
### Adding Attachments to the Scope

src/platforms/common/enriching-events/attachments/index.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ In addition, you can set these parameters:
8484

8585
## Uploading Attachments
8686

87-
<PlatformSection supported={["native", "javascript"]}>
87+
<PlatformSection supported={["native", "javascript", "java"]}>
8888

8989
<PlatformSection supported={["javascript", "node"]}>
9090

0 commit comments

Comments
 (0)