Skip to content

Commit a845ca6

Browse files
committed
Add instrument registry
1 parent c8568c9 commit a845ca6

File tree

2 files changed

+587
-0
lines changed

2 files changed

+587
-0
lines changed
Lines changed: 239 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,239 @@
1+
/*
2+
*
3+
* Copyright 2024 gRPC authors.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*
17+
*/
18+
19+
// Package instrumentregistry is a instrument registry that gRPC components can
20+
// use to register instruments (metrics).
21+
package instrumentregistry
22+
23+
import "log"
24+
25+
// InstrumentDescriptor is a data of a registered instrument (metric).
26+
type InstrumentDescriptor struct {
27+
// Name is the name of this metric.
28+
Name string
29+
// Description is the description of this metric.
30+
Description string
31+
// Unit is the unit of this metric.
32+
Unit string
33+
// Labels are the required label keys for this metric.
34+
Labels []string
35+
// OptionalLabels are the optional label keys for this
36+
// metric.
37+
OptionalLabels []string
38+
// Default is whether this metric is on by default.
39+
Default bool
40+
}
41+
42+
// All of these globals are written to at initialization time only, and are read
43+
// only after initialization.
44+
45+
// Int64CountInsts is information about registered int 64 count instruments in
46+
// order of registration.
47+
var Int64CountInsts []InstrumentDescriptor
48+
49+
// Float64CountInsts is information about registered float 64 count instruments
50+
// in order of registration.
51+
var Float64CountInsts []InstrumentDescriptor
52+
53+
// Int64HistoInsts is information about registered int 64 histo instruments in
54+
// order of registration.
55+
var Int64HistoInsts []InstrumentDescriptor
56+
57+
// Float64HistoInsts is information about registered float 64 histo instruments
58+
// in order of registration.
59+
var Float64HistoInsts []InstrumentDescriptor
60+
61+
// Int64GaugeInsts is information about registered int 64 gauge instruments in
62+
// order of registration.
63+
var Int64GaugeInsts []InstrumentDescriptor
64+
65+
// registeredInsts are the registered instrument descriptor names.
66+
var registeredInsts = make(map[string]bool)
67+
68+
// DefaultNonPerCallMetrics are the instruments registered that are on by
69+
// default.
70+
var DefaultNonPerCallMetrics = make(map[string]bool)
71+
72+
// ClearInstrumentRegistryForTesting clears the instrument registry for testing
73+
// purposes only.
74+
func ClearInstrumentRegistryForTesting() {
75+
Int64CountInsts = nil
76+
Float64CountInsts = nil
77+
Int64HistoInsts = nil
78+
Float64HistoInsts = nil
79+
Int64GaugeInsts = nil
80+
registeredInsts = make(map[string]bool)
81+
DefaultNonPerCallMetrics = make(map[string]bool)
82+
}
83+
84+
// Label represents a string attribute/label to attach to metrics.
85+
type Label struct {
86+
// Key is the key of the label.
87+
Key string
88+
// Value is the value of the label.
89+
Value string
90+
}
91+
92+
func registerInst(name string, def bool) {
93+
if registeredInsts[name] {
94+
log.Panicf("instrument %v already registered", name)
95+
}
96+
registeredInsts[name] = true
97+
if def {
98+
DefaultNonPerCallMetrics[name] = true
99+
}
100+
}
101+
102+
// Int64CountHandle is a typed handle for a int count instrument. This handle is
103+
// passed at the recording point in order to know which instrument to record on.
104+
type Int64CountHandle struct {
105+
Index int
106+
}
107+
108+
// RegisterInt64Count registers the int count instrument description onto the
109+
// global registry. It returns a typed handle to use when recording data.
110+
//
111+
// NOTE: this function must only be called during initialization time (i.e. in
112+
// an init() function), and is not thread-safe. If multiple instruments are
113+
// registered with the same name, this function will panic.
114+
func RegisterInt64Count(name string, desc string, unit string, labels []string, optionalLabels []string, def bool) Int64CountHandle {
115+
registerInst(name, def)
116+
Int64CountInsts = append(Int64CountInsts, InstrumentDescriptor{
117+
Name: name,
118+
Description: desc,
119+
Unit: unit,
120+
Labels: labels,
121+
OptionalLabels: optionalLabels,
122+
Default: def,
123+
})
124+
return Int64CountHandle{
125+
Index: len(Int64CountInsts) - 1,
126+
}
127+
}
128+
129+
// Float64CountHandle is a typed handle for a float count instrument. This handle
130+
// is passed at the recording point in order to know which instrument to record
131+
// on.
132+
type Float64CountHandle struct {
133+
Index int
134+
}
135+
136+
// RegisterFloat64Count registers the float count instrument description onto the
137+
// global registry. It returns a typed handle to use when recording data.
138+
//
139+
// NOTE: this function must only be called during initialization time (i.e. in
140+
// an init() function), and is not thread-safe. If multiple instruments are
141+
// registered with the same name, this function will panic.
142+
func RegisterFloat64Count(name string, desc string, unit string, labels []string, optionalLabels []string, def bool) Float64CountHandle {
143+
registerInst(name, def)
144+
Float64CountInsts = append(Float64CountInsts, InstrumentDescriptor{
145+
Name: name,
146+
Description: desc,
147+
Unit: unit,
148+
Labels: labels,
149+
OptionalLabels: optionalLabels,
150+
Default: def,
151+
})
152+
return Float64CountHandle{
153+
Index: len(Float64CountInsts) - 1,
154+
}
155+
}
156+
157+
// Int64HistoHandle is a typed handle for a int histogram instrument. This handle
158+
// is passed at the recording point in order to know which instrument to record
159+
// on.
160+
type Int64HistoHandle struct {
161+
Index int
162+
}
163+
164+
// RegisterInt64Histo registers the int histogram instrument description onto the
165+
// global registry. It returns a typed handle to use when recording data.
166+
//
167+
// NOTE: this function must only be called during initialization time (i.e. in
168+
// an init() function), and is not thread-safe. If multiple instruments are
169+
// registered with the same name, this function will panic.
170+
func RegisterInt64Histo(name string, desc string, unit string, labels []string, optionalLabels []string, def bool) Int64HistoHandle {
171+
registerInst(name, def)
172+
Int64HistoInsts = append(Int64HistoInsts, InstrumentDescriptor{
173+
Name: name,
174+
Description: desc,
175+
Unit: unit,
176+
Labels: labels,
177+
OptionalLabels: optionalLabels,
178+
Default: def,
179+
})
180+
return Int64HistoHandle{
181+
Index: len(Int64HistoInsts) - 1,
182+
}
183+
}
184+
185+
// Float64HistoHandle is a typed handle for a float histogram instrument. This
186+
// handle is passed at the recording point in order to know which instrument to
187+
// record on.
188+
type Float64HistoHandle struct {
189+
Index int
190+
}
191+
192+
// RegisterFloat64Histo registers the float histogram instrument description
193+
// onto the global registry. It returns a typed handle to use when recording
194+
// data.
195+
//
196+
// NOTE: this function must only be called during initialization time (i.e. in
197+
// an init() function), and is not thread-safe. If multiple instruments are
198+
// registered with the same name, this function will panic.
199+
func RegisterFloat64Histo(name string, desc string, unit string, labels []string, optionalLabels []string, def bool) Float64HistoHandle {
200+
registerInst(name, def)
201+
Float64HistoInsts = append(Float64HistoInsts, InstrumentDescriptor{
202+
Name: name,
203+
Description: desc,
204+
Unit: unit,
205+
Labels: labels,
206+
OptionalLabels: optionalLabels,
207+
Default: def,
208+
})
209+
return Float64HistoHandle{
210+
Index: len(Float64HistoInsts) - 1,
211+
}
212+
}
213+
214+
// Int64GaugeHandle is a typed handle for a int gauge instrument. This handle is
215+
// passed at the recording point in order to know which instrument to record on.
216+
type Int64GaugeHandle struct {
217+
Index int
218+
}
219+
220+
// RegisterInt64Gauge registers the int gauge instrument description onto the
221+
// global registry. It returns a typed handle to use when recording data.
222+
//
223+
// NOTE: this function must only be called during initialization time (i.e. in
224+
// an init() function), and is not thread-safe. If multiple instruments are
225+
// registered with the same name, this function will panic.
226+
func RegisterInt64Gauge(name string, desc string, unit string, labels []string, optionalLabels []string, def bool) Int64GaugeHandle {
227+
registerInst(name, def)
228+
Int64GaugeInsts = append(Int64GaugeInsts, InstrumentDescriptor{
229+
Name: name,
230+
Description: desc,
231+
Unit: unit,
232+
Labels: labels,
233+
OptionalLabels: optionalLabels,
234+
Default: def,
235+
})
236+
return Int64GaugeHandle{
237+
Index: len(Int64GaugeInsts) - 1,
238+
}
239+
}

0 commit comments

Comments
 (0)