-
Notifications
You must be signed in to change notification settings - Fork 4
/
tracing.go
105 lines (83 loc) · 2.34 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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
package util
import (
"encoding/hex"
"fmt"
"regexp"
"go.opentelemetry.io/otel/trace"
)
var traceCtxRegExp = regexp.MustCompile("^(?P<version>[0-9a-f]{2})-(?P<traceID>[a-f0-9]{32})-(?P<spanID>[a-f0-9]{16})-(?P<traceFlags>[a-f0-9]{2})(?:-.*)?$")
// Retrieves traceparent, or empty string if no span exists.
// Uses standard format: {version}-{trace_id}-{span_id}-{trace_flags}
// See specification: https://www.w3.org/TR/trace-context/#traceparent-header-field-values
func GetTraceparent(spanContext trace.SpanContext) string {
if !spanContext.IsValid() {
return ""
}
// Clear all flags other than the trace-context supported sampling bit.
flags := spanContext.TraceFlags() & trace.FlagsSampled
return fmt.Sprintf("%s-%s-%s-%s",
"00",
spanContext.TraceID(),
spanContext.SpanID(),
flags)
}
func ParseTraceparent(traceparent string) trace.SpanContext {
maxVersion := 254
if traceparent == "" {
return trace.SpanContext{}
}
if traceparent == "" {
return trace.SpanContext{}
}
matches := traceCtxRegExp.FindStringSubmatch(traceparent)
if len(matches) == 0 {
return trace.SpanContext{}
}
if len(matches) < 5 { // four subgroups plus the overall match
return trace.SpanContext{}
}
if len(matches[1]) != 2 {
return trace.SpanContext{}
}
ver, err := hex.DecodeString(matches[1])
if err != nil {
return trace.SpanContext{}
}
version := int(ver[0])
if version > maxVersion {
return trace.SpanContext{}
}
if version == 0 && len(matches) != 5 { // four subgroups plus the overall match
return trace.SpanContext{}
}
if len(matches[2]) != 32 {
return trace.SpanContext{}
}
var scc trace.SpanContextConfig
scc.TraceID, err = trace.TraceIDFromHex(matches[2][:32])
if err != nil {
return trace.SpanContext{}
}
if len(matches[3]) != 16 {
return trace.SpanContext{}
}
scc.SpanID, err = trace.SpanIDFromHex(matches[3])
if err != nil {
return trace.SpanContext{}
}
if len(matches[4]) != 2 {
return trace.SpanContext{}
}
opts, err := hex.DecodeString(matches[4])
if err != nil || len(opts) < 1 || (version == 0 && opts[0] > 2) {
return trace.SpanContext{}
}
// Clear all flags other than the trace-context supported sampling bit.
scc.TraceFlags = trace.TraceFlags(opts[0]) & trace.FlagsSampled
scc.Remote = true
sc := trace.NewSpanContext(scc)
if !sc.IsValid() {
return trace.SpanContext{}
}
return sc
}