|
27 | 27 | // tracedDoer is a goa client Doer that inserts the tracing headers for
|
28 | 28 | // each request it makes.
|
29 | 29 | tracedDoer struct {
|
30 |
| - doer client.Doer |
31 |
| - traceID string |
32 |
| - spanID string |
| 30 | + client.Doer |
33 | 31 | }
|
34 | 32 | )
|
35 | 33 |
|
@@ -78,22 +76,8 @@ func Tracer(sampleRate int, spanIDFunc, traceIDFunc IDFunc) goa.Middleware {
|
78 | 76 |
|
79 | 77 | // TraceDoer wraps a goa client Doer and sets the trace headers so that the
|
80 | 78 | // 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} |
97 | 81 | }
|
98 | 82 |
|
99 | 83 | // 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
|
136 | 120 |
|
137 | 121 | // Do adds the tracing headers to the requests before making it.
|
138 | 122 | 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 | + } |
141 | 131 |
|
142 |
| - return d.doer.Do(ctx, req) |
| 132 | + return d.Doer.Do(ctx, req) |
143 | 133 | }
|
0 commit comments