diff --git a/app/app.go b/app/app.go index 037f41e..c36f822 100644 --- a/app/app.go +++ b/app/app.go @@ -169,12 +169,17 @@ func (r *Runner) WithTaskHelpers(t ...taskhelper.Helper) *Runner { } func (r *Runner) WithDefaultHTTPServer(metricNamespace string) *Runner { - return r.WithDefaultHTTPServerAndPrometheusRegisterer(metricNamespace, prometheus.DefaultRegisterer) + return r.WithDefaultHTTPServerAndPrometheusRegisterer(metricNamespace, prometheus.DefaultRegisterer, prometheus.DefaultGatherer) } -func (r *Runner) WithDefaultHTTPServerAndPrometheusRegisterer(metricNamespace string, registerer prometheus.Registerer) *Runner { +// WithDefaultHTTPServerAndPrometheusRegisterer is here to create a default HTTP server with already the metrics API setup. +// Usually you will use it as follows: +// +// promRegistry := prometheus.NewRegistry() +// app.NewRunner().WithDefaultHTTPServerAndPrometheusRegisterer(metricNamespace, promRegistry, promRegistry) +func (r *Runner) WithDefaultHTTPServerAndPrometheusRegisterer(metricNamespace string, registerer prometheus.Registerer, gatherer prometheus.Gatherer) *Runner { r.serverBuilder = echo.NewBuilder(addr). - APIRegistration(echo.NewMetricsAPI(true, registerer)). + APIRegistration(echo.NewMetricsAPI(true, registerer, gatherer)). MetricNamespace(metricNamespace). PrometheusRegisterer(registerer) return r diff --git a/echo/metrics.go b/echo/metrics.go index 2f2a149..0007bd8 100644 --- a/echo/metrics.go +++ b/echo/metrics.go @@ -30,8 +30,11 @@ func init() { flag.StringVar(&telemetryPath, "web.telemetry-path", "/metrics", "Path under which to expose metrics.") } -func NewMetricsAPI(disableCompression bool, r prometheus.Registerer) Register { - return &metrics{disableCompression: disableCompression, promRegisterer: r} +func NewMetricsAPI(disableCompression bool, r prometheus.Registerer, gatherer prometheus.Gatherer) Register { + return &metrics{ + disableCompression: disableCompression, + promRegisterer: r, + promGatherer: gatherer} } // metrics is a struct than handles the endpoint /metrics @@ -44,6 +47,7 @@ type metrics struct { // * https://github.com/prometheus/client_golang/issues/622g disableCompression bool promRegisterer prometheus.Registerer + promGatherer prometheus.Gatherer } func (m *metrics) RegisterRoute(e *echo.Echo) { @@ -53,7 +57,7 @@ func (m *metrics) RegisterRoute(e *echo.Echo) { e.GET(telemetryPath, echo.WrapHandler( promhttp.InstrumentMetricHandler( m.promRegisterer, promhttp.HandlerFor( - prometheus.DefaultGatherer, promhttp.HandlerOpts{ + m.promGatherer, promhttp.HandlerOpts{ DisableCompression: m.disableCompression, }, ),