-
Notifications
You must be signed in to change notification settings - Fork 333
Expand file tree
/
Copy pathRateSampler.java
More file actions
56 lines (43 loc) · 1.47 KB
/
RateSampler.java
File metadata and controls
56 lines (43 loc) · 1.47 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
package com.datadoghq.trace.sampling;
import com.datadoghq.trace.DDBaseSpan;
import com.google.auto.service.AutoService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* This sampler sample the traces at a predefined rate.
* <p>
* Keep (100 * `sample_rate`)% of the traces.
* It samples randomly, its main purpose is to reduce the integration footprint.
*/
@AutoService(Sampler.class)
public class RateSampler extends AbstractSampler {
private final static Logger logger = LoggerFactory.getLogger(RateSampler.class);
/**
* The sample rate used
*/
private final double sampleRate;
/**
* Build an instance of the sampler. The Sample rate is fixed for each instance.
*
* @param sampleRate a number [0,1] representing the rate ratio.
*/
public RateSampler(double sampleRate) {
if (sampleRate <= 0) {
sampleRate = 1;
logger.error("SampleRate is negative or null, disabling the sampler");
} else if (sampleRate > 1) {
sampleRate = 1;
}
this.sampleRate = sampleRate;
logger.debug("Initializing the RateSampler, sampleRate: {} %", this.sampleRate * 100);
}
@Override
public boolean doSample(DDBaseSpan<?> span) {
boolean sample = Math.random() <= this.sampleRate;
logger.debug("{} - Span is sampled: {}", span, sample);
return sample;
}
public double getSampleRate() {
return this.sampleRate;
}
}