Skip to content

Commit 903a8fe

Browse files
committed
Add compile time type assertions
1 parent 6c6ae51 commit 903a8fe

File tree

1 file changed

+75
-59
lines changed

1 file changed

+75
-59
lines changed

pkg/metrics/noop.go

Lines changed: 75 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -12,122 +12,136 @@ import (
1212
"net/http"
1313
)
1414

15-
type counterStub struct{}
15+
type counterNoop struct{}
1616

17-
func (c counterStub) Desc() *Desc { return &Desc{} }
18-
func (c counterStub) Write(_ *MetricDTO) error { return nil }
19-
func (c counterStub) Describe(_ chan<- *Desc) { return }
20-
func (c counterStub) Collect(_ chan<- Metric) { return }
21-
func (c counterStub) Inc() { return }
22-
func (c counterStub) Add(_ float64) { return }
17+
func (c counterNoop) Desc() *Desc { return &Desc{} }
18+
func (c counterNoop) Write(_ *MetricDTO) error { return nil }
19+
func (c counterNoop) Describe(_ chan<- *Desc) { return }
20+
func (c counterNoop) Collect(_ chan<- Metric) { return }
21+
func (c counterNoop) Inc() { return }
22+
func (c counterNoop) Add(_ float64) { return }
2323

2424
func NewCounter(_ CounterOpts) Counter {
25-
return &counterStub{}
25+
return &counterNoop{}
2626
}
2727

28-
type counterVecStub struct{}
28+
var _ CounterMetricVector = (*counterVecNoop)(nil)
2929

