mirror of
https://github.com/ScuroNeko/mtg.git
synced 2026-09-01 01:04:02 +03:00
Merge pull request #53 from 9seconds/prometheus-mtg-only
Use mtg metrics only for prometheus endpoint
This commit is contained in:
+1
-1
@@ -22,7 +22,7 @@ func Init(conf *config.Config) error {
|
|||||||
go prometheus.run()
|
go prometheus.run()
|
||||||
|
|
||||||
go NewStats(conf).start()
|
go NewStats(conf).start()
|
||||||
go startServer(conf)
|
go startServer(conf, prometheus.getHTTPHandler())
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|||||||
+15
-4
@@ -1,10 +1,12 @@
|
|||||||
package stats
|
package stats
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"net/http"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/juju/errors"
|
"github.com/juju/errors"
|
||||||
"github.com/prometheus/client_golang/prometheus"
|
"github.com/prometheus/client_golang/prometheus"
|
||||||
|
"github.com/prometheus/client_golang/prometheus/promhttp"
|
||||||
|
|
||||||
"github.com/9seconds/mtg/config"
|
"github.com/9seconds/mtg/config"
|
||||||
)
|
)
|
||||||
@@ -12,6 +14,8 @@ import (
|
|||||||
const prometheusPollTime = time.Second
|
const prometheusPollTime = time.Second
|
||||||
|
|
||||||
type prometheusExporter struct {
|
type prometheusExporter struct {
|
||||||
|
registry prometheus.Gatherer
|
||||||
|
|
||||||
connections *prometheus.GaugeVec
|
connections *prometheus.GaugeVec
|
||||||
traffic *prometheus.GaugeVec
|
traffic *prometheus.GaugeVec
|
||||||
speed *prometheus.GaugeVec
|
speed *prometheus.GaugeVec
|
||||||
@@ -36,7 +40,13 @@ func (p *prometheusExporter) run() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (p *prometheusExporter) getHTTPHandler() http.Handler {
|
||||||
|
return promhttp.HandlerFor(p.registry, promhttp.HandlerOpts{})
|
||||||
|
}
|
||||||
|
|
||||||
func newPrometheus(conf *config.Config) (*prometheusExporter, error) {
|
func newPrometheus(conf *config.Config) (*prometheusExporter, error) {
|
||||||
|
registry := prometheus.NewRegistry()
|
||||||
|
|
||||||
connections := prometheus.NewGaugeVec(prometheus.GaugeOpts{
|
connections := prometheus.NewGaugeVec(prometheus.GaugeOpts{
|
||||||
Namespace: conf.Prometheus.Prefix,
|
Namespace: conf.Prometheus.Prefix,
|
||||||
Name: "connections",
|
Name: "connections",
|
||||||
@@ -58,20 +68,21 @@ func newPrometheus(conf *config.Config) (*prometheusExporter, error) {
|
|||||||
Help: "How many crashes happened.",
|
Help: "How many crashes happened.",
|
||||||
})
|
})
|
||||||
|
|
||||||
if err := prometheus.Register(connections); err != nil {
|
if err := registry.Register(connections); err != nil {
|
||||||
return nil, errors.Annotate(err, "Cannot register connections collector")
|
return nil, errors.Annotate(err, "Cannot register connections collector")
|
||||||
}
|
}
|
||||||
if err := prometheus.Register(traffic); err != nil {
|
if err := registry.Register(traffic); err != nil {
|
||||||
return nil, errors.Annotate(err, "cannot register traffic collector")
|
return nil, errors.Annotate(err, "cannot register traffic collector")
|
||||||
}
|
}
|
||||||
if err := prometheus.Register(speed); err != nil {
|
if err := registry.Register(speed); err != nil {
|
||||||
return nil, errors.Annotate(err, "cannot register speed collector")
|
return nil, errors.Annotate(err, "cannot register speed collector")
|
||||||
}
|
}
|
||||||
if err := prometheus.Register(crashes); err != nil {
|
if err := registry.Register(crashes); err != nil {
|
||||||
return nil, errors.Annotate(err, "cannot register crashes collector")
|
return nil, errors.Annotate(err, "cannot register crashes collector")
|
||||||
}
|
}
|
||||||
|
|
||||||
return &prometheusExporter{
|
return &prometheusExporter{
|
||||||
|
registry: registry,
|
||||||
connections: connections,
|
connections: connections,
|
||||||
traffic: traffic,
|
traffic: traffic,
|
||||||
speed: speed,
|
speed: speed,
|
||||||
|
|||||||
+2
-3
@@ -4,13 +4,12 @@ import (
|
|||||||
"encoding/json"
|
"encoding/json"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
"github.com/prometheus/client_golang/prometheus/promhttp"
|
|
||||||
"go.uber.org/zap"
|
"go.uber.org/zap"
|
||||||
|
|
||||||
"github.com/9seconds/mtg/config"
|
"github.com/9seconds/mtg/config"
|
||||||
)
|
)
|
||||||
|
|
||||||
func startServer(conf *config.Config) {
|
func startServer(conf *config.Config, prometheusHandler http.Handler) {
|
||||||
log := zap.S().Named("stats")
|
log := zap.S().Named("stats")
|
||||||
|
|
||||||
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
|
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
|
||||||
@@ -33,7 +32,7 @@ func startServer(conf *config.Config) {
|
|||||||
log.Errorw("Cannot encode json", "error", err)
|
log.Errorw("Cannot encode json", "error", err)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
http.Handle("/prometheus/", promhttp.Handler())
|
http.Handle("/prometheus/", prometheusHandler)
|
||||||
|
|
||||||
if err := http.ListenAndServe(conf.StatAddr(), nil); err != nil {
|
if err := http.ListenAndServe(conf.StatAddr(), nil); err != nil {
|
||||||
log.Fatalw("Stats server has been stopped", "error", err)
|
log.Fatalw("Stats server has been stopped", "error", err)
|
||||||
|
|||||||
Reference in New Issue
Block a user