Skip to content

Commit

Permalink
fix: fix context plumbing in gateway handlers (#8871)
Browse files Browse the repository at this point in the history
This ensures that child contexts are passed around between
the handlers so that traces show the call hierarchy correctly.
  • Loading branch information
guseggert committed Apr 11, 2022
1 parent 52bf133 commit 9bd346e
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 18 deletions.
6 changes: 3 additions & 3 deletions core/corehttp/gateway_handler.go
Expand Up @@ -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)
Expand Down
5 changes: 3 additions & 2 deletions core/corehttp/gateway_handler_block.go
Expand Up @@ -2,6 +2,7 @@ package corehttp

import (
"bytes"
"context"
"io/ioutil"
"net/http"
"time"
Expand All @@ -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)
Expand Down
6 changes: 3 additions & 3 deletions core/corehttp/gateway_handler_car.go
Expand Up @@ -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()
Expand Down
11 changes: 6 additions & 5 deletions core/corehttp/gateway_handler_unixfs.go
@@ -1,6 +1,7 @@
package corehttp

import (
"context"
"fmt"
"html"
"net/http"
Expand All @@ -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)
Expand All @@ -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)
}
7 changes: 4 additions & 3 deletions core/corehttp/gateway_handler_unixfs_dir.go
@@ -1,6 +1,7 @@
package corehttp

import (
"context"
"net/http"
"net/url"
gopath "path"
Expand All @@ -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.
Expand Down Expand Up @@ -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)
Expand Down
5 changes: 3 additions & 2 deletions core/corehttp/gateway_handler_unixfs_file.go
@@ -1,6 +1,7 @@
package corehttp

import (
"context"
"fmt"
"io"
"mime"
Expand All @@ -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
Expand Down

0 comments on commit 9bd346e

Please sign in to comment.