-
Notifications
You must be signed in to change notification settings - Fork 2
/
gauge.go
184 lines (158 loc) · 3.98 KB
/
gauge.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
// Copyright (c) 2021 Circonus, Inc. <[email protected]>
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
//
package trapmetrics
import (
"fmt"
"time"
)
// GaugeSet sets a sample with a given timestamp for a gauge to the passed value.
func (tm *TrapMetrics) GaugeSet(name string, tags Tags, val interface{}, ts *time.Time) error {
mt := mtGauge
metricID, err := generateMetricID(name, mt, tags)
if err != nil {
return err
}
sampleKey := generateSampleKey(ts)
rtype := ""
ok, rt := isValidGaugeType(val)
if !ok {
return fmt.Errorf("invalid value for gauge (%v %T)", val, val)
}
rtype = rt
tm.metricsmu.Lock()
defer tm.metricsmu.Unlock()
if m, ok := tm.metrics[metricID]; ok {
if m.Mtype != mtGauge {
return fmt.Errorf("(%s %s) exists with different type (gauge) vs (%s)", name, tags.String(), m.Mtype)
}
m.Samples[sampleKey] = val
return nil
}
m, err := tm.newMetric(name, mt, tags)
if err != nil {
return fmt.Errorf("(%s %s) failed to initialize (gauge): %w", name, tags.String(), err)
}
m.Rtype = rtype
m.Samples[sampleKey] = val
tm.metrics[metricID] = m
return nil
}
// GaugeAdd adds a sample with a given timestamp for a gauge to the passed value.
func (tm *TrapMetrics) GaugeAdd(name string, tags Tags, val interface{}, ts *time.Time) error {
mt := mtGauge
metricID, err := generateMetricID(name, mt, tags)
if err != nil {
return err
}
sampleKey := generateSampleKey(ts)
rtype := ""
ok, rt := isValidGaugeType(val)
if !ok {
return fmt.Errorf("invalid value for gauge (%v %T)", val, val)
}
rtype = rt
tm.metricsmu.Lock()
defer tm.metricsmu.Unlock()
if m, ok := tm.metrics[metricID]; ok {
if m.Mtype != mtGauge {
return fmt.Errorf("(%s %s) exists with different type (gauge) vs (%s)", name, tags.String(), m.Mtype)
}
if v, ok := m.Samples[sampleKey]; ok {
if m.Rtype != rt {
return fmt.Errorf("(%s %s) exists with different reconnoiter type (%s) vs (%s)", name, tags.String(), m.Rtype, rt)
}
m.Samples[sampleKey] = addValByType(m.Rtype, v, val)
} else {
m.Samples[sampleKey] = val
}
return nil
}
m, err := tm.newMetric(name, mt, tags)
if err != nil {
return fmt.Errorf("(%s %s) failed to initialize (gauge): %w", name, tags.String(), err)
}
m.Rtype = rtype
m.Samples[sampleKey] = val
tm.metrics[metricID] = m
return nil
}
func addValByType(rtype string, base interface{}, val interface{}) interface{} {
switch rtype {
case rtInt32:
if i, oki := base.(int32); oki {
if j, okj := val.(int32); okj {
return i + j
}
}
case rtInt64:
if i, oki := base.(int64); oki {
if j, okj := val.(int64); okj {
return i + j
}
}
case rtUint32:
if i, oki := base.(uint32); oki {
if j, okj := val.(uint32); okj {
return i + j
}
}
case rtUint64:
if i, oki := base.(uint64); oki {
if j, okj := val.(uint64); okj {
return i + j
}
}
case rtFloat64:
if i, oki := base.(float64); oki {
if j, okj := val.(float64); okj {
return i + j
}
}
}
return base
}
func isValidGaugeType(val interface{}) (bool, string) {
switch val.(type) {
case int:
return true, rtInt32
case int8:
return true, rtInt32
case int16:
return true, rtInt32
case int32:
return true, rtInt32
case int64:
return true, rtInt64
case uint:
return true, rtUint32
case uint8:
return true, rtUint32
case uint16:
return true, rtUint32
case uint32:
return true, rtUint32
case uint64:
return true, rtUint64
case float32:
return true, rtFloat64
case float64:
return true, rtFloat64
default:
return false, ""
}
}
// GaugeFetch will return the metric identified by name and tags.
func (tm *TrapMetrics) GaugeFetch(name string, tags Tags) (*Metric, error) {
metricID, err := generateMetricID(name, mtGauge, tags)
if err != nil {
return nil, err
}
tm.metricsmu.Lock()
defer tm.metricsmu.Unlock()
if m, ok := tm.metrics[metricID]; ok {
return m, nil
}
return nil, fmt.Errorf("gauge %d (%s %s) not found", metricID, name, tags.String())
}