github.com/m3db/m3@v1.5.1-0.20231129193456-75a402aa583b/src/x/sampler/sampler.go (about)

     1  // Copyright (c) 2018 Uber Technologies, Inc.
     2  //
     3  // Permission is hereby granted, free of charge, to any person obtaining a copy
     4  // of this software and associated documentation files (the "Software"), to deal
     5  // in the Software without restriction, including without limitation the rights
     6  // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
     7  // copies of the Software, and to permit persons to whom the Software is
     8  // furnished to do so, subject to the following conditions:
     9  //
    10  // The above copyright notice and this permission notice shall be included in
    11  // all copies or substantial portions of the Software.
    12  //
    13  // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    14  // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    15  // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    16  // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    17  // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    18  // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    19  // THE SOFTWARE.
    20  
    21  package sampler
    22  
    23  import (
    24  	"fmt"
    25  
    26  	"go.uber.org/atomic"
    27  )
    28  
    29  // Rate is a sample rate.
    30  type Rate float64
    31  
    32  // Value returns the float64 sample rate value.
    33  func (r Rate) Value() float64 {
    34  	return float64(r)
    35  }
    36  
    37  // Validate validates a sample rate.
    38  func (r Rate) Validate() error {
    39  	if r < 0.0 || r > 1.0 {
    40  		return fmt.Errorf("invalid sample rate: actual=%f, valid=[0.0,1.0]", r)
    41  	}
    42  	return nil
    43  }
    44  
    45  // UnmarshalYAML unmarshals a sample rate.
    46  func (r *Rate) UnmarshalYAML(unmarshal func(interface{}) error) error {
    47  	var value float64
    48  	if err := unmarshal(&value); err != nil {
    49  		return err
    50  	}
    51  
    52  	parsed := Rate(value)
    53  	if err := parsed.Validate(); err != nil {
    54  		return err
    55  	}
    56  
    57  	*r = parsed
    58  
    59  	return nil
    60  }
    61  
    62  // Sampler samples the requests, out of 100 sample calls,
    63  // 100*sampleRate calls will be sampled.
    64  type Sampler struct {
    65  	sampleRate  Rate
    66  	sampleEvery int32
    67  	numTried    *atomic.Int32
    68  }
    69  
    70  // NewSampler creates a new sampler with a sample rate.
    71  func NewSampler(sampleRate Rate) (*Sampler, error) {
    72  	if err := sampleRate.Validate(); err != nil {
    73  		return nil, err
    74  	}
    75  	if sampleRate == 0 {
    76  		return &Sampler{
    77  			sampleRate:  sampleRate,
    78  			sampleEvery: 0,
    79  		}, nil
    80  	}
    81  	return &Sampler{
    82  		sampleRate:  sampleRate,
    83  		numTried:    atomic.NewInt32(0),
    84  		sampleEvery: int32(1.0 / sampleRate),
    85  	}, nil
    86  }
    87  
    88  // Sample returns true when the call is sampled.
    89  func (t *Sampler) Sample() bool {
    90  	if t.sampleEvery == 0 {
    91  		return false
    92  	}
    93  	return (t.numTried.Inc()-1)%t.sampleEvery == 0
    94  }
    95  
    96  // SampleRate returns the effective sample rate.
    97  func (t *Sampler) SampleRate() Rate {
    98  	return t.sampleRate
    99  }
   100  
   101  // MustNewSampler returns a sampler that must be valid or otherwise panics.
   102  func MustNewSampler(sampleRate Rate) *Sampler {
   103  	s, err := NewSampler(sampleRate)
   104  	if err != nil {
   105  		panic(err)
   106  	}
   107  	return s
   108  }