forked from getsentry/sentry-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTracesSampler.java
More file actions
44 lines (38 loc) · 1.38 KB
/
TracesSampler.java
File metadata and controls
44 lines (38 loc) · 1.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package io.sentry;
import io.sentry.util.Objects;
import java.util.Random;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.TestOnly;
final class TracesSampler {
private final @NotNull SentryOptions options;
private final @NotNull Random random;
public TracesSampler(final @NotNull SentryOptions options) {
this(Objects.requireNonNull(options, "options are required"), new Random());
}
@TestOnly
TracesSampler(final @NotNull SentryOptions options, final @NotNull Random random) {
this.options = options;
this.random = random;
}
boolean sample(final @NotNull SamplingContext samplingContext) {
if (samplingContext.getTransactionContext().getSampled() != null) {
return samplingContext.getTransactionContext().getSampled();
}
if (options.getTracesSampler() != null) {
final Double samplerResult = options.getTracesSampler().sample(samplingContext);
if (samplerResult != null) {
return sample(samplerResult);
}
}
if (samplingContext.getTransactionContext().getParentSampled() != null) {
return samplingContext.getTransactionContext().getParentSampled();
}
if (options.getTracesSampleRate() != null) {
return sample(options.getTracesSampleRate());
}
return false;
}
private boolean sample(final @NotNull Double aDouble) {
return !(aDouble < random.nextDouble());
}
}