Debian Codegraph @

metrics/metrics_test.go

raw
1 package metrics
2
3 import (
4 "sort"
5 "strings"
6 "testing"
7
8 "github.com/prometheus/client_golang/prometheus"
9 "github.com/stretchr/testify/require"
10 )
11
12 // expectedMetricNames is the exact set of metrics the server exposes. These names are a public
13 // contract: renaming or dropping one silently breaks existing dashboards and alerts.
14 var expectedMetricNames = []string{
15 "ntfy_attachments_total_size",
16 "ntfy_calls_made_failure",
17 "ntfy_calls_made_success",
18 "ntfy_emails_received_failure",
19 "ntfy_emails_received_success",
20 "ntfy_emails_sent_failure",
21 "ntfy_emails_sent_success",
22 "ntfy_firebase_published_failure",
23 "ntfy_firebase_published_success",
24 "ntfy_http_requests_total",
25 "ntfy_matrix_published_failure",
26 "ntfy_matrix_published_success",
27 "ntfy_message_publish_duration_ms",
28 "ntfy_messages_cached_total",
29 "ntfy_messages_published_failure",
30 "ntfy_messages_published_success",
31 "ntfy_subscribers_total",
32 "ntfy_topics_total",
33 "ntfy_unifiedpush_published_success",
34 "ntfy_users_total",
35 "ntfy_visitors_total",
36 }
37
38 func TestRegisteredMetricNames(t *testing.T) {
39 HTTPRequests.WithLabelValues("200", "20000", "GET").Inc()
40 families, err := prometheus.DefaultGatherer.Gather()
41 require.Nil(t, err)
42 names := make([]string, 0)
43 for _, family := range families {
44 if strings.HasPrefix(family.GetName(), "ntfy_") {
45 names = append(names, family.GetName())
46 }
47 }
48 sort.Strings(names)
49 require.Equal(t, expectedMetricNames, names)
50 }
51
52 func TestCollectors_NeverNil(t *testing.T) {
53 // Call sites update metrics unconditionally, even when metrics are disabled, so no collector
54 // may ever be nil
55 MessagesPublishedSuccess.Inc()
56 MessagesCached.Set(1)
57 HTTPRequests.WithLabelValues("200", "20000", "PUT").Inc()
58 }