Skip to content

Commit

Permalink
Runtime SDK implement metrics for runtime hook client
Browse files Browse the repository at this point in the history
  • Loading branch information
chrischdi committed Jun 21, 2022
1 parent 41664d9 commit c3425b3
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 1 deletion.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ require (
github.com/pelletier/go-toml v1.9.4 // indirect
github.com/peterbourgon/diskv v2.0.1+incompatible // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/prometheus/client_golang v1.12.1 // indirect
github.com/prometheus/client_golang v1.12.1
github.com/prometheus/client_model v0.2.0 // indirect
github.com/prometheus/common v0.32.1 // indirect
github.com/prometheus/procfs v0.7.3 // indirect
Expand Down
8 changes: 8 additions & 0 deletions internal/runtime/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ import (
runtimev1 "sigs.k8s.io/cluster-api/exp/runtime/api/v1alpha1"
runtimehooksv1 "sigs.k8s.io/cluster-api/exp/runtime/hooks/api/v1alpha1"
runtimecatalog "sigs.k8s.io/cluster-api/internal/runtime/catalog"
runtimemetrics "sigs.k8s.io/cluster-api/internal/runtime/metrics"
runtimeregistry "sigs.k8s.io/cluster-api/internal/runtime/registry"
"sigs.k8s.io/cluster-api/util"
)
Expand Down Expand Up @@ -375,6 +376,11 @@ func httpCall(ctx context.Context, request, response runtime.Object, opts *httpC
return errors.Wrapf(err, "failed to compute URL of the extension handler %q", opts.name)
}

// Create request latency metric.
start := time.Now()
defer func() {
runtimemetrics.RequestLatency.Observe(opts.hookGVH.Hook, *extensionURL, time.Since(start))
}()
requireConversion := opts.registrationGVH.Version != opts.hookGVH.Version

requestLocal := request
Expand Down Expand Up @@ -448,6 +454,8 @@ func httpCall(ctx context.Context, request, response runtime.Object, opts *httpC
})

resp, err := client.Do(httpRequest)
// Create http request metric.
runtimemetrics.RequestsTotal.Observe(httpRequest, resp, opts.hookGVH.Hook, err)
if err != nil {
return errCallingExtensionHandler(
errors.Wrapf(err, "failed to call ExtensionHandler: %q", opts.name),
Expand Down
97 changes: 97 additions & 0 deletions internal/runtime/metrics/metrics.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
/*
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"
)

func init() {
// Register the metrics at the controller-runtime registry metrics registry
// to expose them.
ctrlmetrics.Registry.MustRegister(RequestsTotal.metric)
ctrlmetrics.Registry.MustRegister(RequestLatency.metric)
}

// Metrics subsystem and all of the keys used by the Runtime SDK.
const (
runtimeSDKSubsystem = "runtime_sdk"
requestLatencyKey = "request_latency_seconds"
requestsTotalKey = "requests_total"
)

var (
// RequestsTotal reports request result by return code, method, host and hook.
RequestsTotal = requestsTotalObserver{
prometheus.NewCounterVec(prometheus.CounterOpts{
Subsystem: runtimeSDKSubsystem,
Name: requestsTotalKey,
Help: "Number of HTTP requests, partitioned by status code, method, and host.",
}, []string{"code", "method", "host", "hook"}),
}
// RequestLatency reports the request latency in seconds per hook and host.
RequestLatency = latencyMetricObserver{
prometheus.NewHistogramVec(prometheus.HistogramOpts{
Subsystem: runtimeSDKSubsystem,
Name: requestLatencyKey,
Help: "Request latency in seconds, broken down by verb and host.",
Buckets: prometheus.ExponentialBuckets(0.001, 2, 10),
}, []string{"hook", "host"}),
}
)

type requestsTotalObserver struct {
metric *prometheus.CounterVec
}

type latencyMetricObserver struct {
metric *prometheus.HistogramVec
}

func (m *requestsTotalObserver) increment(code, method, host, hook string) {
m.metric.WithLabelValues(code, method, host, hook).Inc()
}

// Observe observes a http request result and increments the metric for the given
// error code, method, host and hook.
func (m *requestsTotalObserver) Observe(req *http.Request, resp *http.Response, hook string, err error) {
host := "none"
if req.URL != nil {
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>`.
if err != nil {
m.increment("<error>", req.Method, host, hook)
} else {
// Metrics for failure codes.
m.increment(strconv.Itoa(resp.StatusCode), req.Method, host, hook)
}
}

// Observe increments the request latency metric for the given verb and host.
func (m *latencyMetricObserver) Observe(hook string, u url.URL, latency time.Duration) {
m.metric.WithLabelValues(hook, u.Host).Observe(latency.Seconds())
}

0 comments on commit c3425b3

Please sign in to comment.