-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Emit metrics for requests to the Kubernetes control plane (#118)
* 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
1 parent
231935c
commit 8db9791
Showing
3 changed files
with
51 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file was deleted.
Oops, something went wrong.