|
| 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 | + ctrlmetrics "sigs.k8s.io/controller-runtime/pkg/metrics" |
| 28 | + |
| 29 | + runtimecatalog "sigs.k8s.io/cluster-api/exp/runtime/catalog" |
| 30 | +) |
| 31 | + |
| 32 | +func init() { |
| 33 | + // Register the metrics at the controller-runtime metrics registry. |
| 34 | + ctrlmetrics.Registry.MustRegister(RequestsTotal.metric) |
| 35 | + ctrlmetrics.Registry.MustRegister(RequestDuration.metric) |
| 36 | +} |
| 37 | + |
| 38 | +// Metrics subsystem and all of the keys used by the Runtime SDK. |
| 39 | +const ( |
| 40 | + runtimeSDKSubsystem = "capi_runtime_sdk" |
| 41 | +) |
| 42 | + |
| 43 | +var ( |
| 44 | + // RequestsTotal reports request results. |
| 45 | + RequestsTotal = requestsTotalObserver{ |
| 46 | + prometheus.NewCounterVec(prometheus.CounterOpts{ |
| 47 | + Subsystem: runtimeSDKSubsystem, |
| 48 | + Name: "requests_total", |
| 49 | + Help: "Number of HTTP requests, partitioned by status code, host and hook.", |
| 50 | + }, []string{"code", "host", "group", "version", "hook"}), |
| 51 | + } |
| 52 | + // RequestDuration reports the request latency in seconds. |
| 53 | + RequestDuration = requestDurationObserver{ |
| 54 | + prometheus.NewHistogramVec(prometheus.HistogramOpts{ |
| 55 | + Subsystem: runtimeSDKSubsystem, |
| 56 | + Name: "request_duration_seconds", |
| 57 | + Help: "Request duration in seconds, broken down by hook and host.", |
| 58 | + Buckets: prometheus.ExponentialBuckets(0.001, 2, 10), |
| 59 | + }, []string{"host", "group", "version", "hook"}), |
| 60 | + } |
| 61 | +) |
| 62 | + |
| 63 | +type requestsTotalObserver struct { |
| 64 | + metric *prometheus.CounterVec |
| 65 | +} |
| 66 | + |
| 67 | +// Observe observes a http request result and increments the metric for the given |
| 68 | +// error status code, host and gvh. |
| 69 | +func (m *requestsTotalObserver) Observe(req *http.Request, resp *http.Response, gvh runtimecatalog.GroupVersionHook, err error) { |
| 70 | + host := req.URL.Host |
| 71 | + |
| 72 | + // Errors can be arbitrary strings. Unbound label cardinality is not suitable for a metric |
| 73 | + // system so they are reported as `<error>`. |
| 74 | + code := "<error>" |
| 75 | + if err == nil { |
| 76 | + code = strconv.Itoa(resp.StatusCode) |
| 77 | + } |
| 78 | + m.metric.WithLabelValues(code, host, gvh.Group, gvh.Version, gvh.Hook).Inc() |
| 79 | +} |
| 80 | + |
| 81 | +type requestDurationObserver struct { |
| 82 | + metric *prometheus.HistogramVec |
| 83 | +} |
| 84 | + |
| 85 | +// Observe increments the request latency metric for the given host and gvh. |
| 86 | +func (m *requestDurationObserver) Observe(gvh runtimecatalog.GroupVersionHook, u url.URL, latency time.Duration) { |
| 87 | + m.metric.WithLabelValues(u.Host, gvh.Group, gvh.Version, gvh.Hook).Observe(latency.Seconds()) |
| 88 | +} |
0 commit comments