Debian Codegraph @

plugin/pkg/authorizer/webhook/metrics/metrics_test.go

raw
1 /*
2 Copyright 2024 The Kubernetes Authors.
3
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
15 */
16
17 package metrics
18
19 import (
20 "context"
21 "strings"
22 "testing"
23
24 "k8s.io/component-base/metrics/legacyregistry"
25 "k8s.io/component-base/metrics/testutil"
26 )
27
28 func TestRecordWebhookMetrics(t *testing.T) {
29 testCases := []struct {
30 desc string
31 metrics []string
32 name string
33 result string
34 duration float64
35 want string
36 }{
37 {
38 desc: "evaluation failure total",
39 metrics: []string{
40 "apiserver_authorization_webhook_duration_seconds",
41 "apiserver_authorization_webhook_evaluations_total",
42 "apiserver_authorization_webhook_evaluations_fail_open_total",
43 },
44 name: "wh1.example.com",
45 result: "timeout",
46 duration: 1.5,
47 want: `
48 # HELP apiserver_authorization_webhook_duration_seconds [ALPHA] Request latency in seconds.
49 # TYPE apiserver_authorization_webhook_duration_seconds histogram
50 apiserver_authorization_webhook_duration_seconds_bucket{name="wh1.example.com",result="timeout",le="0.005"} 0
51 apiserver_authorization_webhook_duration_seconds_bucket{name="wh1.example.com",result="timeout",le="0.01"} 0
52 apiserver_authorization_webhook_duration_seconds_bucket{name="wh1.example.com",result="timeout",le="0.025"} 0
53 apiserver_authorization_webhook_duration_seconds_bucket{name="wh1.example.com",result="timeout",le="0.05"} 0
54 apiserver_authorization_webhook_duration_seconds_bucket{name="wh1.example.com",result="timeout",le="0.1"} 0
55 apiserver_authorization_webhook_duration_seconds_bucket{name="wh1.example.com",result="timeout",le="0.25"} 0
56 apiserver_authorization_webhook_duration_seconds_bucket{name="wh1.example.com",result="timeout",le="0.5"} 0
57 apiserver_authorization_webhook_duration_seconds_bucket{name="wh1.example.com",result="timeout",le="1"} 0
58 apiserver_authorization_webhook_duration_seconds_bucket{name="wh1.example.com",result="timeout",le="2.5"} 1
59 apiserver_authorization_webhook_duration_seconds_bucket{name="wh1.example.com",result="timeout",le="5"} 1
60 apiserver_authorization_webhook_duration_seconds_bucket{name="wh1.example.com",result="timeout",le="10"} 1
61 apiserver_authorization_webhook_duration_seconds_bucket{name="wh1.example.com",result="timeout",le="+Inf"} 1
62 apiserver_authorization_webhook_duration_seconds_sum{name="wh1.example.com",result="timeout"} 1.5
63 apiserver_authorization_webhook_duration_seconds_count{name="wh1.example.com",result="timeout"} 1
64 # HELP apiserver_authorization_webhook_evaluations_fail_open_total [ALPHA] NoOpinion results due to webhook timeout or error.
65 # TYPE apiserver_authorization_webhook_evaluations_fail_open_total counter
66 apiserver_authorization_webhook_evaluations_fail_open_total{name="wh1.example.com",result="timeout"} 1
67 # HELP apiserver_authorization_webhook_evaluations_total [ALPHA] Round-trips to authorization webhooks.
68 # TYPE apiserver_authorization_webhook_evaluations_total counter
69 apiserver_authorization_webhook_evaluations_total{name="wh1.example.com",result="timeout"} 1
70 `,
71 },
72 }
73
74 for _, tt := range testCases {
75 t.Run(tt.desc, func(t *testing.T) {
76 ResetWebhookMetricsForTest()
77 m := NewWebhookMetrics()
78 m.RecordWebhookDuration(context.Background(), tt.name, tt.result, tt.duration)
79 m.RecordWebhookEvaluation(context.Background(), tt.name, tt.result)
80 m.RecordWebhookFailOpen(context.Background(), tt.name, tt.result)
81 if err := testutil.GatherAndCompare(legacyregistry.DefaultGatherer, strings.NewReader(tt.want), tt.metrics...); err != nil {
82 t.Fatal(err)
83 }
84 })
85 }
86 }