Skip to content

Commit e81ef92

Browse files
authored
refactor ConsistentFixedThresholdSampler to setup for dynamic alternative (#2018)
1 parent 5d702ed commit e81ef92

File tree

1 file changed

+18
-7
lines changed

1 file changed

+18
-7
lines changed

consistent-sampling/src/main/java/io/opentelemetry/contrib/sampler/consistent56/ConsistentFixedThresholdSampler.java

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
package io.opentelemetry.contrib.sampler.consistent56;
77

88
import static io.opentelemetry.contrib.sampler.consistent56.ConsistentSamplingUtil.calculateSamplingProbability;
9+
import static io.opentelemetry.contrib.sampler.consistent56.ConsistentSamplingUtil.calculateThreshold;
910
import static io.opentelemetry.contrib.sampler.consistent56.ConsistentSamplingUtil.checkThreshold;
1011
import static io.opentelemetry.contrib.sampler.consistent56.ConsistentSamplingUtil.getInvalidThreshold;
1112
import static io.opentelemetry.contrib.sampler.consistent56.ConsistentSamplingUtil.getMaxThreshold;
@@ -22,9 +23,20 @@ public class ConsistentFixedThresholdSampler extends ConsistentSampler {
2223
private final String description;
2324

2425
protected ConsistentFixedThresholdSampler(long threshold) {
26+
this.threshold = getThreshold(threshold);
27+
this.description = getThresholdDescription(threshold);
28+
}
29+
30+
protected ConsistentFixedThresholdSampler(double samplingProbability) {
31+
this(calculateThreshold(samplingProbability));
32+
}
33+
34+
private static long getThreshold(long threshold) {
2535
checkThreshold(threshold);
26-
this.threshold = threshold;
36+
return threshold;
37+
}
2738

39+
private static String getThresholdDescription(long threshold) {
2840
String thresholdString;
2941
if (threshold == getMaxThreshold()) {
3042
thresholdString = "max";
@@ -35,12 +47,11 @@ protected ConsistentFixedThresholdSampler(long threshold) {
3547
.toString();
3648
}
3749

38-
this.description =
39-
"ConsistentFixedThresholdSampler{threshold="
40-
+ thresholdString
41-
+ ", sampling probability="
42-
+ calculateSamplingProbability(threshold)
43-
+ "}";
50+
return "ConsistentFixedThresholdSampler{threshold="
51+
+ thresholdString
52+
+ ", sampling probability="
53+
+ calculateSamplingProbability(threshold)
54+
+ "}";
4455
}
4556

4657
@Override

0 commit comments

Comments
 (0)