Skip to content

Commit f8c7bd9

Browse files
author
Raphaël Simon
authored
Fix TraceDoer so it fetches the trace details properly. (goadesign#1128)
Do it at call time so that the same traced doer can be reused to do many requests potentially concurrently.
1 parent 91cae0e commit f8c7bd9

File tree

1 file changed

+12
-22
lines changed

1 file changed

+12
-22
lines changed

middleware/tracer.go

+12-22
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,7 @@ type (
2727
// tracedDoer is a goa client Doer that inserts the tracing headers for
2828
// each request it makes.
2929
tracedDoer struct {
30-
doer client.Doer
31-
traceID string
32-
spanID string
30+
client.Doer
3331
}
3432
)
3533

@@ -78,22 +76,8 @@ func Tracer(sampleRate int, spanIDFunc, traceIDFunc IDFunc) goa.Middleware {
7876

7977
// TraceDoer wraps a goa client Doer and sets the trace headers so that the
8078
// downstream service may properly retrieve the parent span ID and trace ID.
81-
//
82-
// ctx must contain the current request segment as set by the xray middleware or
83-
// the doer passed as argument is returned.
84-
func TraceDoer(ctx context.Context, doer client.Doer) client.Doer {
85-
var (
86-
traceID = ContextTraceID(ctx)
87-
spanID = ContextSpanID(ctx)
88-
)
89-
if traceID == "" {
90-
return doer
91-
}
92-
return &tracedDoer{
93-
doer: doer,
94-
traceID: traceID,
95-
spanID: spanID,
96-
}
79+
func TraceDoer(doer client.Doer) client.Doer {
80+
return &tracedDoer{doer}
9781
}
9882

9983
// ContextTraceID returns the trace ID extracted from the given context if any,
@@ -136,8 +120,14 @@ func WithTrace(ctx context.Context, traceID, spanID, parentID string) context.Co
136120

137121
// Do adds the tracing headers to the requests before making it.
138122
func (d *tracedDoer) Do(ctx context.Context, req *http.Request) (*http.Response, error) {
139-
req.Header.Set(TraceIDHeader, d.traceID)
140-
req.Header.Set(ParentSpanIDHeader, d.spanID)
123+
var (
124+
traceID = ContextTraceID(ctx)
125+
spanID = ContextSpanID(ctx)
126+
)
127+
if traceID != "" {
128+
req.Header.Set(TraceIDHeader, traceID)
129+
req.Header.Set(ParentSpanIDHeader, spanID)
130+
}
141131

142-
return d.doer.Do(ctx, req)
132+
return d.Doer.Do(ctx, req)
143133
}

0 commit comments

Comments
 (0)