sigs.k8s.io/cluster-api@v1.7.1/internal/runtime/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 provides functions for creating Runtime SDK related metrics. 18 package metrics 19 20 import ( 21 "net/http" 22 "net/url" 23 "strconv" 24 "time" 25 26 "github.com/prometheus/client_golang/prometheus" 27 "k8s.io/apimachinery/pkg/runtime" 28 ctrlmetrics "sigs.k8s.io/controller-runtime/pkg/metrics" 29 30 runtimecatalog "sigs.k8s.io/cluster-api/exp/runtime/catalog" 31 runtimehooksv1 "sigs.k8s.io/cluster-api/exp/runtime/hooks/api/v1alpha1" 32 ) 33 34 func init() { 35 // Register the metrics at the controller-runtime metrics registry. 36 ctrlmetrics.Registry.MustRegister(RequestsTotal.metric) 37 ctrlmetrics.Registry.MustRegister(RequestDuration.metric) 38 } 39 40 // Metrics subsystem and all of the keys used by the Runtime SDK. 41 const ( 42 runtimeSDKSubsystem = "capi_runtime_sdk" 43 unknownResponseStatus = "Unknown" 44 ) 45 46 var ( 47 // RequestsTotal reports request results. 48 RequestsTotal = requestsTotalObserver{ 49 prometheus.NewCounterVec(prometheus.CounterOpts{ 50 Subsystem: runtimeSDKSubsystem, 51 Name: "requests_total", 52 Help: "Number of HTTP requests, partitioned by status code, host, hook and response status.", 53 }, []string{"code", "host", "group", "version", "hook", "status"}), 54 } 55 // RequestDuration reports the request latency in seconds. 56 RequestDuration = requestDurationObserver{ 57 prometheus.NewHistogramVec(prometheus.HistogramOpts{ 58 Subsystem: runtimeSDKSubsystem, 59 Name: "request_duration_seconds", 60 Help: "Request duration in seconds, broken down by hook and host.", 61 Buckets: []float64{0.005, 0.025, 0.05, 0.1, 0.2, 0.4, 0.6, 0.8, 1.0, 1.25, 1.5, 2, 3, 62 4, 5, 6, 8, 10, 15, 20, 30, 45, 60}, 63 }, []string{"host", "group", "version", "hook"}), 64 } 65 ) 66 67 type requestsTotalObserver struct { 68 metric *prometheus.CounterVec 69 } 70 71 // Observe observes a http request result and increments the metric for the given 72 // http status code, host, gvh and response. 73 func (m *requestsTotalObserver) Observe(req *http.Request, resp *http.Response, gvh runtimecatalog.GroupVersionHook, err error, response runtime.Object) { 74 host := req.URL.Host 75 76 // Errors can be arbitrary strings. Unbound label cardinality is not suitable for a metric 77 // system so they are reported as `<error>`. 78 code := "<error>" 79 if err == nil { 80 code = strconv.Itoa(resp.StatusCode) 81 } 82 83 status := unknownResponseStatus 84 if responseObject, ok := response.(runtimehooksv1.ResponseObject); ok && responseObject.GetStatus() != "" { 85 status = string(responseObject.GetStatus()) 86 } 87 88 m.metric.WithLabelValues(code, host, gvh.Group, gvh.Version, gvh.Hook, status).Inc() 89 } 90 91 type requestDurationObserver struct { 92 metric *prometheus.HistogramVec 93 } 94 95 // Observe increments the request latency metric for the given host and gvh. 96 func (m *requestDurationObserver) Observe(gvh runtimecatalog.GroupVersionHook, u url.URL, latency time.Duration) { 97 m.metric.WithLabelValues(u.Host, gvh.Group, gvh.Version, gvh.Hook).Observe(latency.Seconds()) 98 }