|
|
@@ -1,10 +1,12 @@
|
|
1
|
1
|
package stats
|
|
2
|
2
|
|
|
3
|
3
|
import (
|
|
|
4
|
+ "net/http"
|
|
4
|
5
|
"time"
|
|
5
|
6
|
|
|
6
|
7
|
"github.com/juju/errors"
|
|
7
|
8
|
"github.com/prometheus/client_golang/prometheus"
|
|
|
9
|
+ "github.com/prometheus/client_golang/prometheus/promhttp"
|
|
8
|
10
|
|
|
9
|
11
|
"github.com/9seconds/mtg/config"
|
|
10
|
12
|
)
|
|
|
@@ -12,6 +14,8 @@ import (
|
|
12
|
14
|
const prometheusPollTime = time.Second
|
|
13
|
15
|
|
|
14
|
16
|
type prometheusExporter struct {
|
|
|
17
|
+ registry prometheus.Gatherer
|
|
|
18
|
+
|
|
15
|
19
|
connections *prometheus.GaugeVec
|
|
16
|
20
|
traffic *prometheus.GaugeVec
|
|
17
|
21
|
speed *prometheus.GaugeVec
|
|
|
@@ -36,7 +40,13 @@ func (p *prometheusExporter) run() {
|
|
36
|
40
|
}
|
|
37
|
41
|
}
|
|
38
|
42
|
|
|
|
43
|
+func (p *prometheusExporter) getHTTPHandler() http.Handler {
|
|
|
44
|
+ return promhttp.HandlerFor(p.registry, promhttp.HandlerOpts{})
|
|
|
45
|
+}
|
|
|
46
|
+
|
|
39
|
47
|
func newPrometheus(conf *config.Config) (*prometheusExporter, error) {
|
|
|
48
|
+ registry := prometheus.NewRegistry()
|
|
|
49
|
+
|
|
40
|
50
|
connections := prometheus.NewGaugeVec(prometheus.GaugeOpts{
|
|
41
|
51
|
Namespace: conf.Prometheus.Prefix,
|
|
42
|
52
|
Name: "connections",
|
|
|
@@ -58,20 +68,21 @@ func newPrometheus(conf *config.Config) (*prometheusExporter, error) {
|
|
58
|
68
|
Help: "How many crashes happened.",
|
|
59
|
69
|
})
|
|
60
|
70
|
|
|
61
|
|
- if err := prometheus.Register(connections); err != nil {
|
|
|
71
|
+ if err := registry.Register(connections); err != nil {
|
|
62
|
72
|
return nil, errors.Annotate(err, "Cannot register connections collector")
|
|
63
|
73
|
}
|
|
64
|
|
- if err := prometheus.Register(traffic); err != nil {
|
|
|
74
|
+ if err := registry.Register(traffic); err != nil {
|
|
65
|
75
|
return nil, errors.Annotate(err, "cannot register traffic collector")
|
|
66
|
76
|
}
|
|
67
|
|
- if err := prometheus.Register(speed); err != nil {
|
|
|
77
|
+ if err := registry.Register(speed); err != nil {
|
|
68
|
78
|
return nil, errors.Annotate(err, "cannot register speed collector")
|
|
69
|
79
|
}
|
|
70
|
|
- if err := prometheus.Register(crashes); err != nil {
|
|
|
80
|
+ if err := registry.Register(crashes); err != nil {
|
|
71
|
81
|
return nil, errors.Annotate(err, "cannot register crashes collector")
|
|
72
|
82
|
}
|
|
73
|
83
|
|
|
74
|
84
|
return &prometheusExporter{
|
|
|
85
|
+ registry: registry,
|
|
75
|
86
|
connections: connections,
|
|
76
|
87
|
traffic: traffic,
|
|
77
|
88
|
speed: speed,
|