trpc.group/trpc-go/trpc-go@v1.0.3/rpcz/sampler.go (about)

     1  //
     2  //
     3  // Tencent is pleased to support the open source community by making tRPC available.
     4  //
     5  // Copyright (C) 2023 THL A29 Limited, a Tencent company.
     6  // All rights reserved.
     7  //
     8  // If you have downloaded a copy of the tRPC source code from Tencent,
     9  // please note that tRPC source code is licensed under the  Apache 2.0 License,
    10  // A copy of the Apache 2.0 License is included in this file.
    11  //
    12  //
    13  
    14  package rpcz
    15  
    16  import "math"
    17  
    18  type spanIDRatioSampler struct {
    19  	spanIDUpperBound int64
    20  }
    21  
    22  // shouldSample determines whether sampling is required.
    23  func (ss spanIDRatioSampler) shouldSample(id SpanID) bool {
    24  	return int64(id) < ss.spanIDUpperBound
    25  }
    26  
    27  // newSpanIDRatioSampler creates a spanIDRatioSampler that samples a given fraction of span.
    28  // fraction >= 1 will always sample.
    29  // fraction < 0 are treated as zero.
    30  func newSpanIDRatioSampler(fraction float64) *spanIDRatioSampler {
    31  	const maxUpperBound = math.MaxInt64
    32  
    33  	var upperBound int64
    34  	if ub := fraction * maxUpperBound; ub >= maxUpperBound {
    35  		upperBound = maxUpperBound
    36  	} else if ub >= 0 {
    37  		upperBound = int64(ub)
    38  	}
    39  
    40  	return &spanIDRatioSampler{spanIDUpperBound: upperBound}
    41  }