Skip to content
This repository was archived by the owner on Aug 17, 2020. It is now read-only.

Commit 8bb9f59

Browse files
committed
Fix binary propagation
1 parent 72e1070 commit 8bb9f59

File tree

8 files changed

+352
-283
lines changed

8 files changed

+352
-283
lines changed

tracer/api_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package tracer
22

33
import (
4+
"github.com/google/uuid"
45
"testing"
56

67
ot "github.com/opentracing/opentracing-go"
@@ -11,7 +12,7 @@ import (
1112
func newTracer() (tracer ot.Tracer, closer func()) {
1213
tracer = NewWithOptions(Options{
1314
Recorder: NewInMemoryRecorder(),
14-
ShouldSample: func(traceID uint64) bool { return true }, // always sample
15+
ShouldSample: func(traceID uuid.UUID) bool { return true }, // always sample
1516
})
1617
return tracer, nil
1718
}

tracer/bench_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package tracer
33
import (
44
"bytes"
55
"fmt"
6+
"github.com/google/uuid"
67
"net/http"
78
"testing"
89

@@ -80,7 +81,7 @@ func BenchmarkTrimmedSpan_100Events_100Tags_100BaggageItems(b *testing.B) {
8081
var r CountingRecorder
8182
opts := DefaultOptions()
8283
opts.TrimUnsampledSpans = true
83-
opts.ShouldSample = func(_ uint64) bool { return false }
84+
opts.ShouldSample = func(_ uuid.UUID) bool { return false }
8485
opts.Recorder = &r
8586
t := NewWithOptions(opts)
8687
benchmarkWithOpsAndCB(b, func() opentracing.Span {

tracer/propagation_ot.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,8 @@ func (p *binaryPropagator) Inject(
180180

181181
state := wire.TracerState{}
182182
bytes, _ := sc.TraceID.MarshalBinary()
183-
state.TraceId = binary.LittleEndian.Uint64(bytes[8:])
183+
state.TraceIdHi = binary.LittleEndian.Uint64(bytes[:8])
184+
state.TraceIdLo = binary.LittleEndian.Uint64(bytes[8:])
184185
state.SpanId = sc.SpanID
185186
state.Sampled = sc.Sampled
186187
state.BaggageItems = sc.Baggage
@@ -230,7 +231,8 @@ func (p *binaryPropagator) Extract(
230231
}
231232

232233
traceIdBytes := make([]byte, 16)
233-
binary.LittleEndian.PutUint64(traceIdBytes, ctx.TraceId)
234+
binary.LittleEndian.PutUint64(traceIdBytes[:8], ctx.TraceIdHi)
235+
binary.LittleEndian.PutUint64(traceIdBytes[8:], ctx.TraceIdLo)
234236
traceID, _ := uuid.FromBytes(traceIdBytes)
235237
return SpanContext{
236238
TraceID: traceID,

tracer/propagation_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package tracer_test
22

33
import (
44
"bytes"
5+
"github.com/google/uuid"
56
"net/http"
67
"reflect"
78
"testing"
@@ -29,11 +30,11 @@ func (vc *verbatimCarrier) GetBaggage(f func(string, string)) {
2930
}
3031
}
3132

32-
func (vc *verbatimCarrier) SetState(tID, sID uint64, sampled bool) {
33+
func (vc *verbatimCarrier) SetState(tID uuid.UUID, sID uint64, sampled bool) {
3334
vc.SpanContext = tracer.SpanContext{TraceID: tID, SpanID: sID, Sampled: sampled}
3435
}
3536

36-
func (vc *verbatimCarrier) State() (traceID, spanID uint64, sampled bool) {
37+
func (vc *verbatimCarrier) State() (traceID uuid.UUID, spanID uint64, sampled bool) {
3738
return vc.SpanContext.TraceID, vc.SpanContext.SpanID, vc.SpanContext.Sampled
3839
}
3940

tracer/span_test.go

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package tracer
22

33
import (
4+
"github.com/google/uuid"
45
"reflect"
56
"strconv"
67
"testing"
@@ -15,7 +16,7 @@ func TestSpan_Baggage(t *testing.T) {
1516
recorder := NewInMemoryRecorder()
1617
tracer := NewWithOptions(Options{
1718
Recorder: recorder,
18-
ShouldSample: func(traceID uint64) bool { return true }, // always sample
19+
ShouldSample: func(traceID uuid.UUID) bool { return true }, // always sample
1920
})
2021
span := tracer.StartSpan("x")
2122
span.SetBaggageItem("x", "y")
@@ -52,7 +53,7 @@ func TestSpan_Sampling(t *testing.T) {
5253
recorder := NewInMemoryRecorder()
5354
tracer := NewWithOptions(Options{
5455
Recorder: recorder,
55-
ShouldSample: func(traceID uint64) bool { return true },
56+
ShouldSample: func(traceID uuid.UUID) bool { return true },
5657
})
5758
span := tracer.StartSpan("x")
5859
span.Finish()
@@ -66,7 +67,7 @@ func TestSpan_Sampling(t *testing.T) {
6667

6768
tracer = NewWithOptions(Options{
6869
Recorder: recorder,
69-
ShouldSample: func(traceID uint64) bool { return false },
70+
ShouldSample: func(traceID uuid.UUID) bool { return false },
7071
})
7172

7273
recorder.Reset()
@@ -85,7 +86,7 @@ func TestSpan_SingleLoggedTaggedSpan(t *testing.T) {
8586
recorder := NewInMemoryRecorder()
8687
tracer := NewWithOptions(Options{
8788
Recorder: recorder,
88-
ShouldSample: func(traceID uint64) bool { return true }, // always sample
89+
ShouldSample: func(traceID uuid.UUID) bool { return true }, // always sample
8990
})
9091
span := tracer.StartSpan("x")
9192
span.LogEventWithPayload("event", "payload")
@@ -112,7 +113,7 @@ func TestSpan_TrimUnsampledSpans(t *testing.T) {
112113
// Tracer that trims only unsampled but always samples
113114
tracer := NewWithOptions(Options{
114115
Recorder: recorder,
115-
ShouldSample: func(traceID uint64) bool { return true }, // always sample
116+
ShouldSample: func(traceID uuid.UUID) bool { return true }, // always sample
116117
TrimUnsampledSpans: true,
117118
})
118119

@@ -133,7 +134,7 @@ func TestSpan_TrimUnsampledSpans(t *testing.T) {
133134
// Tracer that trims only unsampled and never samples
134135
tracer = NewWithOptions(Options{
135136
Recorder: recorder,
136-
ShouldSample: func(traceID uint64) bool { return false }, // never sample
137+
ShouldSample: func(traceID uuid.UUID) bool { return false }, // never sample
137138
TrimUnsampledSpans: true,
138139
})
139140

@@ -152,7 +153,7 @@ func TestSpan_DropAllLogs(t *testing.T) {
152153
// Tracer that drops logs
153154
tracer := NewWithOptions(Options{
154155
Recorder: recorder,
155-
ShouldSample: func(traceID uint64) bool { return true }, // always sample
156+
ShouldSample: func(traceID uuid.UUID) bool { return true }, // always sample
156157
DropAllLogs: true,
157158
})
158159

@@ -175,7 +176,7 @@ func TestSpan_MaxLogSperSpan(t *testing.T) {
175176
// Tracer that only retains the last <limit> logs.
176177
tracer := NewWithOptions(Options{
177178
Recorder: recorder,
178-
ShouldSample: func(traceID uint64) bool { return true }, // always sample
179+
ShouldSample: func(traceID uuid.UUID) bool { return true }, // always sample
179180
MaxLogsPerSpan: limit,
180181
})
181182

tracer/wire/carrier.go

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,32 @@
11
package wire
22

3+
import (
4+
"encoding/binary"
5+
"github.com/google/uuid"
6+
)
7+
38
// ProtobufCarrier is a DelegatingCarrier that uses protocol buffers as the
49
// the underlying datastructure. The reason for implementing DelagatingCarrier
510
// is to allow for end users to serialize the underlying protocol buffers using
611
// jsonpb or any other serialization forms they want.
712
type ProtobufCarrier TracerState
813

914
// SetState set's the tracer state.
10-
func (p *ProtobufCarrier) SetState(traceID, spanID uint64, sampled bool) {
11-
p.TraceId = traceID
15+
func (p *ProtobufCarrier) SetState(traceID uuid.UUID, spanID uint64, sampled bool) {
16+
bytes, _ := traceID.MarshalBinary()
17+
p.TraceIdHi = binary.LittleEndian.Uint64(bytes[:8])
18+
p.TraceIdLo = binary.LittleEndian.Uint64(bytes[8:])
1219
p.SpanId = spanID
1320
p.Sampled = sampled
1421
}
1522

1623
// State returns the tracer state.
17-
func (p *ProtobufCarrier) State() (traceID, spanID uint64, sampled bool) {
18-
traceID = p.TraceId
24+
func (p *ProtobufCarrier) State() (traceID uuid.UUID, spanID uint64, sampled bool) {
25+
traceIdBytes := make([]byte, 16)
26+
binary.LittleEndian.PutUint64(traceIdBytes[:8], p.TraceIdHi)
27+
binary.LittleEndian.PutUint64(traceIdBytes[8:], p.TraceIdLo)
28+
tId, _ := uuid.FromBytes(traceIdBytes)
29+
traceID = tId
1930
spanID = p.SpanId
2031
sampled = p.Sampled
2132
return traceID, spanID, sampled

0 commit comments

Comments
 (0)