Skip to content

Commit 4614ea9

Browse files
committed
feat: add Trace Context HTTP headers
1 parent 085ed9d commit 4614ea9

8 files changed

Lines changed: 86 additions & 6 deletions

File tree

examples/gateway/car/main.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package main
22

33
import (
4+
"context"
45
"flag"
56
"io"
67
"log"
@@ -32,6 +33,12 @@ func main() {
3233
log.Fatal(err)
3334
}
3435

36+
ctx := context.Background()
37+
tp := common.NewTracerProvider()
38+
defer func() {
39+
_ = tp.Shutdown(ctx)
40+
}()
41+
3542
handler := common.NewHandler(gwAPI)
3643

3744
log.Printf("Listening on http://localhost:%d", *port)

examples/gateway/common/handler.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"github.com/ipfs/boxo/gateway"
77
"github.com/prometheus/client_golang/prometheus"
88
"github.com/prometheus/client_golang/prometheus/promhttp"
9+
"go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp"
910
)
1011

1112
func NewHandler(gwAPI gateway.IPFSBackend) http.Handler {
@@ -58,10 +59,15 @@ func NewHandler(gwAPI gateway.IPFSBackend) http.Handler {
5859
var handler http.Handler
5960
handler = gateway.WithHostname(mux, gwAPI, publicGateways, noDNSLink)
6061

61-
// Finally, wrap with the withConnect middleware. This is required since we use
62+
// Then, wrap with the withConnect middleware. This is required since we use
6263
// http.ServeMux which does not support CONNECT by default.
6364
handler = withConnect(handler)
6465

66+
// Finally, wrap with the otelhttp handler. This will allow the tracing system
67+
// to work and for correct propagation of tracing headers. This step is optional
68+
// and only required if you want to use tracing.
69+
handler = otelhttp.NewHandler(handler, "Gateway.Request")
70+
6571
return handler
6672
}
6773

examples/gateway/common/tracing.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package common
2+
3+
import (
4+
"go.opentelemetry.io/contrib/propagators/autoprop"
5+
"go.opentelemetry.io/otel"
6+
"go.opentelemetry.io/otel/sdk/trace"
7+
)
8+
9+
func NewTracerProvider() *trace.TracerProvider {
10+
tp := trace.NewTracerProvider()
11+
12+
otel.SetTracerProvider(tp)
13+
otel.SetTextMapPropagator(autoprop.NewTextMapPropagator())
14+
15+
return tp
16+
}

examples/gateway/proxy/main.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package main
22

33
import (
4+
"context"
45
"flag"
56
"log"
67
"net/http"
@@ -37,6 +38,12 @@ func main() {
3738
log.Fatal(err)
3839
}
3940

41+
ctx := context.Background()
42+
tp := common.NewTracerProvider()
43+
defer func() {
44+
_ = tp.Shutdown(ctx)
45+
}()
46+
4047
handler := common.NewHandler(gwAPI)
4148

4249
log.Printf("Listening on http://localhost:%d", *port)

examples/go.mod

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ require (
1515
github.com/multiformats/go-multicodec v0.8.1
1616
github.com/prometheus/client_golang v1.14.0
1717
github.com/stretchr/testify v1.8.2
18+
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.40.0
19+
go.opentelemetry.io/contrib/propagators/autoprop v0.40.0
20+
go.opentelemetry.io/otel v1.14.0
21+
go.opentelemetry.io/otel/sdk v1.14.0
1822
)
1923

2024
require (
@@ -33,6 +37,7 @@ require (
3337
github.com/docker/go-units v0.5.0 // indirect
3438
github.com/dustin/go-humanize v1.0.0 // indirect
3539
github.com/elastic/gosigar v0.14.2 // indirect
40+
github.com/felixge/httpsnoop v1.0.3 // indirect
3641
github.com/flynn/noise v1.0.0 // indirect
3742
github.com/francoispqt/gojay v1.2.13 // indirect
3843
github.com/gabriel-vasile/mimetype v1.4.1 // indirect
@@ -125,7 +130,11 @@ require (
125130
github.com/whyrusleeping/chunker v0.0.0-20181014151217-fe64bd25879f // indirect
126131
github.com/whyrusleeping/go-keyspace v0.0.0-20160322163242-5b898ac5add1 // indirect
127132
go.opencensus.io v0.24.0 // indirect
128-
go.opentelemetry.io/otel v1.14.0 // indirect
133+
go.opentelemetry.io/contrib/propagators/aws v1.15.0 // indirect
134+
go.opentelemetry.io/contrib/propagators/b3 v1.15.0 // indirect
135+
go.opentelemetry.io/contrib/propagators/jaeger v1.15.0 // indirect
136+
go.opentelemetry.io/contrib/propagators/ot v1.15.0 // indirect
137+
go.opentelemetry.io/otel/metric v0.37.0 // indirect
129138
go.opentelemetry.io/otel/trace v1.14.0 // indirect
130139
go.uber.org/atomic v1.10.0 // indirect
131140
go.uber.org/dig v1.15.0 // indirect

examples/go.sum

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,8 @@ github.com/envoyproxy/go-control-plane v0.9.0/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymF
105105
github.com/envoyproxy/go-control-plane v0.9.1-0.20191026205805-5f8ba28d4473/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4=
106106
github.com/envoyproxy/go-control-plane v0.9.4/go.mod h1:6rpuAdCZL397s3pYoYcLgu1mIlRU8Am5FuJP05cCM98=
107107
github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c=
108+
github.com/felixge/httpsnoop v1.0.3 h1:s/nj+GCswXYzN5v2DpNMuMQYe+0DDwt5WVCU6CWBdXk=
109+
github.com/felixge/httpsnoop v1.0.3/go.mod h1:m8KPJKqk1gH5J9DgRY2ASl2lWCfGKXixSwevea8zH2U=
108110
github.com/flynn/go-shlex v0.0.0-20150515145356-3f9db97f8568/go.mod h1:xEzjJPgXI435gkrCt3MPfRiAkVrwSbHsst4LCFVfpJc=
109111
github.com/flynn/noise v1.0.0 h1:DlTHqmzmvcEiKj+4RYo/imoswx/4r6iBlCMfVtrMXpQ=
110112
github.com/flynn/noise v1.0.0/go.mod h1:xbMo+0i6+IGbYdJhF31t2eR1BIU0CYc12+BNAKwUTag=
@@ -644,8 +646,24 @@ go.opencensus.io v0.22.3/go.mod h1:yxeiOL68Rb0Xd1ddK5vPZ/oVn4vY4Ynel7k9FzqtOIw=
644646
go.opencensus.io v0.22.4/go.mod h1:yxeiOL68Rb0Xd1ddK5vPZ/oVn4vY4Ynel7k9FzqtOIw=
645647
go.opencensus.io v0.24.0 h1:y73uSU6J157QMP2kn2r30vwW1A2W2WFwSCGnAVxeaD0=
646648
go.opencensus.io v0.24.0/go.mod h1:vNK8G9p7aAivkbmorf4v+7Hgx+Zs0yY+0fOtgBfjQKo=
649+
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.40.0 h1:lE9EJyw3/JhrjWH/hEy9FptnalDQgj7vpbgC2KCCCxE=
650+
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.40.0/go.mod h1:pcQ3MM3SWvrA71U4GDqv9UFDJ3HQsW7y5ZO3tDTlUdI=
651+
go.opentelemetry.io/contrib/propagators/autoprop v0.40.0 h1:Lj33jj7eIrBfIShiK8NU91u2BglKnUS1UUxVemuQJtw=
652+
go.opentelemetry.io/contrib/propagators/autoprop v0.40.0/go.mod h1:6QO816FeZ+6zahs6hYqbUCCsnNBm7o+t4iwVySpzcdI=
653+
go.opentelemetry.io/contrib/propagators/aws v1.15.0 h1:FLe+bRTMAhEALItDQt1U2S/rdq8/rGGJTJpOpCDvMu0=
654+
go.opentelemetry.io/contrib/propagators/aws v1.15.0/go.mod h1:Z/nqdjqKjErrS3gYoEMZt8//dt8VZbqalD0V+7vh7lM=
655+
go.opentelemetry.io/contrib/propagators/b3 v1.15.0 h1:bMaonPyFcAvZ4EVzkUNkfnUHP5Zi63CIDlA3dRsEg8Q=
656+
go.opentelemetry.io/contrib/propagators/b3 v1.15.0/go.mod h1:VjU0g2v6HSQ+NwfifambSLAeBgevjIcqmceaKWEzl0c=
657+
go.opentelemetry.io/contrib/propagators/jaeger v1.15.0 h1:xdJjwy5t/8I+TZehMMQ+r2h50HREihH2oMUhimQ+jug=
658+
go.opentelemetry.io/contrib/propagators/jaeger v1.15.0/go.mod h1:tU0nwW4QTvKceNUP60/PQm0FI8zDSwey7gIFt3RR/yw=
659+
go.opentelemetry.io/contrib/propagators/ot v1.15.0 h1:iBNejawWy7wWZ5msuZDNcMjBy14Wc0v3gCAXukGHN/Q=
660+
go.opentelemetry.io/contrib/propagators/ot v1.15.0/go.mod h1:0P7QQ+MHt6SXR1ATaMpewSiWlp8NbKErNLKcaU4EEKI=
647661
go.opentelemetry.io/otel v1.14.0 h1:/79Huy8wbf5DnIPhemGB+zEPVwnN6fuQybr/SRXa6hM=
648662
go.opentelemetry.io/otel v1.14.0/go.mod h1:o4buv+dJzx8rohcUeRmWUZhqupFvzWis188WlggnNeU=
663+
go.opentelemetry.io/otel/metric v0.37.0 h1:pHDQuLQOZwYD+Km0eb657A25NaRzy0a+eLyKfDXedEs=
664+
go.opentelemetry.io/otel/metric v0.37.0/go.mod h1:DmdaHfGt54iV6UKxsV9slj2bBRJcKC1B1uvDLIioc1s=
665+
go.opentelemetry.io/otel/sdk v1.14.0 h1:PDCppFRDq8A1jL9v6KMI6dYesaq+DFcDZvjsoGvxGzY=
666+
go.opentelemetry.io/otel/sdk v1.14.0/go.mod h1:bwIC5TjrNG6QDCHNWvW4HLHtUQ4I+VQDsnjhvyZCALM=
649667
go.opentelemetry.io/otel/trace v1.14.0 h1:wp2Mmvj41tDsyAJXiWDWpfNsOiIyd38fy85pyKcFq/M=
650668
go.opentelemetry.io/otel/trace v1.14.0/go.mod h1:8avnQLK+CG77yNLUae4ea2JDQ6iT+gozhnZjy/rw9G8=
651669
go.uber.org/atomic v1.6.0/go.mod h1:sABNBOSYdrvTF6hTgEIbc7YasKWGhgEQZyfxyTvoXHQ=

gateway/handler.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,10 @@ type handler struct {
8383
// NewHandler returns an http.Handler that can act as a gateway to IPFS content
8484
// offlineApi is a version of the API that should not make network requests for missing data
8585
func NewHandler(c Config, api IPFSBackend) http.Handler {
86-
return newHandlerWithMetrics(c, api)
86+
var h http.Handler
87+
h = newHandlerWithMetrics(c, api)
88+
h = withTraceContext(h)
89+
return h
8790
}
8891

8992
// StatusResponseWriter enables us to override HTTP Status Code passed to

gateway/metrics.go

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ package gateway
22

33
import (
44
"context"
5-
"fmt"
65
"io"
6+
"net/http"
77
"time"
88

99
"github.com/ipfs/boxo/coreiface/path"
@@ -12,6 +12,7 @@ import (
1212
prometheus "github.com/prometheus/client_golang/prometheus"
1313
"go.opentelemetry.io/otel"
1414
"go.opentelemetry.io/otel/attribute"
15+
"go.opentelemetry.io/otel/propagation"
1516
"go.opentelemetry.io/otel/trace"
1617
)
1718

@@ -294,7 +295,20 @@ func newHistogramMetric(name string, help string) *prometheus.HistogramVec {
294295
return histogramMetric
295296
}
296297

298+
var tracer = otel.Tracer("boxo/gateway")
299+
297300
// spanTrace starts a new span using the standard IPFS tracing conventions.
298-
func spanTrace(ctx context.Context, spanName string, opts ...trace.SpanStartOption) (context.Context, trace.Span) {
299-
return otel.Tracer("boxo").Start(ctx, fmt.Sprintf("%s.%s", " Gateway", spanName), opts...)
301+
func spanTrace(ctx context.Context, name string, opts ...trace.SpanStartOption) (context.Context, trace.Span) {
302+
return tracer.Start(ctx, name, opts...)
303+
}
304+
305+
// withTraceContext sets the Trace Context (https://github.com/w3c/trace-context)
306+
// headers on the request based on the context and the propagator.
307+
func withTraceContext(next http.Handler) http.Handler {
308+
props := otel.GetTextMapPropagator()
309+
310+
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
311+
props.Inject(r.Context(), propagation.HeaderCarrier(w.Header()))
312+
next.ServeHTTP(w, r)
313+
})
300314
}

0 commit comments

Comments
 (0)