github.com/vseinstrumentiru/lego@v1.0.2/internal/lego/monitor/tracer/config.go (about)

     1  package tracer
     2  
     3  import (
     4  	"emperror.dev/errors"
     5  	"github.com/spf13/pflag"
     6  	"github.com/spf13/viper"
     7  	lego2 "github.com/vseinstrumentiru/lego/internal/lego"
     8  	"go.opencensus.io/trace"
     9  	"strings"
    10  )
    11  
    12  type Config struct {
    13  	lego2.WithSwitch `mapstructure:",squash"`
    14  
    15  	// Sampling describes the default sampler used when creating new spans.
    16  	Sampling struct {
    17  		Sampler  string
    18  		Fraction float64
    19  	}
    20  	// MaxAnnotationEventsPerSpan is max number of annotation events per span.
    21  	MaxAnnotationEventsPerSpan int
    22  	// MaxMessageEventsPerSpan is max number of message events per span.
    23  	MaxMessageEventsPerSpan int
    24  	// MaxAnnotationEventsPerSpan is max number of attributes per span.
    25  	MaxAttributesPerSpan int
    26  	// MaxLinksPerSpan is max number of links per span.
    27  	MaxLinksPerSpan int
    28  }
    29  
    30  func (c Config) SetDefaults(env *viper.Viper, flag *pflag.FlagSet) {
    31  	env.SetDefault("srv.monitor.trace.sampling.sampler", "never")
    32  }
    33  
    34  func (c Config) Validate() (err error) {
    35  	if !c.Enabled {
    36  		return
    37  	}
    38  
    39  	if c.Sampling.Sampler != "always" && c.Sampling.Sampler != "never" && c.Sampling.Sampler != "probability" {
    40  		err = errors.Append(err, errors.New("srv.monitor.trace.sampling.sampler must be on of [always, never, probability]"))
    41  	}
    42  
    43  	return
    44  }
    45  
    46  func (c Config) Config() trace.Config {
    47  	config := trace.Config{
    48  		MaxAnnotationEventsPerSpan: c.MaxAnnotationEventsPerSpan,
    49  		MaxMessageEventsPerSpan:    c.MaxMessageEventsPerSpan,
    50  		MaxAttributesPerSpan:       c.MaxAttributesPerSpan,
    51  		MaxLinksPerSpan:            c.MaxLinksPerSpan,
    52  	}
    53  
    54  	switch strings.ToLower(strings.TrimSpace(c.Sampling.Sampler)) {
    55  	case "always":
    56  		config.DefaultSampler = trace.AlwaysSample()
    57  
    58  	case "never":
    59  		config.DefaultSampler = trace.NeverSample()
    60  
    61  	case "probability":
    62  		config.DefaultSampler = trace.ProbabilitySampler(c.Sampling.Fraction)
    63  	}
    64  
    65  	return config
    66  }