forked from dunglas/mercure
-
Notifications
You must be signed in to change notification settings - Fork 0
/
metrics.go
94 lines (80 loc) · 2.88 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
package mercure
import (
"github.com/dunglas/mercure/common"
"github.com/gorilla/mux"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
type Metrics interface {
// SubscriberConnected collects metrics about about subscriber connections.
SubscriberConnected(s *Subscriber)
// SubscriberDisconnected collects metrics about subscriber disconnections.
SubscriberDisconnected(s *Subscriber)
// UpdatePublished collects metrics about update publications.
UpdatePublished(u *Update)
}
type NopMetrics struct{}
func (NopMetrics) SubscriberConnected(s *Subscriber) {}
func (NopMetrics) SubscriberDisconnected(s *Subscriber) {}
func (NopMetrics) UpdatePublished(s *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",
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("/metrics", promhttp.HandlerFor(m.registry.(*prometheus.Registry), promhttp.HandlerOpts{})).Methods("GET")
}
func (m *PrometheusMetrics) SubscriberConnected(s *Subscriber) {
m.subscribersTotal.Inc()
m.subscribers.Inc()
}
func (m *PrometheusMetrics) SubscriberDisconnected(s *Subscriber) {
m.subscribers.Dec()
}
func (m *PrometheusMetrics) UpdatePublished(u *Update) {
m.updatesTotal.Inc()
}