Files
lincodex/backend/internal/adapter/secondary/prometheus/metrics_test.go
T

61 lines
1.9 KiB
Go

package prometheus
import (
"net/http/httptest"
"strings"
"testing"
"time"
"lincodex/backend/internal/port/outbound"
)
func TestMetricsHandlerExposesBoundedRequestLabels(t *testing.T) {
metrics := New()
metrics.ObserveRequest("CUSTOM", "", 599, 25*time.Millisecond)
response := httptest.NewRecorder()
metrics.Handler().ServeHTTP(response, httptest.NewRequest("GET", "/metrics", nil))
body := response.Body.String()
if !strings.Contains(body, `lincodex_http_requests_total{method="OTHER",route="unmatched",status="599"} 1`) {
t.Fatalf("metrics output is missing bounded request labels:\n%s", body)
}
if !strings.Contains(body, `lincodex_http_request_duration_seconds_bucket{method="OTHER",route="unmatched",le="0.05"} 1`) {
t.Fatalf("metrics output is missing request duration:\n%s", body)
}
}
func TestMetricsHandlerExposesOutboxRelayOutcomes(t *testing.T) {
metrics := New()
metrics.ObserveOutboxRelayPass(3, 0)
metrics.ObserveOutboxRelayPass(1, 2)
response := httptest.NewRecorder()
metrics.Handler().ServeHTTP(response, httptest.NewRequest("GET", "/metrics", nil))
body := response.Body.String()
for _, want := range []string{
`lincodex_outbox_relay_events_total{outcome="published"} 4`,
`lincodex_outbox_relay_events_total{outcome="failed"} 2`,
} {
if !strings.Contains(body, want) {
t.Fatalf("metrics output is missing %s:\n%s", want, body)
}
}
}
func TestMetricsHandlerExposesTheOutboxBacklog(t *testing.T) {
metrics := New()
metrics.ObserveOutboxBacklog(outbound.OutboxBacklog{Pending: 42, OldestAge: 90 * time.Second})
response := httptest.NewRecorder()
metrics.Handler().ServeHTTP(response, httptest.NewRequest("GET", "/metrics", nil))
body := response.Body.String()
for _, want := range []string{
"lincodex_outbox_relay_pending_events 42",
"lincodex_outbox_relay_oldest_pending_age_seconds 90",
} {
if !strings.Contains(body, want) {
t.Fatalf("metrics output is missing %s:\n%s", want, body)
}
}
}