@@ -3,7 +3,6 @@ package nebula
3
3
import (
4
4
"errors"
5
5
"fmt"
6
- "log"
7
6
"net"
8
7
"net/http"
9
8
"runtime"
@@ -19,10 +18,12 @@ import (
19
18
"github.com/slackhq/nebula/config"
20
19
)
21
20
21
+ type statsHandlerFunc func (listen , path string ) http.Handler
22
+
22
23
// startStats initializes stats from config. On success, if any further work
23
- // is needed to serve stats, it returns a func to handle that work. If no
24
+ // is needed to serve stats, it returns an http.Handler for that work. If no
24
25
// work is needed, it'll return nil. On failure, it returns nil, error.
25
- func startStats (l * logrus.Logger , c * config.C , buildVersion string , configTest bool ) (func (), error ) {
26
+ func startStats (l * logrus.Logger , c * config.C , listen , buildVersion string , configTest bool ) (f statsHandlerFunc , err error ) {
26
27
mType := c .GetString ("stats.type" , "" )
27
28
if mType == "" || mType == "none" {
28
29
return nil , nil
@@ -33,16 +34,14 @@ func startStats(l *logrus.Logger, c *config.C, buildVersion string, configTest b
33
34
return nil , fmt .Errorf ("stats.interval was an invalid duration: %s" , c .GetString ("stats.interval" , "" ))
34
35
}
35
36
36
- var startFn func ()
37
37
switch mType {
38
38
case "graphite" :
39
39
err := startGraphiteStats (l , interval , c , configTest )
40
40
if err != nil {
41
41
return nil , err
42
42
}
43
43
case "prometheus" :
44
- var err error
45
- startFn , err = startPrometheusStats (l , interval , c , buildVersion , configTest )
44
+ f , err = startPrometheusStats (l , interval , c , listen , buildVersion , configTest )
46
45
if err != nil {
47
46
return nil , err
48
47
}
@@ -56,7 +55,7 @@ func startStats(l *logrus.Logger, c *config.C, buildVersion string, configTest b
56
55
go metrics .CaptureDebugGCStats (metrics .DefaultRegistry , interval )
57
56
go metrics .CaptureRuntimeMemStats (metrics .DefaultRegistry , interval )
58
57
59
- return startFn , nil
58
+ return f , nil
60
59
}
61
60
62
61
func startGraphiteStats (l * logrus.Logger , i time.Duration , c * config.C , configTest bool ) error {
@@ -79,13 +78,12 @@ func startGraphiteStats(l *logrus.Logger, i time.Duration, c *config.C, configTe
79
78
return nil
80
79
}
81
80
82
- func startPrometheusStats (l * logrus.Logger , i time.Duration , c * config.C , buildVersion string , configTest bool ) (func () , error ) {
81
+ func startPrometheusStats (l * logrus.Logger , i time.Duration , c * config.C , listen , buildVersion string , configTest bool ) (statsHandlerFunc , error ) {
83
82
namespace := c .GetString ("stats.namespace" , "" )
84
83
subsystem := c .GetString ("stats.subsystem" , "" )
85
84
86
- listen := c .GetString ("stats.listen" , "" )
87
85
if listen == "" {
88
- return nil , fmt .Errorf ("stats .listen should not be empty " )
86
+ return nil , fmt .Errorf ("http .listen or stats.listen must be defined to use promtheus stats " )
89
87
}
90
88
91
89
path := c .GetString ("stats.path" , "" )
@@ -114,14 +112,13 @@ func startPrometheusStats(l *logrus.Logger, i time.Duration, c *config.C, buildV
114
112
pr .MustRegister (g )
115
113
g .Set (1 )
116
114
117
- var startFn func ()
115
+ var f statsHandlerFunc
118
116
if ! configTest {
119
- startFn = func () {
117
+ f = func (listen , path string ) http. Handler {
120
118
l .Infof ("Prometheus stats listening on %s at %s" , listen , path )
121
- http .Handle (path , promhttp .HandlerFor (pr , promhttp.HandlerOpts {ErrorLog : l }))
122
- log .Fatal (http .ListenAndServe (listen , nil ))
119
+ return promhttp .HandlerFor (pr , promhttp.HandlerOpts {ErrorLog : l })
123
120
}
124
121
}
125
122
126
- return startFn , nil
123
+ return f , nil
127
124
}
0 commit comments