github.com/thanos-io/thanos@v0.32.5/pkg/tracing/elasticapm/elastic_apm.go (about)

     1  // Copyright (c) The Thanos Authors.
     2  // Licensed under the Apache License 2.0.
     3  
     4  package elasticapm
     5  
     6  import (
     7  	"io"
     8  
     9  	"github.com/opentracing/opentracing-go"
    10  	"go.elastic.co/apm"
    11  	"go.elastic.co/apm/module/apmot"
    12  	"gopkg.in/yaml.v2"
    13  )
    14  
    15  func init() {
    16  	apm.DefaultTracer.Close()
    17  }
    18  
    19  type Config struct {
    20  	ServiceName        string  `yaml:"service_name"`
    21  	ServiceVersion     string  `yaml:"service_version"`
    22  	ServiceEnvironment string  `yaml:"service_environment"`
    23  	SampleRate         float64 `yaml:"sample_rate"`
    24  }
    25  
    26  func NewTracer(conf []byte) (opentracing.Tracer, io.Closer, error) {
    27  	config := Config{}
    28  	if err := yaml.Unmarshal(conf, &config); err != nil {
    29  		return nil, nil, err
    30  	}
    31  	tracer, err := apm.NewTracerOptions(apm.TracerOptions{
    32  		ServiceName:        config.ServiceName,
    33  		ServiceVersion:     config.ServiceVersion,
    34  		ServiceEnvironment: config.ServiceEnvironment,
    35  	})
    36  	if err != nil {
    37  		return nil, nil, err
    38  	}
    39  	tracer.SetSampler(apm.NewRatioSampler(config.SampleRate))
    40  	return apmot.New(apmot.WithTracer(tracer)), tracerCloser{tracer}, nil
    41  }
    42  
    43  type tracerCloser struct {
    44  	tracer *apm.Tracer
    45  }
    46  
    47  func (t tracerCloser) Close() error {
    48  	if t.tracer != nil {
    49  		t.tracer.Close()
    50  	}
    51  	return nil
    52  }