1 // Copyright 2018, OpenCensus Authors
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
7 // http://www.apache.org/licenses/LICENSE-2.0
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
15 // Package tracecontext contains HTTP propagator for TraceContext standard.
16 // See https://github.com/w3c/distributed-tracing for more information.
17 package tracecontext // import "go.opencensus.io/plugin/ochttp/propagation/tracecontext"
27 "go.opencensus.io/trace"
28 "go.opencensus.io/trace/propagation"
29 "go.opencensus.io/trace/tracestate"
35 maxTracestateLen = 512
36 traceparentHeader = "traceparent"
37 tracestateHeader = "tracestate"
38 trimOWSRegexFmt = `^[\x09\x20]*(.*[^\x20\x09])[\x09\x20]*$`
41 var trimOWSRegExp = regexp.MustCompile(trimOWSRegexFmt)
43 var _ propagation.HTTPFormat = (*HTTPFormat)(nil)
45 // HTTPFormat implements the TraceContext trace propagation format.
46 type HTTPFormat struct{}
48 // SpanContextFromRequest extracts a span context from incoming requests.
49 func (f *HTTPFormat) SpanContextFromRequest(req *http.Request) (sc trace.SpanContext, ok bool) {
50 h, ok := getRequestHeader(req, traceparentHeader, false)
52 return trace.SpanContext{}, false
54 sections := strings.Split(h, "-")
55 if len(sections) < 4 {
56 return trace.SpanContext{}, false
59 if len(sections[0]) != 2 {
60 return trace.SpanContext{}, false
62 ver, err := hex.DecodeString(sections[0])
64 return trace.SpanContext{}, false
66 version := int(ver[0])
67 if version > maxVersion {
68 return trace.SpanContext{}, false
71 if version == 0 && len(sections) != 4 {
72 return trace.SpanContext{}, false
75 if len(sections[1]) != 32 {
76 return trace.SpanContext{}, false
78 tid, err := hex.DecodeString(sections[1])
80 return trace.SpanContext{}, false
82 copy(sc.TraceID[:], tid)
84 if len(sections[2]) != 16 {
85 return trace.SpanContext{}, false
87 sid, err := hex.DecodeString(sections[2])
89 return trace.SpanContext{}, false
91 copy(sc.SpanID[:], sid)
93 opts, err := hex.DecodeString(sections[3])
94 if err != nil || len(opts) < 1 {
95 return trace.SpanContext{}, false
97 sc.TraceOptions = trace.TraceOptions(opts[0])
99 // Don't allow all zero trace or span ID.
100 if sc.TraceID == [16]byte{} || sc.SpanID == [8]byte{} {
101 return trace.SpanContext{}, false
104 sc.Tracestate = tracestateFromRequest(req)
108 // getRequestHeader returns a combined header field according to RFC7230 section 3.2.2.
109 // If commaSeparated is true, multiple header fields with the same field name using be
110 // combined using ",".
111 // If no header was found using the given name, "ok" would be false.
112 // If more than one headers was found using the given name, while commaSeparated is false,
113 // "ok" would be false.
114 func getRequestHeader(req *http.Request, name string, commaSeparated bool) (hdr string, ok bool) {
115 v := req.Header[textproto.CanonicalMIMEHeaderKey(name)]
122 return strings.Join(v, ","), commaSeparated
126 // TODO(rghetia): return an empty Tracestate when parsing tracestate header encounters an error.
127 // Revisit to return additional boolean value to indicate parsing error when following issues
129 // https://github.com/w3c/distributed-tracing/issues/172
130 // https://github.com/w3c/distributed-tracing/issues/175
131 func tracestateFromRequest(req *http.Request) *tracestate.Tracestate {
132 h, _ := getRequestHeader(req, tracestateHeader, true)
137 var entries []tracestate.Entry
138 pairs := strings.Split(h, ",")
139 hdrLenWithoutOWS := len(pairs) - 1 // Number of commas
140 for _, pair := range pairs {
141 matches := trimOWSRegExp.FindStringSubmatch(pair)
146 hdrLenWithoutOWS += len(pair)
147 if hdrLenWithoutOWS > maxTracestateLen {
150 kv := strings.Split(pair, "=")
154 entries = append(entries, tracestate.Entry{Key: kv[0], Value: kv[1]})
156 ts, err := tracestate.New(nil, entries...)
164 func tracestateToRequest(sc trace.SpanContext, req *http.Request) {
165 var pairs = make([]string, 0, len(sc.Tracestate.Entries()))
166 if sc.Tracestate != nil {
167 for _, entry := range sc.Tracestate.Entries() {
168 pairs = append(pairs, strings.Join([]string{entry.Key, entry.Value}, "="))
170 h := strings.Join(pairs, ",")
172 if h != "" && len(h) <= maxTracestateLen {
173 req.Header.Set(tracestateHeader, h)
178 // SpanContextToRequest modifies the given request to include traceparent and tracestate headers.
179 func (f *HTTPFormat) SpanContextToRequest(sc trace.SpanContext, req *http.Request) {
180 h := fmt.Sprintf("%x-%x-%x-%x",
181 []byte{supportedVersion},
184 []byte{byte(sc.TraceOptions)})
185 req.Header.Set(traceparentHeader, h)
186 tracestateToRequest(sc, req)