Skip to content

Commit 912b358

Browse files
committed
Automatically use SentryOptions.Metrics.BeforeSendMetricCallback Spring beans
1 parent 5bfb646 commit 912b358

File tree

6 files changed

+78
-0
lines changed

6 files changed

+78
-0
lines changed

sentry-spring-boot-4/src/main/java/io/sentry/spring/boot4/SentryAutoConfiguration.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,8 @@ static class HubConfiguration {
105105
beforeSendTransactionCallback,
106106
final @NotNull ObjectProvider<SentryOptions.Logs.BeforeSendLogCallback>
107107
beforeSendLogsCallback,
108+
final @NotNull ObjectProvider<SentryOptions.Metrics.BeforeSendMetricCallback>
109+
beforeSendMetricCallback,
108110
final @NotNull ObjectProvider<SentryOptions.BeforeBreadcrumbCallback>
109111
beforeBreadcrumbCallback,
110112
final @NotNull ObjectProvider<SentryOptions.TracesSamplerCallback> tracesSamplerCallback,
@@ -117,6 +119,8 @@ static class HubConfiguration {
117119
beforeSendCallback.ifAvailable(options::setBeforeSend);
118120
beforeSendTransactionCallback.ifAvailable(options::setBeforeSendTransaction);
119121
beforeSendLogsCallback.ifAvailable(callback -> options.getLogs().setBeforeSend(callback));
122+
beforeSendMetricCallback.ifAvailable(
123+
callback -> options.getMetrics().setBeforeSend(callback));
120124
beforeBreadcrumbCallback.ifAvailable(options::setBeforeBreadcrumb);
121125
tracesSamplerCallback.ifAvailable(options::setTracesSampler);
122126
eventProcessors.forEach(options::addEventProcessor);

sentry-spring-boot-4/src/test/kotlin/io/sentry/spring/boot4/SentryAutoConfigurationTest.kt

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import io.sentry.SentryEvent
2121
import io.sentry.SentryIntegrationPackageStorage
2222
import io.sentry.SentryLevel
2323
import io.sentry.SentryLogEvent
24+
import io.sentry.SentryMetricsEvent
2425
import io.sentry.SentryOptions
2526
import io.sentry.asyncprofiler.profiling.JavaContinuousProfiler
2627
import io.sentry.asyncprofiler.provider.AsyncProfilerProfileConverterProvider
@@ -370,6 +371,17 @@ class SentryAutoConfigurationTest {
370371
}
371372
}
372373

374+
@Test
375+
fun `registers metrics beforeSendCallback on SentryOptions`() {
376+
contextRunner
377+
.withPropertyValues("sentry.dsn=http://key@localhost/proj")
378+
.withUserConfiguration(CustomBeforeSendMetricCallbackConfiguration::class.java)
379+
.run {
380+
assertThat(it.getBean(SentryOptions::class.java).metrics.beforeSend)
381+
.isInstanceOf(CustomBeforeSendMetricCallback::class.java)
382+
}
383+
}
384+
373385
@Test
374386
fun `registers beforeBreadcrumbCallback on SentryOptions`() {
375387
contextRunner
@@ -1240,6 +1252,16 @@ class SentryAutoConfigurationTest {
12401252
override fun execute(event: SentryLogEvent): SentryLogEvent? = null
12411253
}
12421254

1255+
@Configuration(proxyBeanMethods = false)
1256+
open class CustomBeforeSendMetricCallbackConfiguration {
1257+
1258+
@Bean open fun beforeSendCallback() = CustomBeforeSendMetricCallback()
1259+
}
1260+
1261+
class CustomBeforeSendMetricCallback : SentryOptions.Metrics.BeforeSendMetricCallback {
1262+
override fun execute(metric: SentryMetricsEvent, hint: Hint): SentryMetricsEvent? = null
1263+
}
1264+
12431265
@Configuration(proxyBeanMethods = false)
12441266
open class CustomBeforeSendTransactionCallbackConfiguration {
12451267

sentry-spring-boot-jakarta/src/main/java/io/sentry/spring/boot/jakarta/SentryAutoConfiguration.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,8 @@ static class HubConfiguration {
105105
beforeSendTransactionCallback,
106106
final @NotNull ObjectProvider<SentryOptions.Logs.BeforeSendLogCallback>
107107
beforeSendLogsCallback,
108+
final @NotNull ObjectProvider<SentryOptions.Metrics.BeforeSendMetricCallback>
109+
beforeSendMetricCallback,
108110
final @NotNull ObjectProvider<SentryOptions.BeforeBreadcrumbCallback>
109111
beforeBreadcrumbCallback,
110112
final @NotNull ObjectProvider<SentryOptions.OnDiscardCallback> onDiscardCallback,
@@ -118,6 +120,8 @@ static class HubConfiguration {
118120
beforeSendCallback.ifAvailable(options::setBeforeSend);
119121
beforeSendTransactionCallback.ifAvailable(options::setBeforeSendTransaction);
120122
beforeSendLogsCallback.ifAvailable(callback -> options.getLogs().setBeforeSend(callback));
123+
beforeSendMetricCallback.ifAvailable(
124+
callback -> options.getMetrics().setBeforeSend(callback));
121125
beforeBreadcrumbCallback.ifAvailable(options::setBeforeBreadcrumb);
122126
onDiscardCallback.ifAvailable(options::setOnDiscard);
123127
tracesSamplerCallback.ifAvailable(options::setTracesSampler);

sentry-spring-boot-jakarta/src/test/kotlin/io/sentry/spring/boot/jakarta/SentryAutoConfigurationTest.kt

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import io.sentry.SentryEvent
2323
import io.sentry.SentryIntegrationPackageStorage
2424
import io.sentry.SentryLevel
2525
import io.sentry.SentryLogEvent
26+
import io.sentry.SentryMetricsEvent
2627
import io.sentry.SentryOptions
2728
import io.sentry.asyncprofiler.profiling.JavaContinuousProfiler
2829
import io.sentry.asyncprofiler.provider.AsyncProfilerProfileConverterProvider
@@ -381,6 +382,17 @@ class SentryAutoConfigurationTest {
381382
}
382383
}
383384

385+
@Test
386+
fun `registers metrics beforeSendCallback on SentryOptions`() {
387+
contextRunner
388+
.withPropertyValues("sentry.dsn=http://key@localhost/proj")
389+
.withUserConfiguration(CustomBeforeSendMetricCallbackConfiguration::class.java)
390+
.run {
391+
assertThat(it.getBean(SentryOptions::class.java).metrics.beforeSend)
392+
.isInstanceOf(CustomBeforeSendMetricCallback::class.java)
393+
}
394+
}
395+
384396
@Test
385397
fun `registers beforeBreadcrumbCallback on SentryOptions`() {
386398
contextRunner
@@ -1262,6 +1274,16 @@ class SentryAutoConfigurationTest {
12621274
override fun execute(event: SentryLogEvent): SentryLogEvent? = null
12631275
}
12641276

1277+
@Configuration(proxyBeanMethods = false)
1278+
open class CustomBeforeSendMetricCallbackConfiguration {
1279+
1280+
@Bean open fun beforeSendCallback() = CustomBeforeSendMetricCallback()
1281+
}
1282+
1283+
class CustomBeforeSendMetricCallback : SentryOptions.Metrics.BeforeSendMetricCallback {
1284+
override fun execute(metric: SentryMetricsEvent, hint: Hint): SentryMetricsEvent? = null
1285+
}
1286+
12651287
@Configuration(proxyBeanMethods = false)
12661288
open class CustomBeforeSendTransactionCallbackConfiguration {
12671289

sentry-spring-boot/src/main/java/io/sentry/spring/boot/SentryAutoConfiguration.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,8 @@ static class HubConfiguration {
103103
beforeSendTransactionCallback,
104104
final @NotNull ObjectProvider<SentryOptions.Logs.BeforeSendLogCallback>
105105
beforeSendLogsCallback,
106+
final @NotNull ObjectProvider<SentryOptions.Metrics.BeforeSendMetricCallback>
107+
beforeSendMetricCallback,
106108
final @NotNull ObjectProvider<SentryOptions.BeforeBreadcrumbCallback>
107109
beforeBreadcrumbCallback,
108110
final @NotNull ObjectProvider<SentryOptions.OnDiscardCallback> onDiscardCallback,
@@ -116,6 +118,8 @@ static class HubConfiguration {
116118
beforeSendCallback.ifAvailable(options::setBeforeSend);
117119
beforeSendTransactionCallback.ifAvailable(options::setBeforeSendTransaction);
118120
beforeSendLogsCallback.ifAvailable(callback -> options.getLogs().setBeforeSend(callback));
121+
beforeSendMetricCallback.ifAvailable(
122+
callback -> options.getMetrics().setBeforeSend(callback));
119123
beforeBreadcrumbCallback.ifAvailable(options::setBeforeBreadcrumb);
120124
onDiscardCallback.ifAvailable(options::setOnDiscard);
121125
tracesSamplerCallback.ifAvailable(options::setTracesSampler);

sentry-spring-boot/src/test/kotlin/io/sentry/spring/boot/SentryAutoConfigurationTest.kt

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import io.sentry.SentryEvent
2323
import io.sentry.SentryIntegrationPackageStorage
2424
import io.sentry.SentryLevel
2525
import io.sentry.SentryLogEvent
26+
import io.sentry.SentryMetricsEvent
2627
import io.sentry.SentryOptions
2728
import io.sentry.asyncprofiler.profiling.JavaContinuousProfiler
2829
import io.sentry.asyncprofiler.provider.AsyncProfilerProfileConverterProvider
@@ -369,6 +370,17 @@ class SentryAutoConfigurationTest {
369370
}
370371
}
371372

373+
@Test
374+
fun `registers metrics beforeSendCallback on SentryOptions`() {
375+
contextRunner
376+
.withPropertyValues("sentry.dsn=http://key@localhost/proj")
377+
.withUserConfiguration(CustomBeforeSendMetricCallbackConfiguration::class.java)
378+
.run {
379+
assertThat(it.getBean(SentryOptions::class.java).metrics.beforeSend)
380+
.isInstanceOf(CustomBeforeSendMetricCallback::class.java)
381+
}
382+
}
383+
372384
@Test
373385
fun `registers beforeSendTransactionCallback on SentryOptions`() {
374386
contextRunner
@@ -1188,6 +1200,16 @@ class SentryAutoConfigurationTest {
11881200
override fun execute(event: SentryLogEvent): SentryLogEvent? = null
11891201
}
11901202

1203+
@Configuration(proxyBeanMethods = false)
1204+
open class CustomBeforeSendMetricCallbackConfiguration {
1205+
1206+
@Bean open fun beforeSendCallback() = CustomBeforeSendMetricCallback()
1207+
}
1208+
1209+
class CustomBeforeSendMetricCallback : SentryOptions.Metrics.BeforeSendMetricCallback {
1210+
override fun execute(metric: SentryMetricsEvent, hint: Hint): SentryMetricsEvent? = null
1211+
}
1212+
11911213
@Configuration(proxyBeanMethods = false)
11921214
open class CustomBeforeSendTransactionCallbackConfiguration {
11931215

0 commit comments

Comments
 (0)