-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmetrics_test.go
More file actions
118 lines (99 loc) · 4.15 KB
/
Copy pathmetrics_test.go
File metadata and controls
118 lines (99 loc) · 4.15 KB
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
package certwebhook
import (
"context"
"strings"
"sync"
"testing"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/testutil"
"github.com/stretchr/testify/require"
)
func setupTestRegistry(t *testing.T) *prometheus.Registry {
t.Helper()
registry := prometheus.NewRegistry()
// Reset the once so initMetrics re-creates counters on the test registry
metricsOnce = sync.Once{}
metricWebhookDeliveries = nil
metricCertEvents = nil
metricThrottled = nil
metricTLSGetCert = nil
initMetrics(registry)
return registry
}
func TestMetrics_RecordWebhookDelivery(t *testing.T) {
registry := setupTestRegistry(t)
recordWebhookDelivery("example.com", SSLStatusReady, true)
recordWebhookDelivery("example.com", SSLStatusReady, false)
recordWebhookDelivery("other.com", SSLStatusFailed, true)
expected := `
# HELP caddy_cert_webhook_webhook_deliveries_total Total webhook deliveries to portal API.
# TYPE caddy_cert_webhook_webhook_deliveries_total counter
caddy_cert_webhook_webhook_deliveries_total{domain="example.com",result="failure",status="ready"} 1
caddy_cert_webhook_webhook_deliveries_total{domain="example.com",result="success",status="ready"} 1
caddy_cert_webhook_webhook_deliveries_total{domain="other.com",result="success",status="failed"} 1
`
err := testutil.GatherAndCompare(registry, strings.NewReader(expected), "caddy_cert_webhook_webhook_deliveries_total")
require.NoError(t, err)
}
func TestMetrics_RecordCertEvent(t *testing.T) {
registry := setupTestRegistry(t)
recordCertEvent(EventCertObtained, "example.com")
recordCertEvent(EventCertRenewed, "example.com")
recordCertEvent(EventCertExpired, "other.com")
expected := `
# HELP caddy_cert_webhook_cert_events_total Total certificate events processed.
# TYPE caddy_cert_webhook_cert_events_total counter
caddy_cert_webhook_cert_events_total{domain="example.com",event_type="cert_obtained"} 1
caddy_cert_webhook_cert_events_total{domain="example.com",event_type="cert_renewed"} 1
caddy_cert_webhook_cert_events_total{domain="other.com",event_type="cert_expired"} 1
`
err := testutil.GatherAndCompare(registry, strings.NewReader(expected), "caddy_cert_webhook_cert_events_total")
require.NoError(t, err)
}
func TestMetrics_RecordThrottled(t *testing.T) {
registry := setupTestRegistry(t)
recordThrottled("example.com", "cert_event")
recordThrottled("example.com", "tls_get_cert")
recordThrottled("other.com", "cert_event")
expected := `
# HELP caddy_cert_webhook_throttled_total Total events throttled by dedup/throttle.
# TYPE caddy_cert_webhook_throttled_total counter
caddy_cert_webhook_throttled_total{domain="example.com",source="cert_event"} 1
caddy_cert_webhook_throttled_total{domain="example.com",source="tls_get_cert"} 1
caddy_cert_webhook_throttled_total{domain="other.com",source="cert_event"} 1
`
err := testutil.GatherAndCompare(registry, strings.NewReader(expected), "caddy_cert_webhook_throttled_total")
require.NoError(t, err)
}
func TestMetrics_RecordTLSGetCert(t *testing.T) {
registry := setupTestRegistry(t)
recordTLSGetCert("example.com", SSLStatusReady)
recordTLSGetCert("example.com", SSLStatusFailed)
expected := `
# HELP caddy_cert_webhook_tls_get_cert_total Total tls_get_certificate events processed.
# TYPE caddy_cert_webhook_tls_get_cert_total counter
caddy_cert_webhook_tls_get_cert_total{domain="example.com",status="failed"} 1
caddy_cert_webhook_tls_get_cert_total{domain="example.com",status="ready"} 1
`
err := testutil.GatherAndCompare(registry, strings.NewReader(expected), "caddy_cert_webhook_tls_get_cert_total")
require.NoError(t, err)
}
func TestMetrics_NilSafety(t *testing.T) {
// Reset to nil to simulate un-initialized state
metricWebhookDeliveries = nil
metricCertEvents = nil
metricThrottled = nil
metricTLSGetCert = nil
// These should not panic
recordWebhookDelivery("example.com", SSLStatusReady, true)
recordCertEvent(EventCertObtained, "example.com")
recordThrottled("example.com", "cert_event")
recordTLSGetCert("example.com", SSLStatusReady)
}
func TestTracing_StartSpan(t *testing.T) {
initTracer()
require.NotNil(t, tracer)
_, span := startSpan(context.Background(), "test_span")
require.NotNil(t, span)
span.End()
}