Skip to content

Commit 8774349

Browse files
committed
adds convenience functions Sample() and Discard() for request sampler decisions
1 parent 0ef9e89 commit 8774349

File tree

5 files changed

+31
-18
lines changed

5 files changed

+31
-18
lines changed

middleware/http/request_sampler.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package http
2+
3+
import "net/http"
4+
5+
// RequestSamplerFunc can be implemented for client and/or server side sampling decisions that can override the existing
6+
// upstream sampling decision. If the implementation returns nil, the existing sampling decision stays as is.
7+
type RequestSamplerFunc func(r *http.Request) *bool
8+
9+
// Sample is a convenience function that returns a pointer to a boolean true. Use this for RequestSamplerFuncs when
10+
// wanting the RequestSampler to override the sampling decision to yes.
11+
func Sample() *bool {
12+
sample := true
13+
return &sample
14+
}
15+
16+
// Discard is a convenience function that returns a pointer to a boolean false. Use this for RequestSamplerFuncs when
17+
// wanting the RequestSampler to override the sampling decision to no.
18+
func Discard() *bool {
19+
sample := false
20+
return &sample
21+
}

middleware/http/server.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ type handler struct {
3131
next http.Handler
3232
tagResponseSize bool
3333
defaultTags map[string]string
34-
requestSampler func(r *http.Request) *bool
34+
requestSampler RequestSamplerFunc
3535
errHandler ErrHandler
3636
}
3737

@@ -66,7 +66,7 @@ func SpanName(name string) ServerOption {
6666
// RequestSampler allows one to set the sampling decision based on the details
6767
// found in the http.Request. If wanting to keep the existing sampling decision
6868
// from upstream as is, this function should return nil.
69-
func RequestSampler(sampleFunc func(r *http.Request) *bool) ServerOption {
69+
func RequestSampler(sampleFunc RequestSamplerFunc) ServerOption {
7070
return func(h *handler) {
7171
h.requestSampler = sampleFunc
7272
}

middleware/http/server_test.go

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -207,10 +207,6 @@ func TestHTTPDefaultSpanName(t *testing.T) {
207207

208208
func TestHTTPRequestSampler(t *testing.T) {
209209
var (
210-
sample = true
211-
noSample = false
212-
passThrough *bool = nil
213-
214210
spanRecorder = &recorder.ReporterRecorder{}
215211
httpRecorder = httptest.NewRecorder()
216212
requestBuf = bytes.NewBufferString("incoming data")
@@ -220,9 +216,9 @@ func TestHTTPRequestSampler(t *testing.T) {
220216

221217
samplers := [](func(r *http.Request) *bool){
222218
nil,
223-
func(r *http.Request) *bool { return &sample },
224-
func(r *http.Request) *bool { return &noSample },
225-
func(r *http.Request) *bool { return passThrough },
219+
func(r *http.Request) *bool { return mw.Sample() },
220+
func(r *http.Request) *bool { return mw.Discard() },
221+
func(r *http.Request) *bool { return nil },
226222
}
227223

228224
for idx, sampler := range samplers {

middleware/http/transport.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ type transport struct {
5757
errHandler ErrHandler
5858
errResponseReader *ErrResponseReader
5959
logger *log.Logger
60-
requestSampler func(r *http.Request) *bool
60+
requestSampler RequestSamplerFunc
6161
}
6262

6363
// TransportOption allows one to configure optional transport configuration.
@@ -112,7 +112,7 @@ func TransportLogger(l *log.Logger) TransportOption {
112112
// sampling decision contained in the context. The function returns a *bool,
113113
// if returning nil, sampling decision is not being changed whereas returning
114114
// something else than nil is being used as sampling decision.
115-
func TransportRequestSampler(sampleFunc func(r *http.Request) *bool) TransportOption {
115+
func TransportRequestSampler(sampleFunc RequestSamplerFunc) TransportOption {
116116
return func(t *transport) {
117117
t.requestSampler = sampleFunc
118118
}

middleware/http/transport_test.go

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -128,14 +128,10 @@ func TestRoundTripErrResponseReadingSuccess(t *testing.T) {
128128
}
129129
}
130130

131-
func boolToPtr(b bool) *bool {
132-
return &b
133-
}
134-
135131
func TestTransportRequestSamplerOverridesSamplingFromContext(t *testing.T) {
136132
cases := []struct {
137133
Sampler func(uint64) bool
138-
RequestSampler func(*http.Request) *bool
134+
RequestSampler RequestSamplerFunc
139135
ExpectedSampling string
140136
}{
141137
// Test proper handling when there is no RequestSampler
@@ -153,13 +149,13 @@ func TestTransportRequestSamplerOverridesSamplingFromContext(t *testing.T) {
153149
// Test RequestSampler override sample -> no sample
154150
{
155151
Sampler: zipkin.AlwaysSample,
156-
RequestSampler: func(_ *http.Request) *bool { return boolToPtr(false) },
152+
RequestSampler: func(_ *http.Request) *bool { return Discard() },
157153
ExpectedSampling: "0",
158154
},
159155
// Test RequestSampler override no sample -> sample
160156
{
161157
Sampler: zipkin.NeverSample,
162-
RequestSampler: func(_ *http.Request) *bool { return boolToPtr(true) },
158+
RequestSampler: func(_ *http.Request) *bool { return Sample() },
163159
ExpectedSampling: "1",
164160
},
165161
// Test RequestSampler pass through of sampled decision

0 commit comments

Comments
 (0)