From 8723583b3828f179b4ce90f13880ad6440a0b786 Mon Sep 17 00:00:00 2001 From: Alexander Dinauer Date: Tue, 24 May 2022 11:20:31 +0200 Subject: [PATCH 1/4] Document how to manipulate attachments via hints --- .../enriching-events/add-attachment/java.mdx | 119 ++++++++++++++++++ .../enriching-events/attachments/index.mdx | 2 +- 2 files changed, 120 insertions(+), 1 deletion(-) create mode 100644 src/includes/enriching-events/add-attachment/java.mdx diff --git a/src/includes/enriching-events/add-attachment/java.mdx b/src/includes/enriching-events/add-attachment/java.mdx new file mode 100644 index 0000000000000..c640ff4cccda6 --- /dev/null +++ b/src/includes/enriching-events/add-attachment/java.mdx @@ -0,0 +1,119 @@ +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`. + + +### Passing attachments to capture + +You may pass attachments to any of the `capture` methods, e.g. when capturing an Exception: + +```java +import io.sentry.Attachment; +import io.sentry.Hint; +import io.sentry.Sentry; + +Sentry.captureException(new IllegalStateException(), Hint.withAttachment("/path/to/file.txt")) +``` + +```kotlin +import io.sentry.Attachment; +import io.sentry.Hint; +import io.sentry.Sentry; + +Sentry.captureException(IllegalStateException(), Hint.withAttachment("/path/to/file.txt")) +``` + + +### Adding attachments in EventProcessor + +You may also manipulate attachments in `EventProcessor`: + +```java +import io.sentry.Attachment; +import io.sentry.EventProcessor; +import io.sentry.Hint; +import io.sentry.Sentry; +import io.sentry.SentryEvent; +import io.sentry.protocol.Message; + +public class MyClass { + public void myMethod() { + // Add an `EventProcessor` to the current scope. Note that + // this helper will process *all* future events in this scope. + Sentry.configureScope(scope -> { + scope.addEventProcessor(new EventProcessor() { + @Override + public SentryEvent process(SentryEvent event, Hint hint) { + hint.addAttachment(new Attachment("/path/to/file.txt")) + return event; + } + }); + }); + + // Send an event to Sentry. During construction of the event + // the attachment will be added by the event processor. + Sentry.captureMessage("Hello, world!"); + } +} +``` + +```kotlin +import io.sentry.Attachment; +import io.sentry.EventProcessor; +import io.sentry.Hint; +import io.sentry.Sentry +import io.sentry.SentryEvent; +import io.sentry.protocol.Message + +class MyClass { + fun myMethod() { + // Add an `EventProcessor` to the current scope. Note that + // this helper will process *all* future events in this scope. + Sentry.configureScope { scope -> + scope.addEventProcessor(object: EventProcessor { + override fun process(event: SentryEvent, hint: Hint): SentryEvent? { + hint.addAttachment(Attachment("/path/to/file.txt")) + return event + } + }) + } + + // Send an event to Sentry. During construction of the event + // the attachment will be added by the event processor. + Sentry.captureMessage("Hello, world!") + } +} +``` + + +### Adding attachments in beforeSend + +Another way of adding attachments is using `beforeSend`: + +```java +import io.sentry.Attachment; +import io.sentry.Hint; +import io.sentry.Sentry; + +Sentry.init(options -> { + options.setBeforeSend((event, hint) -> { + hint.addAttachment(new Attachment("/path/to/file.txt")) + return event; + }); +}); +``` + +```kotlin +import io.sentry.Attachment; +import io.sentry.Hint; +import io.sentry.Sentry; +import io.sentry.SentryOptions.BeforeSendCallback + +Sentry.init { options -> + options.beforeSend = BeforeSendCallback { event, hint -> + hint.addAttachment(Attachment("/path/to/file.txt")) + event + } +} +``` + + +### Adding attachments to the scope diff --git a/src/platforms/common/enriching-events/attachments/index.mdx b/src/platforms/common/enriching-events/attachments/index.mdx index efb38de644dcf..18cba267b84e9 100644 --- a/src/platforms/common/enriching-events/attachments/index.mdx +++ b/src/platforms/common/enriching-events/attachments/index.mdx @@ -84,7 +84,7 @@ In addition, you can set these parameters: ## Uploading Attachments - + From 1273af15f24e2cb93564da677f652f21dbdf9340 Mon Sep 17 00:00:00 2001 From: Alexander Dinauer Date: Wed, 25 May 2022 06:59:50 +0200 Subject: [PATCH 2/4] Apply suggestions from code review Co-authored-by: Isabel <76437239+imatwawana@users.noreply.github.com> --- src/includes/enriching-events/add-attachment/java.mdx | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/includes/enriching-events/add-attachment/java.mdx b/src/includes/enriching-events/add-attachment/java.mdx index c640ff4cccda6..3cc314248e948 100644 --- a/src/includes/enriching-events/add-attachment/java.mdx +++ b/src/includes/enriching-events/add-attachment/java.mdx @@ -1,7 +1,7 @@ -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`. +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`. -### Passing attachments to capture +### Passing Attachments to Capture You may pass attachments to any of the `capture` methods, e.g. when capturing an Exception: @@ -22,7 +22,7 @@ Sentry.captureException(IllegalStateException(), Hint.withAttachment("/path/to/f ``` -### Adding attachments in EventProcessor +### Adding Attachments in `EventProcessor` You may also manipulate attachments in `EventProcessor`: @@ -84,7 +84,7 @@ class MyClass { ``` -### Adding attachments in beforeSend +### Adding Attachments in `beforeSend` Another way of adding attachments is using `beforeSend`: @@ -116,4 +116,4 @@ Sentry.init { options -> ``` -### Adding attachments to the scope +### Adding Attachments to the Scope From 806f29bb1e951e9d4cd03bc13ab47dffcc71c621 Mon Sep 17 00:00:00 2001 From: Alexander Dinauer Date: Wed, 25 May 2022 07:00:10 +0200 Subject: [PATCH 3/4] Update src/includes/enriching-events/add-attachment/java.mdx Co-authored-by: Isabel <76437239+imatwawana@users.noreply.github.com> --- src/includes/enriching-events/add-attachment/java.mdx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/includes/enriching-events/add-attachment/java.mdx b/src/includes/enriching-events/add-attachment/java.mdx index 3cc314248e948..2c9b3d61e8e0b 100644 --- a/src/includes/enriching-events/add-attachment/java.mdx +++ b/src/includes/enriching-events/add-attachment/java.mdx @@ -3,7 +3,7 @@ To add an attachment, you can either add it to the scope, pass it to any of the ### Passing Attachments to Capture -You may pass attachments to any of the `capture` methods, e.g. when capturing an Exception: +You may pass attachments to any of the `capture` methods, for example, when capturing an exception: ```java import io.sentry.Attachment; From f8e989480090f37f1fc4ec00460d89451787b644 Mon Sep 17 00:00:00 2001 From: Alexander Dinauer Date: Wed, 25 May 2022 07:16:23 +0200 Subject: [PATCH 4/4] Apply suggestions from code review --- .../enriching-events/add-attachment/java.mdx | 85 +++++++++---------- 1 file changed, 38 insertions(+), 47 deletions(-) diff --git a/src/includes/enriching-events/add-attachment/java.mdx b/src/includes/enriching-events/add-attachment/java.mdx index 2c9b3d61e8e0b..f381778d51142 100644 --- a/src/includes/enriching-events/add-attachment/java.mdx +++ b/src/includes/enriching-events/add-attachment/java.mdx @@ -1,6 +1,5 @@ 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`. - ### Passing Attachments to Capture You may pass attachments to any of the `capture` methods, for example, when capturing an exception: @@ -21,7 +20,6 @@ import io.sentry.Sentry; Sentry.captureException(IllegalStateException(), Hint.withAttachment("/path/to/file.txt")) ``` - ### Adding Attachments in `EventProcessor` You may also manipulate attachments in `EventProcessor`: @@ -32,27 +30,20 @@ import io.sentry.EventProcessor; import io.sentry.Hint; import io.sentry.Sentry; import io.sentry.SentryEvent; -import io.sentry.protocol.Message; - -public class MyClass { - public void myMethod() { - // Add an `EventProcessor` to the current scope. Note that - // this helper will process *all* future events in this scope. - Sentry.configureScope(scope -> { - scope.addEventProcessor(new EventProcessor() { - @Override - public SentryEvent process(SentryEvent event, Hint hint) { - hint.addAttachment(new Attachment("/path/to/file.txt")) - return event; - } - }); - }); - - // Send an event to Sentry. During construction of the event - // the attachment will be added by the event processor. - Sentry.captureMessage("Hello, world!"); + +class AttachmentManipulationEventProcessor implements EventProcessor { + @Override + public SentryEvent process(SentryEvent event, Hint hint) { + hint.addAttachment(new Attachment("/path/to/file.txt")) + return event; } } + +// Register the AttachmentManipulationEventProcessor using SentryOptions#addEventProcessor or Scope#addEventProcessor + +// Send an event to Sentry. During construction of the event +// the attachment will be added by the event processor. +Sentry.captureMessage("Hello, world!"); ``` ```kotlin @@ -63,26 +54,25 @@ import io.sentry.Sentry import io.sentry.SentryEvent; import io.sentry.protocol.Message -class MyClass { - fun myMethod() { - // Add an `EventProcessor` to the current scope. Note that - // this helper will process *all* future events in this scope. - Sentry.configureScope { scope -> - scope.addEventProcessor(object: EventProcessor { - override fun process(event: SentryEvent, hint: Hint): SentryEvent? { - hint.addAttachment(Attachment("/path/to/file.txt")) - return event - } - }) - } - - // Send an event to Sentry. During construction of the event - // the attachment will be added by the event processor. - Sentry.captureMessage("Hello, world!") +class AttachmentManipulationEventProcessor: EventProcessor { + override fun process(event: SentryEvent, hint: Hint): SentryEvent? { + hint.addAttachment(Attachment("/path/to/file.txt")) + return event } } + +// Register the AttachmentManipulationEventProcessor using SentryOptions#addEventProcessor or Scope#addEventProcessor + +// Send an event to Sentry. During construction of the event +// the attachment will be added by the event processor. +Sentry.captureMessage("Hello, world!") ``` + + +Instead of adding attachments, you can also remove them, by setting a different (or empty) list of attachments using `Hint#setAttachments()`. + + ### Adding Attachments in `beforeSend` @@ -93,11 +83,9 @@ import io.sentry.Attachment; import io.sentry.Hint; import io.sentry.Sentry; -Sentry.init(options -> { - options.setBeforeSend((event, hint) -> { - hint.addAttachment(new Attachment("/path/to/file.txt")) - return event; - }); +options.setBeforeSend((event, hint) -> { + hint.addAttachment(new Attachment("/path/to/file.txt")) + return event; }); ``` @@ -107,13 +95,16 @@ import io.sentry.Hint; import io.sentry.Sentry; import io.sentry.SentryOptions.BeforeSendCallback -Sentry.init { options -> - options.beforeSend = BeforeSendCallback { event, hint -> - hint.addAttachment(Attachment("/path/to/file.txt")) - event - } +options.beforeSend = BeforeSendCallback { event, hint -> + hint.addAttachment(Attachment("/path/to/file.txt")) + event } ``` + + +Instead of adding attachments, you can also remove them, by setting a different (or empty) list of attachments using `Hint#setAttachments()`. + + ### Adding Attachments to the Scope