Просмотр исходного кода

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

Use mtg metrics only for prometheus endpoint
tags/0.15.1^2
Sergey Arkhipov 7 лет назад
Родитель
Сommit
47fb5c23cb
Аккаунт пользователя с таким Email не найден
3 измененных файлов: 18 добавлений и 8 удалений
  1. 1
    1
      stats/init.go
  2. 15
    4
      stats/prometheus.go
  3. 2
    3
      stats/server.go

+ 1
- 1
stats/init.go Просмотреть файл

22
 	go prometheus.run()
22
 	go prometheus.run()
23
 
23
 
24
 	go NewStats(conf).start()
24
 	go NewStats(conf).start()
25
-	go startServer(conf)
25
+	go startServer(conf, prometheus.getHTTPHandler())
26
 
26
 
27
 	return nil
27
 	return nil
28
 }
28
 }

+ 15
- 4
stats/prometheus.go Просмотреть файл

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

+ 2
- 3
stats/server.go Просмотреть файл

4
 	"encoding/json"
4
 	"encoding/json"
5
 	"net/http"
5
 	"net/http"
6
 
6
 
7
-	"github.com/prometheus/client_golang/prometheus/promhttp"
8
 	"go.uber.org/zap"
7
 	"go.uber.org/zap"
9
 
8
 
10
 	"github.com/9seconds/mtg/config"
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
 	log := zap.S().Named("stats")
13
 	log := zap.S().Named("stats")
15
 
14
 
16
 	http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
15
 	http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
33
 			log.Errorw("Cannot encode json", "error", err)
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
 	if err := http.ListenAndServe(conf.StatAddr(), nil); err != nil {
37
 	if err := http.ListenAndServe(conf.StatAddr(), nil); err != nil {
39
 		log.Fatalw("Stats server has been stopped", "error", err)
38
 		log.Fatalw("Stats server has been stopped", "error", err)

Загрузка…
Отмена
Сохранить