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

Commit 72e1070

Browse files
committed
Change TraceID from UInt64 to UUID
1 parent 87192ee commit 72e1070

File tree

8 files changed

+50
-25
lines changed

8 files changed

+50
-25
lines changed

agent/agent.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -352,7 +352,7 @@ func NewAgent(options ...Option) (*Agent, error) {
352352

353353
agent.tracer = tracer.NewWithOptions(tracer.Options{
354354
Recorder: recorder,
355-
ShouldSample: func(traceID uint64) bool {
355+
ShouldSample: func(traceID uuid.UUID) bool {
356356
return true
357357
},
358358
MaxLogsPerSpan: 10000,

agent/recorder.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -342,7 +342,7 @@ func (r *SpanRecorder) getPayloadComponents(span tracer.RawSpan) (PayloadSpan, [
342342
}
343343
events = append(events, PayloadEvent{
344344
"context": map[string]interface{}{
345-
"trace_id": fmt.Sprintf("%x", span.Context.TraceID),
345+
"trace_id": span.Context.TraceID.String(),
346346
"span_id": fmt.Sprintf("%x", span.Context.SpanID),
347347
"event_id": eventId.String(),
348348
},

tracer/context.go

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

3+
import "github.com/google/uuid"
4+
35
// SpanContext holds the basic Span metadata.
46
type SpanContext struct {
57
// A probabilistically unique identifier for a [multi-span] trace.
6-
TraceID uint64
8+
TraceID uuid.UUID
79

810
// A probabilistically unique identifier for a span.
911
SpanID uint64

tracer/propagation.go

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

3-
import opentracing "github.com/opentracing/opentracing-go"
3+
import (
4+
"github.com/google/uuid"
5+
opentracing "github.com/opentracing/opentracing-go"
6+
)
47

58
type accessorPropagator struct {
69
tracer *tracerImpl
@@ -10,8 +13,8 @@ type accessorPropagator struct {
1013
// by types which have a means of storing the trace metadata and already know
1114
// how to serialize themselves (for example, protocol buffers).
1215
type DelegatingCarrier interface {
13-
SetState(traceID, spanID uint64, sampled bool)
14-
State() (traceID, spanID uint64, sampled bool)
16+
SetState(traceID uuid.UUID, spanID uint64, sampled bool)
17+
State() (traceID uuid.UUID, spanID uint64, sampled bool)
1518
SetBaggageItem(key, value string)
1619
GetBaggage(func(key, value string))
1720
}

tracer/propagation_env_var.go

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

33
import (
44
"fmt"
5+
"github.com/google/uuid"
56
"github.com/opentracing/opentracing-go"
67
"os"
78
"strconv"
@@ -30,7 +31,7 @@ func (p *envVarPropagator) Inject(
3031
if carrier == nil {
3132
return opentracing.ErrInvalidCarrier
3233
}
33-
carrier.Set(fieldNameTraceID, strconv.FormatUint(sc.TraceID, 16))
34+
carrier.Set(fieldNameTraceID, sc.TraceID.String())
3435
carrier.Set(fieldNameSpanID, strconv.FormatUint(sc.SpanID, 16))
3536
carrier.Set(fieldNameSampled, strconv.FormatBool(sc.Sampled))
3637
for k, v := range sc.Baggage {
@@ -50,14 +51,15 @@ func (p *envVarPropagator) Extract(
5051
return nil, opentracing.ErrInvalidCarrier
5152
}
5253
requiredFieldCount := 0
53-
var traceID, spanID uint64
54+
var traceID uuid.UUID
55+
var spanID uint64
5456
var sampled bool
5557
var err error
5658
decodedBaggage := make(map[string]string)
5759
err = carrier.ForeachKey(func(k, v string) error {
5860
switch strings.ToLower(k) {
5961
case fieldNameTraceID:
60-
traceID, err = strconv.ParseUint(v, 16, 64)
62+
traceID, err = uuid.Parse(v)
6163
if err != nil {
6264
return opentracing.ErrSpanContextCorrupted
6365
}

tracer/propagation_ot.go

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package tracer
33
import (
44
"encoding/binary"
55
"fmt"
6+
"github.com/google/uuid"
67
"io"
78
"strconv"
89
"strings"
@@ -44,19 +45,22 @@ func (p *textMapPropagator) Inject(
4445
if !ok {
4546
return opentracing.ErrInvalidCarrier
4647
}
47-
carrier.Set(fieldNameTraceID, strconv.FormatUint(sc.TraceID, 16))
48+
49+
traceId := strings.ReplaceAll(sc.TraceID.String(), "-", "")
50+
51+
carrier.Set(fieldNameTraceID, traceId)
4852
carrier.Set(fieldNameSpanID, strconv.FormatUint(sc.SpanID, 16))
4953
carrier.Set(fieldNameSampled, strconv.FormatBool(sc.Sampled))
5054

5155
tpSampled := "00"
5256
if sc.Sampled {
5357
tpSampled = "01"
5458
}
55-
traceParentValue := fmt.Sprintf("%v-%032x-%016x-%v",
56-
"00", // Version 0
57-
sc.TraceID, // 8bytes TraceId
58-
sc.SpanID, // 8bytes SpanId
59-
tpSampled, // 00 for not sampled, 01 for sampled
59+
traceParentValue := fmt.Sprintf("%v-%v-%016x-%v",
60+
"00", // Version 0
61+
traceId, // 16bytes TraceId
62+
sc.SpanID, // 8bytes SpanId
63+
tpSampled, // 00 for not sampled, 01 for sampled
6064
)
6165
carrier.Set(traceParentKey, traceParentValue)
6266

@@ -81,14 +85,15 @@ func (p *textMapPropagator) Extract(
8185
return nil, opentracing.ErrInvalidCarrier
8286
}
8387
requiredFieldCount := 0
84-
var traceID, spanID uint64
88+
var traceID uuid.UUID
89+
var spanID uint64
8590
var sampled bool
8691
var err error
8792
decodedBaggage := make(map[string]string)
8893
err = carrier.ForeachKey(func(k, v string) error {
8994
switch strings.ToLower(k) {
9095
case fieldNameTraceID:
91-
traceID, err = strconv.ParseUint(v, 16, 64)
96+
traceID, err = uuid.Parse(v)
9297
if err != nil {
9398
return opentracing.ErrSpanContextCorrupted
9499
}
@@ -111,7 +116,7 @@ func (p *textMapPropagator) Extract(
111116
return opentracing.ErrSpanContextCorrupted
112117
}
113118

114-
traceID, err = strconv.ParseUint(traceParentArray[1][16:], 16, 64)
119+
traceID, err = uuid.Parse(traceParentArray[1])
115120
if err != nil {
116121
return opentracing.ErrSpanContextCorrupted
117122
}
@@ -174,7 +179,8 @@ func (p *binaryPropagator) Inject(
174179
}
175180

176181
state := wire.TracerState{}
177-
state.TraceId = sc.TraceID
182+
bytes, _ := sc.TraceID.MarshalBinary()
183+
state.TraceId = binary.LittleEndian.Uint64(bytes[8:])
178184
state.SpanId = sc.SpanID
179185
state.Sampled = sc.Sampled
180186
state.BaggageItems = sc.Baggage
@@ -223,8 +229,11 @@ func (p *binaryPropagator) Extract(
223229
return nil, opentracing.ErrSpanContextCorrupted
224230
}
225231

232+
traceIdBytes := make([]byte, 16)
233+
binary.LittleEndian.PutUint64(traceIdBytes, ctx.TraceId)
234+
traceID, _ := uuid.FromBytes(traceIdBytes)
226235
return SpanContext{
227-
TraceID: ctx.TraceId,
236+
TraceID: traceID,
228237
SpanID: ctx.SpanId,
229238
Sampled: ctx.Sampled,
230239
Baggage: ctx.BaggageItems,

tracer/tracer.go

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

33
import (
4+
"encoding/binary"
5+
"github.com/google/uuid"
46
"time"
57

68
"github.com/go-errors/errors"
@@ -27,7 +29,7 @@ type Options struct {
2729
// func(traceID uint64) { return traceID % 64 == 0 }
2830
//
2931
// samples every 64th trace on average.
30-
ShouldSample func(traceID uint64) bool
32+
ShouldSample func(traceID uuid.UUID) bool
3133
// TrimUnsampledSpans turns potentially expensive operations on unsampled
3234
// Spans into no-ops. More precisely, tags and log events are silently
3335
// discarded. If NewSpanEventListener is set, the callbacks will still fire.
@@ -100,7 +102,13 @@ type Options struct {
100102
// returned object with a Tracer.
101103
func DefaultOptions() Options {
102104
return Options{
103-
ShouldSample: func(traceID uint64) bool { return traceID%64 == 0 },
105+
ShouldSample: func(traceID uuid.UUID) bool {
106+
bytes, err := traceID.MarshalBinary()
107+
if err != nil {
108+
return false
109+
}
110+
return binary.LittleEndian.Uint64(bytes[8:])%64 == 0
111+
},
104112
MaxLogsPerSpan: 100,
105113
}
106114
}
@@ -195,7 +203,7 @@ ReferencesLoop:
195203
break ReferencesLoop
196204
}
197205
}
198-
if sp.raw.Context.TraceID == 0 {
206+
if sp.raw.Context.TraceID == uuid.Nil {
199207
// No parent Span found; allocate new trace and span ids and determine
200208
// the Sampled status.
201209
sp.raw.Context.TraceID, sp.raw.Context.SpanID = randomID2()

tracer/util.go

Lines changed: 3 additions & 2 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
"math/rand"
56
"sync"
67
"time"
@@ -18,8 +19,8 @@ func randomID() uint64 {
1819
return uint64(seededIDGen.Int63())
1920
}
2021

21-
func randomID2() (uint64, uint64) {
22+
func randomID2() (uuid.UUID, uint64) {
2223
seededIDLock.Lock()
2324
defer seededIDLock.Unlock()
24-
return uint64(seededIDGen.Int63()), uint64(seededIDGen.Int63())
25+
return uuid.New(), uint64(seededIDGen.Int63())
2526
}

0 commit comments

Comments
 (0)