forked from open-telemetry/opentelemetry-java-contrib
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConsistentThresholdSampler.java
More file actions
60 lines (50 loc) · 1.93 KB
/
ConsistentThresholdSampler.java
File metadata and controls
60 lines (50 loc) · 1.93 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.appendLast56BitHexEncodedWithoutTrailingZeros;
import static io.opentelemetry.contrib.sampler.consistent56.ConsistentSamplingUtil.calculateSamplingProbability;
import static io.opentelemetry.contrib.sampler.consistent56.ConsistentSamplingUtil.checkThreshold;
import static io.opentelemetry.contrib.sampler.consistent56.ConsistentSamplingUtil.getInvalidThreshold;
import static io.opentelemetry.contrib.sampler.consistent56.ConsistentSamplingUtil.getMaxThreshold;
import io.opentelemetry.api.common.Attributes;
import io.opentelemetry.api.trace.SpanKind;
import io.opentelemetry.context.Context;
import io.opentelemetry.sdk.trace.data.LinkData;
import java.util.List;
public abstract class ConsistentThresholdSampler extends ConsistentSampler {
protected abstract long getThreshold();
protected static long getThreshold(long threshold) {
checkThreshold(threshold);
return threshold;
}
protected static String getThresholdDescription(long threshold) {
String thresholdString;
if (threshold == getMaxThreshold()) {
thresholdString = "max";
} else {
thresholdString =
appendLast56BitHexEncodedWithoutTrailingZeros(new StringBuilder(), threshold).toString();
}
return "ConsistentFixedThresholdSampler{threshold="
+ thresholdString
+ ", sampling probability="
+ calculateSamplingProbability(threshold)
+ "}";
}
@Override
public SamplingIntent getSamplingIntent(
Context parentContext,
String name,
SpanKind spanKind,
Attributes attributes,
List<LinkData> parentLinks) {
return () -> {
if (getThreshold() == getMaxThreshold()) {
return getInvalidThreshold();
}
return getThreshold();
};
}
}