Skip to content

Commit

Permalink
Allow user to set start options perf HTTP request
Browse files Browse the repository at this point in the history
  • Loading branch information
rakyll committed Sep 21, 2018
1 parent 2269ed7 commit 1472e81
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
12 changes: 11 additions & 1 deletion plugin/ochttp/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ type Transport struct {
// for spans started by this transport.
StartOptions trace.StartOptions

// GetStartOptions allows to set start options per request. If set,
// StartOptions is going to be ignored.
GetStartOptions func(*http.Request) trace.StartOptions

// NameFromRequest holds the function to use for generating the span name
// from the information found in the outgoing HTTP Request. By default the
// name equals the URL Path.
Expand Down Expand Up @@ -75,11 +79,17 @@ func (t *Transport) RoundTrip(req *http.Request) (*http.Response, error) {
if spanNameFormatter == nil {
spanNameFormatter = spanNameFromURL
}

startOpts := t.StartOptions
if t.GetStartOptions != nil {
startOpts = t.GetStartOptions(req)
}

rt = &traceTransport{
base: rt,
format: format,
startOptions: trace.StartOptions{
Sampler: t.StartOptions.Sampler,
Sampler: startOpts.Sampler,
SpanKind: trace.SpanKindClient,
},
formatSpanName: spanNameFormatter,
Expand Down
14 changes: 12 additions & 2 deletions plugin/ochttp/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ type Handler struct {
// for spans started by this transport.
StartOptions trace.StartOptions

// GetStartOptions allows to set start options per request. If set,
// StartOptions is going to be ignored.
GetStartOptions func(*http.Request) trace.StartOptions

// IsPublicEndpoint should be set to true for publicly accessible HTTP(S)
// servers. If true, any trace metadata set on the incoming request will
// be added as a linked trace instead of being added as a parent of the
Expand Down Expand Up @@ -93,15 +97,21 @@ func (h *Handler) startTrace(w http.ResponseWriter, r *http.Request) (*http.Requ
name = h.FormatSpanName(r)
}
ctx := r.Context()

startOpts := h.StartOptions
if h.GetStartOptions != nil {
startOpts = h.GetStartOptions(r)
}

var span *trace.Span
sc, ok := h.extractSpanContext(r)
if ok && !h.IsPublicEndpoint {
ctx, span = trace.StartSpanWithRemoteParent(ctx, name, sc,
trace.WithSampler(h.StartOptions.Sampler),
trace.WithSampler(startOpts.Sampler),
trace.WithSpanKind(trace.SpanKindServer))
} else {
ctx, span = trace.StartSpan(ctx, name,
trace.WithSampler(h.StartOptions.Sampler),
trace.WithSampler(startOpts.Sampler),
trace.WithSpanKind(trace.SpanKindServer),
)
if ok {
Expand Down

0 comments on commit 1472e81

Please sign in to comment.