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 28, 2022
1 parent 28d146b commit e36b78f
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 1 deletion.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ require (
github.com/pelletier/go-toml v1.9.5 // 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"
runtimecatalog "sigs.k8s.io/cluster-api/exp/runtime/catalog"
runtimehooksv1 "sigs.k8s.io/cluster-api/exp/runtime/hooks/api/v1alpha1"
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 @@ -377,6 +378,11 @@ func httpCall(ctx context.Context, request, response runtime.Object, opts *httpC
return errors.Wrap(err, "http call failed")
}

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

requestLocal := request
Expand Down Expand Up @@ -450,6 +456,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, err)
if err != nil {
return errCallingExtensionHandler(
errors.Wrapf(err, "http call failed"),
Expand Down
88 changes: 88 additions & 0 deletions internal/runtime/metrics/metrics.go
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())
}

0 comments on commit e36b78f

Please sign in to comment.