io.sentry.TracesSampler Maven / Gradle / Ivy
package io.sentry;
import io.sentry.util.Objects;
import java.security.SecureRandom;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.annotations.TestOnly;
final class TracesSampler {
private static final @NotNull Double DEFAULT_TRACES_SAMPLE_RATE = 1.0;
private final @NotNull SentryOptions options;
private final @NotNull SecureRandom random;
public TracesSampler(final @NotNull SentryOptions options) {
this(Objects.requireNonNull(options, "options are required"), new SecureRandom());
}
@TestOnly
TracesSampler(final @NotNull SentryOptions options, final @NotNull SecureRandom random) {
this.options = options;
this.random = random;
}
@NotNull
TracesSamplingDecision sample(final @NotNull SamplingContext samplingContext) {
final TracesSamplingDecision samplingContextSamplingDecision =
samplingContext.getTransactionContext().getSamplingDecision();
if (samplingContextSamplingDecision != null) {
return samplingContextSamplingDecision;
}
Double profilesSampleRate = null;
if (options.getProfilesSampler() != null) {
try {
profilesSampleRate = options.getProfilesSampler().sample(samplingContext);
} catch (Throwable t) {
options
.getLogger()
.log(SentryLevel.ERROR, "Error in the 'ProfilesSamplerCallback' callback.", t);
}
}
if (profilesSampleRate == null) {
profilesSampleRate = options.getProfilesSampleRate();
}
Boolean profilesSampled = profilesSampleRate != null && sample(profilesSampleRate);
if (options.getTracesSampler() != null) {
Double samplerResult = null;
try {
samplerResult = options.getTracesSampler().sample(samplingContext);
} catch (Throwable t) {
options
.getLogger()
.log(SentryLevel.ERROR, "Error in the 'TracesSamplerCallback' callback.", t);
}
if (samplerResult != null) {
return new TracesSamplingDecision(
sample(samplerResult), samplerResult, profilesSampled, profilesSampleRate);
}
}
final TracesSamplingDecision parentSamplingDecision =
samplingContext.getTransactionContext().getParentSamplingDecision();
if (parentSamplingDecision != null) {
return parentSamplingDecision;
}
final @Nullable Double tracesSampleRateFromOptions = options.getTracesSampleRate();
final @Nullable Boolean isEnableTracing = options.getEnableTracing();
final @Nullable Double defaultSampleRate =
Boolean.TRUE.equals(isEnableTracing) ? DEFAULT_TRACES_SAMPLE_RATE : null;
final @Nullable Double tracesSampleRateOrDefault =
tracesSampleRateFromOptions == null ? defaultSampleRate : tracesSampleRateFromOptions;
final @NotNull Double downsampleFactor =
Math.pow(2, options.getBackpressureMonitor().getDownsampleFactor());
final @Nullable Double downsampledTracesSampleRate =
tracesSampleRateOrDefault == null ? null : tracesSampleRateOrDefault / downsampleFactor;
if (downsampledTracesSampleRate != null) {
return new TracesSamplingDecision(
sample(downsampledTracesSampleRate),
downsampledTracesSampleRate,
profilesSampled,
profilesSampleRate);
}
return new TracesSamplingDecision(false, null, false, null);
}
private boolean sample(final @NotNull Double aDouble) {
return !(aDouble < random.nextDouble());
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy