Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adds request sampler into zipkin tracing. #813

Merged
merged 2 commits into from
Jan 2, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 spanContext.Sampled == nil && config.requestSampler != 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) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Http -> HTTP, not too important for this PR.

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
}