-
Notifications
You must be signed in to change notification settings - Fork 1
/
registry.go
79 lines (71 loc) · 1.67 KB
/
registry.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
package tracer
import (
"errors"
"strings"
"golang.org/x/sync/syncmap"
)
var (
tracers syncmap.Map
openTracers syncmap.Map
)
type tracerRegistryItem struct {
tracer Tracer
new func(serviceName string, options ...Option) (Tracer, error)
}
func FromName(s string) (Tracer, error) {
s = strings.ToLower(s)
val, ok := tracers.Load(s)
if !ok {
if s != "noop" {
log.WithField("tracer", s).
Warn("cannot find tracer")
}
return nil, errors.New("cannot find tracer")
}
tracer, ok := val.(tracerRegistryItem)
if !ok {
log.WithField("tracer", s).
Warn("invalid tracer")
return nil, errors.New("invalid tracer")
}
return tracer.tracer, nil
}
func NewFromName(serviceName, backendName string, options ...Option) (Tracer, error) {
s := strings.ToLower(backendName)
val, ok := tracers.Load(s)
if !ok {
if s != "noop" {
log.WithField("tracer", s).
Warn("cannot find tracer")
}
return nil, errors.New("cannot find tracer")
}
tracer, ok := val.(tracerRegistryItem)
if !ok {
log.WithField("tracer", s).
Warn("invalid tracer")
return nil, errors.New("invalid tracer")
}
tr, err := tracer.new(serviceName, options...)
if err != nil {
return nil, err
}
if tr == nil {
return nil, errors.New("nil tracer")
}
openTracers.Store(tr.ID(), tr)
return tr, nil
}
func Register(name string, s Tracer, newFunc func(serviceName string, options ...Option) (Tracer, error)) {
tracers.Store(strings.ToLower(name), tracerRegistryItem{tracer: s, new: newFunc})
}
func Tracers() []string {
names := []string{}
tracers.Range(func(key, _ interface{}) bool {
if name, ok := key.(string); ok {
names = append(names, name)
}
return true
})
return names
}