Skip to content

Commit

Permalink
refactor: unixfs_get_latency_seconds
Browse files Browse the repository at this point in the history
Include block and car in unixfs_get_latency_seconds for now,
so we keep basic visibility into gateway behavior until better metrics
are added by #8441
  • Loading branch information
lidel committed Mar 8, 2022
1 parent 9fbfb0b commit ee7b0ae
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 5 deletions.
13 changes: 10 additions & 3 deletions core/corehttp/gateway_handler.go
Expand Up @@ -64,7 +64,6 @@ type gatewayHandler struct {
config GatewayConfig
api coreiface.CoreAPI

// TODO: add metrics for non-unixfs responses (block, car)
unixfsGetMetric *prometheus.SummaryVec
}

Expand All @@ -90,6 +89,7 @@ func (sw *statusResponseWriter) WriteHeader(code int) {

func newGatewayHandler(c GatewayConfig, api coreiface.CoreAPI) *gatewayHandler {
unixfsGetMetric := prometheus.NewSummaryVec(
// TODO: deprecate and switch to content type agnostic metrics: https://github.com/ipfs/go-ipfs/issues/8441
prometheus.SummaryOpts{
Namespace: "ipfs",
Subsystem: "http",
Expand Down Expand Up @@ -305,6 +305,15 @@ func (i *gatewayHandler) getOrHeadHandler(w http.ResponseWriter, r *http.Request
return
}

// Update the global metric of the time it takes to read the final root block of the requested resource
// NOTE: for legacy reasons this happens before we go into content-type specific code paths
_, err = i.api.Block().Get(r.Context(), resolvedPath)
if err != nil {
webError(w, "ipfs block get "+resolvedPath.Cid().String(), err, http.StatusInternalServerError)
return
}
i.unixfsGetMetric.WithLabelValues(parsedPath.Namespace()).Observe(time.Since(begin).Seconds())

// HTTP Headers
i.addUserHeaders(w) // ok, _now_ write user's headers.
w.Header().Set("X-Ipfs-Path", urlPath)
Expand Down Expand Up @@ -348,8 +357,6 @@ func (i *gatewayHandler) getOrHeadHandler(w http.ResponseWriter, r *http.Request
webError(w, "ipfs cat "+escapedURLPath, err, http.StatusNotFound)
return
}
// TODO: do we want to reuse unixfsGetMetric for block/car, or should we have separate ones?
i.unixfsGetMetric.WithLabelValues(parsedPath.Namespace()).Observe(time.Since(begin).Seconds())
defer dr.Close()

// Handling Unixfs file
Expand Down
4 changes: 2 additions & 2 deletions core/corehttp/gateway_handler_block.go
Expand Up @@ -13,12 +13,12 @@ import (
func (i *gatewayHandler) serveRawBlock(w http.ResponseWriter, r *http.Request, blockCid cid.Cid, contentPath ipath.Path) {
blockReader, err := i.api.Block().Get(r.Context(), contentPath)
if err != nil {
webError(w, "failed to get block", err, http.StatusInternalServerError)
webError(w, "ipfs block get "+blockCid.String(), err, http.StatusInternalServerError)
return
}
block, err := ioutil.ReadAll(blockReader)
if err != nil {
webError(w, "failed to read block", err, http.StatusInternalServerError)
webError(w, "ipfs block get "+blockCid.String(), err, http.StatusInternalServerError)
return
}
content := bytes.NewReader(block)
Expand Down
1 change: 1 addition & 0 deletions core/corehttp/gateway_handler_car.go
Expand Up @@ -45,6 +45,7 @@ func (i *gatewayHandler) serveCar(w http.ResponseWriter, r *http.Request, rootCi

if err := car.Write(w); err != nil {
// TODO: can we do any error handling here?
// TODO: idea: add best-effort proxy reader which will set http.StatusOK only if the first block is yielded correctly
}
}

Expand Down

0 comments on commit ee7b0ae

Please sign in to comment.