Class ProbabilitySampler
- java.lang.Object
-
- co.elastic.apm.agent.impl.sampling.ProbabilitySampler
-
- All Implemented Interfaces:
Sampler
public class ProbabilitySampler extends java.lang.Object implements Sampler
This implementation ofSamplersamples based on a sampling probability (or sampling rate) between 0.0 and 1.0.A sampling rate of 0.5 means that 50% of all transactions should be sampled.
Implementation notes:
We are taking advantage of the fact, that theIdis randomly generated. So instead of generating another random number, we just see if the long value returned byId.getLeastSignificantBits()falls into the range between thelowerBoundand thehigherBound. This is a visual representation of the mechanism with a sampling rate of 0.5 (=50%):Long.MIN_VALUE 0 Long.MAX_VALUE v v v [----------[----------|----------]----------] ^ ^ lowerBound higherBound = Long.MAX_VALUE * samplingRate = Long.MAX_VALUE * samplingRate * -1
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description doublegetSampleRate()java.lang.StringgetTraceStateHeader()booleanisSampled(Id traceId)Determines whether the given transaction should be sampled.static Samplerof(double samplingRate)
-
-
-
Method Detail
-
of
public static Sampler of(double samplingRate)
-
isSampled
public boolean isSampled(Id traceId)
Description copied from interface:SamplerDetermines whether the given transaction should be sampled.
-
getSampleRate
public double getSampleRate()
- Specified by:
getSampleRatein interfaceSampler- Returns:
- current sample rate
-
getTraceStateHeader
public java.lang.String getTraceStateHeader()
- Specified by:
getTraceStateHeaderin interfaceSampler- Returns:
- sample rate as (constant) header for context propagation
While the
tracestateheader is not related to sampler itself, putting this here allows to reuse the sameStringinstance as long as the sample rate does not change to minimize allocation
-
-