Skip to content

Commit 1baf5ed

Browse files
Add documentation for onDiscard callback in Java, Android, and Spring Boot (#14632)
1 parent 65a4ea7 commit 1baf5ed

File tree

3 files changed

+57
-2
lines changed

3 files changed

+57
-2
lines changed

docs/platforms/android/configuration/options.mdx

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ This can be used to disable integrations that are enabled by default if the SDK
231231

232232
## Hooks
233233

234-
These options can be used to hook the SDK in various ways to customize the reporting of events.
234+
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.
235235

236236
<ConfigKey name="before-send">
237237

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

256256
</ConfigKey>
257257

258+
<ConfigKey name="on-discard">
259+
260+
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.
261+
262+
</ConfigKey>
263+
258264
## Transport Options
259265

260266
Transports are used to send events to Sentry. Transports can be customized to some degree to better support highly specific deployments.

docs/platforms/java/common/configuration/options.mdx

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ In some SDKs, the integrations are configured through this parameter on library
190190

191191
## Hooks
192192

193-
These options can be used to hook the SDK in various ways to customize the reporting of events.
193+
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.
194194

195195
<ConfigKey name="before-send">
196196

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

214214
</ConfigKey>
215215

216+
<ConfigKey name="on-discard">
217+
218+
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.
219+
220+
</ConfigKey>
221+
216222
## Transport Options
217223

218224
Transports are used to send events to Sentry. Transports can be customized to some degree to better support highly specific deployments.

docs/platforms/java/guides/spring-boot/advanced-usage.mdx

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,3 +136,46 @@ class CustomBeforeBreadcrumbCallback : SentryOptions.BeforeBreadcrumbCallback {
136136
}
137137
}
138138
```
139+
140+
## Registering Custom On Discard Callback
141+
142+
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.
143+
144+
```java
145+
import io.sentry.SentryOptions;
146+
import io.sentry.clientreport.DiscardReason;
147+
import io.sentry.DataCategory;
148+
import org.springframework.stereotype.Component;
149+
150+
@Component
151+
class CustomOnDiscardCallback implements SentryOptions.OnDiscardCallback {
152+
@Override
153+
public void execute(DiscardReason reason, DataCategory category, Long count) {
154+
// Only record the number of lost spans due to overflow conditions
155+
if ((reason == DiscardReason.CACHE_OVERFLOW
156+
|| reason == DiscardReason.QUEUE_OVERFLOW)
157+
&& category == DataCategory.Span) {
158+
System.out.println("Discarded " + number + " spans due to overflow.");
159+
}
160+
}
161+
}
162+
```
163+
164+
```kotlin
165+
import io.sentry.SentryOptions
166+
import io.sentry.clientreport.DiscardReason
167+
import io.sentry.DataCategory
168+
import org.springframework.stereotype.Component
169+
170+
@Component
171+
class CustomOnDiscardCallback : SentryOptions.OnDiscardCallback {
172+
override fun execute(reason: DiscardReason, category: DataCategory, countToAdd: Long) {
173+
// Only record the number of lost spans due to overflow conditions
174+
if ((reason == DiscardReason.CACHE_OVERFLOW
175+
|| reason == DiscardReason.QUEUE_OVERFLOW)
176+
&& category == DataCategory.Span) {
177+
println("Discarded " + number + " spans due to overflow.")
178+
}
179+
}
180+
}
181+
```

0 commit comments

Comments
 (0)