/
tracing.go
83 lines (72 loc) · 2.22 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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package thriftbp
import (
"context"
"strconv"
"github.com/apache/thrift/lib/go/thrift"
"github.com/reddit/baseplate.go/tracing"
"github.com/reddit/baseplate.go/transport"
)
// CreateThriftContextFromSpan injects span info into a context object that can
// be used in thrift client code. If you are using a client pool created using
// thriftbp.NewBaseplateClientPool, all of your thrift calls will already be
// call this automatically, so there is no need to use it directly.
//
// Caller should first create a client child-span for the thrift call as usual,
// then use that span and the parent context object with this call,
// then use the returned context object in the thrift call.
// Something like:
//
// span, clientCtx := opentracing.StartSpanFromContext(
// ctx,
// "myCall",
// tracing.SpanTypeOption{Type: tracing.SpanTypeClient},
// )
// clientCtx = thriftbp.CreateThriftContextFromSpan(clientCtx, tracing.AsSpan(span))
// result, err := client.MyCall(clientCtx, arg1, arg2)
// span.FinishWithOptions(tracing.FinishOptions{
// Ctx: clientCtx,
// Err: err,
// }.Convert())
func CreateThriftContextFromSpan(ctx context.Context, span *tracing.Span) context.Context {
headers := thrift.GetWriteHeaderList(ctx)
ctx = thrift.SetHeader(
ctx,
transport.HeaderTracingTrace,
span.TraceID(),
)
headers = append(headers, transport.HeaderTracingTrace)
ctx = thrift.SetHeader(
ctx,
transport.HeaderTracingSpan,
span.ID(),
)
headers = append(headers, transport.HeaderTracingSpan)
ctx = thrift.SetHeader(
ctx,
transport.HeaderTracingFlags,
strconv.FormatInt(span.Flags(), 10),
)
headers = append(headers, transport.HeaderTracingFlags)
if span.ParentID() != "" {
ctx = thrift.SetHeader(
ctx,
transport.HeaderTracingParent,
span.ParentID(),
)
headers = append(headers, transport.HeaderTracingParent)
} else {
ctx = thrift.UnsetHeader(ctx, transport.HeaderTracingParent)
}
if span.Sampled() {
ctx = thrift.SetHeader(
ctx,
transport.HeaderTracingSampled,
transport.HeaderTracingSampledTrue,
)
headers = append(headers, transport.HeaderTracingSampled)
} else {
ctx = thrift.UnsetHeader(ctx, transport.HeaderTracingSampled)
}
ctx = thrift.SetWriteHeaderList(ctx, headers)
return ctx
}