-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmulti_incarnation_scope.go
More file actions
197 lines (157 loc) · 4.99 KB
/
multi_incarnation_scope.go
File metadata and controls
197 lines (157 loc) · 4.99 KB
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
package stats
import (
"fmt"
"strconv"
"sync"
"github.com/upfluence/stats/internal/hash"
)
var globalIncarnationRegistry = &incarnationRegistry{key: "incarnation", counters: make(map[counterKey]uint)}
type incarnationRegistry struct {
countersMu sync.Mutex
counters map[counterKey]uint
key string
}
func (ir *incarnationRegistry) next(ck counterKey) uint {
ir.countersMu.Lock()
defer ir.countersMu.Unlock()
current, ok := ir.counters[ck]
next := current + 1
if !ok {
next--
}
ir.counters[ck] = next
return next
}
type counterKey struct {
keySum uint64
tagsSum uint64
}
func newCounterKey() counterKey {
return counterKey{keySum: hash.New()}
}
func (ck counterKey) add(key string, tags map[string]string) counterKey {
if key != "" {
if ck.keySum != hash.New() {
key = "_" + key
}
ck.keySum = hash.AddNoScramble(ck.keySum, key)
}
for k, v := range tags {
ck.tagsSum |= hash.Add(hash.Add(hash.New(), k), v)
}
return ck
}
func (ck counterKey) appendTag(k, v string) counterKey {
ck.tagsSum |= hash.Add(hash.Add(hash.New(), k), v)
return ck
}
type multiIncarnationScope struct {
scope Scope
currentKey counterKey
registry *incarnationRegistry
}
func newMultiIncarnationScope(sc Scope, r *incarnationRegistry) *multiIncarnationScope {
return &multiIncarnationScope{
scope: sc,
currentKey: newCounterKey().add(sc.namespace(), sc.tags()),
registry: r,
}
}
// GlobalIncarnationScope wraps a scope to automatically add an "incarnation" label
// to all metrics. The incarnation counter increments each time a metric with the
// same name and labels is created, which typically happens on service restart.
//
// This allows tracking metrics across service restarts while maintaining historical data.
// Useful for distinguishing between multiple instances or restarts of the same service.
//
// The incarnation label is automatically managed and should not be set manually.
func GlobalIncarnationScope(sc Scope) Scope {
return newMultiIncarnationScope(sc, globalIncarnationRegistry)
}
// LocalIncarnationScope is similar to GlobalIncarnationScope but uses a custom
// key name for the incarnation label instead of the default "incarnation".
//
// This is useful when you need multiple incarnation tracking scopes with different
// semantic meanings.
func LocalIncarnationScope(sc Scope, k string) Scope {
return newMultiIncarnationScope(
sc,
&incarnationRegistry{key: k, counters: make(map[counterKey]uint)},
)
}
func (mis *multiIncarnationScope) namespace() string { return mis.scope.namespace() }
func (mis *multiIncarnationScope) tags() map[string]string { return mis.scope.tags() }
func (mis *multiIncarnationScope) rootScope() *rootScope { return mis.scope.rootScope() }
func (mis *multiIncarnationScope) Scope(k string, vs map[string]string) Scope {
if _, ok := vs[mis.registry.key]; ok {
panic(fmt.Sprintf("scope can not include the incarnation key %q", mis.registry.key))
}
return &multiIncarnationScope{
scope: mis.scope.Scope(k, vs),
currentKey: mis.currentKey.add(k, vs),
registry: mis.registry,
}
}
func (mis *multiIncarnationScope) RootScope() Scope {
return &multiIncarnationScope{
scope: mis.scope.RootScope(),
registry: mis.registry,
}
}
type abstractVector[T any] interface {
WithLabels(...string) T
}
type multiIncarnationVector[T any] struct {
cv abstractVector[T]
ls []string
currentKey counterKey
registry *incarnationRegistry
}
func (micv *multiIncarnationVector[T]) WithLabels(vs ...string) T {
if len(vs) != len(micv.ls) {
panic("wrong number of label values")
}
ck := micv.currentKey
for i, l := range micv.ls {
ck = ck.appendTag(l, vs[i])
}
return micv.cv.WithLabels(
append(
vs,
strconv.Itoa(int(micv.registry.next(ck))),
)...,
)
}
func (mis *multiIncarnationScope) Counter(k string) Counter {
return mis.CounterVector(k, nil).WithLabels()
}
func (mis *multiIncarnationScope) CounterVector(k string, ls []string) CounterVector {
return &multiIncarnationVector[Counter]{
cv: mis.scope.CounterVector(k, append(ls, mis.registry.key)),
ls: ls,
currentKey: mis.currentKey.add(k, nil),
registry: mis.registry,
}
}
func (mis *multiIncarnationScope) Gauge(k string) Gauge {
return mis.GaugeVector(k, nil).WithLabels()
}
func (mis *multiIncarnationScope) GaugeVector(k string, ls []string) GaugeVector {
return &multiIncarnationVector[Gauge]{
cv: mis.scope.GaugeVector(k, append(ls, mis.registry.key)),
ls: ls,
currentKey: mis.currentKey.add(k, nil),
registry: mis.registry,
}
}
func (mis *multiIncarnationScope) Histogram(k string, opts ...HistogramOption) Histogram {
return mis.HistogramVector(k, nil, opts...).WithLabels()
}
func (mis *multiIncarnationScope) HistogramVector(k string, ls []string, opts ...HistogramOption) HistogramVector {
return &multiIncarnationVector[Histogram]{
cv: mis.scope.HistogramVector(k, append(ls, mis.registry.key), opts...),
ls: ls,
currentKey: mis.currentKey.add(k, nil),
registry: mis.registry,
}
}