Skip to content

Commit 92c8034

Browse files
committed
feat: add proxy error metric
Signed-off-by: Jim Ma <majinjing3@gmail.com>
1 parent b2babf8 commit 92c8034

File tree

2 files changed

+32
-16
lines changed

2 files changed

+32
-16
lines changed

client/daemon/metrics/metrics.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,13 @@ var (
6060
Help: "Counter of the total proxy request via Dragonfly.",
6161
})
6262

63+
ProxyErrorRequestViaDragonflyCount = promauto.NewCounter(prometheus.CounterOpts{
64+
Namespace: types.MetricsNamespace,
65+
Subsystem: types.DfdaemonMetricsName,
66+
Name: "proxy_error_request_via_dragonfly_total",
67+
Help: "Counter of the total error proxy request via Dragonfly.",
68+
})
69+
6370
ProxyRequestNotViaDragonflyCount = promauto.NewCounter(prometheus.CounterOpts{
6471
Namespace: types.MetricsNamespace,
6572
Subsystem: types.DfdaemonMetricsName,

client/daemon/transport/transport.go

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -229,30 +229,23 @@ func New(options ...Option) http.RoundTripper {
229229
// RoundTrip only process first redirect at present
230230
func (rt *transport) RoundTrip(req *http.Request) (resp *http.Response, err error) {
231231
if rt.shouldUseDragonfly(req) {
232-
// delete the Accept-Encoding header to avoid returning the same cached
233-
// result for different requests
234-
req.Header.Del("Accept-Encoding")
235-
236-
ctx := req.Context()
237-
if req.URL.Scheme == "https" {
238-
// for https, the trace info is in request header
239-
ctx = traceContext.Extract(req.Context(), propagation.HeaderCarrier(req.Header))
232+
resp, err = rt.roundTripWithDragonfly(req)
233+
if err != nil {
234+
metrics.ProxyErrorRequestViaDragonflyCount.Add(1)
235+
logger.With("method", req.Method, "url", req.URL.String()).Errorf("round trip with dragonfly error: %s", err)
236+
return resp, err
240237
}
241-
242-
logger.Debugf("round trip with dragonfly: %s", req.URL.String())
243-
metrics.ProxyRequestViaDragonflyCount.Add(1)
244-
resp, err = rt.download(ctx, req)
245238
} else {
246239
logger.Debugf("round trip directly, method: %s, url: %s", req.Method, req.URL.String())
247240
req.Host = req.URL.Host
248241
req.Header.Set("Host", req.Host)
249242
metrics.ProxyRequestNotViaDragonflyCount.Add(1)
250243
resp, err = rt.baseRoundTripper.RoundTrip(req)
251-
}
252244

253-
if err != nil {
254-
logger.With("method", req.Method, "url", req.URL.String()).Errorf("round trip error: %s", err)
255-
return resp, err
245+
if err != nil {
246+
logger.With("method", req.Method, "url", req.URL.String()).Errorf("round trip directly error: %s", err)
247+
return resp, err
248+
}
256249
}
257250

258251
if resp.ContentLength > 0 {
@@ -263,6 +256,22 @@ func (rt *transport) RoundTrip(req *http.Request) (resp *http.Response, err erro
263256
return resp, err
264257
}
265258

259+
func (rt *transport) roundTripWithDragonfly(req *http.Request) (*http.Response, error) {
260+
// delete the Accept-Encoding header to avoid returning the same cached
261+
// result for different requests
262+
req.Header.Del("Accept-Encoding")
263+
264+
ctx := req.Context()
265+
if req.URL.Scheme == "https" {
266+
// for https, the trace info is in request header
267+
ctx = traceContext.Extract(req.Context(), propagation.HeaderCarrier(req.Header))
268+
}
269+
270+
logger.Debugf("round trip with dragonfly: %s", req.URL.String())
271+
metrics.ProxyRequestViaDragonflyCount.Add(1)
272+
return rt.download(ctx, req)
273+
}
274+
266275
// NeedUseDragonfly is the default value for shouldUseDragonfly, which downloads all
267276
// images layers with dragonfly.
268277
func NeedUseDragonfly(req *http.Request) bool {

0 commit comments

Comments
 (0)