Repository navigation
Expand file tree
/
Copy pathvector.go
More file actions
137 lines (103 loc) · 2.45 KB
/
Copy pathvector.go
File metadata and controls
137 lines (103 loc) · 2.45 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
package stats
import (
"fmt"
"sync"
)
// Int64Value represents a single int64 metric value with its associated tags.
type Int64Value struct {
Tags map[string]string
Value int64
}
// Int64VectorGetter provides read access to int64 vectors for collectors.
// Used by both counters and gauges.
type Int64VectorGetter interface {
// Labels returns the label names for this vector.
Labels() []string
// Get returns all values with their label combinations.
Get() []*Int64Value
}
type atomicInt64Vector struct {
entityVector[*atomicInt64]
}
func newAtomicInt64Vector(ls []string, lm *hashingMarshaler) *atomicInt64Vector {
return &atomicInt64Vector{
entityVector: entityVector[*atomicInt64]{
labels: ls,
entities: make(map[uint64]*atomicInt64),
marshaler: lm,
newFunc: func(map[string]string) *atomicInt64 { return &atomicInt64{} },
},
}
}
func (v *atomicInt64Vector) Labels() []string { return v.labels }
func (v *atomicInt64Vector) buildTags(key uint64) map[string]string {
var tags = make(map[string]string, len(v.labels))
for i, val := range v.marshaler.unmarshal(key, len(v.labels)) {
tags[v.labels[i]] = val
}
return tags
}
func (v *atomicInt64Vector) Get() []*Int64Value {
type entry struct {
key uint64
value *atomicInt64
}
v.mu.RLock()
var entries = make([]entry, 0, len(v.entities))
for k, vv := range v.entities {
entries = append(entries, entry{key: k, value: vv})
}
v.mu.RUnlock()
var res = make([]*Int64Value, 0, len(entries))
for _, entry := range entries {
res = append(
res,
&Int64Value{
Tags: v.buildTags(entry.key),
Value: entry.value.Get(),
},
)
}
return res
}
func (v *atomicInt64Vector) fetchValue(ls []string) *atomicInt64 {
return v.entity(ls)
}
type entityVector[T any] struct {
newFunc func(map[string]string) T
labels []string
mu sync.RWMutex
entities map[uint64]T
marshaler *hashingMarshaler
}
func (ev *entityVector[T]) entity(ls []string) T {
if len(ls) != len(ev.labels) {
panic(
fmt.Sprintf(
"Not the correct number of labels: labels: %v, values: %v",
ev.labels,
ls,
),
)
}
k := ev.marshaler.marshal(ls)
ev.mu.RLock()
v, ok := ev.entities[k]
ev.mu.RUnlock()
if ok {
return v
}
vs := make(map[string]string, len(ev.labels))
for i, k := range ev.labels {
vs[k] = ls[i]
}
v = ev.newFunc(vs)
ev.mu.Lock()
if existing, ok := ev.entities[k]; ok {
v = existing
} else {
ev.entities[k] = v
}
ev.mu.Unlock()
return v
}