Class Sampling


  • @API(EXPERIMENTAL)
    public final class Sampling
    extends java.lang.Object
    Utility methods and classes to improve the construction of various different sampling methods.
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static Sampler eventSampler​(int maxSamplesPerEvent, long eventRefreshInterval)
      Samples events based on a fixed throughput of samples per events.
      static Sampler newSampler​(int maxSamplesPerUnit, java.util.concurrent.TimeUnit timeUnit)
      Samples events based on a psuedo-fixed long-term throughput of samples per time unit.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Method Detail

      • newSampler

        public static Sampler newSampler​(int maxSamplesPerUnit,
                                         java.util.concurrent.TimeUnit timeUnit)
        Samples events based on a psuedo-fixed long-term throughput of samples per time unit.

        Over the long term, this will allow roughly maxSamplesPerUnit samples every timeUnit unit of time that passes. If the throughput of the events being sampled is lower than this throughput, then all events will be sampled, if it is higher, then some will be rejected. If the event traffic is bursty in time (that is, a bunch of events occur close together in time), they may be sampled at the cost of future traffic being throttled to keep the overall average throughput the same.

        Parameters:
        maxSamplesPerUnit - the maximum number of samples to allow per unit time
        timeUnit - the time unit to compute within.
        Returns:
        a sampler which will sample events aiming at a long-term rate of maxSamplesPerUnit samples per unit of timeUnit
      • eventSampler

        public static Sampler eventSampler​(int maxSamplesPerEvent,
                                           long eventRefreshInterval)
        Samples events based on a fixed throughput of samples per events.

        This should provide maxSamplesPerEvent samples/event, gaining one sample every eventRefreshInterval. However, because this is event sampling, and not time sampling, the returned sampler will not really allow any burst-sampling, it will more-or-less confine itself to a fixed rate. However, if the events are input in a burst, then this sampler will sample in bursts as well.

        Parameters:
        maxSamplesPerEvent - the maximum number of samples to allow per event.
        eventRefreshInterval - how many events to wait before refreshing the token bucket. This is to amortize the cost of refreshing the token counter across many requests.