From 2c0ae7819ecf77069c7a021d2cc9517370ec8ed7 Mon Sep 17 00:00:00 2001 From: Jim Ma Date: Wed, 28 Aug 2024 17:35:21 +0800 Subject: [PATCH] feat: add proxy error metric (#3470) Signed-off-by: Jim Ma --- client/daemon/metrics/metrics.go | 7 +++++ client/daemon/transport/transport.go | 41 +++++++++++++++++----------- 2 files changed, 32 insertions(+), 16 deletions(-) diff --git a/client/daemon/metrics/metrics.go b/client/daemon/metrics/metrics.go index 61a4d158b98..dd64fad4fca 100644 --- a/client/daemon/metrics/metrics.go +++ b/client/daemon/metrics/metrics.go @@ -60,6 +60,13 @@ var ( Help: "Counter of the total proxy request via Dragonfly.", }) + ProxyErrorRequestViaDragonflyCount = promauto.NewCounter(prometheus.CounterOpts{ + Namespace: types.MetricsNamespace, + Subsystem: types.DfdaemonMetricsName, + Name: "proxy_error_request_via_dragonfly_total", + Help: "Counter of the total error proxy request via Dragonfly.", + }) + ProxyRequestNotViaDragonflyCount = promauto.NewCounter(prometheus.CounterOpts{ Namespace: types.MetricsNamespace, Subsystem: types.DfdaemonMetricsName, diff --git a/client/daemon/transport/transport.go b/client/daemon/transport/transport.go index d4fe6751243..00ceac492ce 100644 --- a/client/daemon/transport/transport.go +++ b/client/daemon/transport/transport.go @@ -229,30 +229,23 @@ func New(options ...Option) http.RoundTripper { // RoundTrip only process first redirect at present func (rt *transport) RoundTrip(req *http.Request) (resp *http.Response, err error) { if rt.shouldUseDragonfly(req) { - // delete the Accept-Encoding header to avoid returning the same cached - // result for different requests - req.Header.Del("Accept-Encoding") - - ctx := req.Context() - if req.URL.Scheme == "https" { - // for https, the trace info is in request header - ctx = traceContext.Extract(req.Context(), propagation.HeaderCarrier(req.Header)) + resp, err = rt.roundTripWithDragonfly(req) + if err != nil { + metrics.ProxyErrorRequestViaDragonflyCount.Add(1) + logger.With("method", req.Method, "url", req.URL.String()).Errorf("round trip with dragonfly error: %s", err) + return resp, err } - - logger.Debugf("round trip with dragonfly: %s", req.URL.String()) - metrics.ProxyRequestViaDragonflyCount.Add(1) - resp, err = rt.download(ctx, req) } else { logger.Debugf("round trip directly, method: %s, url: %s", req.Method, req.URL.String()) req.Host = req.URL.Host req.Header.Set("Host", req.Host) metrics.ProxyRequestNotViaDragonflyCount.Add(1) resp, err = rt.baseRoundTripper.RoundTrip(req) - } - if err != nil { - logger.With("method", req.Method, "url", req.URL.String()).Errorf("round trip error: %s", err) - return resp, err + if err != nil { + logger.With("method", req.Method, "url", req.URL.String()).Errorf("round trip directly error: %s", err) + return resp, err + } } if resp.ContentLength > 0 { @@ -263,6 +256,22 @@ func (rt *transport) RoundTrip(req *http.Request) (resp *http.Response, err erro return resp, err } +func (rt *transport) roundTripWithDragonfly(req *http.Request) (*http.Response, error) { + // delete the Accept-Encoding header to avoid returning the same cached + // result for different requests + req.Header.Del("Accept-Encoding") + + ctx := req.Context() + if req.URL.Scheme == "https" { + // for https, the trace info is in request header + ctx = traceContext.Extract(req.Context(), propagation.HeaderCarrier(req.Header)) + } + + logger.Debugf("round trip with dragonfly: %s", req.URL.String()) + metrics.ProxyRequestViaDragonflyCount.Add(1) + return rt.download(ctx, req) +} + // NeedUseDragonfly is the default value for shouldUseDragonfly, which downloads all // images layers with dragonfly. func NeedUseDragonfly(req *http.Request) bool {