Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Add support for prometheus with exemplars #79

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 46 additions & 40 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Gopkg.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

[[constraint]]
name = "github.com/prometheus/client_golang"
version = "1.1.0"
version = "1.4.1"

[[constraint]]
name = "github.com/stretchr/testify"
Expand Down
11 changes: 10 additions & 1 deletion metrics/counter.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,18 @@ type Counter interface {
Inc(int64)
}

// CounterWithExemplar tracks the number of times an event has occurred and
// supports exemplars
type CounterWithExemplar interface {
Counter
IncWithExemplar(int64, map[string]string)
}

// NullCounter counter that does nothing
var NullCounter Counter = nullCounter{}
var NullCounter CounterWithExemplar = nullCounter{}

type nullCounter struct{}

func (nullCounter) Inc(int64) {}

func (nullCounter) IncWithExemplar(int64, map[string]string) {}
11 changes: 10 additions & 1 deletion metrics/histogram.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,18 @@ type Histogram interface {
Record(float64)
}

// HistogramWithExemplar that keeps track of a distribution of values and
// supports exemplars.
type HistogramWithExemplar interface {
Histogram
RecordWithExemplar(float64, map[string]string)
}

// NullHistogram that does nothing
var NullHistogram Histogram = nullHistogram{}
var NullHistogram HistogramWithExemplar = nullHistogram{}

type nullHistogram struct{}

func (nullHistogram) Record(float64) {}

func (nullHistogram) RecordWithExemplar(float64, map[string]string) {}
59 changes: 53 additions & 6 deletions metrics/prometheus/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,8 +138,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 @@ -177,8 +185,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 @@ -205,8 +219,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 @@ -216,13 +236,22 @@ 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, l map[string]string) {
if c.exemplarAdder != nil {
c.exemplarAdder.AddWithExemplar(float64(v), l)
return
}
c.Inc(v)
}

type gauge struct {
gauge prometheus.Gauge
}
Expand All @@ -236,21 +265,39 @@ 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, l map[string]string) {
if t.exemplarObserver != nil {
t.exemplarObserver.ObserveWithExemplar(float64(v.Nanoseconds())/float64(time.Second/time.Nanosecond), l)
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, l map[string]string) {
if h.exemplarObserver != nil {
h.exemplarObserver.ObserveWithExemplar(v, l)
return
}
h.Record(v)
}

func (f *Factory) subScope(name string) string {
if f.scope == "" {
return f.normalize(name)
Expand Down
11 changes: 10 additions & 1 deletion metrics/timer.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,24 @@ import (
)

// Timer accumulates observations about how long some operation took,
// and also maintains a historgam of percentiles.
// and also maintains a histogram of percentiles.
type Timer interface {
// Records the time passed in.
Record(time.Duration)
}

// TimerWithExemplar accumulates observations about how long some operation
// took, maintains a histogram of percentiles and supports exemplars.
type TimerWithExemplar interface {
Timer
RecordWithExemplar(time.Duration, map[string]string)
}

// NullTimer timer that does nothing
var NullTimer Timer = nullTimer{}

type nullTimer struct{}

func (nullTimer) Record(time.Duration) {}

func (nullTimer) RecordWithExemplar(time.Duration, map[string]string) {}