11package http
22
33import (
4+ "context"
45 "errors"
56 "io"
67 "io/ioutil"
@@ -9,6 +10,7 @@ import (
910 "testing"
1011
1112 zipkin "github.com/openzipkin/zipkin-go"
13+ "github.com/openzipkin/zipkin-go/reporter/recorder"
1214)
1315
1416type errRoundTripper struct {
@@ -125,3 +127,90 @@ func TestRoundTripErrResponseReadingSuccess(t *testing.T) {
125127 t .Errorf ("unexpected body: want %s, have %s" , want , have )
126128 }
127129}
130+
131+ func boolToPtr (b bool ) * bool {
132+ return & b
133+ }
134+
135+ func TestTransportRequestSamplerOverridesSamplingFromContext (t * testing.T ) {
136+ cases := []struct {
137+ Sampler func (uint64 ) bool
138+ RequestSampler func (* http.Request ) * bool
139+ ExpectedSampling string
140+ }{
141+ // Test proper handling when there is no RequestSampler
142+ {
143+ Sampler : zipkin .AlwaysSample ,
144+ RequestSampler : nil ,
145+ ExpectedSampling : "1" ,
146+ },
147+ // Test proper handling when there is no RequestSampler
148+ {
149+ Sampler : zipkin .NeverSample ,
150+ RequestSampler : nil ,
151+ ExpectedSampling : "0" ,
152+ },
153+ // Test RequestSampler override sample -> no sample
154+ {
155+ Sampler : zipkin .AlwaysSample ,
156+ RequestSampler : func (_ * http.Request ) * bool { return boolToPtr (false ) },
157+ ExpectedSampling : "0" ,
158+ },
159+ // Test RequestSampler override no sample -> sample
160+ {
161+ Sampler : zipkin .NeverSample ,
162+ RequestSampler : func (_ * http.Request ) * bool { return boolToPtr (true ) },
163+ ExpectedSampling : "1" ,
164+ },
165+ // Test RequestSampler pass through of sampled decision
166+ {
167+ Sampler : zipkin .AlwaysSample ,
168+ RequestSampler : func (r * http.Request ) * bool {
169+ return nil
170+ },
171+ ExpectedSampling : "1" ,
172+ },
173+ // Test RequestSampler pass through of not sampled decision
174+ {
175+ Sampler : zipkin .NeverSample ,
176+ RequestSampler : func (r * http.Request ) * bool {
177+ return nil
178+ },
179+ ExpectedSampling : "0" ,
180+ },
181+ }
182+
183+ for i , c := range cases {
184+ srv := httptest .NewServer (http .HandlerFunc (func (_ http.ResponseWriter , r * http.Request ) {
185+ if want , have := c .ExpectedSampling , r .Header .Get ("x-b3-sampled" ); want != have {
186+ t .Errorf ("unexpected sampling decision in case #%d, want %q, have %q" , i , want , have )
187+ }
188+ }))
189+
190+ // we need to use a valid reporter or Tracer will implement noop mode which makes this test invalid
191+ rep := recorder .NewReporter ()
192+
193+ tracer , err := zipkin .NewTracer (rep , zipkin .WithSampler (c .Sampler ))
194+ if err != nil {
195+ t .Fatalf ("unexpected error when creating tracer: %v" , err )
196+ }
197+
198+ sp := tracer .StartSpan ("op1" )
199+ defer sp .Finish ()
200+ ctx := zipkin .NewContext (context .Background (), sp )
201+
202+ req , _ := http .NewRequest ("GET" , srv .URL , nil )
203+ transport , _ := NewTransport (
204+ tracer ,
205+ TransportRequestSampler (c .RequestSampler ),
206+ )
207+
208+ _ , err = transport .RoundTrip (req .WithContext (ctx ))
209+ if err != nil {
210+ t .Fatalf ("unexpected error: %v" , err )
211+ }
212+
213+ rep .Close ()
214+ srv .Close ()
215+ }
216+ }
0 commit comments