forked from open-telemetry/opentelemetry-java
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RateLimitingSampler.java
82 lines (71 loc) · 2.91 KB
/
RateLimitingSampler.java
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/
package io.opentelemetry.sdk.extension.trace.jaeger.sampler;
import static io.opentelemetry.api.common.AttributeKey.doubleKey;
import static io.opentelemetry.api.common.AttributeKey.stringKey;
import io.opentelemetry.api.common.AttributeKey;
import io.opentelemetry.api.common.Attributes;
import io.opentelemetry.api.trace.SpanKind;
import io.opentelemetry.context.Context;
import io.opentelemetry.sdk.common.Clock;
import io.opentelemetry.sdk.internal.RateLimiter;
import io.opentelemetry.sdk.trace.data.LinkData;
import io.opentelemetry.sdk.trace.samplers.Sampler;
import io.opentelemetry.sdk.trace.samplers.SamplingDecision;
import io.opentelemetry.sdk.trace.samplers.SamplingResult;
import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.util.List;
/**
* {@link RateLimitingSampler} sampler uses a leaky bucket rate limiter to ensure that traces are
* sampled with a certain constant rate.
*/
class RateLimitingSampler implements Sampler {
static final String TYPE = "ratelimiting";
static final AttributeKey<String> SAMPLER_TYPE = stringKey("sampler.type");
static final AttributeKey<Double> SAMPLER_PARAM = doubleKey("sampler.param");
private final double maxTracesPerSecond;
private final RateLimiter rateLimiter;
private final SamplingResult onSamplingResult;
private final SamplingResult offSamplingResult;
/**
* Creates rate limiting sampler.
*
* @param maxTracesPerSecond the maximum number of sampled traces per second.
*/
RateLimitingSampler(int maxTracesPerSecond) {
this.maxTracesPerSecond = maxTracesPerSecond;
double maxBalance = maxTracesPerSecond < 1.0 ? 1.0 : maxTracesPerSecond;
this.rateLimiter = new RateLimiter(maxTracesPerSecond, maxBalance, Clock.getDefault());
Attributes attributes =
Attributes.of(SAMPLER_TYPE, TYPE, SAMPLER_PARAM, (double) maxTracesPerSecond);
this.onSamplingResult = SamplingResult.create(SamplingDecision.RECORD_AND_SAMPLE, attributes);
this.offSamplingResult = SamplingResult.create(SamplingDecision.DROP, attributes);
}
@Override
public SamplingResult shouldSample(
Context parentContext,
String traceId,
String name,
SpanKind spanKind,
Attributes attributes,
List<LinkData> parentLinks) {
return this.rateLimiter.trySpend(1.0) ? onSamplingResult : offSamplingResult;
}
@Override
public String getDescription() {
return "RateLimitingSampler{" + decimalFormat(maxTracesPerSecond) + "}";
}
@Override
public String toString() {
return getDescription();
}
private static String decimalFormat(double value) {
DecimalFormatSymbols decimalFormatSymbols = DecimalFormatSymbols.getInstance();
decimalFormatSymbols.setDecimalSeparator('.');
DecimalFormat decimalFormat = new DecimalFormat("0.00", decimalFormatSymbols);
return decimalFormat.format(value);
}
}