From bd99b4626200846b3ae6959ca6a9c5cf3da1867c Mon Sep 17 00:00:00 2001 From: Alexander Alderman Webb Date: Tue, 12 Aug 2025 15:15:57 +0200 Subject: [PATCH 1/2] Add documentation for onDiscard callback in Java, Android, and Spring Boot --- .../android/configuration/options.mdx | 8 ++++- .../java/common/configuration/options.mdx | 8 ++++- .../guides/spring-boot/advanced-usage.mdx | 29 +++++++++++++++++++ 3 files changed, 43 insertions(+), 2 deletions(-) diff --git a/docs/platforms/android/configuration/options.mdx b/docs/platforms/android/configuration/options.mdx index 72f905bb05daec..47090b69eeee9b 100644 --- a/docs/platforms/android/configuration/options.mdx +++ b/docs/platforms/android/configuration/options.mdx @@ -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. @@ -255,6 +255,12 @@ The callback typically gets a second argument (called a "hint") which contains t + + +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. + + + ## Transport Options Transports are used to send events to Sentry. Transports can be customized to some degree to better support highly specific deployments. diff --git a/docs/platforms/java/common/configuration/options.mdx b/docs/platforms/java/common/configuration/options.mdx index ee73ae47d2e26b..ea41473ed78e5f 100644 --- a/docs/platforms/java/common/configuration/options.mdx +++ b/docs/platforms/java/common/configuration/options.mdx @@ -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. @@ -213,6 +213,12 @@ The callback typically gets a second argument (called a "hint") which contains t + + +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. + + + ## Transport Options Transports are used to send events to Sentry. Transports can be customized to some degree to better support highly specific deployments. diff --git a/docs/platforms/java/guides/spring-boot/advanced-usage.mdx b/docs/platforms/java/guides/spring-boot/advanced-usage.mdx index 8bcff925f92f04..e43d9ef922bcd1 100644 --- a/docs/platforms/java/guides/spring-boot/advanced-usage.mdx +++ b/docs/platforms/java/guides/spring-boot/advanced-usage.mdx @@ -136,3 +136,32 @@ 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) {} +} +``` + +```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) {} +} +``` From 2ecda7058f0f5d815ae98f433fcd95b37c344b85 Mon Sep 17 00:00:00 2001 From: Alexander Alderman Webb Date: Tue, 12 Aug 2025 15:27:33 +0200 Subject: [PATCH 2/2] add onDiscard example implementation --- .../java/guides/spring-boot/advanced-usage.mdx | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/docs/platforms/java/guides/spring-boot/advanced-usage.mdx b/docs/platforms/java/guides/spring-boot/advanced-usage.mdx index e43d9ef922bcd1..84ef08c5775918 100644 --- a/docs/platforms/java/guides/spring-boot/advanced-usage.mdx +++ b/docs/platforms/java/guides/spring-boot/advanced-usage.mdx @@ -150,7 +150,14 @@ import org.springframework.stereotype.Component; @Component class CustomOnDiscardCallback implements SentryOptions.OnDiscardCallback { @Override - public void execute(DiscardReason reason, DataCategory category, Long count) {} + 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."); + } + } } ``` @@ -162,6 +169,13 @@ import org.springframework.stereotype.Component @Component class CustomOnDiscardCallback : SentryOptions.OnDiscardCallback { - override fun execute(reason: DiscardReason, category: DataCategory, countToAdd: Long) {} + 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.") + } + } } ```