11package main
22
33import (
4+ "context"
5+ "fmt"
46 "io"
57 "net/http"
68 "net/http/httptest"
9+ "strings"
710 "testing"
811
912 "github.com/ipfs/boxo/blockservice"
@@ -14,12 +17,24 @@ import (
1417 "github.com/ipfs/go-datastore"
1518 dssync "github.com/ipfs/go-datastore/sync"
1619 "github.com/stretchr/testify/assert"
20+ "go.opentelemetry.io/otel"
21+ "go.opentelemetry.io/otel/propagation"
22+ "go.opentelemetry.io/otel/sdk/trace"
1723)
1824
1925const (
2026 HelloWorldCID = "bafkreifzjut3te2nhyekklss27nh3k72ysco7y32koao5eei66wof36n5e"
2127)
2228
29+ func newTracerProvider (t * testing.T ) * trace.TracerProvider {
30+ tp := trace .NewTracerProvider ()
31+ otel .SetTracerProvider (tp )
32+ otel .SetTextMapPropagator (propagation .NewCompositeTextMapPropagator (common .Propagators ))
33+ ctx := context .Background ()
34+ t .Cleanup (func () { _ = tp .Shutdown (ctx ) })
35+ return tp
36+ }
37+
2338func newProxyGateway (t * testing.T , rs * httptest.Server ) * httptest.Server {
2439 blockStore := blockstore .NewBlockstore (dssync .MutexWrap (datastore .NewMapDatastore ()))
2540 exch := newProxyExchange (rs .URL , nil )
@@ -51,7 +66,7 @@ func TestErrorOnInvalidContent(t *testing.T) {
5166 body , err := io .ReadAll (res .Body )
5267 res .Body .Close ()
5368 assert .Nil (t , err )
54- assert .EqualValues (t , res . StatusCode , http . StatusInternalServerError )
69+ assert .EqualValues (t , http . StatusInternalServerError , res . StatusCode )
5570 assert .Contains (t , string (body ), blocks .ErrWrongHash .Error ())
5671}
5772
@@ -68,6 +83,67 @@ func TestPassOnOnCorrectContent(t *testing.T) {
6883 body , err := io .ReadAll (res .Body )
6984 res .Body .Close ()
7085 assert .Nil (t , err )
71- assert .EqualValues (t , res . StatusCode , http . StatusOK )
86+ assert .EqualValues (t , http . StatusOK , res . StatusCode )
7287 assert .EqualValues (t , string (body ), "hello world" )
7388}
89+
90+ func TestTraceContext (t * testing.T ) {
91+ doCheckRequest := func (t * testing.T , req * http.Request ) {
92+ res , err := http .DefaultClient .Do (req )
93+ assert .Nil (t , err )
94+ assert .EqualValues (t , http .StatusOK , res .StatusCode )
95+ defer res .Body .Close ()
96+
97+ body , err := io .ReadAll (res .Body )
98+ assert .Nil (t , err )
99+ assert .EqualValues (t , string (body ), "hello world" )
100+ }
101+
102+ const (
103+ traceVersion = "00"
104+ traceID = "4bf92f3577b34da6a3ce929d0e0e4736"
105+ traceParentID = "00f067aa0ba902b7"
106+ traceFlags = "00"
107+ )
108+
109+ // Creating a trace provider and registering it will make OTel enable tracing.
110+ _ = newTracerProvider (t )
111+
112+ t .Run ("Re-use Traceparent Trace ID Of Initial Request" , func (t * testing.T ) {
113+ rs := httptest .NewServer (http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
114+ // The expected prefix for the traceparent header consists of the version and trace id.
115+ expectedPrefix := fmt .Sprintf ("%s-%s-" , traceVersion , traceID )
116+ if ! strings .HasPrefix (r .Header .Get ("traceparent" ), expectedPrefix ) {
117+ w .WriteHeader (http .StatusBadRequest )
118+ } else {
119+ w .Write ([]byte ("hello world" ))
120+ }
121+ }))
122+
123+ t .Cleanup (rs .Close )
124+ ts := newProxyGateway (t , rs )
125+
126+ req , err := http .NewRequest (http .MethodGet , ts .URL + "/ipfs/" + HelloWorldCID , nil )
127+ assert .Nil (t , err )
128+ req .Header .Set ("Traceparent" , fmt .Sprintf ("%s-%s-%s-%s" , traceVersion , traceID , traceParentID , traceFlags ))
129+ doCheckRequest (t , req )
130+ })
131+
132+ t .Run ("Create New Trace ID If Not Given" , func (t * testing.T ) {
133+ rs := httptest .NewServer (http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
134+ // In this request we are not sending a traceparent header, so a new one should be created.
135+ if r .Header .Get ("traceparent" ) == "" {
136+ w .WriteHeader (http .StatusBadRequest )
137+ } else {
138+ w .Write ([]byte ("hello world" ))
139+ }
140+ }))
141+
142+ t .Cleanup (rs .Close )
143+ ts := newProxyGateway (t , rs )
144+
145+ req , err := http .NewRequest (http .MethodGet , ts .URL + "/ipfs/" + HelloWorldCID , nil )
146+ assert .Nil (t , err )
147+ doCheckRequest (t , req )
148+ })
149+ }
0 commit comments