Skip to content

Commit

Permalink
cleanup again
Browse files Browse the repository at this point in the history
Signed-off-by: Tim Vaillancourt <[email protected]>
  • Loading branch information
timvaillancourt committed Jul 6, 2023
1 parent 341d26f commit 40bb017
Showing 1 changed file with 12 additions and 5 deletions.
17 changes: 12 additions & 5 deletions stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
}
Expand All @@ -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 {
Expand Down Expand Up @@ -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 {
Expand All @@ -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
}

0 comments on commit 40bb017

Please sign in to comment.