/
standard.go
159 lines (143 loc) · 3.18 KB
/
standard.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package tracer
import (
"context"
"runtime"
"sync"
opentracing "github.com/opentracing/opentracing-go"
"github.com/rai-project/config"
"github.com/rai-project/tracer/defaults"
"github.com/rai-project/tracer/observer"
"golang.org/x/sync/syncmap"
)
var (
stdTracer Tracer
mut sync.Mutex
noop Tracer
usingPerf bool
)
func SetStd(t Tracer) {
stdTracer = t
opentracing.SetGlobalTracer(t)
}
func Std() Tracer {
return stdTracer
}
func New(serviceName string) (Tracer, error) {
backendName := Config.Provider
if backendName == "" || !Config.Enabled {
backendName = "noop"
}
return NewFromName(serviceName, backendName)
}
func MustNew(serviceName string) Tracer {
backendName := Config.Provider
if backendName == "" || !Config.Enabled {
backendName = "noop"
}
tr, err := NewFromName(serviceName, backendName)
if err != nil {
// just use the noop tracer
tr, err = NewFromName(serviceName, "noop")
if err != nil {
panic(err)
}
}
return tr
}
func StartSpan(lvl Level, operationName string, opts ...opentracing.StartSpanOption) opentracing.Span {
if stdTracer == nil {
return nil
}
if lvl > stdTracer.Level() {
return noop.StartSpan(operationName, opts...)
}
opts = append(opts, opentracing.Tag{"trace_level", lvl.String()})
if usingPerf {
opts = append(opts, opentracing.Tag{"perfevents", defaults.PerfEvents})
}
return stdTracer.StartSpan(operationName, opts...)
}
func StartSpanFromContext(ctx context.Context, lvl Level, operationName string, opts ...opentracing.StartSpanOption) (opentracing.Span, context.Context) {
if stdTracer == nil {
return nil, ctx
}
if lvl > stdTracer.Level() {
return noop.StartSpanFromContext(ctx, operationName, opts...)
}
opts = append(opts, opentracing.Tag{"trace_level", lvl.String()})
if usingPerf {
opts = append(opts, opentracing.Tag{"perfevents", defaults.PerfEvents})
}
return opentracing.StartSpanFromContext(ctx, operationName, opts...)
}
func Enabled() bool {
if stdTracer == nil {
return false
}
return Config.Enabled
}
func Close() error {
mut.Lock()
defer mut.Unlock()
openTracers.Range(func(_ interface{}, value interface{}) bool {
tr, ok := value.(Tracer)
if !ok {
return true
}
err := tr.Close()
if err != nil {
log.WithError(err).WithField("tracer", tr.Name()).Error("Failed to close tracer")
}
return true
})
openTracers = syncmap.Map{}
return nil
}
func Endpoints() []string {
if stdTracer == nil {
return []string{}
}
return stdTracer.Endpoints()
}
func Provider() string {
if stdTracer == nil {
return Config.Provider
}
return stdTracer.Name()
}
func SetLevel(lvl Level) {
if stdTracer == nil {
return
}
stdTracer.SetLevel(lvl)
}
func init() {
loadNoop := func(name string) {
if name == "" {
name = "tracer"
}
no, err := NewFromName("tracer", "noop")
if err != nil {
return
}
noop = no
}
config.AfterInit(func() {
loadNoop(config.App.Name)
std, err := New(config.App.Name)
if err != nil {
SetStd(noop)
return
}
SetStd(std)
if runtime.GOOS == "linux" {
for _, o := range observer.Config.ObserverNames {
if o == "perf" || o == "perf_events" || o == "perfevents" {
usingPerf = true
break
}
}
}
})
loadNoop("")
}