-
Notifications
You must be signed in to change notification settings - Fork 13
/
tracing.go
41 lines (33 loc) · 1.19 KB
/
tracing.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
package interceptor
import (
"context"
"fmt"
"github.com/gojek/fiber"
"github.com/opentracing/opentracing-go"
)
// NewTracingInterceptor creates a TracingInterceptor
func NewTracingInterceptor(tracer opentracing.Tracer) fiber.Interceptor {
return &TracingInterceptor{
tracer: tracer,
}
}
// TracingInterceptor allows for tracing requests
type TracingInterceptor struct {
fiber.NoopAfterDispatchInterceptor
tracer opentracing.Tracer
}
func (i *TracingInterceptor) operationName(ctx context.Context, req fiber.Request) string {
componentID := ctx.Value(fiber.CtxComponentIDKey)
return fmt.Sprintf("[%s] %s ", componentID, req.OperationName())
}
// BeforeDispatch starts and returns a span with the given operation name
func (i *TracingInterceptor) BeforeDispatch(ctx context.Context, req fiber.Request) context.Context {
_, ctx = opentracing.StartSpanFromContextWithTracer(ctx, i.tracer, i.operationName(ctx, req))
return ctx
}
// AfterCompletion returns the Span previously associated with context
func (i *TracingInterceptor) AfterCompletion(ctx context.Context, req fiber.Request, queue fiber.ResponseQueue) {
if span := opentracing.SpanFromContext(ctx); span != nil {
span.Finish()
}
}