Skip to content

Commit 9bd346e

Browse files
authored
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.
1 parent 52bf133 commit 9bd346e

6 files changed

+22
-18
lines changed

core/corehttp/gateway_handler.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -443,16 +443,16 @@ func (i *gatewayHandler) getOrHeadHandler(w http.ResponseWriter, r *http.Request
443443
switch responseFormat {
444444
case "": // The implicit response format is UnixFS
445445
logger.Debugw("serving unixfs", "path", contentPath)
446-
i.serveUnixFs(w, r, resolvedPath, contentPath, begin, logger)
446+
i.serveUnixFS(r.Context(), w, r, resolvedPath, contentPath, begin, logger)
447447
return
448448
case "application/vnd.ipld.raw":
449449
logger.Debugw("serving raw block", "path", contentPath)
450-
i.serveRawBlock(w, r, resolvedPath, contentPath, begin)
450+
i.serveRawBlock(r.Context(), w, r, resolvedPath, contentPath, begin)
451451
return
452452
case "application/vnd.ipld.car":
453453
logger.Debugw("serving car stream", "path", contentPath)
454454
carVersion := formatParams["version"]
455-
i.serveCar(w, r, resolvedPath, contentPath, carVersion, begin)
455+
i.serveCAR(r.Context(), w, r, resolvedPath, contentPath, carVersion, begin)
456456
return
457457
default: // catch-all for unsuported application/vnd.*
458458
err := fmt.Errorf("unsupported format %q", responseFormat)

core/corehttp/gateway_handler_block.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package corehttp
22

33
import (
44
"bytes"
5+
"context"
56
"io/ioutil"
67
"net/http"
78
"time"
@@ -13,8 +14,8 @@ import (
1314
)
1415

1516
// serveRawBlock returns bytes behind a raw block
16-
func (i *gatewayHandler) serveRawBlock(w http.ResponseWriter, r *http.Request, resolvedPath ipath.Resolved, contentPath ipath.Path, begin time.Time) {
17-
ctx, span := tracing.Span(r.Context(), "Gateway", "ServeRawBlock", trace.WithAttributes(attribute.String("path", resolvedPath.String())))
17+
func (i *gatewayHandler) serveRawBlock(ctx context.Context, w http.ResponseWriter, r *http.Request, resolvedPath ipath.Resolved, contentPath ipath.Path, begin time.Time) {
18+
ctx, span := tracing.Span(ctx, "Gateway", "ServeRawBlock", trace.WithAttributes(attribute.String("path", resolvedPath.String())))
1819
defer span.End()
1920
blockCid := resolvedPath.Cid()
2021
blockReader, err := i.api.Block().Get(ctx, resolvedPath)

core/corehttp/gateway_handler_car.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@ import (
1717
"go.opentelemetry.io/otel/trace"
1818
)
1919

20-
// serveCar returns a CAR stream for specific DAG+selector
21-
func (i *gatewayHandler) serveCar(w http.ResponseWriter, r *http.Request, resolvedPath ipath.Resolved, contentPath ipath.Path, carVersion string, begin time.Time) {
22-
ctx, span := tracing.Span(r.Context(), "Gateway", "ServeCar", trace.WithAttributes(attribute.String("path", resolvedPath.String())))
20+
// serveCAR returns a CAR stream for specific DAG+selector
21+
func (i *gatewayHandler) serveCAR(ctx context.Context, w http.ResponseWriter, r *http.Request, resolvedPath ipath.Resolved, contentPath ipath.Path, carVersion string, begin time.Time) {
22+
ctx, span := tracing.Span(ctx, "Gateway", "ServeCAR", trace.WithAttributes(attribute.String("path", resolvedPath.String())))
2323
defer span.End()
2424
ctx, cancel := context.WithCancel(ctx)
2525
defer cancel()

core/corehttp/gateway_handler_unixfs.go

+6-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package corehttp
22

33
import (
4+
"context"
45
"fmt"
56
"html"
67
"net/http"
@@ -14,8 +15,8 @@ import (
1415
"go.uber.org/zap"
1516
)
1617

17-
func (i *gatewayHandler) serveUnixFs(w http.ResponseWriter, r *http.Request, resolvedPath ipath.Resolved, contentPath ipath.Path, begin time.Time, logger *zap.SugaredLogger) {
18-
ctx, span := tracing.Span(r.Context(), "Gateway", "ServeUnixFs", trace.WithAttributes(attribute.String("path", resolvedPath.String())))
18+
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) {
19+
ctx, span := tracing.Span(ctx, "Gateway", "ServeUnixFS", trace.WithAttributes(attribute.String("path", resolvedPath.String())))
1920
defer span.End()
2021
// Handling UnixFS
2122
dr, err := i.api.Unixfs().Get(ctx, resolvedPath)
@@ -28,16 +29,16 @@ func (i *gatewayHandler) serveUnixFs(w http.ResponseWriter, r *http.Request, res
2829
// Handling Unixfs file
2930
if f, ok := dr.(files.File); ok {
3031
logger.Debugw("serving unixfs file", "path", contentPath)
31-
i.serveFile(w, r, resolvedPath, contentPath, f, begin)
32+
i.serveFile(ctx, w, r, resolvedPath, contentPath, f, begin)
3233
return
3334
}
3435

3536
// Handling Unixfs directory
3637
dir, ok := dr.(files.Directory)
3738
if !ok {
38-
internalWebError(w, fmt.Errorf("unsupported UnixFs type"))
39+
internalWebError(w, fmt.Errorf("unsupported UnixFS type"))
3940
return
4041
}
4142
logger.Debugw("serving unixfs directory", "path", contentPath)
42-
i.serveDirectory(w, r, resolvedPath, contentPath, dir, begin, logger)
43+
i.serveDirectory(ctx, w, r, resolvedPath, contentPath, dir, begin, logger)
4344
}

core/corehttp/gateway_handler_unixfs_dir.go

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package corehttp
22

33
import (
4+
"context"
45
"net/http"
56
"net/url"
67
gopath "path"
@@ -23,8 +24,8 @@ import (
2324
// serveDirectory returns the best representation of UnixFS directory
2425
//
2526
// It will return index.html if present, or generate directory listing otherwise.
26-
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) {
27-
ctx, span := tracing.Span(r.Context(), "Gateway", "ServeDirectory", trace.WithAttributes(attribute.String("path", resolvedPath.String())))
27+
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) {
28+
ctx, span := tracing.Span(ctx, "Gateway", "ServeDirectory", trace.WithAttributes(attribute.String("path", resolvedPath.String())))
2829
defer span.End()
2930

3031
// 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,
6970

7071
logger.Debugw("serving index.html file", "path", idxPath)
7172
// write to request
72-
i.serveFile(w, r, resolvedPath, idxPath, f, begin)
73+
i.serveFile(ctx, w, r, resolvedPath, idxPath, f, begin)
7374
return
7475
case resolver.ErrNoLink:
7576
logger.Debugw("no index.html; noop", "path", idxPath)

core/corehttp/gateway_handler_unixfs_file.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package corehttp
22

33
import (
4+
"context"
45
"fmt"
56
"io"
67
"mime"
@@ -19,8 +20,8 @@ import (
1920

2021
// serveFile returns data behind a file along with HTTP headers based on
2122
// the file itself, its CID and the contentPath used for accessing it.
22-
func (i *gatewayHandler) serveFile(w http.ResponseWriter, r *http.Request, resolvedPath ipath.Resolved, contentPath ipath.Path, file files.File, begin time.Time) {
23-
_, span := tracing.Span(r.Context(), "Gateway", "ServeFile", trace.WithAttributes(attribute.String("path", resolvedPath.String())))
23+
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) {
24+
_, span := tracing.Span(ctx, "Gateway", "ServeFile", trace.WithAttributes(attribute.String("path", resolvedPath.String())))
2425
defer span.End()
2526

2627
// Set Cache-Control and read optional Last-Modified time

0 commit comments

Comments
 (0)