Bläddra i källkod

Merge pull request #53 from 9seconds/prometheus-mtg-only

Use mtg metrics only for prometheus endpoint
tags/0.15.1^2
Sergey Arkhipov 7 år sedan
förälder
incheckning
47fb5c23cb
Inget konto är kopplat till bidragsgivarens mejladress
3 ändrade filer med 18 tillägg och 8 borttagningar
  1. 1
    1
      stats/init.go
  2. 15
    4
      stats/prometheus.go
  3. 2
    3
      stats/server.go

+ 1
- 1
stats/init.go Visa fil

@@ -22,7 +22,7 @@ func Init(conf *config.Config) error {
22 22
 	go prometheus.run()
23 23
 
24 24
 	go NewStats(conf).start()
25
-	go startServer(conf)
25
+	go startServer(conf, prometheus.getHTTPHandler())
26 26
 
27 27
 	return nil
28 28
 }

+ 15
- 4
stats/prometheus.go Visa fil

@@ -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,

+ 2
- 3
stats/server.go Visa fil

@@ -4,13 +4,12 @@ import (
4 4
 	"encoding/json"
5 5
 	"net/http"
6 6
 
7
-	"github.com/prometheus/client_golang/prometheus/promhttp"
8 7
 	"go.uber.org/zap"
9 8
 
10 9
 	"github.com/9seconds/mtg/config"
11 10
 )
12 11
 
13
-func startServer(conf *config.Config) {
12
+func startServer(conf *config.Config, prometheusHandler http.Handler) {
14 13
 	log := zap.S().Named("stats")
15 14
 
16 15
 	http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
@@ -33,7 +32,7 @@ func startServer(conf *config.Config) {
33 32
 			log.Errorw("Cannot encode json", "error", err)
34 33
 		}
35 34
 	})
36
-	http.Handle("/prometheus/", promhttp.Handler())
35
+	http.Handle("/prometheus/", prometheusHandler)
37 36
 
38 37
 	if err := http.ListenAndServe(conf.StatAddr(), nil); err != nil {
39 38
 		log.Fatalw("Stats server has been stopped", "error", err)

Laddar…
Avbryt
Spara