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 b3 contains a propagation.HTTPFormat implementation
16 // for B3 propagation. See https://github.com/openzipkin/b3-propagation
18 package b3 // import "go.opencensus.io/plugin/ochttp/propagation/b3"
24 "go.opencensus.io/trace"
25 "go.opencensus.io/trace/propagation"
28 // B3 headers that OpenCensus understands.
30 TraceIDHeader = "X-B3-TraceId"
31 SpanIDHeader = "X-B3-SpanId"
32 SampledHeader = "X-B3-Sampled"
35 // HTTPFormat implements propagation.HTTPFormat to propagate
36 // traces in HTTP headers in B3 propagation format.
37 // HTTPFormat skips the X-B3-ParentId and X-B3-Flags headers
38 // because there are additional fields not represented in the
39 // OpenCensus span context. Spans created from the incoming
40 // header will be the direct children of the client-side span.
41 // Similarly, receiver of the outgoing spans should use client-side
42 // span created by OpenCensus as the parent.
43 type HTTPFormat struct{}
45 var _ propagation.HTTPFormat = (*HTTPFormat)(nil)
47 // SpanContextFromRequest extracts a B3 span context from incoming requests.
48 func (f *HTTPFormat) SpanContextFromRequest(req *http.Request) (sc trace.SpanContext, ok bool) {
49 tid, ok := ParseTraceID(req.Header.Get(TraceIDHeader))
51 return trace.SpanContext{}, false
53 sid, ok := ParseSpanID(req.Header.Get(SpanIDHeader))
55 return trace.SpanContext{}, false
57 sampled, _ := ParseSampled(req.Header.Get(SampledHeader))
58 return trace.SpanContext{
61 TraceOptions: sampled,
65 // ParseTraceID parses the value of the X-B3-TraceId header.
66 func ParseTraceID(tid string) (trace.TraceID, bool) {
68 return trace.TraceID{}, false
70 b, err := hex.DecodeString(tid)
72 return trace.TraceID{}, false
74 var traceID trace.TraceID
77 start := 8 + (8 - len(b))
78 copy(traceID[start:], b)
81 copy(traceID[start:], b)
87 // ParseSpanID parses the value of the X-B3-SpanId or X-B3-ParentSpanId headers.
88 func ParseSpanID(sid string) (spanID trace.SpanID, ok bool) {
90 return trace.SpanID{}, false
92 b, err := hex.DecodeString(sid)
94 return trace.SpanID{}, false
97 copy(spanID[start:], b)
101 // ParseSampled parses the value of the X-B3-Sampled header.
102 func ParseSampled(sampled string) (trace.TraceOptions, bool) {
105 return trace.TraceOptions(1), true
107 return trace.TraceOptions(0), false
111 // SpanContextToRequest modifies the given request to include B3 headers.
112 func (f *HTTPFormat) SpanContextToRequest(sc trace.SpanContext, req *http.Request) {
113 req.Header.Set(TraceIDHeader, hex.EncodeToString(sc.TraceID[:]))
114 req.Header.Set(SpanIDHeader, hex.EncodeToString(sc.SpanID[:]))
122 req.Header.Set(SampledHeader, sampled)