sentry-java icon indicating copy to clipboard operation
sentry-java copied to clipboard

Support dynamic error sampling

Open markushi opened this issue 1 year ago • 0 comments

Description

Just like TracesSamplerCallback, we want to have a callback capable sampling errors. This feature already exists in our Python SDK.

sentry_sdk.init(
    # ...

    error_sampler=my_sampler,
)

Right now the workaround would be to keep hold of the options and change the sampleRate after the SDK has been initialized. This should work just fine, as SentryClient reads out the sample rate every time, but is still discouraged, as we don't officially support changing options on the fly.

final AtomicReference<SentryOptions> optionsRef = new AtomicReference<>();
Sentry.init(new Sentry.OptionsConfiguration<SentryOptions>() {
  @Override
  public void configure(@NotNull SentryOptions options) {
    options.setDsn("...");
    options.setSampleRate(1.0); // some sane default
    // ...
    optionsRef.set(options);
  }
});

// later, dynamically update the sampleRate
private void onRemoteConfigChanged(final @NotNull RemoteConfig remoteConfig){
  final @Nullable SentryOptions options = optionsRef.get();
  if (options != null) {
    options.setSampleRate(remoteConfig.sampleRate);
  }
}

markushi avatar Jul 19 '24 05:07 markushi