Class TraceIdRatioBasedSampler

java.lang.Object
io.opentelemetry.sdk.trace.samplers.TraceIdRatioBasedSampler
All Implemented Interfaces:
Sampler

@Immutable final class TraceIdRatioBasedSampler extends Object implements Sampler
We assume the lower 64 bits of the traceId's are randomly distributed around the whole (long) range. We convert an incoming probability into an upper bound on that value, such that we can just compare the absolute value of the id and the bound to see if we are within the desired probability range. Using the low bits of the traceId also ensures that systems that only use 64 bit ID's will also work with this sampler.
  • Field Details

    • POSITIVE_SAMPLING_RESULT

      private static final SamplingResult POSITIVE_SAMPLING_RESULT
    • NEGATIVE_SAMPLING_RESULT

      private static final SamplingResult NEGATIVE_SAMPLING_RESULT
    • idUpperBound

      private final long idUpperBound
    • description

      private final String description
  • Constructor Details

    • TraceIdRatioBasedSampler

      TraceIdRatioBasedSampler(double ratio, long idUpperBound)
  • Method Details

    • create

      static TraceIdRatioBasedSampler create(double ratio)
    • shouldSample

      public SamplingResult shouldSample(Context parentContext, String traceId, String name, SpanKind spanKind, Attributes attributes, List<LinkData> parentLinks)
      Description copied from interface: Sampler
      Called during Span creation to make a sampling samplingResult.
      Specified by:
      shouldSample in interface Sampler
      Parameters:
      parentContext - the parent span's SpanContext. This can be SpanContext.INVALID if this is a root span.
      traceId - the TraceId for the new Span. This will be identical to that in the parentContext, unless this is a root span.
      name - the name of the new Span.
      spanKind - the SpanKind of the Span.
      attributes - Attributes associated with the span.
      parentLinks - the parentLinks associated with the new Span.
      Returns:
      sampling samplingResult whether span should be sampled or not.
    • getDescription

      public String getDescription()
      Description copied from interface: Sampler
      Returns the description of this Sampler. This may be displayed on debug pages or in the logs.

      Example: "TraceIdRatioBased{0.000100}"

      Specified by:
      getDescription in interface Sampler
      Returns:
      the description of this Sampler.
    • equals

      public boolean equals(@Nullable Object obj)
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • getIdUpperBound

      long getIdUpperBound()
    • getTraceIdRandomPart

      private static long getTraceIdRandomPart(String traceId)
    • decimalFormat

      private static String decimalFormat(double value)