Skip to content

Commit b39d6a0

Browse files
authored
Add beforeSendTransaction which allows users to filter and change transactions (#2388)
1 parent b85d8aa commit b39d6a0

File tree

13 files changed

+246
-0
lines changed

13 files changed

+246
-0
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
## Unreleased
44

5+
### Features
6+
7+
- Add beforeSendTransaction which allows users to filter and change transactions ([#2388](https://github.com/getsentry/sentry-java/pull/2388))
8+
59
### Fixes
610

711
- Use `canonicalName` in Fragment Integration for better de-obfuscation ([#2379](https://github.com/getsentry/sentry-java/pull/2379))

sentry-samples/sentry-samples-console/src/main/java/io/sentry/samples/console/Main.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,16 @@ public static void main(String[] args) throws InterruptedException {
3737
return event;
3838
});
3939

40+
options.setBeforeSendTransaction(
41+
(transaction, hint) -> {
42+
// Drop a transaction:
43+
if (transaction.getTag("SomeTransactionTag") != null) {
44+
return null;
45+
}
46+
47+
return transaction;
48+
});
49+
4050
// Allows inspecting and modifying, returning a new or simply rejecting (returning null)
4151
options.setBeforeBreadcrumb(
4252
(breadcrumb, hint) -> {

sentry-samples/sentry-samples-servlet/src/main/java/io/sentry/samples/servlet/SentryInitializer.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,17 @@ public void contextInitialized(ServletContextEvent sce) {
3535
return event;
3636
});
3737

38+
// Modifications to transaction before it goes out. Could replace the transaction
39+
// altogether
40+
options.setBeforeSendTransaction(
41+
(transaction, hint) -> {
42+
// Drop a transaction altogether:
43+
if (transaction.getTag("SomeTag") != null) {
44+
return null;
45+
}
46+
return transaction;
47+
});
48+
3849
// Allows inspecting and modifying, returning a new or simply rejecting (returning null)
3950
options.setBeforeBreadcrumb(
4051
(breadcrumb, hint) -> {

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ static class HubConfiguration {
7171
@Order(Ordered.HIGHEST_PRECEDENCE)
7272
public @NotNull Sentry.OptionsConfiguration<SentryOptions> sentryOptionsConfiguration(
7373
final @NotNull ObjectProvider<SentryOptions.BeforeSendCallback> beforeSendCallback,
74+
final @NotNull ObjectProvider<SentryOptions.BeforeSendTransactionCallback> beforeSendTransactionCallback,
7475
final @NotNull ObjectProvider<SentryOptions.BeforeBreadcrumbCallback>
7576
beforeBreadcrumbCallback,
7677
final @NotNull ObjectProvider<SentryOptions.TracesSamplerCallback> tracesSamplerCallback,
@@ -81,6 +82,7 @@ static class HubConfiguration {
8182
final @NotNull InAppIncludesResolver inAppPackagesResolver) {
8283
return options -> {
8384
beforeSendCallback.ifAvailable(options::setBeforeSend);
85+
beforeSendTransactionCallback.ifAvailable(options::setBeforeSendTransaction);
8486
beforeBreadcrumbCallback.ifAvailable(options::setBeforeBreadcrumb);
8587
tracesSamplerCallback.ifAvailable(options::setTracesSampler);
8688
eventProcessors.forEach(options::addEventProcessor);

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import io.sentry.SentryEvent
1515
import io.sentry.SentryLevel
1616
import io.sentry.SentryOptions
1717
import io.sentry.checkEvent
18+
import io.sentry.protocol.SentryTransaction
1819
import io.sentry.protocol.User
1920
import io.sentry.spring.jakarta.ContextTagsEventProcessor
2021
import io.sentry.spring.jakarta.HttpServletRequestSentryUserProvider
@@ -275,6 +276,15 @@ class SentryAutoConfigurationTest {
275276
}
276277
}
277278

279+
@Test
280+
fun `registers beforeSendTransactionCallback on SentryOptions`() {
281+
contextRunner.withPropertyValues("sentry.dsn=http://key@localhost/proj")
282+
.withUserConfiguration(CustomBeforeSendTransactionCallbackConfiguration::class.java)
283+
.run {
284+
assertThat(it.getBean(SentryOptions::class.java).beforeSendTransaction).isInstanceOf(CustomBeforeSendTransactionCallback::class.java)
285+
}
286+
}
287+
278288
@Test
279289
fun `registers beforeBreadcrumbCallback on SentryOptions`() {
280290
contextRunner.withPropertyValues("sentry.dsn=http://key@localhost/proj")
@@ -720,6 +730,17 @@ class SentryAutoConfigurationTest {
720730
override fun execute(event: SentryEvent, hint: Hint): SentryEvent? = null
721731
}
722732

733+
@Configuration(proxyBeanMethods = false)
734+
open class CustomBeforeSendTransactionCallbackConfiguration {
735+
736+
@Bean
737+
open fun beforeSendTransactionCallback() = CustomBeforeSendTransactionCallback()
738+
}
739+
740+
class CustomBeforeSendTransactionCallback : SentryOptions.BeforeSendTransactionCallback {
741+
override fun execute(event: SentryTransaction, hint: Hint): SentryTransaction? = null
742+
}
743+
723744
@Configuration(proxyBeanMethods = false)
724745
open class CustomBeforeBreadcrumbCallbackConfiguration {
725746

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,8 @@ static class HubConfiguration {
7171
@Order(Ordered.HIGHEST_PRECEDENCE)
7272
public @NotNull Sentry.OptionsConfiguration<SentryOptions> sentryOptionsConfiguration(
7373
final @NotNull ObjectProvider<SentryOptions.BeforeSendCallback> beforeSendCallback,
74+
final @NotNull ObjectProvider<SentryOptions.BeforeSendTransactionCallback>
75+
beforeSendTransactionCallback,
7476
final @NotNull ObjectProvider<SentryOptions.BeforeBreadcrumbCallback>
7577
beforeBreadcrumbCallback,
7678
final @NotNull ObjectProvider<SentryOptions.TracesSamplerCallback> tracesSamplerCallback,
@@ -81,6 +83,7 @@ static class HubConfiguration {
8183
final @NotNull InAppIncludesResolver inAppPackagesResolver) {
8284
return options -> {
8385
beforeSendCallback.ifAvailable(options::setBeforeSend);
86+
beforeSendTransactionCallback.ifAvailable(options::setBeforeSendTransaction);
8487
beforeBreadcrumbCallback.ifAvailable(options::setBeforeBreadcrumb);
8588
tracesSamplerCallback.ifAvailable(options::setTracesSampler);
8689
eventProcessors.forEach(options::addEventProcessor);

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import io.sentry.SentryEvent
1515
import io.sentry.SentryLevel
1616
import io.sentry.SentryOptions
1717
import io.sentry.checkEvent
18+
import io.sentry.protocol.SentryTransaction
1819
import io.sentry.protocol.User
1920
import io.sentry.spring.ContextTagsEventProcessor
2021
import io.sentry.spring.HttpServletRequestSentryUserProvider
@@ -275,6 +276,15 @@ class SentryAutoConfigurationTest {
275276
}
276277
}
277278

279+
@Test
280+
fun `registers beforeSendTransactionCallback on SentryOptions`() {
281+
contextRunner.withPropertyValues("sentry.dsn=http://key@localhost/proj")
282+
.withUserConfiguration(CustomBeforeSendTransactionCallbackConfiguration::class.java)
283+
.run {
284+
assertThat(it.getBean(SentryOptions::class.java).beforeSendTransaction).isInstanceOf(CustomBeforeSendTransactionCallback::class.java)
285+
}
286+
}
287+
278288
@Test
279289
fun `registers beforeBreadcrumbCallback on SentryOptions`() {
280290
contextRunner.withPropertyValues("sentry.dsn=http://key@localhost/proj")
@@ -720,6 +730,17 @@ class SentryAutoConfigurationTest {
720730
override fun execute(event: SentryEvent, hint: Hint): SentryEvent? = null
721731
}
722732

733+
@Configuration(proxyBeanMethods = false)
734+
open class CustomBeforeSendTransactionCallbackConfiguration {
735+
736+
@Bean
737+
open fun beforeSendTransactionCallback() = CustomBeforeSendTransactionCallback()
738+
}
739+
740+
class CustomBeforeSendTransactionCallback : SentryOptions.BeforeSendTransactionCallback {
741+
override fun execute(event: SentryTransaction, hint: Hint): SentryTransaction? = null
742+
}
743+
723744
@Configuration(proxyBeanMethods = false)
724745
open class CustomBeforeBreadcrumbCallbackConfiguration {
725746

sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/SentryInitBeanPostProcessor.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,9 @@ public SentryInitBeanPostProcessor() {
5555
applicationContext
5656
.getBeanProvider(SentryOptions.BeforeSendCallback.class)
5757
.ifAvailable(options::setBeforeSend);
58+
applicationContext
59+
.getBeanProvider(SentryOptions.BeforeSendTransactionCallback.class)
60+
.ifAvailable(options::setBeforeSendTransaction);
5861
applicationContext
5962
.getBeanProvider(SentryOptions.BeforeBreadcrumbCallback.class)
6063
.ifAvailable(options::setBeforeBreadcrumb);

sentry-spring/src/main/java/io/sentry/spring/SentryInitBeanPostProcessor.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,9 @@ public SentryInitBeanPostProcessor() {
5555
applicationContext
5656
.getBeanProvider(SentryOptions.BeforeSendCallback.class)
5757
.ifAvailable(options::setBeforeSend);
58+
applicationContext
59+
.getBeanProvider(SentryOptions.BeforeSendTransactionCallback.class)
60+
.ifAvailable(options::setBeforeSendTransaction);
5861
applicationContext
5962
.getBeanProvider(SentryOptions.BeforeBreadcrumbCallback.class)
6063
.ifAvailable(options::setBeforeBreadcrumb);

sentry/api/sentry.api

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1354,6 +1354,7 @@ public class io/sentry/SentryOptions {
13541354
public fun addTracingOrigin (Ljava/lang/String;)V
13551355
public fun getBeforeBreadcrumb ()Lio/sentry/SentryOptions$BeforeBreadcrumbCallback;
13561356
public fun getBeforeSend ()Lio/sentry/SentryOptions$BeforeSendCallback;
1357+
public fun getBeforeSendTransaction ()Lio/sentry/SentryOptions$BeforeSendTransactionCallback;
13571358
public fun getCacheDirPath ()Ljava/lang/String;
13581359
public fun getClientReportRecorder ()Lio/sentry/clientreport/IClientReportRecorder;
13591360
public fun getConnectionTimeoutMillis ()I
@@ -1432,6 +1433,7 @@ public class io/sentry/SentryOptions {
14321433
public fun setAttachThreads (Z)V
14331434
public fun setBeforeBreadcrumb (Lio/sentry/SentryOptions$BeforeBreadcrumbCallback;)V
14341435
public fun setBeforeSend (Lio/sentry/SentryOptions$BeforeSendCallback;)V
1436+
public fun setBeforeSendTransaction (Lio/sentry/SentryOptions$BeforeSendTransactionCallback;)V
14351437
public fun setCacheDirPath (Ljava/lang/String;)V
14361438
public fun setConnectionTimeoutMillis (I)V
14371439
public fun setDebug (Z)V
@@ -1501,6 +1503,10 @@ public abstract interface class io/sentry/SentryOptions$BeforeSendCallback {
15011503
public abstract fun execute (Lio/sentry/SentryEvent;Lio/sentry/Hint;)Lio/sentry/SentryEvent;
15021504
}
15031505

1506+
public abstract interface class io/sentry/SentryOptions$BeforeSendTransactionCallback {
1507+
public abstract fun execute (Lio/sentry/protocol/SentryTransaction;Lio/sentry/Hint;)Lio/sentry/protocol/SentryTransaction;
1508+
}
1509+
15041510
public abstract interface class io/sentry/SentryOptions$ProfilesSamplerCallback {
15051511
public abstract fun sample (Lio/sentry/SamplingContext;)Ljava/lang/Double;
15061512
}

0 commit comments

Comments
 (0)