-
Notifications
You must be signed in to change notification settings - Fork 1
/
standard.go
164 lines (146 loc) · 3.19 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
160
161
162
163
164
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"
)
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 ResetStd(options ...Option) Tracer {
if stdTracer != nil {
stdTracer.Close()
}
std, err := New(config.App.Name, options...)
if err != nil {
SetStd(noop)
return nil
}
SetStd(std)
return std
}
func New(serviceName string, options ...Option) (Tracer, error) {
backendName := Config.Provider
if backendName == "" || !Config.Enabled {
backendName = "noop"
}
return NewFromName(serviceName, backendName, options...)
}
func MustNew(serviceName string, options ...Option) Tracer {
backendName := Config.Provider
if backendName == "" || !Config.Enabled {
backendName = "noop"
}
tr, err := NewFromName(serviceName, backendName, options...)
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 stdTracer.StartSpanFromContext(ctx, operationName, opts...)
}
func Enabled() bool {
if stdTracer == nil {
return false
}
return Config.Enabled
}
func Close() error {
if stdTracer != nil {
err := stdTracer.Close()
stdTracer = nil
return err
}
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 GetLevel() Level {
if stdTracer == nil {
return NO_TRACE
}
return stdTracer.Level()
}
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)
ResetStd()
if runtime.GOOS == "linux" {
for _, o := range observer.Config.ObserverNames {
if o == "perf" || o == "perf_events" || o == "perfevents" {
usingPerf = true
break
}
}
}
})
loadNoop("")
}