Codebase list golang-github-go-kit-kit / 765ff6c
[#812] adds request sampler into zipkin tracing. José Carlos Chávez 5 years ago
3 changed file(s) with 63 addition(s) and 6 deletion(s). Raw diff Collapse all Expand all
155155
156156 if config.propagate {
157157 spanContext = tracer.Extract(b3.ExtractHTTP(req))
158
159 if config.requestSampler != nil && spanContext.Sampled == nil {
160 sample := config.requestSampler(req)
161 spanContext.Sampled = &sample
162 }
163
158164 if spanContext.Err != nil {
159165 config.logger.Log("err", spanContext.Err)
160166 }
2626 testTagValue = "test_value"
2727 )
2828
29 func TestHttpClientTracePropagatesParentSpan(t *testing.T) {
29 func TestHTTPClientTracePropagatesParentSpan(t *testing.T) {
3030 rec := recorder.NewReporter()
3131 defer rec.Close()
3232
219219 t.Fatalf("incorrect error tag, want %s, have %s", want, have)
220220 }
221221 }
222
223 func TestHTTPServerTraceIsRequestBasedSampled(t *testing.T) {
224 rec := recorder.NewReporter()
225 defer rec.Close()
226
227 const httpMethod = "DELETE"
228
229 tr, _ := zipkin.NewTracer(rec)
230
231 handler := kithttp.NewServer(
232 endpoint.Nop,
233 func(context.Context, *http.Request) (interface{}, error) { return nil, nil },
234 func(context.Context, http.ResponseWriter, interface{}) error { return nil },
235 zipkinkit.HTTPServerTrace(tr, zipkinkit.RequestSampler(func(r *http.Request) bool {
236 return r.Method == httpMethod
237 })),
238 )
239
240 server := httptest.NewServer(handler)
241 defer server.Close()
242
243 req, err := http.NewRequest(httpMethod, server.URL, nil)
244 if err != nil {
245 t.Fatalf("unable to create HTTP request: %s", err.Error())
246 }
247
248 client := http.Client{}
249 resp, err := client.Do(req)
250 if err != nil {
251 t.Fatalf("unable to send HTTP request: %s", err.Error())
252 }
253 resp.Body.Close()
254
255 spans := rec.Flush()
256 if want, have := 1, len(spans); want != have {
257 t.Fatalf("incorrect number of spans, want %d, have %d", want, have)
258 }
259 }
00 package zipkin
11
2 import "github.com/go-kit/kit/log"
2 import (
3 "net/http"
4
5 "github.com/go-kit/kit/log"
6 )
37
48 // TracerOption allows for functional options to our Zipkin tracing middleware.
59 type TracerOption func(o *tracerOptions)
4549 }
4650 }
4751
52 // RequestSampler allows one to set the sampling decision based on the details
53 // found in the http.Request.
54 func RequestSampler(sampleFunc func(r *http.Request) bool) TracerOption {
55 return func(o *tracerOptions) {
56 o.requestSampler = sampleFunc
57 }
58 }
59
4860 type tracerOptions struct {
49 tags map[string]string
50 name string
51 logger log.Logger
52 propagate bool
61 tags map[string]string
62 name string
63 logger log.Logger
64 propagate bool
65 requestSampler func(r *http.Request) bool
5366 }