Skip to content

Commit 1528dfb

Browse files
authored
Automatically use SentryOptions.Logs.BeforeSendLogCallback Spring beans (#4509)
* Add user id, username and email to log attributes * wip * Check log event count before sending envelope * changelog * Min event level config option; enable logs in logback sample * Flush logs when client flushes * Improve log flushing and add test * review feedback * Add logs to Spring Boot logback config * treat unformatted message and params as pii if logback encoder is present * add tests for pii handling * Auto pick up logs.beforeSend from Spring beans * changelog
1 parent 8489642 commit 1528dfb

File tree

5 files changed

+51
-0
lines changed

5 files changed

+51
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
```properties
5353
logs.enabled=true
5454
```
55+
- Automatically use `SentryOptions.Logs.BeforeSendLogCallback` Spring beans ([#4509](https://github.com/getsentry/sentry-java/pull/4509))
5556

5657
### Dependencies
5758

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,8 @@ static class HubConfiguration {
103103
final @NotNull ObjectProvider<SentryOptions.BeforeSendCallback> beforeSendCallback,
104104
final @NotNull ObjectProvider<SentryOptions.BeforeSendTransactionCallback>
105105
beforeSendTransactionCallback,
106+
final @NotNull ObjectProvider<SentryOptions.Logs.BeforeSendLogCallback>
107+
beforeSendLogsCallback,
106108
final @NotNull ObjectProvider<SentryOptions.BeforeBreadcrumbCallback>
107109
beforeBreadcrumbCallback,
108110
final @NotNull ObjectProvider<SentryOptions.TracesSamplerCallback> tracesSamplerCallback,
@@ -114,6 +116,7 @@ static class HubConfiguration {
114116
return options -> {
115117
beforeSendCallback.ifAvailable(options::setBeforeSend);
116118
beforeSendTransactionCallback.ifAvailable(options::setBeforeSendTransaction);
119+
beforeSendLogsCallback.ifAvailable(callback -> options.getLogs().setBeforeSend(callback));
117120
beforeBreadcrumbCallback.ifAvailable(options::setBeforeBreadcrumb);
118121
tracesSamplerCallback.ifAvailable(options::setTracesSampler);
119122
eventProcessors.forEach(options::addEventProcessor);

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
@@ -16,6 +16,7 @@ import io.sentry.Sentry
1616
import io.sentry.SentryEvent
1717
import io.sentry.SentryIntegrationPackageStorage
1818
import io.sentry.SentryLevel
19+
import io.sentry.SentryLogEvent
1920
import io.sentry.SentryOptions
2021
import io.sentry.checkEvent
2122
import io.sentry.opentelemetry.SentryAutoConfigurationCustomizerProvider
@@ -350,6 +351,17 @@ class SentryAutoConfigurationTest {
350351
}
351352
}
352353

354+
@Test
355+
fun `registers logs beforeSendCallback on SentryOptions`() {
356+
contextRunner
357+
.withPropertyValues("sentry.dsn=http://key@localhost/proj")
358+
.withUserConfiguration(CustomBeforeSendLogsCallbackConfiguration::class.java)
359+
.run {
360+
assertThat(it.getBean(SentryOptions::class.java).logs.beforeSend)
361+
.isInstanceOf(CustomBeforeSendLogsCallback::class.java)
362+
}
363+
}
364+
353365
@Test
354366
fun `registers beforeBreadcrumbCallback on SentryOptions`() {
355367
contextRunner
@@ -1095,6 +1107,16 @@ class SentryAutoConfigurationTest {
10951107
override fun execute(event: SentryEvent, hint: Hint): SentryEvent? = null
10961108
}
10971109

1110+
@Configuration(proxyBeanMethods = false)
1111+
open class CustomBeforeSendLogsCallbackConfiguration {
1112+
1113+
@Bean open fun beforeSendCallback() = CustomBeforeSendLogsCallback()
1114+
}
1115+
1116+
class CustomBeforeSendLogsCallback : SentryOptions.Logs.BeforeSendLogCallback {
1117+
override fun execute(event: SentryLogEvent): SentryLogEvent? = null
1118+
}
1119+
10981120
@Configuration(proxyBeanMethods = false)
10991121
open class CustomBeforeSendTransactionCallbackConfiguration {
11001122

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,8 @@ static class HubConfiguration {
101101
final @NotNull ObjectProvider<SentryOptions.BeforeSendCallback> beforeSendCallback,
102102
final @NotNull ObjectProvider<SentryOptions.BeforeSendTransactionCallback>
103103
beforeSendTransactionCallback,
104+
final @NotNull ObjectProvider<SentryOptions.Logs.BeforeSendLogCallback>
105+
beforeSendLogsCallback,
104106
final @NotNull ObjectProvider<SentryOptions.BeforeBreadcrumbCallback>
105107
beforeBreadcrumbCallback,
106108
final @NotNull ObjectProvider<SentryOptions.TracesSamplerCallback> tracesSamplerCallback,
@@ -112,6 +114,7 @@ static class HubConfiguration {
112114
return options -> {
113115
beforeSendCallback.ifAvailable(options::setBeforeSend);
114116
beforeSendTransactionCallback.ifAvailable(options::setBeforeSendTransaction);
117+
beforeSendLogsCallback.ifAvailable(callback -> options.getLogs().setBeforeSend(callback));
115118
beforeBreadcrumbCallback.ifAvailable(options::setBeforeBreadcrumb);
116119
tracesSamplerCallback.ifAvailable(options::setTracesSampler);
117120
eventProcessors.forEach(options::addEventProcessor);

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
@@ -16,6 +16,7 @@ import io.sentry.Sentry
1616
import io.sentry.SentryEvent
1717
import io.sentry.SentryIntegrationPackageStorage
1818
import io.sentry.SentryLevel
19+
import io.sentry.SentryLogEvent
1920
import io.sentry.SentryOptions
2021
import io.sentry.checkEvent
2122
import io.sentry.opentelemetry.SentryAutoConfigurationCustomizerProvider
@@ -338,6 +339,17 @@ class SentryAutoConfigurationTest {
338339
}
339340
}
340341

342+
@Test
343+
fun `registers logs beforeSendCallback on SentryOptions`() {
344+
contextRunner
345+
.withPropertyValues("sentry.dsn=http://key@localhost/proj")
346+
.withUserConfiguration(CustomBeforeSendLogsCallbackConfiguration::class.java)
347+
.run {
348+
assertThat(it.getBean(SentryOptions::class.java).logs.beforeSend)
349+
.isInstanceOf(CustomBeforeSendLogsCallback::class.java)
350+
}
351+
}
352+
341353
@Test
342354
fun `registers beforeSendTransactionCallback on SentryOptions`() {
343355
contextRunner
@@ -1021,6 +1033,16 @@ class SentryAutoConfigurationTest {
10211033
override fun execute(event: SentryEvent, hint: Hint): SentryEvent? = null
10221034
}
10231035

1036+
@Configuration(proxyBeanMethods = false)
1037+
open class CustomBeforeSendLogsCallbackConfiguration {
1038+
1039+
@Bean open fun beforeSendCallback() = CustomBeforeSendLogsCallback()
1040+
}
1041+
1042+
class CustomBeforeSendLogsCallback : SentryOptions.Logs.BeforeSendLogCallback {
1043+
override fun execute(event: SentryLogEvent): SentryLogEvent? = null
1044+
}
1045+
10241046
@Configuration(proxyBeanMethods = false)
10251047
open class CustomBeforeSendTransactionCallbackConfiguration {
10261048

0 commit comments

Comments
 (0)