Search code examples
javaspringlogginglog4jsentry

Spring Sentry Integration. Event processor doesn't work (prevent health check)


I need to prevent sending some events by condition. (health check)

I'm using spring libraries:

implementation "io.sentry:sentry-spring-boot-starter:5.4.1"
implementation "io.sentry:sentry-logback:5.4.1"

It works, sentry has a lot of transactions etc. The configuration is:

sentry:
  dsn: https://....ingest.sentry.io/....
  traces-sample-rate: 1.0

I'm going to prevent some events which has health check data using event processor: https://docs.sentry.io/platforms/java/guides/spring-boot/advanced-usage/#registering-custom-event-processor

@Component
public class SentryCallbackExtension implements EventProcessor {

    @Value("${app.sentry.exclude.transactions}")
    private List<String> excludes;

    @Override
    public @Nullable SentryEvent process(@NotNull SentryEvent event, @Nullable Object hint) {
        return StringUtils.containsAnyIgnoreCase(
            event.getTransaction(), excludes.toArray(new String[0])
        ) ? null : event;
    }
}

Configuration for this block is:

app:
  sentry:
    exclude:
      transactions: "/actuator/health"

And it doesn't work. There are a lot of health check events in sentry service. enter image description here


Solution

  • I used TracesSamplerCallback (https://docs.sentry.io/platforms/java/guides/spring-boot/configuration/filtering/#using-sampling-to-filter-transaction-events) and it helps. It can prevent all events/transactions (not only custom from application).

    @Component
    public class SentryTraceSamplerCallback implements TracesSamplerCallback {
    
        /**
         * Excludes transaction paths
         */
        @Value("${tn.sentry.exclude.transactions}")
        private List<String> excludes;
    
        @Override
        @SuppressWarnings("ConstantConditions")
        public @Nullable Double sample(@NotNull SamplingContext context) {
            HttpServletRequest request = (HttpServletRequest) context.getCustomSamplingContext().get("request");
            String url = request.getRequestURI();
            return StringUtils.containsAnyIgnoreCase(url, this.excludes.toArray(String[]::new)) ? 0.0D : 1.0D;
        }
    }
    

    enter image description here