diff --git a/staging/src/k8s.io/apiserver/pkg/admission/metrics/metrics_test.go b/staging/src/k8s.io/apiserver/pkg/admission/metrics/metrics_test.go index 1b96d25a217..318e689f45b 100644 --- a/staging/src/k8s.io/apiserver/pkg/admission/metrics/metrics_test.go +++ b/staging/src/k8s.io/apiserver/pkg/admission/metrics/metrics_test.go @@ -81,6 +81,37 @@ func TestObserveWebhook(t *testing.T) { expectHistogramCountTotal(t, "apiserver_admission_webhook_admission_duration_seconds", wantLabels, 1) } +func TestObserveWebhookRejection(t *testing.T) { + Metrics.reset() + Metrics.ObserveWebhookRejection("x", stepAdmit, string(admission.Create), WebhookRejectionNoError, 500) + Metrics.ObserveWebhookRejection("x", stepAdmit, string(admission.Create), WebhookRejectionNoError, 654) + Metrics.ObserveWebhookRejection("x", stepValidate, string(admission.Update), WebhookRejectionCallingWebhookError, 0) + wantLabels := map[string]string{ + "name": "x", + "operation": string(admission.Create), + "type": "admit", + "error_type": "no_error", + "rejection_code": "500", + } + wantLabels600 := map[string]string{ + "name": "x", + "operation": string(admission.Create), + "type": "admit", + "error_type": "no_error", + "rejection_code": "600", + } + wantLabelsCallingWebhookError := map[string]string{ + "name": "x", + "operation": string(admission.Update), + "type": "validate", + "error_type": "calling_webhook_error", + "rejection_code": "0", + } + expectCounterValue(t, "apiserver_admission_webhook_rejection_count", wantLabels, 1) + expectCounterValue(t, "apiserver_admission_webhook_rejection_count", wantLabels600, 1) + expectCounterValue(t, "apiserver_admission_webhook_rejection_count", wantLabelsCallingWebhookError, 1) +} + func TestWithMetrics(t *testing.T) { Metrics.reset() diff --git a/staging/src/k8s.io/apiserver/pkg/admission/metrics/testutil_test.go b/staging/src/k8s.io/apiserver/pkg/admission/metrics/testutil_test.go index af5ee79a8b6..4fbbd3e60aa 100644 --- a/staging/src/k8s.io/apiserver/pkg/admission/metrics/testutil_test.go +++ b/staging/src/k8s.io/apiserver/pkg/admission/metrics/testutil_test.go @@ -89,3 +89,35 @@ func expectHistogramCountTotal(t *testing.T, name string, labelFilter map[string } } } + +// expectCounterValue ensures that the counts of metrics matching the labelFilter is as +// expected. +func expectCounterValue(t *testing.T, name string, labelFilter map[string]string, wantCount int) { + metrics, err := prometheus.DefaultGatherer.Gather() + if err != nil { + t.Fatalf("Failed to gather metrics: %s", err) + } + + counterSum := 0 + for _, mf := range metrics { + if mf.GetName() != name { + continue // Ignore other metrics. + } + for _, metric := range mf.GetMetric() { + if !labelsMatch(metric, labelFilter) { + continue + } + counterSum += int(metric.GetCounter().GetValue()) + } + } + if wantCount != counterSum { + t.Errorf("Wanted count %d, got %d for metric %s with labels %#+v", wantCount, counterSum, name, labelFilter) + for _, mf := range metrics { + if mf.GetName() == name { + for _, metric := range mf.GetMetric() { + t.Logf("\tnear match: %s", metric.String()) + } + } + } + } +}