Skip to content

Commit 490c85b

Browse files
authored
Merge pull request #7956 from MaxFedotov/runtime-sdk-response-status-metrics
✨ add response metrics for RuntimeSDK hook client
2 parents 72ad2dc + 60317e0 commit 490c85b

File tree

2 files changed

+22
-8
lines changed

2 files changed

+22
-8
lines changed

internal/runtime/client/client.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -488,8 +488,12 @@ func httpCall(ctx context.Context, request, response runtime.Object, opts *httpC
488488
})
489489

490490
resp, err := client.Do(httpRequest)
491+
491492
// Create http request metric.
492-
runtimemetrics.RequestsTotal.Observe(httpRequest, resp, opts.hookGVH, err)
493+
defer func() {
494+
runtimemetrics.RequestsTotal.Observe(httpRequest, resp, opts.hookGVH, err, response)
495+
}()
496+
493497
if err != nil {
494498
return errCallingExtensionHandler(
495499
errors.Wrapf(err, "http call failed"),

internal/runtime/metrics/metrics.go

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,11 @@ import (
2424
"time"
2525

2626
"github.com/prometheus/client_golang/prometheus"
27+
"k8s.io/apimachinery/pkg/runtime"
2728
ctrlmetrics "sigs.k8s.io/controller-runtime/pkg/metrics"
2829

2930
runtimecatalog "sigs.k8s.io/cluster-api/exp/runtime/catalog"
31+
runtimehooksv1 "sigs.k8s.io/cluster-api/exp/runtime/hooks/api/v1alpha1"
3032
)
3133

3234
func init() {
@@ -37,7 +39,8 @@ func init() {
3739

3840
// Metrics subsystem and all of the keys used by the Runtime SDK.
3941
const (
40-
runtimeSDKSubsystem = "capi_runtime_sdk"
42+
runtimeSDKSubsystem = "capi_runtime_sdk"
43+
unknownResponseStatus = "Unknown"
4144
)
4245

4346
var (
@@ -46,16 +49,17 @@ var (
4649
prometheus.NewCounterVec(prometheus.CounterOpts{
4750
Subsystem: runtimeSDKSubsystem,
4851
Name: "requests_total",
49-
Help: "Number of HTTP requests, partitioned by status code, host and hook.",
50-
}, []string{"code", "host", "group", "version", "hook"}),
52+
Help: "Number of HTTP requests, partitioned by status code, host, hook and response status.",
53+
}, []string{"code", "host", "group", "version", "hook", "status"}),
5154
}
5255
// RequestDuration reports the request latency in seconds.
5356
RequestDuration = requestDurationObserver{
5457
prometheus.NewHistogramVec(prometheus.HistogramOpts{
5558
Subsystem: runtimeSDKSubsystem,
5659
Name: "request_duration_seconds",
5760
Help: "Request duration in seconds, broken down by hook and host.",
58-
Buckets: prometheus.ExponentialBuckets(0.001, 2, 10),
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},
5963
}, []string{"host", "group", "version", "hook"}),
6064
}
6165
)
@@ -65,8 +69,8 @@ type requestsTotalObserver struct {
6569
}
6670

6771
// 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) {
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) {
7074
host := req.URL.Host
7175

7276
// Errors can be arbitrary strings. Unbound label cardinality is not suitable for a metric
@@ -75,7 +79,13 @@ func (m *requestsTotalObserver) Observe(req *http.Request, resp *http.Response,
7579
if err == nil {
7680
code = strconv.Itoa(resp.StatusCode)
7781
}
78-
m.metric.WithLabelValues(code, host, gvh.Group, gvh.Version, gvh.Hook).Inc()
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()
7989
}
8090

8191
type requestDurationObserver struct {

0 commit comments

Comments
 (0)