Spring哨兵整合。事件处理器不工作(阻止健康检查)

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

我需要阻止按条件发送某些事件。 (健康检查)

我正在使用 spring 库:

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

有效,哨兵有很多事务等。配置是:

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

我将使用事件处理器阻止一些具有健康检查数据的事件: 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;
    }
}

此块的配置是:

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

而且它不起作用。哨兵服务中有很多健康检查事件。

EventProcessor接口有两个方法:

  • SentryEvent process(SentryEvent event, Object hint) - 用于处理事件
  • SentryTransaction process(SentryTransaction transaction, Object hint) - 用于处理交易

由于要过滤掉从对健康端点的请求创建的事务,因此需要实现以 SentryTransaction 作为第一个参数的第二个方法。

我使用了 TracesSamplerCallback (https://docs.sentry.io/platforms/java/guides/spring-boot/configuration/filtering/#using-sampling-to-filter-transaction-events),它很有帮助。它可以防止所有 events/transactions(不仅来自应用程序的自定义)。

@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;
    }
}