Skip to content

Commit

Permalink
Emit metrics for requests to the Kubernetes control plane (#118)
Browse files Browse the repository at this point in the history
* Rename file and type in preparation for adding metrics support

* Emit latency and result metrics for requests to the Kubernetes control plane

* Sort imports
  • Loading branch information
charleskorn authored Jan 17, 2024
1 parent 231935c commit 8db9791
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 27 deletions.
6 changes: 1 addition & 5 deletions cmd/rollout-operator/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import (
"github.com/go-kit/log"
"github.com/go-kit/log/level"
"github.com/grafana/dskit/tracing"
"github.com/opentracing-contrib/go-stdlib/nethttp"
"github.com/pkg/errors"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
Expand Down Expand Up @@ -141,10 +140,7 @@ func main() {
// Build the Kubernetes client config.
kubeConfig, err := buildKubeConfig(cfg.kubeAPIURL, cfg.kubeConfigFile)
check(errors.Wrap(err, "failed to build Kubernetes client config"))

kubeConfig.Wrap(func(rt http.RoundTripper) http.RoundTripper {
return instrumentation.NewKubernetesAPIClientTracer(&nethttp.Transport{RoundTripper: rt})
})
instrumentation.InstrumentKubernetesAPIClient(kubeConfig, reg)

kubeClient, err := kubernetes.NewForConfig(kubeConfig)
check(errors.Wrap(err, "failed to create Kubernetes client"))
Expand Down
50 changes: 50 additions & 0 deletions pkg/instrumentation/kubernetes_api_client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package instrumentation

import (
"net/http"
"strconv"
"time"

"github.com/grafana/dskit/instrument"
"github.com/opentracing-contrib/go-stdlib/nethttp"
"github.com/opentracing/opentracing-go"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
"k8s.io/client-go/rest"
)

type kubernetesAPIClientInstrumentation struct {
next http.RoundTripper
hist *prometheus.HistogramVec
}

func InstrumentKubernetesAPIClient(cfg *rest.Config, reg prometheus.Registerer) {
hist := promauto.With(reg).NewHistogramVec(
prometheus.HistogramOpts{
Name: "rollout_operator_kubernetes_api_client_request_duration_seconds",
Help: "Time (in seconds) spent waiting for requests to the Kubernetes API",
Buckets: instrument.DefBuckets,
},
[]string{"path", "method", "status_code"},
)

cfg.Wrap(func(rt http.RoundTripper) http.RoundTripper {
return &kubernetesAPIClientInstrumentation{
next: &nethttp.Transport{RoundTripper: rt},
hist: hist,
}
})
}

func (k *kubernetesAPIClientInstrumentation) RoundTrip(req *http.Request) (*http.Response, error) {
start := time.Now()

req, ht := nethttp.TraceRequest(opentracing.GlobalTracer(), req)
defer ht.Finish()

resp, err := k.next.RoundTrip(req)
duration := time.Since(start)
instrument.ObserveWithExemplar(req.Context(), k.hist.WithLabelValues(req.URL.EscapedPath(), req.Method, strconv.Itoa(resp.StatusCode)), duration.Seconds())

return resp, err
}
22 changes: 0 additions & 22 deletions pkg/instrumentation/kubernetes_api_tracing.go

This file was deleted.

0 comments on commit 8db9791

Please sign in to comment.