-
Notifications
You must be signed in to change notification settings - Fork 450
/
couge.go
211 lines (187 loc) · 5.38 KB
/
couge.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
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
// Copyright (C) 2019-2024 Algorand, Inc.
// This file is part of go-algorand
//
// go-algorand is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// go-algorand is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with go-algorand. If not, see <https://www.gnu.org/licenses/>.
// Common code for COUnters and gaUGEs.
package metrics
import (
"math"
"strconv"
"strings"
"sync/atomic"
"github.com/algorand/go-deadlock"
)
type couge struct {
// Collects value for special fast-path with no labels through Inc(nil) AddUint64(x, nil)
intValue atomic.Uint64
deadlock.Mutex
name string
description string
values []*cougeValues
labels map[string]int // map each label ( i.e. httpErrorCode ) to an index.
valuesIndices map[int]int
}
type cougeValues struct {
value uint64
labels map[string]string
formattedLabels string
}
func (cv *cougeValues) createFormattedLabel() {
var buf strings.Builder
if len(cv.labels) < 1 {
return
}
for k, v := range cv.labels {
buf.WriteString("," + k + "=\"" + v + "\"")
}
cv.formattedLabels = buf.String()[1:]
}
func (cg *couge) findLabelIndex(labels map[string]string) int {
accumulatedIndex := 0
for k, v := range labels {
t := k + ":" + v
// do we already have this key ( label ) in our map ?
if i, has := cg.labels[t]; has {
// yes, we do. use this index.
accumulatedIndex += i
} else {
// no, we don't have it.
cg.labels[t] = int(math.Exp2(float64(len(cg.labels))))
accumulatedIndex += cg.labels[t]
}
}
return accumulatedIndex
}
func (cg *couge) fastAddUint64(x uint64) {
if cg.intValue.Add(x) == x {
// What we just added is the whole value, this
// is the first Add. Create a dummy
// counterValue for the no-labels value.
// Dummy counterValue simplifies display in WriteMetric.
cg.addLabels(0, nil)
}
}
// addLabels increases counter by x
func (cg *couge) addLabels(x uint64, labels map[string]string) {
cg.Lock()
defer cg.Unlock()
labelIndex := cg.findLabelIndex(labels)
// find where we have the same labels.
if counterIdx, has := cg.valuesIndices[labelIndex]; !has {
// we need to add a new counter.
val := &cougeValues{
value: x,
labels: labels,
}
val.createFormattedLabel()
cg.values = append(cg.values, val)
cg.valuesIndices[labelIndex] = len(cg.values) - 1
} else {
// update existing value.
cg.values[counterIdx].value += x
}
}
// setLabels sets value to x
func (cg *couge) setLabels(x uint64, labels map[string]string) {
cg.Lock()
defer cg.Unlock()
labelIndex := cg.findLabelIndex(labels)
// find where we have the same labels.
if counterIdx, has := cg.valuesIndices[labelIndex]; !has {
// we need to set a new value.
val := &cougeValues{
value: x,
labels: labels,
}
val.createFormattedLabel()
cg.values = append(cg.values, val)
cg.valuesIndices[labelIndex] = len(cg.values) - 1
} else {
// update existing value.
cg.values[counterIdx].value = x
}
}
// getUint64ValueForLabels returns the value of the counter for the given labels or 0 if it's not found.
func (cg *couge) getUint64ValueForLabels(labels map[string]string) uint64 {
cg.Lock()
defer cg.Unlock()
labelIndex := cg.findLabelIndex(labels)
counterIdx, has := cg.valuesIndices[labelIndex]
if !has {
return 0
}
return cg.values[counterIdx].value
}
// writeMetric writes the metric into the output stream
func (cg *couge) writeMetric(buf *strings.Builder, metricType string, parentLabels string) {
cg.Lock()
defer cg.Unlock()
buf.WriteString("# HELP ")
buf.WriteString(cg.name)
buf.WriteString(" ")
buf.WriteString(cg.description)
buf.WriteString("\n# TYPE ")
buf.WriteString(cg.name)
buf.WriteString(" " + metricType + "\n")
// if counter is zero, report 0 using parentLabels and no tags
if len(cg.values) == 0 {
buf.WriteString(cg.name)
if len(parentLabels) > 0 {
buf.WriteString("{" + parentLabels + "}")
}
buf.WriteString(" " + strconv.FormatUint(cg.intValue.Load(), 10))
buf.WriteString("\n")
return
}
// otherwise iterate through values and write one line per label
for _, l := range cg.values {
buf.WriteString(cg.name)
if len(parentLabels) > 0 || len(l.formattedLabels) > 0 {
buf.WriteString("{")
if len(parentLabels) > 0 {
buf.WriteString(parentLabels)
if len(l.formattedLabels) > 0 {
buf.WriteString(",")
}
}
buf.WriteString(l.formattedLabels)
buf.WriteString("}")
}
value := l.value
if len(l.labels) == 0 {
value += cg.intValue.Load()
}
buf.WriteString(" " + strconv.FormatUint(value, 10))
buf.WriteString("\n")
}
}
// addMetric adds the metric into the map
func (cg *couge) addMetric(values map[string]float64) {
cg.Lock()
defer cg.Unlock()
if len(cg.values) < 1 {
return
}
for _, l := range cg.values {
sum := l.value
if len(l.labels) == 0 {
sum += cg.intValue.Load()
}
var suffix string
if len(l.formattedLabels) > 0 {
suffix = ":" + l.formattedLabels
}
values[sanitizeTelemetryName(cg.name+suffix)] = float64(sum)
}
}