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