From f2b9562e90064c62c8d046712b501a792fc5a1b8 Mon Sep 17 00:00:00 2001 From: Kevin Klues Date: Wed, 15 May 2019 15:48:35 +0200 Subject: [PATCH] Add support for specifying 'Gatherer' in prometheus Reporter options. (#80) Passing the Gatherer is required to be able return the HTTPHandler appropriate for a customer Registerer that is passed in. Without this change, the HTTPHandler is *always* associated with the Gatherer for the promhttp.DefaultRegisterer, which is confusing if a custom Registerer is passed. --- prometheus/reporter.go | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/prometheus/reporter.go b/prometheus/reporter.go index 8d11a8c7..04ef5fd9 100644 --- a/prometheus/reporter.go +++ b/prometheus/reporter.go @@ -143,6 +143,7 @@ type metricID string type reporter struct { sync.RWMutex registerer prom.Registerer + gatherer prom.Gatherer timerType TimerType objectives map[float64]float64 buckets []float64 @@ -224,7 +225,7 @@ func (m noopMetric) DurationBucket(lower, upper time.Duration) tally.CachedHisto } func (r *reporter) HTTPHandler() http.Handler { - return promhttp.Handler() + return promhttp.HandlerFor(r.gatherer, promhttp.HandlerOpts{}) } // TimerType describes a type of timer @@ -244,6 +245,10 @@ type Options struct { // metrics with. Use nil to specify the default registerer. Registerer prom.Registerer + // Gatherer is the prometheus gatherer to gather + // metrics with. Use nil to specify the default gatherer. + Gatherer prom.Gatherer + // DefaultTimerType is the default type timer type to create // when using timers. It's default value is a summary timer type. DefaultTimerType TimerType @@ -269,6 +274,9 @@ func NewReporter(opts Options) Reporter { if opts.Registerer == nil { opts.Registerer = prom.DefaultRegisterer } + if opts.Gatherer == nil { + opts.Gatherer = prom.DefaultGatherer + } if opts.DefaultHistogramBuckets == nil { opts.DefaultHistogramBuckets = DefaultHistogramBuckets() } @@ -280,8 +288,10 @@ func NewReporter(opts Options) Reporter { panic(err) } } + return &reporter{ registerer: opts.Registerer, + gatherer: opts.Gatherer, timerType: opts.DefaultTimerType, buckets: opts.DefaultHistogramBuckets, objectives: opts.DefaultSummaryObjectives,