Skip to content

Commit defd658

Browse files
committed
add RequestSampler option to HTTP Server middleware
1 parent 0f43e24 commit defd658

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

middleware/http/server.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ type handler struct {
1717
next http.Handler
1818
tagResponseSize bool
1919
defaultTags map[string]string
20+
requestSampler func(r *http.Request) bool
2021
}
2122

2223
// ServerOption allows Middleware to be optionally configured.
@@ -47,6 +48,14 @@ func SpanName(name string) ServerOption {
4748
}
4849
}
4950

51+
// RequestSampler allows one to set the sampling decision based on the details
52+
// found in the http.Request.
53+
func RequestSampler(sampleFunc func(r *http.Request) bool) ServerOption {
54+
return func(h *handler) {
55+
h.requestSampler = sampleFunc
56+
}
57+
}
58+
5059
// NewServerMiddleware returns a http.Handler middleware with Zipkin tracing.
5160
func NewServerMiddleware(t *zipkin.Tracer, options ...ServerOption) func(http.Handler) http.Handler {
5261
return func(next http.Handler) http.Handler {
@@ -68,6 +77,11 @@ func (h handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
6877
// try to extract B3 Headers from upstream
6978
sc := h.tracer.Extract(b3.ExtractHTTP(r))
7079

80+
if h.requestSampler != nil && sc.Sampled == nil {
81+
sample := h.requestSampler(r)
82+
sc.Sampled = &sample
83+
}
84+
7185
remoteEndpoint, _ := zipkin.NewEndpoint("", r.RemoteAddr)
7286

7387
if len(h.name) == 0 {

0 commit comments

Comments
 (0)