Skip to content
This repository was archived by the owner on Aug 17, 2025. It is now read-only.

Commit 7cbf4b2

Browse files
cast to Adder/Observer interfaces only once
1 parent cfc27c9 commit 7cbf4b2

File tree

2 files changed

+39
-19
lines changed

2 files changed

+39
-19
lines changed

metrics/metricstest/local.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,7 @@ func (l *localTimer) Record(d time.Duration) {
276276
l.localBackend.RecordTimer(l.name, l.tags, d)
277277
}
278278

279-
func (l *localTimer) RecordWithExemplar(d time.Duration, t string) {
279+
func (l *localTimer) RecordWithExemplar(d time.Duration, _ string) {
280280
l.localBackend.RecordTimer(l.name, l.tags, d)
281281
}
282282

@@ -288,7 +288,7 @@ func (l *localHistogram) Record(v float64) {
288288
l.localBackend.RecordHistogram(l.name, l.tags, v)
289289
}
290290

291-
func (l *localHistogram) RecordWithExemplar(v float64, t string) {
291+
func (l *localHistogram) RecordWithExemplar(v float64, _ string) {
292292
l.localBackend.RecordHistogram(l.name, l.tags, v)
293293
}
294294

@@ -300,7 +300,7 @@ func (l *localCounter) Inc(delta int64) {
300300
l.localBackend.IncCounter(l.name, l.tags, delta)
301301
}
302302

303-
func (l *localCounter) IncWithExemplar(delta int64, t string) {
303+
func (l *localCounter) IncWithExemplar(delta int64, _ string) {
304304
l.localBackend.IncCounter(l.name, l.tags, delta)
305305
}
306306

metrics/prometheus/factory.go

Lines changed: 36 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@ const (
5151
SeparatorColon = ':'
5252

5353
traceIDTag = "trace_id"
54-
spanIDTag = "span_id"
5554
)
5655

5756
// Option is a function that sets some option for the Factory constructor.
@@ -141,8 +140,16 @@ func (f *Factory) Counter(options metrics.Options) metrics.Counter {
141140
Help: help,
142141
}
143142
cv := f.cache.getOrMakeCounterVec(opts, labelNames)
143+
144+
ctr := cv.WithLabelValues(f.tagsAsLabelValues(labelNames, tags)...)
145+
exemplarAdder, ok := ctr.(prometheus.ExemplarAdder)
146+
if !ok {
147+
exemplarAdder = nil
148+
}
149+
144150
return &counter{
145-
counter: cv.WithLabelValues(f.tagsAsLabelValues(labelNames, tags)...),
151+
counter: ctr,
152+
exemplarAdder: exemplarAdder,
146153
}
147154
}
148155

@@ -180,8 +187,14 @@ func (f *Factory) Timer(options metrics.TimerOptions) metrics.Timer {
180187
Buckets: asFloatBuckets(options.Buckets),
181188
}
182189
hv := f.cache.getOrMakeHistogramVec(opts, labelNames)
190+
hist := hv.WithLabelValues(f.tagsAsLabelValues(labelNames, tags)...)
191+
exemplarObserver, ok := hist.(prometheus.ExemplarObserver)
192+
if !ok {
193+
exemplarObserver = nil
194+
}
183195
return &timer{
184-
histogram: hv.WithLabelValues(f.tagsAsLabelValues(labelNames, tags)...),
196+
histogram: hist,
197+
exemplarObserver: exemplarObserver,
185198
}
186199
}
187200

@@ -208,8 +221,14 @@ func (f *Factory) Histogram(options metrics.HistogramOptions) metrics.Histogram
208221
Buckets: options.Buckets,
209222
}
210223
hv := f.cache.getOrMakeHistogramVec(opts, labelNames)
224+
hist := hv.WithLabelValues(f.tagsAsLabelValues(labelNames, tags)...)
225+
exemplarObserver, ok := hist.(prometheus.ExemplarObserver)
226+
if !ok {
227+
exemplarObserver = nil
228+
}
211229
return &histogram{
212-
histogram: hv.WithLabelValues(f.tagsAsLabelValues(labelNames, tags)...),
230+
histogram: hist,
231+
exemplarObserver: exemplarObserver,
213232
}
214233
}
215234

@@ -219,19 +238,20 @@ func (f *Factory) Namespace(scope metrics.NSOptions) metrics.Factory {
219238
}
220239

221240
type counter struct {
222-
counter prometheus.Counter
241+
counter prometheus.Counter
242+
exemplarAdder prometheus.ExemplarAdder
223243
}
224244

225245
func (c *counter) Inc(v int64) {
226246
c.counter.Add(float64(v))
227247
}
228248

229249
func (c *counter) IncWithExemplar(v int64, traceID string) {
230-
ctr, ok := c.counter.(prometheus.ExemplarAdder)
231-
if ok {
250+
if c.exemplarAdder != nil {
232251
labels := make(map[string]string, 1)
233252
labels[traceIDTag] = traceID
234-
ctr.AddWithExemplar(float64(v), labels)
253+
c.exemplarAdder.AddWithExemplar(float64(v), labels)
254+
return
235255
}
236256
c.Inc(v)
237257
}
@@ -249,38 +269,38 @@ type observer interface {
249269
}
250270

251271
type timer struct {
252-
histogram observer
272+
histogram observer
273+
exemplarObserver prometheus.ExemplarObserver
253274
}
254275

255276
func (t *timer) Record(v time.Duration) {
256277
t.histogram.Observe(float64(v.Nanoseconds()) / float64(time.Second/time.Nanosecond))
257278
}
258279

259280
func (t *timer) RecordWithExemplar(v time.Duration, traceID string) {
260-
hist, ok := t.histogram.(prometheus.ExemplarObserver)
261-
if ok {
281+
if t.exemplarObserver != nil {
262282
labels := make(map[string]string, 1)
263283
labels[traceIDTag] = traceID
264-
hist.ObserveWithExemplar(float64(v.Nanoseconds())/float64(time.Second/time.Nanosecond), labels)
284+
t.exemplarObserver.ObserveWithExemplar(float64(v.Nanoseconds())/float64(time.Second/time.Nanosecond), labels)
265285
return
266286
}
267287
t.Record(v)
268288
}
269289

270290
type histogram struct {
271-
histogram observer
291+
histogram observer
292+
exemplarObserver prometheus.ExemplarObserver
272293
}
273294

274295
func (h *histogram) Record(v float64) {
275296
h.histogram.Observe(v)
276297
}
277298

278299
func (h *histogram) RecordWithExemplar(v float64, traceID string) {
279-
hist, ok := h.histogram.(prometheus.ExemplarObserver)
280-
if ok {
300+
if h.exemplarObserver != nil {
281301
labels := make(map[string]string, 1)
282302
labels[traceIDTag] = traceID
283-
hist.ObserveWithExemplar(v, labels)
303+
h.exemplarObserver.ObserveWithExemplar(v, labels)
284304
return
285305
}
286306
h.Record(v)

0 commit comments

Comments
 (0)