-
-
Notifications
You must be signed in to change notification settings - Fork 298
/
metrics.go
98 lines (82 loc) · 2.97 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package mercure
import (
"net/http"
"github.com/dunglas/mercure/common"
"github.com/gorilla/mux"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
const metricsPath = "/metrics"
type Metrics interface {
// SubscriberConnected collects metrics about subscriber connections.
SubscriberConnected(s *LocalSubscriber)
// SubscriberDisconnected collects metrics about subscriber disconnections.
SubscriberDisconnected(s *LocalSubscriber)
// UpdatePublished collects metrics about update publications.
UpdatePublished(u *Update)
}
type NopMetrics struct{}
func (NopMetrics) SubscriberConnected(_ *LocalSubscriber) {}
func (NopMetrics) SubscriberDisconnected(_ *LocalSubscriber) {}
func (NopMetrics) UpdatePublished(_ *Update) {}
// PrometheusMetrics store Hub collected metrics.
type PrometheusMetrics struct {
registry prometheus.Registerer
subscribersTotal prometheus.Counter
subscribers prometheus.Gauge
updatesTotal prometheus.Counter
}
// NewPrometheusMetrics creates a Prometheus metrics collector.
// This method must be called only one time, or it will panic.
func NewPrometheusMetrics(registry prometheus.Registerer) *PrometheusMetrics {
if registry == nil {
registry = prometheus.NewRegistry()
}
m := &PrometheusMetrics{
registry: registry,
subscribersTotal: prometheus.NewCounter(
prometheus.CounterOpts{
Name: "mercure_subscribers_total",
Help: "Total number of handled subscribers",
},
),
subscribers: prometheus.NewGauge(
prometheus.GaugeOpts{
Name: "mercure_subscribers_connected",
Help: "The current number of running subscribers",
},
),
updatesTotal: prometheus.NewCounter(
prometheus.CounterOpts{
Name: "mercure_updates_total",
Help: "Total number of handled updates",
},
),
}
m.registry.MustRegister(m.subscribers)
m.registry.MustRegister(m.subscribersTotal)
m.registry.MustRegister(m.updatesTotal)
return m
}
// Register configures the Prometheus registry with all collected metrics.
//
// Deprecated: use the Caddy server module or the standalone library instead.
func (m *PrometheusMetrics) Register(r *mux.Router) {
// Metrics about current version
m.registry.MustRegister(common.AppVersion.NewMetricsCollector())
// Go-specific metrics about the process (GC stats, goroutines, etc.).
m.registry.MustRegister(prometheus.NewGoCollector())
// Go-unrelated process metrics (memory usage, file descriptors, etc.).
m.registry.MustRegister(prometheus.NewProcessCollector(prometheus.ProcessCollectorOpts{}))
r.Handle(metricsPath, promhttp.HandlerFor(m.registry.(*prometheus.Registry), promhttp.HandlerOpts{})).Methods(http.MethodGet)
}
func (m *PrometheusMetrics) SubscriberConnected(_ *LocalSubscriber) {
m.subscribersTotal.Inc()
m.subscribers.Inc()
}
func (m *PrometheusMetrics) SubscriberDisconnected(_ *LocalSubscriber) {
m.subscribers.Dec()
}
func (m *PrometheusMetrics) UpdatePublished(_ *Update) {
m.updatesTotal.Inc()
}