Skip to content

Commit

Permalink
cast to Adder/Observer interfaces only once
Browse files Browse the repository at this point in the history
Signed-off-by: Shreyas Srivatsan <[email protected]>
  • Loading branch information
shreyassrivatsan committed Feb 29, 2020
1 parent f821e64 commit c026527
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 19 deletions.
6 changes: 3 additions & 3 deletions metrics/metricstest/local.go
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ func (l *localTimer) Record(d time.Duration) {
l.localBackend.RecordTimer(l.name, l.tags, d)
}

func (l *localTimer) RecordWithExemplar(d time.Duration, t string) {
func (l *localTimer) RecordWithExemplar(d time.Duration, _ string) {
l.localBackend.RecordTimer(l.name, l.tags, d)
}

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

func (l *localHistogram) RecordWithExemplar(v float64, t string) {
func (l *localHistogram) RecordWithExemplar(v float64, _ string) {
l.localBackend.RecordHistogram(l.name, l.tags, v)
}

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

func (l *localCounter) IncWithExemplar(delta int64, t string) {
func (l *localCounter) IncWithExemplar(delta int64, _ string) {
l.localBackend.IncCounter(l.name, l.tags, delta)
}

Expand Down
52 changes: 36 additions & 16 deletions metrics/prometheus/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ const (
SeparatorColon = ':'

traceIDTag = "trace_id"
spanIDTag = "span_id"
)

// Option is a function that sets some option for the Factory constructor.
Expand Down Expand Up @@ -141,8 +140,16 @@ func (f *Factory) Counter(options metrics.Options) metrics.Counter {
Help: help,
}
cv := f.cache.getOrMakeCounterVec(opts, labelNames)

ctr := cv.WithLabelValues(f.tagsAsLabelValues(labelNames, tags)...)
exemplarAdder, ok := ctr.(prometheus.ExemplarAdder)
if !ok {
exemplarAdder = nil
}

return &counter{
counter: cv.WithLabelValues(f.tagsAsLabelValues(labelNames, tags)...),
counter: ctr,
exemplarAdder: exemplarAdder,
}
}

Expand Down Expand Up @@ -180,8 +187,14 @@ func (f *Factory) Timer(options metrics.TimerOptions) metrics.Timer {
Buckets: asFloatBuckets(options.Buckets),
}
hv := f.cache.getOrMakeHistogramVec(opts, labelNames)
hist := hv.WithLabelValues(f.tagsAsLabelValues(labelNames, tags)...)
exemplarObserver, ok := hist.(prometheus.ExemplarObserver)
if !ok {
exemplarObserver = nil
}
return &timer{
histogram: hv.WithLabelValues(f.tagsAsLabelValues(labelNames, tags)...),
histogram: hist,
exemplarObserver: exemplarObserver,
}
}

Expand All @@ -208,8 +221,14 @@ func (f *Factory) Histogram(options metrics.HistogramOptions) metrics.Histogram
Buckets: options.Buckets,
}
hv := f.cache.getOrMakeHistogramVec(opts, labelNames)
hist := hv.WithLabelValues(f.tagsAsLabelValues(labelNames, tags)...)
exemplarObserver, ok := hist.(prometheus.ExemplarObserver)
if !ok {
exemplarObserver = nil
}
return &histogram{
histogram: hv.WithLabelValues(f.tagsAsLabelValues(labelNames, tags)...),
histogram: hist,
exemplarObserver: exemplarObserver,
}
}

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

type counter struct {
counter prometheus.Counter
counter prometheus.Counter
exemplarAdder prometheus.ExemplarAdder
}

func (c *counter) Inc(v int64) {
c.counter.Add(float64(v))
}

func (c *counter) IncWithExemplar(v int64, traceID string) {
ctr, ok := c.counter.(prometheus.ExemplarAdder)
if ok {
if c.exemplarAdder != nil {
labels := make(map[string]string, 1)
labels[traceIDTag] = traceID
ctr.AddWithExemplar(float64(v), labels)
c.exemplarAdder.AddWithExemplar(float64(v), labels)
return
}
c.Inc(v)
}
Expand All @@ -249,38 +269,38 @@ type observer interface {
}

type timer struct {
histogram observer
histogram observer
exemplarObserver prometheus.ExemplarObserver
}

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

func (t *timer) RecordWithExemplar(v time.Duration, traceID string) {
hist, ok := t.histogram.(prometheus.ExemplarObserver)
if ok {
if t.exemplarObserver != nil {
labels := make(map[string]string, 1)
labels[traceIDTag] = traceID
hist.ObserveWithExemplar(float64(v.Nanoseconds())/float64(time.Second/time.Nanosecond), labels)
t.exemplarObserver.ObserveWithExemplar(float64(v.Nanoseconds())/float64(time.Second/time.Nanosecond), labels)
return
}
t.Record(v)
}

type histogram struct {
histogram observer
histogram observer
exemplarObserver prometheus.ExemplarObserver
}

func (h *histogram) Record(v float64) {
h.histogram.Observe(v)
}

func (h *histogram) RecordWithExemplar(v float64, traceID string) {
hist, ok := h.histogram.(prometheus.ExemplarObserver)
if ok {
if h.exemplarObserver != nil {
labels := make(map[string]string, 1)
labels[traceIDTag] = traceID
hist.ObserveWithExemplar(v, labels)
h.exemplarObserver.ObserveWithExemplar(v, labels)
return
}
h.Record(v)
Expand Down

0 comments on commit c026527

Please sign in to comment.