Skip to content

Commit 9e5eb4c

Browse files
Fix: how to configure BeforeSend callback with Spring (#4629)
1 parent a0df384 commit 9e5eb4c

File tree

4 files changed

+101
-1
lines changed

4 files changed

+101
-1
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
```java
2+
import io.sentry.SentryEvent;
3+
import io.sentry.SentryOptions;
4+
import org.springframework.stereotype.Component;
5+
6+
@Component
7+
public class CustomBeforeSendCallback implements SentryOptions.BeforeSendCallback {
8+
@Override
9+
public SentryEvent execute(SentryEvent event, Object hint) {
10+
if (event.getThrowable() instanceof SQLException) {
11+
event.setFingerprints(Arrays.asList("database-connection-error"));
12+
}
13+
return event;
14+
}
15+
}
16+
```
17+
18+
```kotlin
19+
import io.sentry.SentryEvent
20+
import io.sentry.SentryOptions
21+
import org.springframework.stereotype.Component
22+
23+
@Component
24+
class CustomBeforeSendCallback : SentryOptions.BeforeSendCallback {
25+
override fun execute(event: SentryEvent, hint: Any?): SentryEvent? {
26+
if (event.throwable is SQLException) {
27+
event.fingerprints = listOf("database-connection-error")
28+
}
29+
event
30+
}
31+
}
32+
```
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
A `BiFunction<SentryEvent, Object, SentryEvent>` can be used to mutate, discard (return null), or return a completely new event.
2+
3+
```java
4+
import io.sentry.SentryEvent;
5+
import io.sentry.SentryOptions;
6+
import org.springframework.stereotype.Component;
7+
8+
@Component
9+
public class CustomBeforeSendCallback implements SentryOptions.BeforeSendCallback {
10+
@Override
11+
public SentryEvent execute(SentryEvent event, Object hint) {
12+
if (hint instanceof MyHint) {
13+
return null;
14+
} else {
15+
return event;
16+
}
17+
}
18+
}
19+
```
20+
21+
```kotlin
22+
import io.sentry.SentryEvent
23+
import io.sentry.SentryOptions
24+
import org.springframework.stereotype.Component
25+
26+
@Component
27+
class CustomBeforeSendCallback : SentryOptions.BeforeSendCallback {
28+
override fun execute(event: SentryEvent, hint: Any?): SentryEvent? {
29+
if (hint is MyHint) {
30+
null
31+
} else {
32+
event
33+
}
34+
}
35+
}
36+
```

src/includes/configuration/before-send/java.spring-boot.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
`beforeSend` callback can be registered simply by creating a Spring bean implementing the `BeforeSendCallback`.
1+
`beforeSend` callback can be registered by creating a Spring bean implementing the `BeforeSendCallback`.
22

33
```java
44
import io.sentry.SentryEvent;
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
`beforeSend` callback can be registered by creating a Spring bean implementing the `BeforeSendCallback`.
2+
3+
```java
4+
import io.sentry.SentryEvent;
5+
import io.sentry.SentryOptions;
6+
import org.springframework.stereotype.Component;
7+
8+
@Component
9+
public class CustomBeforeSendCallback implements SentryOptions.BeforeSendCallback {
10+
@Override
11+
public SentryEvent execute(SentryEvent event, Object hint) {
12+
// Example: Never send server name in events
13+
event.setServerName(null);
14+
return event;
15+
}
16+
}
17+
```
18+
19+
```kotlin
20+
import io.sentry.SentryEvent
21+
import io.sentry.SentryOptions
22+
import org.springframework.stereotype.Component
23+
24+
@Component
25+
class CustomBeforeSendCallback : SentryOptions.BeforeSendCallback {
26+
override fun execute(event: SentryEvent, hint: Any?): SentryEvent? {
27+
// Example: Never send server name in events
28+
event.serverName = null
29+
return event
30+
}
31+
}
32+
```

0 commit comments

Comments
 (0)