k8s.io/apiserver@v0.31.1/pkg/authorization/authorizerfactory/metrics.go (about) 1 /* 2 Copyright 2021 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 authorizerfactory 18 19 import ( 20 "context" 21 "sync" 22 23 celmetrics "k8s.io/apiserver/pkg/authorization/cel" 24 webhookmetrics "k8s.io/apiserver/plugin/pkg/authorizer/webhook/metrics" 25 compbasemetrics "k8s.io/component-base/metrics" 26 "k8s.io/component-base/metrics/legacyregistry" 27 ) 28 29 var registerMetrics sync.Once 30 31 // RegisterMetrics registers authorizer metrics. 32 func RegisterMetrics() { 33 registerMetrics.Do(func() { 34 legacyregistry.MustRegister(requestTotal) 35 legacyregistry.MustRegister(requestLatency) 36 }) 37 } 38 39 var ( 40 requestTotal = compbasemetrics.NewCounterVec( 41 &compbasemetrics.CounterOpts{ 42 Name: "apiserver_delegated_authz_request_total", 43 Help: "Number of HTTP requests partitioned by status code.", 44 StabilityLevel: compbasemetrics.ALPHA, 45 }, 46 []string{"code"}, 47 ) 48 49 requestLatency = compbasemetrics.NewHistogramVec( 50 &compbasemetrics.HistogramOpts{ 51 Name: "apiserver_delegated_authz_request_duration_seconds", 52 Help: "Request latency in seconds. Broken down by status code.", 53 Buckets: []float64{0.25, 0.5, 0.7, 1, 1.5, 3, 5, 10}, 54 StabilityLevel: compbasemetrics.ALPHA, 55 }, 56 []string{"code"}, 57 ) 58 ) 59 60 var _ = webhookmetrics.AuthorizerMetrics(delegatingAuthorizerMetrics{}) 61 62 type delegatingAuthorizerMetrics struct { 63 // no-op for webhook metrics for now, delegating authorization reports original total/latency metrics 64 webhookmetrics.NoopWebhookMetrics 65 // no-op for matchCondition metrics for now, delegating authorization doesn't configure match conditions 66 celmetrics.NoopMatcherMetrics 67 } 68 69 func NewDelegatingAuthorizerMetrics() delegatingAuthorizerMetrics { 70 RegisterMetrics() 71 return delegatingAuthorizerMetrics{} 72 } 73 74 // RecordRequestTotal increments the total number of requests for the delegated authorization. 75 func (delegatingAuthorizerMetrics) RecordRequestTotal(ctx context.Context, code string) { 76 requestTotal.WithContext(ctx).WithLabelValues(code).Add(1) 77 } 78 79 // RecordRequestLatency measures request latency in seconds for the delegated authorization. Broken down by status code. 80 func (delegatingAuthorizerMetrics) RecordRequestLatency(ctx context.Context, code string, latency float64) { 81 requestLatency.WithContext(ctx).WithLabelValues(code).Observe(latency) 82 }