-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathtracer.go
65 lines (52 loc) · 1.16 KB
/
tracer.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
53
54
55
56
57
58
59
60
61
62
63
64
65
package tracing
import (
"github.com/opentracing/opentracing-go"
jaegercfg "github.com/uber/jaeger-client-go/config"
)
var globalFlusher func()
func GetGlobalTracer() opentracing.Tracer {
return opentracing.GlobalTracer()
}
type tracerConfig struct {
TracingService string
AppName string
TracingHost string
}
type tracerOption func(config *tracerConfig)
// Pass UsingJaeger's result as argument to SetGlobalTracer to set Jaeger as your tracing system
// This is the default behavior
func UsingJaeger() tracerOption {
return func(config *tracerConfig) {
config.TracingService = "jaeger"
}
}
func SetGlobalTracer(options ...tracerOption) error {
config := &tracerConfig{}
for _, option := range options {
option(config)
}
setGlobalJaegerTracer()
return nil
}
func FlushCollector() {
if globalFlusher != nil {
globalFlusher()
}
}
func setGlobalJaegerTracer() error {
cfg, err := jaegercfg.FromEnv()
if err != nil {
return err
}
tracer, globalCloser, err := cfg.NewTracer()
if err != nil {
return err
}
opentracing.SetGlobalTracer(tracer)
globalFlusher = func() {
if globalCloser != nil {
globalCloser.Close()
}
}
return err
}