Debian Codegraph @

metrics/metrics.go

raw
1 // Package metrics defines the Prometheus metrics exposed by the ntfy server, and registers them
2 // with the default Prometheus registry on import. It is decoupled from the ntfy server, so that
3 // call sites can update metrics without depending on the server package.
4 package metrics
5
6 import (
7 "github.com/prometheus/client_golang/prometheus"
8 )
9
10 // Collectors for all metrics exposed by the server.
11 //
12 // These are never nil, so that call sites can update them unconditionally. If metrics are
13 // disabled, the server never mounts the /metrics handler, and the values are simply never read.
14 var (
15 MessagesPublishedSuccess = prometheus.NewCounter(prometheus.CounterOpts{
16 Name: "ntfy_messages_published_success",
17 })
18 MessagesPublishedFailure = prometheus.NewCounter(prometheus.CounterOpts{
19 Name: "ntfy_messages_published_failure",
20 })
21 MessagesCached = prometheus.NewGauge(prometheus.GaugeOpts{
22 Name: "ntfy_messages_cached_total",
23 })
24 MessagePublishDurationMillis = prometheus.NewGauge(prometheus.GaugeOpts{
25 Name: "ntfy_message_publish_duration_ms",
26 })
27 FirebasePublishedSuccess = prometheus.NewCounter(prometheus.CounterOpts{
28 Name: "ntfy_firebase_published_success",
29 })
30 FirebasePublishedFailure = prometheus.NewCounter(prometheus.CounterOpts{
31 Name: "ntfy_firebase_published_failure",
32 })
33 EmailsPublishedSuccess = prometheus.NewCounter(prometheus.CounterOpts{
34 Name: "ntfy_emails_sent_success",
35 })
36 EmailsPublishedFailure = prometheus.NewCounter(prometheus.CounterOpts{
37 Name: "ntfy_emails_sent_failure",
38 })
39 EmailsReceivedSuccess = prometheus.NewCounter(prometheus.CounterOpts{
40 Name: "ntfy_emails_received_success",
41 })
42 EmailsReceivedFailure = prometheus.NewCounter(prometheus.CounterOpts{
43 Name: "ntfy_emails_received_failure",
44 })
45 CallsMadeSuccess = prometheus.NewCounter(prometheus.CounterOpts{
46 Name: "ntfy_calls_made_success",
47 })
48 CallsMadeFailure = prometheus.NewCounter(prometheus.CounterOpts{
49 Name: "ntfy_calls_made_failure",
50 })
51 UnifiedPushPublishedSuccess = prometheus.NewCounter(prometheus.CounterOpts{
52 Name: "ntfy_unifiedpush_published_success",
53 })
54 MatrixPublishedSuccess = prometheus.NewCounter(prometheus.CounterOpts{
55 Name: "ntfy_matrix_published_success",
56 })
57 MatrixPublishedFailure = prometheus.NewCounter(prometheus.CounterOpts{
58 Name: "ntfy_matrix_published_failure",
59 })
60 AttachmentsTotalSize = prometheus.NewGauge(prometheus.GaugeOpts{
61 Name: "ntfy_attachments_total_size",
62 })
63 Visitors = prometheus.NewGauge(prometheus.GaugeOpts{
64 Name: "ntfy_visitors_total",
65 })
66 Users = prometheus.NewGauge(prometheus.GaugeOpts{
67 Name: "ntfy_users_total",
68 })
69 Subscribers = prometheus.NewGauge(prometheus.GaugeOpts{
70 Name: "ntfy_subscribers_total",
71 })
72 Topics = prometheus.NewGauge(prometheus.GaugeOpts{
73 Name: "ntfy_topics_total",
74 })
75 HTTPRequests = prometheus.NewCounterVec(prometheus.CounterOpts{
76 Name: "ntfy_http_requests_total",
77 }, []string{"http_code", "ntfy_code", "http_method"})
78 )
79
80 // init registers all collectors with the default Prometheus registry. Registration is
81 // unconditional: the collectors are only ever exposed if the server mounts the /metrics handler,
82 // so there is nothing to be gained by tying registration to the config.
83 func init() {
84 prometheus.MustRegister(
85 MessagesPublishedSuccess,
86 MessagesPublishedFailure,
87 MessagesCached,
88 MessagePublishDurationMillis,
89 FirebasePublishedSuccess,
90 FirebasePublishedFailure,
91 EmailsPublishedSuccess,
92 EmailsPublishedFailure,
93 EmailsReceivedSuccess,
94 EmailsReceivedFailure,
95 CallsMadeSuccess,
96 CallsMadeFailure,
97 UnifiedPushPublishedSuccess,
98 MatrixPublishedSuccess,
99 MatrixPublishedFailure,
100 AttachmentsTotalSize,
101 Visitors,
102 Users,
103 Subscribers,
104 Topics,
105 HTTPRequests,
106 )
107 }