-
Notifications
You must be signed in to change notification settings - Fork 178
Expand file tree
/
Copy pathConsistentVariableThresholdSampler.java
More file actions
60 lines (49 loc) · 1.94 KB
/
ConsistentVariableThresholdSampler.java
File metadata and controls
60 lines (49 loc) · 1.94 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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/
package io.opentelemetry.contrib.sampler.consistent56;
import static io.opentelemetry.contrib.sampler.consistent56.ConsistentSamplingUtil.calculateSamplingProbability;
import static io.opentelemetry.contrib.sampler.consistent56.ConsistentSamplingUtil.calculateThreshold;
import static io.opentelemetry.contrib.sampler.consistent56.ConsistentSamplingUtil.checkThreshold;
import static io.opentelemetry.contrib.sampler.consistent56.ConsistentSamplingUtil.getMaxThreshold;
public class ConsistentVariableThresholdSampler extends ConsistentThresholdSampler {
private volatile long threshold;
private volatile String description = "";
protected ConsistentVariableThresholdSampler(double samplingProbability) {
updateSamplingProbability(samplingProbability);
}
@Override
public String getDescription() {
return description;
}
@Override
public long getThreshold() {
return threshold;
}
public void setSamplingProbability(double samplingProbability) {
updateSamplingProbability(samplingProbability);
}
private void updateSamplingProbability(double samplingProbability) {
long threshold = calculateThreshold(samplingProbability);
checkThreshold(threshold);
this.threshold = threshold;
String thresholdString;
if (threshold == getMaxThreshold()) {
thresholdString = "max";
} else {
thresholdString =
ConsistentSamplingUtil.appendLast56BitHexEncodedWithoutTrailingZeros(
new StringBuilder(), threshold)
.toString();
}
// tiny eventual consistency where the description would be out of date with the threshold,
// but this doesn't really matter
this.description =
"ConsistentVariableThresholdSampler{threshold="
+ thresholdString
+ ", sampling probability="
+ calculateSamplingProbability(threshold)
+ "}";
}
}