Skip to content
This repository was archived by the owner on Oct 3, 2023. It is now read-only.

update SpanID type in http propagation to adhere to trace API V2 spec #254

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 7 additions & 9 deletions propagation/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
package propagation // import "contrib.go.opencensus.io/exporter/stackdriver/propagation"

import (
"encoding/binary"
"encoding/hex"
"fmt"
"net/http"
Expand Down Expand Up @@ -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=") {
Expand All @@ -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)
}
16 changes: 11 additions & 5 deletions propagation/http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package propagation

import (
"encoding/hex"
"net/http"
"reflect"
"testing"
Expand All @@ -24,23 +25,28 @@ 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,
TraceOptions: 1,
},
},
{
incoming: "105445aa7843bc8bf206b12000100000/123;o=0",
incoming: "105445aa7843bc8bf206b12000100000/1234567812345678;o=0",
wantSpanContext: trace.SpanContext{
TraceID: traceID,
SpanID: spanID2,
Expand Down