Skip to content

Add documentation for onDiscard callback in Java, Android, and Spring Boot #14632

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 13, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion docs/platforms/android/configuration/options.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ This can be used to disable integrations that are enabled by default if the SDK

## Hooks

These options can be used to hook the SDK in various ways to customize the reporting of events.
These options can be used to hook the SDK in various ways to customize the reporting of events or to monitor the type and amount of discarded data.

<ConfigKey name="before-send">

Expand All @@ -255,6 +255,12 @@ The callback typically gets a second argument (called a "hint") which contains t

</ConfigKey>

<ConfigKey name="on-discard">

This function is called when data is discarded, for reasons including sampling, network errors, or queue overflows. The function arguments give the user insight into the reason for discarding, the type of data discarded, and the number of items discarded.

</ConfigKey>

## Transport Options

Transports are used to send events to Sentry. Transports can be customized to some degree to better support highly specific deployments.
Expand Down
8 changes: 7 additions & 1 deletion docs/platforms/java/common/configuration/options.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ In some SDKs, the integrations are configured through this parameter on library

## Hooks

These options can be used to hook the SDK in various ways to customize the reporting of events.
These options can be used to hook the SDK in various ways to customize the reporting of events or to monitor the type and amount of discarded data.

<ConfigKey name="before-send">

Expand All @@ -213,6 +213,12 @@ The callback typically gets a second argument (called a "hint") which contains t

</ConfigKey>

<ConfigKey name="on-discard">

This function is called when data is discarded, for reasons including sampling, network errors, or queue overflows. The function arguments give the user insight into the reason for discarding, the type of data discarded, and the number of items discarded.

</ConfigKey>

## Transport Options

Transports are used to send events to Sentry. Transports can be customized to some degree to better support highly specific deployments.
Expand Down
43 changes: 43 additions & 0 deletions docs/platforms/java/guides/spring-boot/advanced-usage.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -136,3 +136,46 @@ class CustomBeforeBreadcrumbCallback : SentryOptions.BeforeBreadcrumbCallback {
}
}
```

## Registering Custom On Discard Callback

Any Spring bean implementing the `OnDiscardCallback` will be automatically set on `SentryOptions` during the SDK's auto-configuration. Note that there can be only a single bean set this way.

```java
import io.sentry.SentryOptions;
import io.sentry.clientreport.DiscardReason;
import io.sentry.DataCategory;
import org.springframework.stereotype.Component;

@Component
class CustomOnDiscardCallback implements SentryOptions.OnDiscardCallback {
@Override
public void execute(DiscardReason reason, DataCategory category, Long count) {
// Only record the number of lost spans due to overflow conditions
if ((reason == DiscardReason.CACHE_OVERFLOW
|| reason == DiscardReason.QUEUE_OVERFLOW)
&& category == DataCategory.Span) {
System.out.println("Discarded " + number + " spans due to overflow.");
}
}
}
```

```kotlin
import io.sentry.SentryOptions
import io.sentry.clientreport.DiscardReason
import io.sentry.DataCategory
import org.springframework.stereotype.Component

@Component
class CustomOnDiscardCallback : SentryOptions.OnDiscardCallback {
override fun execute(reason: DiscardReason, category: DataCategory, countToAdd: Long) {
// Only record the number of lost spans due to overflow conditions
if ((reason == DiscardReason.CACHE_OVERFLOW
|| reason == DiscardReason.QUEUE_OVERFLOW)
&& category == DataCategory.Span) {
println("Discarded " + number + " spans due to overflow.")
}
}
}
```
Loading