Skip to content

Commit 4d16452

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

File tree

12 files changed

+87
-9
lines changed

12 files changed

+87
-9
lines changed

sentry-samples/sentry-samples-spring-7/src/main/java/io/sentry/samples/spring7/web/MetricController.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,13 @@ String count() {
2020
}
2121

2222
@GetMapping("gauge/{count}")
23-
String gauge(@PathVariable Long count) {
23+
String gauge(@PathVariable("count") Long count) {
2424
Sentry.metrics().gauge("memory.free", count.doubleValue(), "byte");
2525
return "gauge metric tracked";
2626
}
2727

2828
@GetMapping("distribution/{count}")
29-
String distribution(@PathVariable Long count) {
29+
String distribution(@PathVariable("count") Long count) {
3030
Sentry.metrics().distribution("distributionMetric", count.doubleValue(), "child");
3131
return "distribution metric tracked";
3232
}

sentry-samples/sentry-samples-spring-7/src/test/kotlin/io/sentry/systemtest/MetricsSystemTest.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ class MetricsSystemTest {
1010

1111
@Before
1212
fun setup() {
13-
testHelper = TestHelper("http://localhost:8080")
13+
testHelper = TestHelper("http://localhost:8080/sentry-samples-spring-7-0.0.1-SNAPSHOT")
1414
testHelper.reset()
1515
}
1616

sentry-samples/sentry-samples-spring-jakarta/src/main/java/io/sentry/samples/spring/jakarta/web/MetricController.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,13 @@ String count() {
2020
}
2121

2222
@GetMapping("gauge/{count}")
23-
String gauge(@PathVariable Long count) {
23+
String gauge(@PathVariable("count") Long count) {
2424
Sentry.metrics().gauge("memory.free", count.doubleValue(), "byte");
2525
return "gauge metric tracked";
2626
}
2727

2828
@GetMapping("distribution/{count}")
29-
String distribution(@PathVariable Long count) {
29+
String distribution(@PathVariable("count") Long count) {
3030
Sentry.metrics().distribution("distributionMetric", count.doubleValue(), "child");
3131
return "distribution metric tracked";
3232
}

sentry-samples/sentry-samples-spring-jakarta/src/test/kotlin/io/sentry/systemtest/MetricsSystemTest.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ class MetricsSystemTest {
1010

1111
@Before
1212
fun setup() {
13-
testHelper = TestHelper("http://localhost:8080")
13+
testHelper = TestHelper("http://localhost:8080/sentry-samples-spring-jakarta-0.0.1-SNAPSHOT")
1414
testHelper.reset()
1515
}
1616

sentry-samples/sentry-samples-spring/src/main/java/io/sentry/samples/spring/web/MetricController.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,13 @@ String count() {
2020
}
2121

2222
@GetMapping("gauge/{count}")
23-
String gauge(@PathVariable Long count) {
23+
String gauge(@PathVariable("count") Long count) {
2424
Sentry.metrics().gauge("memory.free", count.doubleValue(), "byte");
2525
return "gauge metric tracked";
2626
}
2727

2828
@GetMapping("distribution/{count}")
29-
String distribution(@PathVariable Long count) {
29+
String distribution(@PathVariable("count") Long count) {
3030
Sentry.metrics().distribution("distributionMetric", count.doubleValue(), "child");
3131
return "distribution metric tracked";
3232
}

sentry-samples/sentry-samples-spring/src/test/kotlin/io/sentry/systemtest/MetricsSystemTest.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ class MetricsSystemTest {
1010

1111
@Before
1212
fun setup() {
13-
testHelper = TestHelper("http://localhost:8080")
13+
testHelper = TestHelper("http://localhost:8080/sentry-samples-spring-0.0.1-SNAPSHOT")
1414
testHelper.reset()
1515
}
1616

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

0 commit comments

Comments
 (0)