This repository has been archived by the owner on Jul 31, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 327
/
registry.go
284 lines (256 loc) · 7.79 KB
/
registry.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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
// Copyright 2018, OpenCensus Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package metric
import (
"sort"
"sync"
"time"
"go.opencensus.io/metric/metricdata"
)
// Registry creates and manages a set of gauges and cumulative.
// External synchronization is required if you want to add gauges and cumulative to the same
// registry from multiple goroutines.
type Registry struct {
baseMetrics sync.Map
}
type metricOptions struct {
unit metricdata.Unit
labelkeys []metricdata.LabelKey
constLabels map[metricdata.LabelKey]metricdata.LabelValue
desc string
}
// Options apply changes to metricOptions.
type Options func(*metricOptions)
// WithDescription applies provided description.
func WithDescription(desc string) Options {
return func(mo *metricOptions) {
mo.desc = desc
}
}
// WithUnit applies provided unit.
func WithUnit(unit metricdata.Unit) Options {
return func(mo *metricOptions) {
mo.unit = unit
}
}
// WithLabelKeys applies provided label.
func WithLabelKeys(keys ...string) Options {
return func(mo *metricOptions) {
labelKeys := make([]metricdata.LabelKey, 0)
for _, key := range keys {
labelKeys = append(labelKeys, metricdata.LabelKey{Key: key})
}
mo.labelkeys = labelKeys
}
}
// WithLabelKeysAndDescription applies provided label.
func WithLabelKeysAndDescription(labelKeys ...metricdata.LabelKey) Options {
return func(mo *metricOptions) {
mo.labelkeys = labelKeys
}
}
// WithConstLabel applies provided constant label.
func WithConstLabel(constLabels map[metricdata.LabelKey]metricdata.LabelValue) Options {
return func(mo *metricOptions) {
mo.constLabels = constLabels
}
}
// NewRegistry initializes a new Registry.
func NewRegistry() *Registry {
return &Registry{}
}
// AddFloat64Gauge creates and adds a new float64-valued gauge to this registry.
func (r *Registry) AddFloat64Gauge(name string, mos ...Options) (*Float64Gauge, error) {
f := &Float64Gauge{
bm: baseMetric{
bmType: gaugeFloat64,
},
}
_, err := r.initBaseMetric(&f.bm, name, mos...)
if err != nil {
return nil, err
}
return f, nil
}
// AddInt64Gauge creates and adds a new int64-valued gauge to this registry.
func (r *Registry) AddInt64Gauge(name string, mos ...Options) (*Int64Gauge, error) {
i := &Int64Gauge{
bm: baseMetric{
bmType: gaugeInt64,
},
}
_, err := r.initBaseMetric(&i.bm, name, mos...)
if err != nil {
return nil, err
}
return i, nil
}
// AddInt64DerivedGauge creates and adds a new derived int64-valued gauge to this registry.
// A derived gauge is convenient form of gauge where the object associated with the gauge
// provides its value by implementing func() int64.
func (r *Registry) AddInt64DerivedGauge(name string, mos ...Options) (*Int64DerivedGauge, error) {
i := &Int64DerivedGauge{
bm: baseMetric{
bmType: derivedGaugeInt64,
},
}
_, err := r.initBaseMetric(&i.bm, name, mos...)
if err != nil {
return nil, err
}
return i, nil
}
// AddFloat64DerivedGauge creates and adds a new derived float64-valued gauge to this registry.
// A derived gauge is convenient form of gauge where the object associated with the gauge
// provides its value by implementing func() float64.
func (r *Registry) AddFloat64DerivedGauge(name string, mos ...Options) (*Float64DerivedGauge, error) {
f := &Float64DerivedGauge{
bm: baseMetric{
bmType: derivedGaugeFloat64,
},
}
_, err := r.initBaseMetric(&f.bm, name, mos...)
if err != nil {
return nil, err
}
return f, nil
}
func bmTypeToMetricType(bm *baseMetric) metricdata.Type {
switch bm.bmType {
case derivedGaugeFloat64:
return metricdata.TypeGaugeFloat64
case derivedGaugeInt64:
return metricdata.TypeGaugeInt64
case gaugeFloat64:
return metricdata.TypeGaugeFloat64
case gaugeInt64:
return metricdata.TypeGaugeInt64
case derivedCumulativeFloat64:
return metricdata.TypeCumulativeFloat64
case derivedCumulativeInt64:
return metricdata.TypeCumulativeInt64
case cumulativeFloat64:
return metricdata.TypeCumulativeFloat64
case cumulativeInt64:
return metricdata.TypeCumulativeInt64
default:
panic("unsupported metric type")
}
}
// AddFloat64Cumulative creates and adds a new float64-valued cumulative to this registry.
func (r *Registry) AddFloat64Cumulative(name string, mos ...Options) (*Float64Cumulative, error) {
f := &Float64Cumulative{
bm: baseMetric{
bmType: cumulativeFloat64,
},
}
_, err := r.initBaseMetric(&f.bm, name, mos...)
if err != nil {
return nil, err
}
return f, nil
}
// AddInt64Cumulative creates and adds a new int64-valued cumulative to this registry.
func (r *Registry) AddInt64Cumulative(name string, mos ...Options) (*Int64Cumulative, error) {
i := &Int64Cumulative{
bm: baseMetric{
bmType: cumulativeInt64,
},
}
_, err := r.initBaseMetric(&i.bm, name, mos...)
if err != nil {
return nil, err
}
return i, nil
}
// AddInt64DerivedCumulative creates and adds a new derived int64-valued cumulative to this registry.
// A derived cumulative is convenient form of cumulative where the object associated with the cumulative
// provides its value by implementing func() int64.
func (r *Registry) AddInt64DerivedCumulative(name string, mos ...Options) (*Int64DerivedCumulative, error) {
i := &Int64DerivedCumulative{
bm: baseMetric{
bmType: derivedCumulativeInt64,
},
}
_, err := r.initBaseMetric(&i.bm, name, mos...)
if err != nil {
return nil, err
}
return i, nil
}
// AddFloat64DerivedCumulative creates and adds a new derived float64-valued gauge to this registry.
// A derived cumulative is convenient form of cumulative where the object associated with the cumulative
// provides its value by implementing func() float64.
func (r *Registry) AddFloat64DerivedCumulative(name string, mos ...Options) (*Float64DerivedCumulative, error) {
f := &Float64DerivedCumulative{
bm: baseMetric{
bmType: derivedCumulativeFloat64,
},
}
_, err := r.initBaseMetric(&f.bm, name, mos...)
if err != nil {
return nil, err
}
return f, nil
}
func createMetricOption(mos ...Options) *metricOptions {
o := &metricOptions{}
for _, mo := range mos {
mo(o)
}
return o
}
func (r *Registry) initBaseMetric(bm *baseMetric, name string, mos ...Options) (*baseMetric, error) {
val, ok := r.baseMetrics.Load(name)
if ok {
existing := val.(*baseMetric)
if existing.bmType != bm.bmType {
return nil, errMetricExistsWithDiffType
}
}
bm.start = time.Now()
o := createMetricOption(mos...)
var constLabelKeys []metricdata.LabelKey
for k := range o.constLabels {
constLabelKeys = append(constLabelKeys, k)
}
sort.Slice(constLabelKeys, func(i, j int) bool {
return constLabelKeys[i].Key < constLabelKeys[j].Key
})
var constLabelValues []metricdata.LabelValue
for _, k := range constLabelKeys {
constLabelValues = append(constLabelValues, o.constLabels[k])
}
bm.keys = append(constLabelKeys, o.labelkeys...)
bm.constLabelValues = constLabelValues
bm.desc = metricdata.Descriptor{
Name: name,
Description: o.desc,
Unit: o.unit,
LabelKeys: bm.keys,
Type: bmTypeToMetricType(bm),
}
r.baseMetrics.Store(name, bm)
return bm, nil
}
// Read reads all gauges and cumulatives in this registry and returns their values as metrics.
func (r *Registry) Read() []*metricdata.Metric {
ms := []*metricdata.Metric{}
r.baseMetrics.Range(func(k, v interface{}) bool {
bm := v.(*baseMetric)
ms = append(ms, bm.read())
return true
})
return ms
}