mirror of
https://github.com/ipfs/kubo.git
synced 2026-03-04 07:48:00 +08:00
fix: fix context plumbing in gateway handlers (#8871)
This ensures that child contexts are passed around between the handlers so that traces show the call hierarchy correctly.
This commit is contained in:
parent
52bf133946
commit
9bd346e250
@ -443,16 +443,16 @@ func (i *gatewayHandler) getOrHeadHandler(w http.ResponseWriter, r *http.Request
|
||||
switch responseFormat {
|
||||
case "": // The implicit response format is UnixFS
|
||||
logger.Debugw("serving unixfs", "path", contentPath)
|
||||
i.serveUnixFs(w, r, resolvedPath, contentPath, begin, logger)
|
||||
i.serveUnixFS(r.Context(), w, r, resolvedPath, contentPath, begin, logger)
|
||||
return
|
||||
case "application/vnd.ipld.raw":
|
||||
logger.Debugw("serving raw block", "path", contentPath)
|
||||
i.serveRawBlock(w, r, resolvedPath, contentPath, begin)
|
||||
i.serveRawBlock(r.Context(), w, r, resolvedPath, contentPath, begin)
|
||||
return
|
||||
case "application/vnd.ipld.car":
|
||||
logger.Debugw("serving car stream", "path", contentPath)
|
||||
carVersion := formatParams["version"]
|
||||
i.serveCar(w, r, resolvedPath, contentPath, carVersion, begin)
|
||||
i.serveCAR(r.Context(), w, r, resolvedPath, contentPath, carVersion, begin)
|
||||
return
|
||||
default: // catch-all for unsuported application/vnd.*
|
||||
err := fmt.Errorf("unsupported format %q", responseFormat)
|
||||
|
||||
@ -2,6 +2,7 @@ package corehttp
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"time"
|
||||
@ -13,8 +14,8 @@ import (
|
||||
)
|
||||
|
||||
// serveRawBlock returns bytes behind a raw block
|
||||
func (i *gatewayHandler) serveRawBlock(w http.ResponseWriter, r *http.Request, resolvedPath ipath.Resolved, contentPath ipath.Path, begin time.Time) {
|
||||
ctx, span := tracing.Span(r.Context(), "Gateway", "ServeRawBlock", trace.WithAttributes(attribute.String("path", resolvedPath.String())))
|
||||
func (i *gatewayHandler) serveRawBlock(ctx context.Context, w http.ResponseWriter, r *http.Request, resolvedPath ipath.Resolved, contentPath ipath.Path, begin time.Time) {
|
||||
ctx, span := tracing.Span(ctx, "Gateway", "ServeRawBlock", trace.WithAttributes(attribute.String("path", resolvedPath.String())))
|
||||
defer span.End()
|
||||
blockCid := resolvedPath.Cid()
|
||||
blockReader, err := i.api.Block().Get(ctx, resolvedPath)
|
||||
|
||||
@ -17,9 +17,9 @@ import (
|
||||
"go.opentelemetry.io/otel/trace"
|
||||
)
|
||||
|
||||
// serveCar returns a CAR stream for specific DAG+selector
|
||||
func (i *gatewayHandler) serveCar(w http.ResponseWriter, r *http.Request, resolvedPath ipath.Resolved, contentPath ipath.Path, carVersion string, begin time.Time) {
|
||||
ctx, span := tracing.Span(r.Context(), "Gateway", "ServeCar", trace.WithAttributes(attribute.String("path", resolvedPath.String())))
|
||||
// serveCAR returns a CAR stream for specific DAG+selector
|
||||
func (i *gatewayHandler) serveCAR(ctx context.Context, w http.ResponseWriter, r *http.Request, resolvedPath ipath.Resolved, contentPath ipath.Path, carVersion string, begin time.Time) {
|
||||
ctx, span := tracing.Span(ctx, "Gateway", "ServeCAR", trace.WithAttributes(attribute.String("path", resolvedPath.String())))
|
||||
defer span.End()
|
||||
ctx, cancel := context.WithCancel(ctx)
|
||||
defer cancel()
|
||||
|
||||
@ -1,6 +1,7 @@
|
||||
package corehttp
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"html"
|
||||
"net/http"
|
||||
@ -14,8 +15,8 @@ import (
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
func (i *gatewayHandler) serveUnixFs(w http.ResponseWriter, r *http.Request, resolvedPath ipath.Resolved, contentPath ipath.Path, begin time.Time, logger *zap.SugaredLogger) {
|
||||
ctx, span := tracing.Span(r.Context(), "Gateway", "ServeUnixFs", trace.WithAttributes(attribute.String("path", resolvedPath.String())))
|
||||
func (i *gatewayHandler) serveUnixFS(ctx context.Context, w http.ResponseWriter, r *http.Request, resolvedPath ipath.Resolved, contentPath ipath.Path, begin time.Time, logger *zap.SugaredLogger) {
|
||||
ctx, span := tracing.Span(ctx, "Gateway", "ServeUnixFS", trace.WithAttributes(attribute.String("path", resolvedPath.String())))
|
||||
defer span.End()
|
||||
// Handling UnixFS
|
||||
dr, err := i.api.Unixfs().Get(ctx, resolvedPath)
|
||||
@ -28,16 +29,16 @@ func (i *gatewayHandler) serveUnixFs(w http.ResponseWriter, r *http.Request, res
|
||||
// Handling Unixfs file
|
||||
if f, ok := dr.(files.File); ok {
|
||||
logger.Debugw("serving unixfs file", "path", contentPath)
|
||||
i.serveFile(w, r, resolvedPath, contentPath, f, begin)
|
||||
i.serveFile(ctx, w, r, resolvedPath, contentPath, f, begin)
|
||||
return
|
||||
}
|
||||
|
||||
// Handling Unixfs directory
|
||||
dir, ok := dr.(files.Directory)
|
||||
if !ok {
|
||||
internalWebError(w, fmt.Errorf("unsupported UnixFs type"))
|
||||
internalWebError(w, fmt.Errorf("unsupported UnixFS type"))
|
||||
return
|
||||
}
|
||||
logger.Debugw("serving unixfs directory", "path", contentPath)
|
||||
i.serveDirectory(w, r, resolvedPath, contentPath, dir, begin, logger)
|
||||
i.serveDirectory(ctx, w, r, resolvedPath, contentPath, dir, begin, logger)
|
||||
}
|
||||
|
||||
@ -1,6 +1,7 @@
|
||||
package corehttp
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
"net/url"
|
||||
gopath "path"
|
||||
@ -23,8 +24,8 @@ import (
|
||||
// serveDirectory returns the best representation of UnixFS directory
|
||||
//
|
||||
// It will return index.html if present, or generate directory listing otherwise.
|
||||
func (i *gatewayHandler) serveDirectory(w http.ResponseWriter, r *http.Request, resolvedPath ipath.Resolved, contentPath ipath.Path, dir files.Directory, begin time.Time, logger *zap.SugaredLogger) {
|
||||
ctx, span := tracing.Span(r.Context(), "Gateway", "ServeDirectory", trace.WithAttributes(attribute.String("path", resolvedPath.String())))
|
||||
func (i *gatewayHandler) serveDirectory(ctx context.Context, w http.ResponseWriter, r *http.Request, resolvedPath ipath.Resolved, contentPath ipath.Path, dir files.Directory, begin time.Time, logger *zap.SugaredLogger) {
|
||||
ctx, span := tracing.Span(ctx, "Gateway", "ServeDirectory", trace.WithAttributes(attribute.String("path", resolvedPath.String())))
|
||||
defer span.End()
|
||||
|
||||
// HostnameOption might have constructed an IPNS/IPFS path using the Host header.
|
||||
@ -69,7 +70,7 @@ func (i *gatewayHandler) serveDirectory(w http.ResponseWriter, r *http.Request,
|
||||
|
||||
logger.Debugw("serving index.html file", "path", idxPath)
|
||||
// write to request
|
||||
i.serveFile(w, r, resolvedPath, idxPath, f, begin)
|
||||
i.serveFile(ctx, w, r, resolvedPath, idxPath, f, begin)
|
||||
return
|
||||
case resolver.ErrNoLink:
|
||||
logger.Debugw("no index.html; noop", "path", idxPath)
|
||||
|
||||
@ -1,6 +1,7 @@
|
||||
package corehttp
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"io"
|
||||
"mime"
|
||||
@ -19,8 +20,8 @@ import (
|
||||
|
||||
// serveFile returns data behind a file along with HTTP headers based on
|
||||
// the file itself, its CID and the contentPath used for accessing it.
|
||||
func (i *gatewayHandler) serveFile(w http.ResponseWriter, r *http.Request, resolvedPath ipath.Resolved, contentPath ipath.Path, file files.File, begin time.Time) {
|
||||
_, span := tracing.Span(r.Context(), "Gateway", "ServeFile", trace.WithAttributes(attribute.String("path", resolvedPath.String())))
|
||||
func (i *gatewayHandler) serveFile(ctx context.Context, w http.ResponseWriter, r *http.Request, resolvedPath ipath.Resolved, contentPath ipath.Path, file files.File, begin time.Time) {
|
||||
_, span := tracing.Span(ctx, "Gateway", "ServeFile", trace.WithAttributes(attribute.String("path", resolvedPath.String())))
|
||||
defer span.End()
|
||||
|
||||
// Set Cache-Control and read optional Last-Modified time
|
||||
|
||||
Loading…
Reference in New Issue
Block a user