Skip to content

Commit 805b801

Browse files
add support for conditional registration of metrics (#620)
* add support for conditional registration Signed-off-by: Rama Chavali <[email protected]> * make with noop Signed-off-by: Rama Chavali <[email protected]> * change to disabledMetric Signed-off-by: Rama Chavali <[email protected]> Signed-off-by: Rama Chavali <[email protected]>
1 parent f8c82a9 commit 805b801

File tree

2 files changed

+79
-0
lines changed

2 files changed

+79
-0
lines changed

monitoring/monitoring.go

+48
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,10 @@ type (
8585
ValueFrom(valueFn func() float64, labelValues ...string)
8686
}
8787

88+
disabledMetric struct {
89+
name string
90+
}
91+
8892
// Options encode changes to the options passed to a Metric at creation time.
8993
Options func(*options)
9094

@@ -116,6 +120,35 @@ type (
116120
}
117121
)
118122

123+
// Decrement implements Metric
124+
func (dm *disabledMetric) Decrement() {}
125+
126+
// Increment implements Metric
127+
func (dm *disabledMetric) Increment() {}
128+
129+
// Name implements Metric
130+
func (dm *disabledMetric) Name() string {
131+
return dm.name
132+
}
133+
134+
// Record implements Metric
135+
func (dm *disabledMetric) Record(value float64) {}
136+
137+
// RecordInt implements Metric
138+
func (dm *disabledMetric) RecordInt(value int64) {}
139+
140+
// Register implements Metric
141+
func (dm *disabledMetric) Register() error {
142+
return nil
143+
}
144+
145+
// With implements Metric
146+
func (dm *disabledMetric) With(labelValues ...LabelValue) Metric {
147+
return dm
148+
}
149+
150+
var _ Metric = &disabledMetric{}
151+
119152
var (
120153
recordHooks map[string]RecordHook
121154
recordHookMutex sync.RWMutex
@@ -204,6 +237,21 @@ func MustRegister(metrics ...Metric) {
204237
}
205238
}
206239

240+
// RegisterIf is a helper function that will ensure that the provided
241+
// Metric is registered if enabled function returns true.
242+
// If a metric fails to register, this method will panic.
243+
// It returns the registered metric or no-op metric based on enabled function.
244+
// NOTE: It is important to use the returned Metric if RegisterIf is used.
245+
func RegisterIf(metric Metric, enabled func() bool) Metric {
246+
if enabled() {
247+
if err := metric.Register(); err != nil {
248+
panic(err)
249+
}
250+
return metric
251+
}
252+
return &disabledMetric{name: metric.Name()}
253+
}
254+
207255
// NewSum creates a new Metric with an aggregation type of Sum (the values will be cumulative).
208256
// That means that data collected by the new Metric will be summed before export.
209257
func NewSum(name, description string, opts ...Options) Metric {

monitoring/monitoring_test.go

+31
Original file line numberDiff line numberDiff line change
@@ -69,10 +69,24 @@ var (
6969
"test_gauge",
7070
"Testing gauge functionality",
7171
)
72+
73+
testDisabledSum = monitoring.NewSum(
74+
"events_disabled_total",
75+
"Number of events observed, by name and kind",
76+
monitoring.WithLabels(name, kind),
77+
)
78+
79+
testConditionalSum = monitoring.NewSum(
80+
"events_conditional_total",
81+
"Number of events observed, by name and kind",
82+
monitoring.WithLabels(name, kind),
83+
)
7284
)
7385

7486
func init() {
7587
monitoring.MustRegister(testSum, hookSum, int64Sum, testDistribution, testGauge)
88+
testDisabledSum = monitoring.RegisterIf(testDisabledSum, func() bool { return false })
89+
testConditionalSum = monitoring.RegisterIf(testConditionalSum, func() bool { return true })
7690
}
7791

7892
func TestSum(t *testing.T) {
@@ -141,6 +155,23 @@ func TestSum(t *testing.T) {
141155
}
142156
}
143157

158+
func TestRegisterIfSum(t *testing.T) {
159+
testDisabledSum.With(name.Value("foo"), kind.Value("bar")).Increment()
160+
var err error
161+
_, err = view.RetrieveData(testDisabledSum.Name())
162+
if err == nil || !strings.EqualFold(err.Error(), "cannot retrieve data; view \"events_disabled_total\" is not registered") {
163+
t.Errorf("failure validating disabled metrics. exptected error but got %v", err)
164+
}
165+
testConditionalSum.With(name.Value("foo"), kind.Value("bar")).Increment()
166+
rows, err := view.RetrieveData(testConditionalSum.Name())
167+
if err != nil {
168+
t.Errorf("exptected to register metric %s but got %v", testConditionalSum.Name(), err)
169+
}
170+
if len(rows) == 0 {
171+
t.Errorf("exptected to metric %s has values but got %v", testConditionalSum.Name(), len(rows))
172+
}
173+
}
174+
144175
func TestGauge(t *testing.T) {
145176
exp := &testExporter{rows: make(map[string][]*view.Row)}
146177
view.RegisterExporter(exp)

0 commit comments

Comments
 (0)