From c0329fac3b8e794643e40c0151893f88b18c6076 Mon Sep 17 00:00:00 2001 From: Alan Novitskiy Date: Wed, 25 Mar 2020 14:00:44 -0700 Subject: [PATCH] update http propagation to handle SpanID as a 16-character hexadecimal encoding of an 8-byte array --- propagation/http.go | 16 +++++++--------- propagation/http_test.go | 16 +++++++++++----- 2 files changed, 18 insertions(+), 14 deletions(-) diff --git a/propagation/http.go b/propagation/http.go index 1797d37..317f5f5 100644 --- a/propagation/http.go +++ b/propagation/http.go @@ -17,7 +17,6 @@ package propagation // import "contrib.go.opencensus.io/exporter/stackdriver/propagation" import ( - "encoding/binary" "encoding/hex" "fmt" "net/http" @@ -56,23 +55,23 @@ func (f *HTTPFormat) SpanContextFromRequest(req *http.Request) (sc trace.SpanCon } tid, h := h[:slash], h[slash+1:] - buf, err := hex.DecodeString(tid) + tidBuf, err := hex.DecodeString(tid) if err != nil { return trace.SpanContext{}, false } - copy(sc.TraceID[:], buf) + copy(sc.TraceID[:], tidBuf) // Parse the span id field. - spanstr := h + sid := h semicolon := strings.Index(h, `;`) if semicolon != -1 { - spanstr, h = h[:semicolon], h[semicolon+1:] + sid, h = h[:semicolon], h[semicolon+1:] } - sid, err := strconv.ParseUint(spanstr, 10, 64) + sidBuf, err := hex.DecodeString(sid) if err != nil { return trace.SpanContext{}, false } - binary.BigEndian.PutUint64(sc.SpanID[:], sid) + copy(sc.SpanID[:], sidBuf) // Parse the options field, options field is optional. if !strings.HasPrefix(h, "o=") { @@ -88,7 +87,6 @@ func (f *HTTPFormat) SpanContextFromRequest(req *http.Request) (sc trace.SpanCon // SpanContextToRequest modifies the given request to include a Stackdriver Trace header. func (f *HTTPFormat) SpanContextToRequest(sc trace.SpanContext, req *http.Request) { - sid := binary.BigEndian.Uint64(sc.SpanID[:]) - header := fmt.Sprintf("%s/%d;o=%d", hex.EncodeToString(sc.TraceID[:]), sid, int64(sc.TraceOptions)) + header := fmt.Sprintf("%s/%s;o=%d", hex.EncodeToString(sc.TraceID[:]), hex.EncodeToString(sc.SpanID[:]), int64(sc.TraceOptions)) req.Header.Set(httpHeader, header) } diff --git a/propagation/http_test.go b/propagation/http_test.go index 9ad93b7..f8d0aa1 100644 --- a/propagation/http_test.go +++ b/propagation/http_test.go @@ -15,6 +15,7 @@ package propagation import ( + "encoding/hex" "net/http" "reflect" "testing" @@ -24,15 +25,20 @@ import ( func TestHTTPFormat(t *testing.T) { format := &HTTPFormat{} - traceID := [16]byte{16, 84, 69, 170, 120, 67, 188, 139, 242, 6, 177, 32, 0, 16, 0, 0} - spanID1 := [8]byte{255, 0, 0, 0, 0, 0, 0, 123} - spanID2 := [8]byte{0, 0, 0, 0, 0, 0, 0, 123} + var traceID [16]byte + var spanID1, spanID2 [8]byte + traceBuf, _ := hex.DecodeString("105445aa7843bc8bf206b12000100000") + spanBuf1, _ := hex.DecodeString("1234567890abcdef") + spanBuf2, _ := hex.DecodeString("1234567812345678") + copy(traceID[:], traceBuf) + copy(spanID1[:], spanBuf1) + copy(spanID2[:], spanBuf2) tests := []struct { incoming string wantSpanContext trace.SpanContext }{ { - incoming: "105445aa7843bc8bf206b12000100000/18374686479671623803;o=1", + incoming: "105445aa7843bc8bf206b12000100000/1234567890abcdef;o=1", wantSpanContext: trace.SpanContext{ TraceID: traceID, SpanID: spanID1, @@ -40,7 +46,7 @@ func TestHTTPFormat(t *testing.T) { }, }, { - incoming: "105445aa7843bc8bf206b12000100000/123;o=0", + incoming: "105445aa7843bc8bf206b12000100000/1234567812345678;o=0", wantSpanContext: trace.SpanContext{ TraceID: traceID, SpanID: spanID2,