@@ -24,9 +24,11 @@ import (
24
24
"time"
25
25
26
26
"github.com/prometheus/client_golang/prometheus"
27
+ "k8s.io/apimachinery/pkg/runtime"
27
28
ctrlmetrics "sigs.k8s.io/controller-runtime/pkg/metrics"
28
29
29
30
runtimecatalog "sigs.k8s.io/cluster-api/exp/runtime/catalog"
31
+ runtimehooksv1 "sigs.k8s.io/cluster-api/exp/runtime/hooks/api/v1alpha1"
30
32
)
31
33
32
34
func init () {
@@ -37,7 +39,8 @@ func init() {
37
39
38
40
// Metrics subsystem and all of the keys used by the Runtime SDK.
39
41
const (
40
- runtimeSDKSubsystem = "capi_runtime_sdk"
42
+ runtimeSDKSubsystem = "capi_runtime_sdk"
43
+ unknownResponseStatus = "Unknown"
41
44
)
42
45
43
46
var (
@@ -46,16 +49,17 @@ var (
46
49
prometheus .NewCounterVec (prometheus.CounterOpts {
47
50
Subsystem : runtimeSDKSubsystem ,
48
51
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" }),
51
54
}
52
55
// RequestDuration reports the request latency in seconds.
53
56
RequestDuration = requestDurationObserver {
54
57
prometheus .NewHistogramVec (prometheus.HistogramOpts {
55
58
Subsystem : runtimeSDKSubsystem ,
56
59
Name : "request_duration_seconds" ,
57
60
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 },
59
63
}, []string {"host" , "group" , "version" , "hook" }),
60
64
}
61
65
)
@@ -65,8 +69,8 @@ type requestsTotalObserver struct {
65
69
}
66
70
67
71
// 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 ) {
70
74
host := req .URL .Host
71
75
72
76
// 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,
75
79
if err == nil {
76
80
code = strconv .Itoa (resp .StatusCode )
77
81
}
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 ()
79
89
}
80
90
81
91
type requestDurationObserver struct {
0 commit comments