Skip to content

Commit

Permalink
feat: add proxy error metric (#3470)
Browse files Browse the repository at this point in the history
Signed-off-by: Jim Ma <majinjing3@gmail.com>
  • Loading branch information
jim3ma authored Aug 28, 2024
1 parent b23b18d commit 2c0ae78
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 16 deletions.
7 changes: 7 additions & 0 deletions client/daemon/metrics/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
41 changes: 25 additions & 16 deletions client/daemon/transport/transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down

0 comments on commit 2c0ae78

Please sign in to comment.