forked from influxdata/influxdb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
registry.go
87 lines (72 loc) · 2.35 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
80
81
82
83
84
85
86
87
package metrics
import (
"fmt"
"sort"
)
type Registry struct {
descriptors []*groupDesc
groups []groupRegistry
}
const (
// DefaultGroup is the identifier for the default group.
DefaultGroup = GID(0)
)
// NewRegistry creates a new Registry with a single group identified by DefaultGroup.
func NewRegistry() *Registry {
var r Registry
r.MustRegisterGroup("global")
return &r
}
func (r *Registry) register(gd *groupDesc) error {
p := sort.Search(len(r.descriptors), func(i int) bool {
return r.descriptors[i].Name == gd.Name
})
if p != len(r.descriptors) {
return fmt.Errorf("group name '%s' already in use", gd.Name)
}
r.descriptors = append(r.descriptors, gd)
sort.Slice(r.descriptors, func(i, j int) bool {
return r.descriptors[i].Name < r.descriptors[j].Name
})
gd.id = GID(len(r.groups))
r.groups = append(r.groups, groupRegistry{desc: gd})
return nil
}
func (r *Registry) mustRegister(gd *groupDesc) {
if err := r.register(gd); err != nil {
panic(err.Error())
}
}
// MustRegisterGroup registers a new group and panics if a group already exists with the same name.
//
// MustRegisterGroup is not safe to call from concurrent goroutines.
func (r *Registry) MustRegisterGroup(name string) GID {
gd := &groupDesc{Name: name}
r.mustRegister(gd)
return gd.id
}
func (r *Registry) mustGetGroupRegistry(id GID) *groupRegistry {
if int(id) >= len(r.groups) {
panic(fmt.Sprintf("invalid group ID"))
}
return &r.groups[id]
}
// MustRegisterCounter registers a new counter metric using the provided descriptor.
// If the metric name is not unique within the group, MustRegisterCounter will panic.
//
// MustRegisterCounter is not safe to call from concurrent goroutines.
func (r *Registry) MustRegisterCounter(name string, opts ...descOption) ID {
desc := newDesc(name, opts...)
return r.mustGetGroupRegistry(desc.gid).mustRegisterCounter(desc)
}
// MustRegisterTimer registers a new timer metric using the provided descriptor.
// If the metric name is not unique within the group, MustRegisterTimer will panic.
//
// MustRegisterTimer is not safe to call from concurrent goroutines.
func (r *Registry) MustRegisterTimer(name string, opts ...descOption) ID {
desc := newDesc(name, opts...)
return r.mustGetGroupRegistry(desc.gid).mustRegisterTimer(desc)
}
func (r *Registry) NewGroup(gid GID) *Group {
return r.mustGetGroupRegistry(gid).newGroup()
}