From 40bb017f8eb1b96bc99a8e6249b73041cd718c01 Mon Sep 17 00:00:00 2001 From: Tim Vaillancourt Date: Fri, 7 Jul 2023 01:20:05 +0200 Subject: [PATCH] cleanup again Signed-off-by: Tim Vaillancourt --- stats.go | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/stats.go b/stats.go index 1e4bbd3ca..5149cf938 100644 --- a/stats.go +++ b/stats.go @@ -21,7 +21,7 @@ import ( // startStats initializes stats from config. On success, if any further work // is needed to serve stats, it returns an http.Handler for that work. If no // work is needed, it'll return nil. On failure, it returns nil, error. -func startStats(l *logrus.Logger, c *config.C, listen, buildVersion string, configTest bool) (handler http.Handler, err error) { +func startStats(l *logrus.Logger, c *config.C, listen, buildVersion string, configTest bool) (h http.Handler, err error) { mType := c.GetString("stats.type", "") if mType == "" || mType == "none" { return nil, nil @@ -39,7 +39,7 @@ func startStats(l *logrus.Logger, c *config.C, listen, buildVersion string, conf return nil, err } case "prometheus": - handler, err = startPrometheusStats(l, interval, c, listen, buildVersion, configTest) + h, err = startPrometheusStats(l, interval, c, listen, buildVersion, configTest) if err != nil { return nil, err } @@ -53,7 +53,7 @@ func startStats(l *logrus.Logger, c *config.C, listen, buildVersion string, conf go metrics.CaptureDebugGCStats(metrics.DefaultRegistry, interval) go metrics.CaptureRuntimeMemStats(metrics.DefaultRegistry, interval) - return handler, nil + return h, nil } func startGraphiteStats(l *logrus.Logger, i time.Duration, c *config.C, configTest bool) error { @@ -84,6 +84,11 @@ func startPrometheusStats(l *logrus.Logger, i time.Duration, c *config.C, listen return nil, fmt.Errorf("http.listen or stats.listen must be defined to use promtheus stats") } + path := c.GetString("stats.path", "") + if path == "" { + return nil, fmt.Errorf("stats.path should not be empty") + } + pr := prometheus.NewRegistry() pClient := mp.NewPrometheusProvider(metrics.DefaultRegistry, namespace, subsystem, pr, i) if !configTest { @@ -105,9 +110,11 @@ func startPrometheusStats(l *logrus.Logger, i time.Duration, c *config.C, listen pr.MustRegister(g) g.Set(1) + var handler http.Handler if !configTest { - return promhttp.HandlerFor(pr, promhttp.HandlerOpts{ErrorLog: l}), nil + handler = promhttp.HandlerFor(pr, promhttp.HandlerOpts{ErrorLog: l}) + l.Infof("Prometheus stats listening on %s at %s", listen, path) } - return nil, nil + return handler, nil }