-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Runtime SDK implement metrics for runtime hook client
- Loading branch information
Showing
3 changed files
with
97 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
/* | ||
Copyright 2022 The Kubernetes Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
// Package metrics provides functions for creating Runtime SDK related metrics. | ||
package metrics | ||
|
||
import ( | ||
"net/http" | ||
"net/url" | ||
"strconv" | ||
"time" | ||
|
||
"github.com/prometheus/client_golang/prometheus" | ||
ctrlmetrics "sigs.k8s.io/controller-runtime/pkg/metrics" | ||
|
||
runtimecatalog "sigs.k8s.io/cluster-api/exp/runtime/catalog" | ||
) | ||
|
||
func init() { | ||
// Register the metrics at the controller-runtime metrics registry. | ||
ctrlmetrics.Registry.MustRegister(RequestsTotal.metric) | ||
ctrlmetrics.Registry.MustRegister(RequestDuration.metric) | ||
} | ||
|
||
// Metrics subsystem and all of the keys used by the Runtime SDK. | ||
const ( | ||
runtimeSDKSubsystem = "capi_runtime_sdk" | ||
) | ||
|
||
var ( | ||
// RequestsTotal reports request results. | ||
RequestsTotal = requestsTotalObserver{ | ||
prometheus.NewCounterVec(prometheus.CounterOpts{ | ||
Subsystem: runtimeSDKSubsystem, | ||
Name: "requests_total", | ||
Help: "Number of HTTP requests, partitioned by status code, host and hook.", | ||
}, []string{"code", "host", "group", "version", "hook"}), | ||
} | ||
// RequestDuration reports the request latency in seconds. | ||
RequestDuration = requestDurationObserver{ | ||
prometheus.NewHistogramVec(prometheus.HistogramOpts{ | ||
Subsystem: runtimeSDKSubsystem, | ||
Name: "request_duration_seconds", | ||
Help: "Request duration in seconds, broken down by hook and host.", | ||
Buckets: prometheus.ExponentialBuckets(0.001, 2, 10), | ||
}, []string{"host", "group", "version", "hook"}), | ||
} | ||
) | ||
|
||
type requestsTotalObserver struct { | ||
metric *prometheus.CounterVec | ||
} | ||
|
||
// Observe observes a http request result and increments the metric for the given | ||
// error status code, host and gvh. | ||
func (m *requestsTotalObserver) Observe(req *http.Request, resp *http.Response, gvh runtimecatalog.GroupVersionHook, err error) { | ||
host := req.URL.Host | ||
|
||
// Errors can be arbitrary strings. Unbound label cardinality is not suitable for a metric | ||
// system so they are reported as `<error>`. | ||
code := "<error>" | ||
if err == nil { | ||
code = strconv.Itoa(resp.StatusCode) | ||
} | ||
m.metric.WithLabelValues(code, host, gvh.Group, gvh.Version, gvh.Hook).Inc() | ||
} | ||
|
||
type requestDurationObserver struct { | ||
metric *prometheus.HistogramVec | ||
} | ||
|
||
// Observe increments the request latency metric for the given host and gvh. | ||
func (m *requestDurationObserver) Observe(gvh runtimecatalog.GroupVersionHook, u url.URL, latency time.Duration) { | ||
m.metric.WithLabelValues(u.Host, gvh.Group, gvh.Version, gvh.Hook).Observe(latency.Seconds()) | ||
} |