Skip to content
This repository was archived by the owner on Jul 31, 2023. It is now read-only.

Commit f261ec0

Browse files
authored
Allow user to set start options perf HTTP request (#924)
Fixes #869.
1 parent fb92c34 commit f261ec0

File tree

2 files changed

+23
-3
lines changed

2 files changed

+23
-3
lines changed

plugin/ochttp/client.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,10 @@ type Transport struct {
4747
// for spans started by this transport.
4848
StartOptions trace.StartOptions
4949

50+
// GetStartOptions allows to set start options per request. If set,
51+
// StartOptions is going to be ignored.
52+
GetStartOptions func(*http.Request) trace.StartOptions
53+
5054
// NameFromRequest holds the function to use for generating the span name
5155
// from the information found in the outgoing HTTP Request. By default the
5256
// name equals the URL Path.
@@ -75,11 +79,17 @@ func (t *Transport) RoundTrip(req *http.Request) (*http.Response, error) {
7579
if spanNameFormatter == nil {
7680
spanNameFormatter = spanNameFromURL
7781
}
82+
83+
startOpts := t.StartOptions
84+
if t.GetStartOptions != nil {
85+
startOpts = t.GetStartOptions(req)
86+
}
87+
7888
rt = &traceTransport{
7989
base: rt,
8090
format: format,
8191
startOptions: trace.StartOptions{
82-
Sampler: t.StartOptions.Sampler,
92+
Sampler: startOpts.Sampler,
8393
SpanKind: trace.SpanKindClient,
8494
},
8595
formatSpanName: spanNameFormatter,

plugin/ochttp/server.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,10 @@ type Handler struct {
5656
// for spans started by this transport.
5757
StartOptions trace.StartOptions
5858

59+
// GetStartOptions allows to set start options per request. If set,
60+
// StartOptions is going to be ignored.
61+
GetStartOptions func(*http.Request) trace.StartOptions
62+
5963
// IsPublicEndpoint should be set to true for publicly accessible HTTP(S)
6064
// servers. If true, any trace metadata set on the incoming request will
6165
// be added as a linked trace instead of being added as a parent of the
@@ -93,15 +97,21 @@ func (h *Handler) startTrace(w http.ResponseWriter, r *http.Request) (*http.Requ
9397
name = h.FormatSpanName(r)
9498
}
9599
ctx := r.Context()
100+
101+
startOpts := h.StartOptions
102+
if h.GetStartOptions != nil {
103+
startOpts = h.GetStartOptions(r)
104+
}
105+
96106
var span *trace.Span
97107
sc, ok := h.extractSpanContext(r)
98108
if ok && !h.IsPublicEndpoint {
99109
ctx, span = trace.StartSpanWithRemoteParent(ctx, name, sc,
100-
trace.WithSampler(h.StartOptions.Sampler),
110+
trace.WithSampler(startOpts.Sampler),
101111
trace.WithSpanKind(trace.SpanKindServer))
102112
} else {
103113
ctx, span = trace.StartSpan(ctx, name,
104-
trace.WithSampler(h.StartOptions.Sampler),
114+
trace.WithSampler(startOpts.Sampler),
105115
trace.WithSpanKind(trace.SpanKindServer),
106116
)
107117
if ok {

0 commit comments

Comments
 (0)