@@ -7,18 +7,20 @@ import (
7
7
"io"
8
8
"net/http"
9
9
10
- "goa.design/goa"
10
+ "goa.design/goa/http/middleware/logging "
11
11
)
12
12
13
- // middlewareKey is the private type used for goa middlewares to store values in
14
- // the context. It is private to avoid possible collisions with keys used by
15
- // other packages.
16
- type middlewareKey int
17
-
18
13
const (
19
- traceKey middlewareKey = iota + 1
20
- spanKey
21
- parentSpanKey
14
+ // TraceIDKey is the request context key used to store its Trace ID if any.
15
+ TraceIDKey = "TraceIDCtxKey"
16
+
17
+ // TraceSpanIDKey is the request context key used to store the current
18
+ // trace span ID if any.
19
+ TraceSpanIDKey = "TraceSpanIDCtxKey"
20
+
21
+ // TraceParentSpanIDKey is the request context key used to store the current
22
+ // trace parent span ID if any.
23
+ TraceParentSpanIDKey = "TraceParentSpanIDCtxKey"
22
24
)
23
25
24
26
const (
@@ -45,16 +47,16 @@ type (
45
47
// the middleware.
46
48
Option func (* options ) * options
47
49
48
- // tracedDoer is a client Doer that inserts the tracing headers for
49
- // each request it makes.
50
+ // tracedDoer is a client Doer that inserts the tracing headers for each
51
+ // request it makes.
50
52
tracedDoer struct {
51
53
Doer
52
54
}
53
55
54
- // tracedLogger is a logger which logs the trace ID with every log
55
- // entry when one is present.
56
+ // tracedLogger is a logger which logs the trace ID with every log entry
57
+ // when one is present.
56
58
tracedLogger struct {
57
- goa. LogAdapter
59
+ logging. Adapter
58
60
}
59
61
60
62
// options is the struct storing all the options.
@@ -185,88 +187,61 @@ func WrapDoer(ctx context.Context, doer Doer) Doer {
185
187
186
188
// WrapLogger returns a logger which logs the trace ID with every message if
187
189
// there is one.
188
- func WrapLogger (l goa. LogAdapter ) goa. LogAdapter {
190
+ func WrapLogger (l logging. Adapter ) logging. Adapter {
189
191
return & tracedLogger {l }
190
192
}
191
193
192
- // ContextTraceID returns the trace ID extracted from the given context if any,
193
- // the empty string otherwise.
194
- func ContextTraceID (ctx context.Context ) string {
195
- if t := ctx .Value (traceKey ); t != nil {
196
- return t .(string )
197
- }
198
- return ""
199
- }
200
-
201
- // ContextSpanID returns the span ID extracted from the given context if any,
202
- // the empty string otherwise.
203
- func ContextSpanID (ctx context.Context ) string {
204
- if s := ctx .Value (spanKey ); s != nil {
205
- return s .(string )
206
- }
207
- return ""
208
- }
209
-
210
- // ContextParentSpanID returns the parent span ID extracted from the given
211
- // context if any, the empty string otherwise.
212
- func ContextParentSpanID (ctx context.Context ) string {
213
- if p := ctx .Value (parentSpanKey ); p != nil {
214
- return p .(string )
215
- }
216
- return ""
217
- }
218
-
219
194
// WithTrace returns a context containing the given trace ID.
220
195
func WithTrace (ctx context.Context , traceID string ) context.Context {
221
- ctx = context .WithValue (ctx , traceKey , traceID )
196
+ ctx = context .WithValue (ctx , TraceIDKey , traceID )
222
197
return ctx
223
198
}
224
199
225
200
// WithSpan returns a context containing the given trace, span and parent span
226
201
// IDs.
227
202
func WithSpan (ctx context.Context , traceID , spanID , parentID string ) context.Context {
228
203
if parentID != "" {
229
- ctx = context .WithValue (ctx , parentSpanKey , parentID )
204
+ ctx = context .WithValue (ctx , TraceParentSpanIDKey , parentID )
230
205
}
231
- ctx = context .WithValue (ctx , traceKey , traceID )
232
- ctx = context .WithValue (ctx , spanKey , spanID )
206
+ ctx = context .WithValue (ctx , TraceIDKey , traceID )
207
+ ctx = context .WithValue (ctx , TraceSpanIDKey , spanID )
233
208
return ctx
234
209
}
235
210
236
211
// Do adds the tracing headers to the requests before making it.
237
212
func (d * tracedDoer ) Do (r * http.Request ) (* http.Response , error ) {
238
213
var (
239
- traceID = ContextTraceID ( r .Context ())
240
- spanID = ContextSpanID ( r .Context ())
214
+ traceID = r .Context (). Value ( TraceIDKey )
215
+ spanID = r .Context (). Value ( TraceSpanIDKey )
241
216
)
242
- if traceID != "" {
243
- r .Header .Set (TraceIDHeader , traceID )
244
- r .Header .Set (ParentSpanIDHeader , spanID )
217
+ if traceID != nil {
218
+ r .Header .Set (TraceIDHeader , traceID .( string ) )
219
+ r .Header .Set (ParentSpanIDHeader , spanID .( string ) )
245
220
}
246
221
247
222
return d .Doer .Do (r )
248
223
}
249
224
250
225
// Info logs the trace ID when present then the values passed as argument.
251
226
func (l * tracedLogger ) Info (ctx context.Context , keyvals ... interface {}) {
252
- traceID := ContextTraceID ( ctx )
253
- if traceID == "" {
254
- l .LogAdapter .Info (ctx , keyvals ... )
227
+ traceID := ctx . Value ( TraceIDKey )
228
+ if traceID == nil {
229
+ l .Adapter .Info (ctx , keyvals ... )
255
230
return
256
231
}
257
- keyvals = append ([]interface {}{"trace" , traceID }, keyvals ... )
258
- l .LogAdapter .Info (ctx , keyvals )
232
+ keyvals = append ([]interface {}{"trace" , traceID .( string ) }, keyvals ... )
233
+ l .Adapter .Info (ctx , keyvals )
259
234
}
260
235
261
236
// Error logs the trace ID when present then the values passed as argument.
262
237
func (l * tracedLogger ) Error (ctx context.Context , keyvals ... interface {}) {
263
- traceID := ContextTraceID ( ctx )
264
- if traceID == "" {
265
- l .LogAdapter .Error (ctx , keyvals ... )
238
+ traceID := ctx . Value ( TraceIDKey )
239
+ if traceID == nil {
240
+ l .Adapter .Error (ctx , keyvals ... )
266
241
return
267
242
}
268
- keyvals = append ([]interface {}{"trace" , traceID }, keyvals ... )
269
- l .LogAdapter .Error (ctx , keyvals )
243
+ keyvals = append ([]interface {}{"trace" , traceID .( string ) }, keyvals ... )
244
+ l .Adapter .Error (ctx , keyvals )
270
245
}
271
246
272
247
// shortID produces a " unique" 6 bytes long string.
0 commit comments