Home | History | Annotate | Download | only in samplers
      1 /*
      2  * Copyright 2017, OpenCensus Authors
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *     http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package io.opencensus.trace.samplers;
     18 
     19 import io.opencensus.trace.Sampler;
     20 import io.opencensus.trace.Span;
     21 
     22 /**
     23  * Static class to access a set of pre-defined {@link Sampler Samplers}.
     24  *
     25  * @since 0.5
     26  */
     27 public final class Samplers {
     28   private static final Sampler ALWAYS_SAMPLE = new AlwaysSampleSampler();
     29   private static final Sampler NEVER_SAMPLE = new NeverSampleSampler();
     30 
     31   // No instance of this class.
     32   private Samplers() {}
     33 
     34   /**
     35    * Returns a {@link Sampler} that always makes a "yes" decision on {@link Span} sampling.
     36    *
     37    * @return a {@code Sampler} that always makes a "yes" decision on {@code Span} sampling.
     38    * @since 0.5
     39    */
     40   public static Sampler alwaysSample() {
     41     return ALWAYS_SAMPLE;
     42   }
     43 
     44   /**
     45    * Returns a {@link Sampler} that always makes a "no" decision on {@link Span} sampling.
     46    *
     47    * @return a {@code Sampler} that always makes a "no" decision on {@code Span} sampling.
     48    * @since 0.5
     49    */
     50   public static Sampler neverSample() {
     51     return NEVER_SAMPLE;
     52   }
     53 
     54   /**
     55    * Returns a {@link Sampler} that makes a "yes" decision with a given probability.
     56    *
     57    * @param probability The desired probability of sampling. Must be within [0.0, 1.0].
     58    * @return a {@code Sampler} that makes a "yes" decision with a given probability.
     59    * @throws IllegalArgumentException if {@code probability} is out of range
     60    * @since 0.5
     61    */
     62   public static Sampler probabilitySampler(double probability) {
     63     return ProbabilitySampler.create(probability);
     64   }
     65 }
     66