Skip to content

Commit

Permalink
Inject prometheus Gatherer
Browse files Browse the repository at this point in the history
Signed-off-by: Augustin Husson <[email protected]>
  • Loading branch information
Nexucis committed Apr 17, 2024
1 parent 3b19519 commit 62af8f3
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 6 deletions.
11 changes: 8 additions & 3 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 7 additions & 3 deletions echo/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -44,6 +47,7 @@ type metrics struct {
// * https:/prometheus/client_golang/issues/622g
disableCompression bool
promRegisterer prometheus.Registerer
promGatherer prometheus.Gatherer
}

func (m *metrics) RegisterRoute(e *echo.Echo) {
Expand All @@ -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,
},
),
Expand Down

0 comments on commit 62af8f3

Please sign in to comment.