-
Notifications
You must be signed in to change notification settings - Fork 5
/
metrics.go
67 lines (56 loc) · 1.71 KB
/
metrics.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package main
import (
"net/http"
"github.com/go-kit/kit/log"
"github.com/go-kit/kit/log/level"
"github.com/go-kit/kit/metrics/discard"
"github.com/go-kit/kit/metrics/prometheus"
"github.com/go-pluto/pluto/distributor"
prom "github.com/prometheus/client_golang/prometheus"
)
// PlutoMetrics wraps all metrics for Pluto into one struct.
type PlutoMetrics struct {
Distributor *distributor.Metrics
}
// NewPlutoMetrics returns Prometheus metrics when addr isn't
// an empty string. Otherwise discard metrics are returned.
func NewPlutoMetrics(distributorAddr string) *PlutoMetrics {
m := &PlutoMetrics{}
if distributorAddr == "" {
m.Distributor = &distributor.Metrics{
Commands: discard.NewCounter(),
Connections: discard.NewCounter(),
}
} else {
m.Distributor = &distributor.Metrics{
Commands: prometheus.NewCounterFrom(
prom.CounterOpts{
Namespace: "pluto",
Subsystem: "distributor",
Name: "commands_total",
Help: "Number of commands",
}, []string{"command", "status"},
),
Connections: prometheus.NewCounterFrom(
prom.CounterOpts{
Namespace: "pluto",
Subsystem: "distributor",
Name: "connections_total",
Help: "Number of connections opened to pluto",
}, nil,
),
}
}
return m
}
func runPromHTTP(logger log.Logger, addr string) {
if addr == "" {
level.Debug(logger).Log("msg", "prometheus addr is empty, not exposing prometheus metrics")
return
}
http.Handle("/metrics", prom.UninstrumentedHandler())
level.Info(logger).Log("msg", "prometheus handler listening", "addr", addr)
if err := http.ListenAndServe(addr, nil); err != nil {
level.Warn(logger).Log("msg", "failed to serve prometheus metrics", "err", err)
}
}