forked from infobloxopen/atlas-app-toolkit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
annotator.go
39 lines (30 loc) · 936 Bytes
/
annotator.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
package tracing
import (
"context"
"net/http"
"go.opencensus.io/plugin/ochttp/propagation/b3"
"go.opencensus.io/trace"
"go.opencensus.io/trace/propagation"
"google.golang.org/grpc/metadata"
)
const (
traceContextKey = "grpc-trace-bin"
)
var defaultFormat propagation.HTTPFormat = &b3.HTTPFormat{}
// SpanContextAnnotator retrieve information about current span from context or HTTP headers
// and propagate in binary format to gRPC service
func SpanContextAnnotator(ctx context.Context, req *http.Request) metadata.MD {
md := make(metadata.MD)
span := trace.FromContext(ctx)
if span != nil {
traceContextBinary := propagation.Binary(span.SpanContext())
md[traceContextKey] = []string{string(traceContextBinary)}
return md
}
sc, ok := defaultFormat.SpanContextFromRequest(req)
if ok {
traceContextBinary := propagation.Binary(sc)
md[traceContextKey] = []string{string(traceContextBinary)}
}
return md
}