From e4682237d5e32c19781168bce27625ec7fa9c59b Mon Sep 17 00:00:00 2001 From: Marcin Rataj Date: Mon, 22 May 2023 13:59:55 +0200 Subject: [PATCH] feat(gw): add ipfs_http_gw_car_stream_fail_duration_seconds (#312) Closes #288 --- gateway/handler.go | 2 ++ gateway/handler_car.go | 3 +++ gateway/handler_tar.go | 3 +++ gateway/metrics.go | 9 +++++++++ 4 files changed, 17 insertions(+) diff --git a/gateway/handler.go b/gateway/handler.go index 743fb9942..fcb61e416 100644 --- a/gateway/handler.go +++ b/gateway/handler.go @@ -73,8 +73,10 @@ type handler struct { unixfsDirIndexGetMetric *prometheus.HistogramVec unixfsGenDirListingGetMetric *prometheus.HistogramVec carStreamGetMetric *prometheus.HistogramVec + carStreamFailMetric *prometheus.HistogramVec rawBlockGetMetric *prometheus.HistogramVec tarStreamGetMetric *prometheus.HistogramVec + tarStreamFailMetric *prometheus.HistogramVec jsoncborDocumentGetMetric *prometheus.HistogramVec ipnsRecordGetMetric *prometheus.HistogramVec } diff --git a/gateway/handler_car.go b/gateway/handler_car.go index beb17396b..f2e7ca006 100644 --- a/gateway/handler_car.go +++ b/gateway/handler_car.go @@ -81,6 +81,9 @@ func (i *handler) serveCAR(ctx context.Context, w http.ResponseWriter, r *http.R carErr := <-errCh streamErr := multierr.Combine(carErr, copyErr) if streamErr != nil { + // Update fail metric + i.carStreamFailMetric.WithLabelValues(contentPath.Namespace()).Observe(time.Since(begin).Seconds()) + // We return error as a trailer, however it is not something browsers can access // (https://github.com/mdn/browser-compat-data/issues/14703) // Due to this, we suggest client always verify that diff --git a/gateway/handler_tar.go b/gateway/handler_tar.go index 0825b8a49..f397e57b5 100644 --- a/gateway/handler_tar.go +++ b/gateway/handler_tar.go @@ -81,6 +81,9 @@ func (i *handler) serveTAR(ctx context.Context, w http.ResponseWriter, r *http.R // The TAR has a top-level directory (or file) named by the CID. if err := tarw.WriteFile(file, rootCid.String()); err != nil { + // Update fail metric + i.tarStreamFailMetric.WithLabelValues(contentPath.Namespace()).Observe(time.Since(begin).Seconds()) + w.Header().Set("X-Stream-Error", err.Error()) // Trailer headers do not work in web browsers // (see https://github.com/mdn/browser-compat-data/issues/14703) diff --git a/gateway/metrics.go b/gateway/metrics.go index 5fc9a796a..d6368f92e 100644 --- a/gateway/metrics.go +++ b/gateway/metrics.go @@ -219,6 +219,10 @@ func newHandlerWithMetrics(c Config, api IPFSBackend) *handler { "gw_car_stream_get_duration_seconds", "The time to GET an entire CAR stream from the gateway.", ), + carStreamFailMetric: newHistogramMetric( + "gw_car_stream_fail_duration_seconds", + "How long a CAR was streamed before failing mid-stream.", + ), // Block: time it takes to return requested Block rawBlockGetMetric: newHistogramMetric( "gw_raw_block_get_duration_seconds", @@ -229,6 +233,11 @@ func newHandlerWithMetrics(c Config, api IPFSBackend) *handler { "gw_tar_stream_get_duration_seconds", "The time to GET an entire TAR stream from the gateway.", ), + // TAR: time it takes to return requested TAR stream + tarStreamFailMetric: newHistogramMetric( + "gw_tar_stream_fail_duration_seconds", + "How long a TAR was streamed before failing mid-stream.", + ), // JSON/CBOR: time it takes to return requested DAG-JSON/-CBOR document jsoncborDocumentGetMetric: newHistogramMetric( "gw_jsoncbor_get_duration_seconds",