Skip to content

Commit

Permalink
[go-kit#812] adds request sampler into zipkin tracing.
Browse files Browse the repository at this point in the history
  • Loading branch information
jcchavezs committed Dec 14, 2018
1 parent f688401 commit 765ff6c
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 6 deletions.
6 changes: 6 additions & 0 deletions tracing/zipkin/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,12 @@ func HTTPServerTrace(tracer *zipkin.Tracer, options ...TracerOption) kithttp.Ser

if config.propagate {
spanContext = tracer.Extract(b3.ExtractHTTP(req))

if config.requestSampler != nil && spanContext.Sampled == nil {
sample := config.requestSampler(req)
spanContext.Sampled = &sample
}

if spanContext.Err != nil {
config.logger.Log("err", spanContext.Err)
}
Expand Down
40 changes: 39 additions & 1 deletion tracing/zipkin/http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ const (
testTagValue = "test_value"
)

func TestHttpClientTracePropagatesParentSpan(t *testing.T) {
func TestHTTPClientTracePropagatesParentSpan(t *testing.T) {
rec := recorder.NewReporter()
defer rec.Close()

Expand Down Expand Up @@ -220,3 +220,41 @@ func TestHTTPServerTrace(t *testing.T) {
t.Fatalf("incorrect error tag, want %s, have %s", want, have)
}
}

func TestHTTPServerTraceIsRequestBasedSampled(t *testing.T) {
rec := recorder.NewReporter()
defer rec.Close()

const httpMethod = "DELETE"

tr, _ := zipkin.NewTracer(rec)

handler := kithttp.NewServer(
endpoint.Nop,
func(context.Context, *http.Request) (interface{}, error) { return nil, nil },
func(context.Context, http.ResponseWriter, interface{}) error { return nil },
zipkinkit.HTTPServerTrace(tr, zipkinkit.RequestSampler(func(r *http.Request) bool {
return r.Method == httpMethod
})),
)

server := httptest.NewServer(handler)
defer server.Close()

req, err := http.NewRequest(httpMethod, server.URL, nil)
if err != nil {
t.Fatalf("unable to create HTTP request: %s", err.Error())
}

client := http.Client{}
resp, err := client.Do(req)
if err != nil {
t.Fatalf("unable to send HTTP request: %s", err.Error())
}
resp.Body.Close()

spans := rec.Flush()
if want, have := 1, len(spans); want != have {
t.Fatalf("incorrect number of spans, want %d, have %d", want, have)
}
}
23 changes: 18 additions & 5 deletions tracing/zipkin/options.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package zipkin

import "github.com/go-kit/kit/log"
import (
"net/http"

"github.com/go-kit/kit/log"
)

// TracerOption allows for functional options to our Zipkin tracing middleware.
type TracerOption func(o *tracerOptions)
Expand Down Expand Up @@ -46,9 +50,18 @@ func AllowPropagation(propagate bool) TracerOption {
}
}

// RequestSampler allows one to set the sampling decision based on the details
// found in the http.Request.
func RequestSampler(sampleFunc func(r *http.Request) bool) TracerOption {
return func(o *tracerOptions) {
o.requestSampler = sampleFunc
}
}

type tracerOptions struct {
tags map[string]string
name string
logger log.Logger
propagate bool
tags map[string]string
name string
logger log.Logger
propagate bool
requestSampler func(r *http.Request) bool
}

0 comments on commit 765ff6c

Please sign in to comment.