30-
func (c counterVecStub) WithLabelValues(lvs ...string) Counter {
30+
type counterVecNoop struct{}
31+
32+
func (c counterVecNoop) WithLabelValues(lvs ...string) Counter {
3133
return NewCounter(CounterOpts{})
3234
}
3335

3436
func NewCounterVec(opts CounterOpts, names []string) CounterMetricVector {
35-
return &counterVecStub{}
37+
return &counterVecNoop{}
3638
}
3739

38-
type gaugeStub struct{}
40+
type gaugeNoop struct{}
3941

40-
func (g gaugeStub) Desc() *Desc { return &Desc{} }
41-
func (g gaugeStub) Write(_ *MetricDTO) error { return nil }
42-
func (g gaugeStub) Describe(_ chan<- *Desc) { return }
43-
func (g gaugeStub) Collect(_ chan<- Metric) { return }
44-
func (g gaugeStub) Set(_ float64) { return }
45-
func (g gaugeStub) Inc() { return }
46-
func (g gaugeStub) Dec() { return }
47-
func (g gaugeStub) Add(_ float64) { return }
48-
func (g gaugeStub) Sub(_ float64) { return }
49-
func (g gaugeStub) SetToCurrentTime() { return }
42+
func (g gaugeNoop) Desc() *Desc { return &Desc{} }
43+
func (g gaugeNoop) Write(_ *MetricDTO) error { return nil }
44+
func (g gaugeNoop) Describe(_ chan<- *Desc) { return }
45+
func (g gaugeNoop) Collect(_ chan<- Metric) { return }
46+
func (g gaugeNoop) Set(_ float64) { return }
47+
func (g gaugeNoop) Inc() { return }
48+
func (g gaugeNoop) Dec() { return }
49+
func (g gaugeNoop) Add(_ float64) { return }
50+
func (g gaugeNoop) Sub(_ float64) { return }
51+
func (g gaugeNoop) SetToCurrentTime() { return }
5052

5153
func NewGauge(_ GaugeOpts) Gauge {
52-
return &gaugeStub{}
54+
return &gaugeNoop{}
5355
}
5456

55-
type gaugeVecStub struct{}
57+
var _ GaugeMetricVector = (*gaugeVecNoop)(nil)
58+
59+
type gaugeVecNoop struct{}
5660

57-
func (g gaugeVecStub) WithLabelValues(lvs ...string) Gauge {
61+
func (g gaugeVecNoop) WithLabelValues(lvs ...string) Gauge {
5862
return NewGauge(GaugeOpts{})
5963
}
6064

6165
func NewGaugeVec(opts GaugeOpts, names []string) GaugeMetricVector {
62-
return &gaugeVecStub{}
66+
return &gaugeVecNoop{}
6367
}
6468

65-
type histogramStub struct{}
69+
var _ Histogram = (*histogramNoop)(nil)
6670

67-
func (h histogramStub) Desc() *Desc { return &Desc{} }
68-
func (h histogramStub) Write(_ *MetricDTO) error { return nil }
69-
func (h histogramStub) Describe(_ chan<- *Desc) { return }
70-
func (h histogramStub) Collect(_ chan<- Metric) { return }
71-
func (h histogramStub) Observe(_ float64) { return }
71+
type histogramNoop struct{}
72+
73+
func (h histogramNoop) Desc() *Desc { return &Desc{} }
74+
func (h histogramNoop) Write(_ *MetricDTO) error { return nil }
75+
func (h histogramNoop) Describe(_ chan<- *Desc) { return }
76+
func (h histogramNoop) Collect(_ chan<- Metric) { return }
77+
func (h histogramNoop) Observe(_ float64) { return }
7278

7379
func NewHistogram(_ HistogramOpts) Histogram {
74-
return &histogramStub{}
80+
return &histogramNoop{}
7581
}
7682

77-
type histogramVecStub struct{}
83+
var _ HistogramMetricVector = (*histogramVecNoop)(nil)
84+
85+
type histogramVecNoop struct{}
7886

79-
func (h histogramVecStub) Describe(descs chan<- *Desc) { return }
80-
func (h histogramVecStub) Collect(metrics chan<- Metric) { return }
81-
func (h histogramVecStub) WithLabelValues(lvs ...string) Observer {
87+
func (h histogramVecNoop) Describe(descs chan<- *Desc) { return }
88+
func (h histogramVecNoop) Collect(metrics chan<- Metric) { return }
89+
func (h histogramVecNoop) WithLabelValues(lvs ...string) Observer {
8290
return NewHistogram(HistogramOpts{})
8391
}
8492

8593
func NewHistogramVec(opts HistogramOpts, names []string) HistogramMetricVector {
86-
return &histogramVecStub{}
94+
return &histogramVecNoop{}
8795
}
8896

89-
type summaryStub struct{}
97+
var _ Summary = (*summaryNoop)(nil)
9098

91-
func (s summaryStub) Desc() *Desc { return &Desc{} }
92-
func (s summaryStub) Write(_ *MetricDTO) error { return nil }
93-
func (s summaryStub) Describe(_ chan<- *Desc) { return }
94-
func (s summaryStub) Collect(_ chan<- Metric) { return }
95-
func (s summaryStub) Observe(_ float64) { return }
99+
type summaryNoop struct{}
100+
101+
func (s summaryNoop) Desc() *Desc { return &Desc{} }
102+
func (s summaryNoop) Write(_ *MetricDTO) error { return nil }
103+
func (s summaryNoop) Describe(_ chan<- *Desc) { return }
104+
func (s summaryNoop) Collect(_ chan<- Metric) { return }
105+
func (s summaryNoop) Observe(_ float64) { return }
96106

97107
func NewSummary(_ SummaryOpts) Summary {
98-
return &summaryStub{}
108+
return &summaryNoop{}
99109
}
100110

101-
type collectorStub struct{}
111+
var _ Collector = (*collectorNoop)(nil)
112+
113+
type collectorNoop struct{}
102114

103-
func (c collectorStub) Describe(_ chan<- *Desc) { return }
104-
func (c collectorStub) Collect(_ chan<- Metric) { return }
115+
func (c collectorNoop) Describe(_ chan<- *Desc) { return }
116+
func (c collectorNoop) Collect(_ chan<- Metric) { return }
105117

106118
func NewGoCollector() Collector {
107-
return &collectorStub{}
119+
return &collectorNoop{}
108120
}
109121

110122
func NewProcessCollector(opts ProcessCollectorOpts) Collector {
111-
return &collectorStub{}
123+
return &collectorNoop{}
112124
}
113125

114-
type registryStub struct{}
126+
var _ MetricsRegistererGatherer = (*registryNoop)(nil)
115127

116-
func (r registryStub) Register(collector Collector) error {
128+
type registryNoop struct{}
129+
130+
func (r registryNoop) Register(collector Collector) error {
117131
return nil
118132
}
119133

120-
func (r registryStub) Unregister(collector Collector) bool {
134+
func (r registryNoop) Unregister(collector Collector) bool {
121135
return true
122136
}
123137

124-
func (r registryStub) MustRegister(collector ...Collector) { return }
125-
func (r registryStub) Gather() ([]*MetricFamily, error) {
138+
func (r registryNoop) MustRegister(collector ...Collector) { return }
139+
func (r registryNoop) Gather() ([]*MetricFamily, error) {
126140
return []*MetricFamily{}, nil
127141
}
128142

129143
func NewRegistry() MetricsRegistererGatherer {
130-
return &registryStub{}
144+
return &registryNoop{}
131145
}
132146

133147
func NewExporter(o ExporterOptions) error {
@@ -146,12 +160,14 @@ func HandlerFor(_ MetricsRegistererGatherer, _ HandlerOpts) http.Handler {
146160
return http.HandlerFunc(func(_ http.ResponseWriter, _ *http.Request) { return })
147161
}
148162

149-
type encoderStub struct{}
163+
var _ Encoder = (*encoderNoop)(nil)
164+
165+
type encoderNoop struct{}
150166

151-
func (e encoderStub) Encode(family *MetricFamily) error { return nil }
167+
func (e encoderNoop) Encode(family *MetricFamily) error { return nil }
152168

153169
func NewEncoder(w io.Writer, format Format, options ...EncoderOption) Encoder {
154-
return &encoderStub{}
170+
return &encoderNoop{}
155171
}
156172

157173
func NewFormat(t FormatType) Format {

0 commit comments

Comments
 (0)