-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathvalue.go
140 lines (119 loc) · 3.82 KB
/
value.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
// Copyright (c) 2017 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
package metrics
import (
"sync"
promproto "github.com/prometheus/client_model/go"
"go.uber.org/atomic"
"go.uber.org/net/metrics/push"
)
// Value is an atomic with some associated metadata. It's a building block
// for higher-level metric types.
type value struct {
atomic.Int64
meta metadata
tagPairs []*promproto.LabelPair
}
func newValue(m metadata) value {
return value{
meta: m,
tagPairs: m.MergeTags(nil /* variable tags */),
}
}
func newDynamicValue(m metadata, variableTagPairs []string) value {
return value{
meta: m,
tagPairs: m.MergeTags(variableTagPairs),
}
}
func (v value) snapshot() Snapshot {
return Snapshot{
Name: *v.meta.Name,
Tags: zip(v.tagPairs),
Value: v.Load(),
}
}
// A vector is a collection of values that share the same metadata.
type vector struct {
meta metadata
// The factory function creates a new metric given the vector's metadata
// and the variable tag keys and values.
factory func(metadata, []string) metric
metricsMu sync.RWMutex
// key is variable tag vals, the value is the index to metricsStorage. We do this to reduce GC.
metrics map[string]uint32
// this is needed to reduce overhead of for loop because looping a map is more expensive
metricsStorage []metric
}
func (vec *vector) getOrCreate(variableTagPairs []string) (metric, error) {
if err := vec.meta.ValidateVariableTags(variableTagPairs); err != nil {
return nil, err
}
digester := newDigester()
for i := 0; i < len(variableTagPairs)/2; i++ {
digester.add("", scrubTagValue(variableTagPairs[i*2+1]))
}
vec.metricsMu.RLock()
mIndex, ok := vec.metrics[string(digester.digest())]
if ok {
m := vec.metricsStorage[mIndex]
vec.metricsMu.RUnlock()
digester.free()
return m, nil
}
vec.metricsMu.RUnlock()
vec.metricsMu.Lock()
m, err := vec.newValue(digester.digest(), variableTagPairs)
vec.metricsMu.Unlock()
digester.free()
return m, err
}
func (vec *vector) newValue(key []byte, variableTagPairs []string) (metric, error) {
mIndex, ok := vec.metrics[string(key)]
if ok {
m := vec.metricsStorage[mIndex]
return m, nil
}
m := vec.factory(vec.meta, variableTagPairs)
vec.metrics[string(key)] = uint32(len(vec.metricsStorage))
vec.metricsStorage = append(vec.metricsStorage, m)
return m, nil
}
func (vec *vector) snapshot() []Snapshot {
vec.metricsMu.RLock()
defer vec.metricsMu.RUnlock()
snaps := make([]Snapshot, 0, len(vec.metrics))
for _, m := range vec.metricsStorage {
switch v := m.(type) {
case *Counter:
snaps = append(snaps, v.snapshot())
case *Gauge:
snaps = append(snaps, v.snapshot())
}
}
return snaps
}
func (vec *vector) push(target push.Target) {
vec.metricsMu.RLock()
for _, m := range vec.metricsStorage {
m.push(target)
}
vec.metricsMu.RUnlock()
}