@@ -16,42 +16,43 @@ import (
16
16
"net/http"
17
17
"strconv"
18
18
"strings"
19
-
20
- "go.opencensus.io/trace"
21
- "go.opencensus.io/trace/propagation"
22
19
)
23
20
24
21
const (
25
22
httpHeaderMaxSize = 200
26
23
httpHeader = `X-Cloud-Trace-Context`
27
24
)
28
25
29
- var _ propagation.HTTPFormat = (* HTTPFormat )(nil )
30
-
31
26
// HTTPFormat implements propagation.HTTPFormat to propagate
32
27
// traces in HTTP headers for Google Cloud Platform and Stackdriver Trace.
33
28
type HTTPFormat struct {}
34
29
30
+ type SpanContext struct {
31
+ TraceID [16 ]byte
32
+ SpanID [8 ]byte
33
+ TraceOptions uint32
34
+ }
35
+
35
36
// SpanContextFromRequest extracts a Stackdriver Trace span context from incoming requests.
36
- func (f * HTTPFormat ) SpanContextFromRequest (req * http.Request ) (sc trace. SpanContext , ok bool ) {
37
+ func (f * HTTPFormat ) SpanContextFromRequest (req * http.Request ) (sc SpanContext , ok bool ) {
37
38
h := req .Header .Get (httpHeader )
38
39
// See https://cloud.google.com/trace/docs/faq for the header HTTPFormat.
39
40
// Return if the header is empty or missing, or if the header is unreasonably
40
41
// large, to avoid making unnecessary copies of a large string.
41
42
if h == "" || len (h ) > httpHeaderMaxSize {
42
- return trace. SpanContext {}, false
43
+ return SpanContext {}, false
43
44
}
44
45
45
46
// Parse the trace id field.
46
47
slash := strings .Index (h , `/` )
47
48
if slash == - 1 {
48
- return trace. SpanContext {}, false
49
+ return SpanContext {}, false
49
50
}
50
51
tid , h := h [:slash ], h [slash + 1 :]
51
52
52
53
buf , err := hex .DecodeString (tid )
53
54
if err != nil {
54
- return trace. SpanContext {}, false
55
+ return SpanContext {}, false
55
56
}
56
57
copy (sc .TraceID [:], buf )
57
58
@@ -63,7 +64,7 @@ func (f *HTTPFormat) SpanContextFromRequest(req *http.Request) (sc trace.SpanCon
63
64
}
64
65
sid , err := strconv .ParseUint (spanstr , 10 , 64 )
65
66
if err != nil {
66
- return trace. SpanContext {}, false
67
+ return SpanContext {}, false
67
68
}
68
69
binary .BigEndian .PutUint64 (sc .SpanID [:], sid )
69
70
@@ -73,14 +74,14 @@ func (f *HTTPFormat) SpanContextFromRequest(req *http.Request) (sc trace.SpanCon
73
74
}
74
75
o , err := strconv .ParseUint (h [2 :], 10 , 64 )
75
76
if err != nil {
76
- return trace. SpanContext {}, false
77
+ return SpanContext {}, false
77
78
}
78
- sc .TraceOptions = trace . TraceOptions (o )
79
+ sc .TraceOptions = uint32 (o )
79
80
return sc , true
80
81
}
81
82
82
83
// SpanContextToRequest modifies the given request to include a Stackdriver Trace header.
83
- func (f * HTTPFormat ) SpanContextToRequest (sc trace. SpanContext , req * http.Request ) {
84
+ func (f * HTTPFormat ) SpanContextToRequest (sc SpanContext , req * http.Request ) {
84
85
sid := binary .BigEndian .Uint64 (sc .SpanID [:])
85
86
header := fmt .Sprintf ("%s/%d;o=%d" , hex .EncodeToString (sc .TraceID [:]), sid , int64 (sc .TraceOptions ))
86
87
req .Header .Set (httpHeader , header )
0 commit comments