-
Notifications
You must be signed in to change notification settings - Fork 3.2k
/
k8s_request_total_metric.go
47 lines (40 loc) · 1.13 KB
/
k8s_request_total_metric.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package metrics
import (
"net/http"
"strconv"
"github.com/prometheus/client_golang/prometheus"
"k8s.io/client-go/rest"
"github.com/argoproj/argo/v3/util/k8s"
)
var (
K8sRequestTotalMetric = prometheus.NewCounterVec(
prometheus.CounterOpts{
Namespace: argoNamespace,
Subsystem: workflowsSubsystem,
Name: "k8s_request_total",
Help: "Number of kubernetes requests executed. https://argoproj.github.io/argo/metrics/#argo_workflows_k8s_request_total",
},
[]string{"kind", "verb", "status_code"},
)
)
type metricsRoundTripper struct {
roundTripper http.RoundTripper
}
func (m metricsRoundTripper) RoundTrip(r *http.Request) (*http.Response, error) {
x, err := m.roundTripper.RoundTrip(r)
if x != nil {
verb, kind := k8s.ParseRequest(r)
K8sRequestTotalMetric.WithLabelValues(kind, verb, strconv.Itoa(x.StatusCode)).Inc()
}
return x, err
}
func AddMetricsTransportWrapper(config *rest.Config) *rest.Config {
wrap := config.WrapTransport
config.WrapTransport = func(rt http.RoundTripper) http.RoundTripper {
if wrap != nil {
rt = wrap(rt)
}
return &metricsRoundTripper{roundTripper: rt}
}
return config
}