k8s.io/apiserver@v0.31.1/pkg/cel/metrics/metrics.go (about) 1 /* 2 Copyright 2022 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 "time" 21 22 "k8s.io/component-base/metrics" 23 "k8s.io/component-base/metrics/legacyregistry" 24 ) 25 26 // TODO(jiahuif) CEL is to be used in multiple components, revise naming when that happens. 27 const ( 28 namespace = "apiserver" 29 subsystem = "cel" 30 ) 31 32 // Metrics provides access to CEL metrics. 33 var Metrics = newCelMetrics() 34 35 type CelMetrics struct { 36 compilationTime *metrics.Histogram 37 evaluationTime *metrics.Histogram 38 } 39 40 func newCelMetrics() *CelMetrics { 41 m := &CelMetrics{ 42 compilationTime: metrics.NewHistogram(&metrics.HistogramOpts{ 43 Namespace: namespace, 44 Subsystem: subsystem, 45 Name: "compilation_duration_seconds", 46 Help: "CEL compilation time in seconds.", 47 StabilityLevel: metrics.BETA, 48 }), 49 evaluationTime: metrics.NewHistogram(&metrics.HistogramOpts{ 50 Namespace: namespace, 51 Subsystem: subsystem, 52 Name: "evaluation_duration_seconds", 53 Help: "CEL evaluation time in seconds.", 54 StabilityLevel: metrics.BETA, 55 }), 56 } 57 58 legacyregistry.MustRegister(m.compilationTime) 59 legacyregistry.MustRegister(m.evaluationTime) 60 61 return m 62 } 63 64 // ObserveCompilation records a CEL compilation with the time the compilation took. 65 func (m *CelMetrics) ObserveCompilation(elapsed time.Duration) { 66 seconds := elapsed.Seconds() 67 m.compilationTime.Observe(seconds) 68 } 69 70 // ObserveEvaluation records a CEL evaluation with the time the evaluation took. 71 func (m *CelMetrics) ObserveEvaluation(elapsed time.Duration) { 72 seconds := elapsed.Seconds() 73 m.evaluationTime.Observe(seconds) 74 }