From d9f7eb3a14f74de0532722072cc8c8bbfec51c0c Mon Sep 17 00:00:00 2001 From: Daniel Redondo Date: Wed, 6 May 2020 17:31:06 +0200 Subject: [PATCH 1/3] traceid in 128bits support --- agent/agent.go | 2 +- agent/recorder.go | 4 +- tracer/api_test.go | 3 +- tracer/bench_test.go | 3 +- tracer/context.go | 4 +- tracer/propagation.go | 9 +- tracer/propagation_env_var.go | 9 +- tracer/propagation_ot.go | 17 +- tracer/propagation_test.go | 5 +- tracer/span_test.go | 17 +- tracer/tracer.go | 14 +- tracer/util.go | 30 +- tracer/wire/carrier.go | 12 +- tracer/wire/wire.pb.go | 548 +++++----------------------------- tracer/wire/wire.proto | 9 +- 15 files changed, 174 insertions(+), 512 deletions(-) diff --git a/agent/agent.go b/agent/agent.go index c4425c41..c495b140 100644 --- a/agent/agent.go +++ b/agent/agent.go @@ -385,7 +385,7 @@ func NewAgent(options ...Option) (*Agent, error) { agent.tracer = tracer.NewWithOptions(tracer.Options{ Recorder: recorder, - ShouldSample: func(traceID uint64) bool { + ShouldSample: func(traceID uuid.UUID) bool { return true }, MaxLogsPerSpan: 10000, diff --git a/agent/recorder.go b/agent/recorder.go index e430a9ba..103a37ad 100644 --- a/agent/recorder.go +++ b/agent/recorder.go @@ -324,7 +324,7 @@ func (r *SpanRecorder) getPayloadComponents(span tracer.RawSpan) (PayloadSpan, [ } payloadSpan := PayloadSpan{ "context": map[string]interface{}{ - "trace_id": fmt.Sprintf("%x", span.Context.TraceID), + "trace_id": tracer.UUIDToString(span.Context.TraceID), "span_id": fmt.Sprintf("%x", span.Context.SpanID), "baggage": span.Context.Baggage, }, @@ -345,7 +345,7 @@ func (r *SpanRecorder) getPayloadComponents(span tracer.RawSpan) (PayloadSpan, [ } events = append(events, PayloadEvent{ "context": map[string]interface{}{ - "trace_id": fmt.Sprintf("%x", span.Context.TraceID), + "trace_id": tracer.UUIDToString(span.Context.TraceID), "span_id": fmt.Sprintf("%x", span.Context.SpanID), "event_id": eventId.String(), }, diff --git a/tracer/api_test.go b/tracer/api_test.go index 482e4a1a..a762c6bf 100644 --- a/tracer/api_test.go +++ b/tracer/api_test.go @@ -1,6 +1,7 @@ package tracer import ( + "github.com/google/uuid" "testing" ot "github.com/opentracing/opentracing-go" @@ -11,7 +12,7 @@ import ( func newTracer() (tracer ot.Tracer, closer func()) { tracer = NewWithOptions(Options{ Recorder: NewInMemoryRecorder(), - ShouldSample: func(traceID uint64) bool { return true }, // always sample + ShouldSample: func(traceID uuid.UUID) bool { return true }, // always sample }) return tracer, nil } diff --git a/tracer/bench_test.go b/tracer/bench_test.go index 68b5ffff..dc7bee19 100644 --- a/tracer/bench_test.go +++ b/tracer/bench_test.go @@ -3,6 +3,7 @@ package tracer import ( "bytes" "fmt" + "github.com/google/uuid" "net/http" "testing" @@ -80,7 +81,7 @@ func BenchmarkTrimmedSpan_100Events_100Tags_100BaggageItems(b *testing.B) { var r CountingRecorder opts := DefaultOptions() opts.TrimUnsampledSpans = true - opts.ShouldSample = func(_ uint64) bool { return false } + opts.ShouldSample = func(_ uuid.UUID) bool { return false } opts.Recorder = &r t := NewWithOptions(opts) benchmarkWithOpsAndCB(b, func() opentracing.Span { diff --git a/tracer/context.go b/tracer/context.go index ba1a23c5..5b85f887 100644 --- a/tracer/context.go +++ b/tracer/context.go @@ -1,9 +1,11 @@ package tracer +import "github.com/google/uuid" + // SpanContext holds the basic Span metadata. type SpanContext struct { // A probabilistically unique identifier for a [multi-span] trace. - TraceID uint64 + TraceID uuid.UUID // A probabilistically unique identifier for a span. SpanID uint64 diff --git a/tracer/propagation.go b/tracer/propagation.go index 00d96544..38fac2df 100644 --- a/tracer/propagation.go +++ b/tracer/propagation.go @@ -1,6 +1,9 @@ package tracer -import opentracing "github.com/opentracing/opentracing-go" +import ( + "github.com/google/uuid" + opentracing "github.com/opentracing/opentracing-go" +) type accessorPropagator struct { tracer *tracerImpl @@ -10,8 +13,8 @@ type accessorPropagator struct { // by types which have a means of storing the trace metadata and already know // how to serialize themselves (for example, protocol buffers). type DelegatingCarrier interface { - SetState(traceID, spanID uint64, sampled bool) - State() (traceID, spanID uint64, sampled bool) + SetState(traceID uuid.UUID, spanID uint64, sampled bool) + State() (traceID uuid.UUID, spanID uint64, sampled bool) SetBaggageItem(key, value string) GetBaggage(func(key, value string)) } diff --git a/tracer/propagation_env_var.go b/tracer/propagation_env_var.go index dab74f72..a09a3141 100644 --- a/tracer/propagation_env_var.go +++ b/tracer/propagation_env_var.go @@ -2,6 +2,7 @@ package tracer import ( "fmt" + "github.com/google/uuid" "github.com/opentracing/opentracing-go" "os" "strconv" @@ -30,7 +31,8 @@ func (p *envVarPropagator) Inject( if carrier == nil { return opentracing.ErrInvalidCarrier } - carrier.Set(fieldNameTraceID, strconv.FormatUint(sc.TraceID, 16)) + + carrier.Set(fieldNameTraceID, UUIDToString(sc.TraceID)) carrier.Set(fieldNameSpanID, strconv.FormatUint(sc.SpanID, 16)) carrier.Set(fieldNameSampled, strconv.FormatBool(sc.Sampled)) for k, v := range sc.Baggage { @@ -50,14 +52,15 @@ func (p *envVarPropagator) Extract( return nil, opentracing.ErrInvalidCarrier } requiredFieldCount := 0 - var traceID, spanID uint64 + var traceID uuid.UUID + var spanID uint64 var sampled bool var err error decodedBaggage := make(map[string]string) err = carrier.ForeachKey(func(k, v string) error { switch strings.ToLower(k) { case fieldNameTraceID: - traceID, err = strconv.ParseUint(v, 16, 64) + traceID, err = StringToUUID(v) if err != nil { return opentracing.ErrSpanContextCorrupted } diff --git a/tracer/propagation_ot.go b/tracer/propagation_ot.go index e42b3afe..2028924a 100644 --- a/tracer/propagation_ot.go +++ b/tracer/propagation_ot.go @@ -2,6 +2,7 @@ package tracer import ( "encoding/binary" + "github.com/google/uuid" "io" "strconv" "strings" @@ -40,7 +41,7 @@ func (p *textMapPropagator) Inject( if !ok { return opentracing.ErrInvalidCarrier } - carrier.Set(fieldNameTraceID, strconv.FormatUint(sc.TraceID, 16)) + carrier.Set(fieldNameTraceID, UUIDToString(sc.TraceID)) carrier.Set(fieldNameSpanID, strconv.FormatUint(sc.SpanID, 16)) carrier.Set(fieldNameSampled, strconv.FormatBool(sc.Sampled)) @@ -58,14 +59,15 @@ func (p *textMapPropagator) Extract( return nil, opentracing.ErrInvalidCarrier } requiredFieldCount := 0 - var traceID, spanID uint64 + var traceID uuid.UUID + var spanID uint64 var sampled bool var err error decodedBaggage := make(map[string]string) err = carrier.ForeachKey(func(k, v string) error { switch strings.ToLower(k) { case fieldNameTraceID: - traceID, err = strconv.ParseUint(v, 16, 64) + traceID, err = StringToUUID(v) if err != nil { return opentracing.ErrSpanContextCorrupted } @@ -122,7 +124,8 @@ func (p *binaryPropagator) Inject( } state := wire.TracerState{} - state.TraceId = sc.TraceID + state.TraceIdHi = binary.LittleEndian.Uint64(sc.TraceID[:8]) + state.TraceIdLo = binary.LittleEndian.Uint64(sc.TraceID[8:]) state.SpanId = sc.SpanID state.Sampled = sc.Sampled state.BaggageItems = sc.Baggage @@ -171,8 +174,12 @@ func (p *binaryPropagator) Extract( return nil, opentracing.ErrSpanContextCorrupted } + traceId := uuid.UUID{} + binary.LittleEndian.PutUint64(traceId[:8], ctx.TraceIdHi) + binary.LittleEndian.PutUint64(traceId[8:], ctx.TraceIdLo) + return SpanContext{ - TraceID: ctx.TraceId, + TraceID: traceId, SpanID: ctx.SpanId, Sampled: ctx.Sampled, Baggage: ctx.BaggageItems, diff --git a/tracer/propagation_test.go b/tracer/propagation_test.go index 5b56fc25..b5db12c9 100644 --- a/tracer/propagation_test.go +++ b/tracer/propagation_test.go @@ -8,6 +8,7 @@ import ( "time" "github.com/davecgh/go-spew/spew" + "github.com/google/uuid" opentracing "github.com/opentracing/opentracing-go" "go.undefinedlabs.com/scopeagent/tracer" ) @@ -29,11 +30,11 @@ func (vc *verbatimCarrier) GetBaggage(f func(string, string)) { } } -func (vc *verbatimCarrier) SetState(tID, sID uint64, sampled bool) { +func (vc *verbatimCarrier) SetState(tID uuid.UUID, sID uint64, sampled bool) { vc.SpanContext = tracer.SpanContext{TraceID: tID, SpanID: sID, Sampled: sampled} } -func (vc *verbatimCarrier) State() (traceID, spanID uint64, sampled bool) { +func (vc *verbatimCarrier) State() (traceID uuid.UUID, spanID uint64, sampled bool) { return vc.SpanContext.TraceID, vc.SpanContext.SpanID, vc.SpanContext.Sampled } diff --git a/tracer/span_test.go b/tracer/span_test.go index b3d25d6f..ab8a952d 100644 --- a/tracer/span_test.go +++ b/tracer/span_test.go @@ -1,6 +1,7 @@ package tracer import ( + "github.com/google/uuid" "reflect" "strconv" "testing" @@ -15,7 +16,7 @@ func TestSpan_Baggage(t *testing.T) { recorder := NewInMemoryRecorder() tracer := NewWithOptions(Options{ Recorder: recorder, - ShouldSample: func(traceID uint64) bool { return true }, // always sample + ShouldSample: func(traceID uuid.UUID) bool { return true }, // always sample }) span := tracer.StartSpan("x") span.SetBaggageItem("x", "y") @@ -52,7 +53,7 @@ func TestSpan_Sampling(t *testing.T) { recorder := NewInMemoryRecorder() tracer := NewWithOptions(Options{ Recorder: recorder, - ShouldSample: func(traceID uint64) bool { return true }, + ShouldSample: func(traceID uuid.UUID) bool { return true }, }) span := tracer.StartSpan("x") span.Finish() @@ -66,7 +67,7 @@ func TestSpan_Sampling(t *testing.T) { tracer = NewWithOptions(Options{ Recorder: recorder, - ShouldSample: func(traceID uint64) bool { return false }, + ShouldSample: func(traceID uuid.UUID) bool { return false }, }) recorder.Reset() @@ -85,7 +86,7 @@ func TestSpan_SingleLoggedTaggedSpan(t *testing.T) { recorder := NewInMemoryRecorder() tracer := NewWithOptions(Options{ Recorder: recorder, - ShouldSample: func(traceID uint64) bool { return true }, // always sample + ShouldSample: func(traceID uuid.UUID) bool { return true }, // always sample }) span := tracer.StartSpan("x") span.LogEventWithPayload("event", "payload") @@ -112,7 +113,7 @@ func TestSpan_TrimUnsampledSpans(t *testing.T) { // Tracer that trims only unsampled but always samples tracer := NewWithOptions(Options{ Recorder: recorder, - ShouldSample: func(traceID uint64) bool { return true }, // always sample + ShouldSample: func(traceID uuid.UUID) bool { return true }, // always sample TrimUnsampledSpans: true, }) @@ -133,7 +134,7 @@ func TestSpan_TrimUnsampledSpans(t *testing.T) { // Tracer that trims only unsampled and never samples tracer = NewWithOptions(Options{ Recorder: recorder, - ShouldSample: func(traceID uint64) bool { return false }, // never sample + ShouldSample: func(traceID uuid.UUID) bool { return false }, // never sample TrimUnsampledSpans: true, }) @@ -152,7 +153,7 @@ func TestSpan_DropAllLogs(t *testing.T) { // Tracer that drops logs tracer := NewWithOptions(Options{ Recorder: recorder, - ShouldSample: func(traceID uint64) bool { return true }, // always sample + ShouldSample: func(traceID uuid.UUID) bool { return true }, // always sample DropAllLogs: true, }) @@ -175,7 +176,7 @@ func TestSpan_MaxLogSperSpan(t *testing.T) { // Tracer that only retains the last logs. tracer := NewWithOptions(Options{ Recorder: recorder, - ShouldSample: func(traceID uint64) bool { return true }, // always sample + ShouldSample: func(traceID uuid.UUID) bool { return true }, // always sample MaxLogsPerSpan: limit, }) diff --git a/tracer/tracer.go b/tracer/tracer.go index dcc38cbf..9abb6619 100644 --- a/tracer/tracer.go +++ b/tracer/tracer.go @@ -4,9 +4,12 @@ import ( "time" "github.com/go-errors/errors" + "github.com/google/uuid" "github.com/opentracing/opentracing-go" ) +const emptyUUID = "00000000-0000-0000-0000-000000000000" + // Tracer extends the opentracing.Tracer interface with methods to // probe implementation state, for use by basictracer consumers. type Tracer interface { @@ -24,10 +27,9 @@ type Options struct { // to allow deterministic sampling decisions to be made across different nodes. // For example, // - // func(traceID uint64) { return traceID % 64 == 0 } + // func(traceID uuid.UUID) { return true } // - // samples every 64th trace on average. - ShouldSample func(traceID uint64) bool + ShouldSample func(traceID uuid.UUID) bool // TrimUnsampledSpans turns potentially expensive operations on unsampled // Spans into no-ops. More precisely, tags and log events are silently // discarded. If NewSpanEventListener is set, the callbacks will still fire. @@ -100,7 +102,7 @@ type Options struct { // returned object with a Tracer. func DefaultOptions() Options { return Options{ - ShouldSample: func(traceID uint64) bool { return traceID%64 == 0 }, + ShouldSample: func(traceID uuid.UUID) bool { return true }, MaxLogsPerSpan: 100, } } @@ -195,10 +197,10 @@ ReferencesLoop: break ReferencesLoop } } - if sp.raw.Context.TraceID == 0 { + if sp.raw.Context.TraceID.String() == emptyUUID { // No parent Span found; allocate new trace and span ids and determine // the Sampled status. - sp.raw.Context.TraceID = getRandomId() + sp.raw.Context.TraceID = getRandomUUID() sp.raw.Context.SpanID = getRandomId() sp.raw.Context.Sampled = t.options.ShouldSample(sp.raw.Context.TraceID) } diff --git a/tracer/util.go b/tracer/util.go index 04f5c33e..2015d605 100644 --- a/tracer/util.go +++ b/tracer/util.go @@ -2,12 +2,14 @@ package tracer import ( cryptorand "crypto/rand" + "encoding/hex" "math" "math/big" "math/rand" "sync" "time" + "github.com/google/uuid" "go.undefinedlabs.com/scopeagent/instrumentation" ) @@ -17,17 +19,25 @@ var ( ) func getRandomId() uint64 { + mu.Lock() + defer mu.Unlock() ensureRandom() return random.Uint64() } -func ensureRandom() { +func getRandomUUID() uuid.UUID { mu.Lock() defer mu.Unlock() + ensureRandom() + return uuid.New() +} + +func ensureRandom() { if random == nil { random = rand.New(&safeSource{ source: rand.NewSource(getSeed()), }) + uuid.SetRand(random) } } @@ -75,3 +85,21 @@ func (rs *safeSource) Seed(seed int64) { rs.source.Seed(seed) rs.Unlock() } + +func UUIDToString(uuid uuid.UUID) string { + if val, err := uuid.MarshalBinary(); err != nil { + panic(err) + } else { + return hex.EncodeToString(val) + } +} + +func StringToUUID(val string) (uuid.UUID, error) { + if data, err := hex.DecodeString(val); err != nil { + return uuid.UUID{}, err + } else { + res := uuid.UUID{} + err := res.UnmarshalBinary(data) + return res, err + } +} diff --git a/tracer/wire/carrier.go b/tracer/wire/carrier.go index 12ec98e9..d0f4c151 100644 --- a/tracer/wire/carrier.go +++ b/tracer/wire/carrier.go @@ -7,18 +7,20 @@ package wire type ProtobufCarrier TracerState // SetState set's the tracer state. -func (p *ProtobufCarrier) SetState(traceID, spanID uint64, sampled bool) { - p.TraceId = traceID +func (p *ProtobufCarrier) SetState(traceIDHi, traceIDLo, spanID uint64, sampled bool) { + p.TraceIdHi = traceIDHi + p.TraceIdLo = traceIDLo p.SpanId = spanID p.Sampled = sampled } // State returns the tracer state. -func (p *ProtobufCarrier) State() (traceID, spanID uint64, sampled bool) { - traceID = p.TraceId +func (p *ProtobufCarrier) State() (traceIDHi, traceIDLo, spanID uint64, sampled bool) { + traceIDHi = p.TraceIdHi + traceIDLo = p.TraceIdLo spanID = p.SpanId sampled = p.Sampled - return traceID, spanID, sampled + return traceIDHi, traceIDLo, spanID, sampled } // SetBaggageItem sets a baggage item. diff --git a/tracer/wire/wire.pb.go b/tracer/wire/wire.pb.go index b78d7ea9..70025e28 100644 --- a/tracer/wire/wire.pb.go +++ b/tracer/wire/wire.pb.go @@ -1,23 +1,13 @@ -// Code generated by protoc-gen-gogo. +// Code generated by protoc-gen-gogo. DO NOT EDIT. // source: wire.proto -// DO NOT EDIT! -/* - Package wire is a generated protocol buffer package. - - It is generated from these files: - wire.proto - - It has these top-level messages: - TracerState -*/ package wire -import proto "github.com/gogo/protobuf/proto" -import fmt "fmt" -import math "math" - -import io "io" +import ( + fmt "fmt" + proto "github.com/gogo/protobuf/proto" + math "math" +) // Reference imports to suppress errors if they are not otherwise used. var _ = proto.Marshal @@ -26,483 +16,103 @@ var _ = math.Inf // This is a compile-time assertion to ensure that this generated file // is compatible with the proto package it is being compiled against. -const _ = proto.GoGoProtoPackageIsVersion1 +// A compilation error at this line likely means your copy of the +// proto package needs to be updated. +const _ = proto.GoGoProtoPackageIsVersion3 // please upgrade the proto package type TracerState struct { - TraceId uint64 `protobuf:"fixed64,1,opt,name=trace_id,json=traceId,proto3" json:"trace_id,omitempty"` - SpanId uint64 `protobuf:"fixed64,2,opt,name=span_id,json=spanId,proto3" json:"span_id,omitempty"` - Sampled bool `protobuf:"varint,3,opt,name=sampled,proto3" json:"sampled,omitempty"` - BaggageItems map[string]string `protobuf:"bytes,4,rep,name=baggage_items,json=baggageItems" json:"baggage_items,omitempty" protobuf_key:"bytes,1,opt,name=key,proto3" protobuf_val:"bytes,2,opt,name=value,proto3"` + TraceIdHi uint64 `protobuf:"fixed64,1,opt,name=trace_id_hi,json=traceIdHi,proto3" json:"trace_id_hi,omitempty"` + TraceIdLo uint64 `protobuf:"fixed64,2,opt,name=trace_id_lo,json=traceIdLo,proto3" json:"trace_id_lo,omitempty"` + SpanId uint64 `protobuf:"fixed64,3,opt,name=span_id,json=spanId,proto3" json:"span_id,omitempty"` + Sampled bool `protobuf:"varint,4,opt,name=sampled,proto3" json:"sampled,omitempty"` + BaggageItems map[string]string `protobuf:"bytes,5,rep,name=baggage_items,json=baggageItems,proto3" json:"baggage_items,omitempty" protobuf_key:"bytes,1,opt,name=key,proto3" protobuf_val:"bytes,2,opt,name=value,proto3"` + XXX_NoUnkeyedLiteral struct{} `json:"-"` + XXX_unrecognized []byte `json:"-"` + XXX_sizecache int32 `json:"-"` } -func (m *TracerState) Reset() { *m = TracerState{} } -func (m *TracerState) String() string { return proto.CompactTextString(m) } -func (*TracerState) ProtoMessage() {} -func (*TracerState) Descriptor() ([]byte, []int) { return fileDescriptorWire, []int{0} } - -func (m *TracerState) GetBaggageItems() map[string]string { - if m != nil { - return m.BaggageItems - } - return nil +func (m *TracerState) Reset() { *m = TracerState{} } +func (m *TracerState) String() string { return proto.CompactTextString(m) } +func (*TracerState) ProtoMessage() {} +func (*TracerState) Descriptor() ([]byte, []int) { + return fileDescriptor_f2dcdddcdf68d8e0, []int{0} } - -func init() { - proto.RegisterType((*TracerState)(nil), "basictracer_go.wire.TracerState") +func (m *TracerState) XXX_Unmarshal(b []byte) error { + return xxx_messageInfo_TracerState.Unmarshal(m, b) } -func (m *TracerState) Marshal() (data []byte, err error) { - size := m.Size() - data = make([]byte, size) - n, err := m.MarshalTo(data) - if err != nil { - return nil, err - } - return data[:n], nil +func (m *TracerState) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) { + return xxx_messageInfo_TracerState.Marshal(b, m, deterministic) } - -func (m *TracerState) MarshalTo(data []byte) (int, error) { - var i int - _ = i - var l int - _ = l - if m.TraceId != 0 { - data[i] = 0x9 - i++ - i = encodeFixed64Wire(data, i, uint64(m.TraceId)) - } - if m.SpanId != 0 { - data[i] = 0x11 - i++ - i = encodeFixed64Wire(data, i, uint64(m.SpanId)) - } - if m.Sampled { - data[i] = 0x18 - i++ - if m.Sampled { - data[i] = 1 - } else { - data[i] = 0 - } - i++ - } - if len(m.BaggageItems) > 0 { - for k, _ := range m.BaggageItems { - data[i] = 0x22 - i++ - v := m.BaggageItems[k] - mapSize := 1 + len(k) + sovWire(uint64(len(k))) + 1 + len(v) + sovWire(uint64(len(v))) - i = encodeVarintWire(data, i, uint64(mapSize)) - data[i] = 0xa - i++ - i = encodeVarintWire(data, i, uint64(len(k))) - i += copy(data[i:], k) - data[i] = 0x12 - i++ - i = encodeVarintWire(data, i, uint64(len(v))) - i += copy(data[i:], v) - } - } - return i, nil +func (m *TracerState) XXX_Merge(src proto.Message) { + xxx_messageInfo_TracerState.Merge(m, src) } - -func encodeFixed64Wire(data []byte, offset int, v uint64) int { - data[offset] = uint8(v) - data[offset+1] = uint8(v >> 8) - data[offset+2] = uint8(v >> 16) - data[offset+3] = uint8(v >> 24) - data[offset+4] = uint8(v >> 32) - data[offset+5] = uint8(v >> 40) - data[offset+6] = uint8(v >> 48) - data[offset+7] = uint8(v >> 56) - return offset + 8 +func (m *TracerState) XXX_Size() int { + return xxx_messageInfo_TracerState.Size(m) } -func encodeFixed32Wire(data []byte, offset int, v uint32) int { - data[offset] = uint8(v) - data[offset+1] = uint8(v >> 8) - data[offset+2] = uint8(v >> 16) - data[offset+3] = uint8(v >> 24) - return offset + 4 +func (m *TracerState) XXX_DiscardUnknown() { + xxx_messageInfo_TracerState.DiscardUnknown(m) } -func encodeVarintWire(data []byte, offset int, v uint64) int { - for v >= 1<<7 { - data[offset] = uint8(v&0x7f | 0x80) - v >>= 7 - offset++ + +var xxx_messageInfo_TracerState proto.InternalMessageInfo + +func (m *TracerState) GetTraceIdHi() uint64 { + if m != nil { + return m.TraceIdHi } - data[offset] = uint8(v) - return offset + 1 + return 0 } -func (m *TracerState) Size() (n int) { - var l int - _ = l - if m.TraceId != 0 { - n += 9 - } - if m.SpanId != 0 { - n += 9 - } - if m.Sampled { - n += 2 - } - if len(m.BaggageItems) > 0 { - for k, v := range m.BaggageItems { - _ = k - _ = v - mapEntrySize := 1 + len(k) + sovWire(uint64(len(k))) + 1 + len(v) + sovWire(uint64(len(v))) - n += mapEntrySize + 1 + sovWire(uint64(mapEntrySize)) - } + +func (m *TracerState) GetTraceIdLo() uint64 { + if m != nil { + return m.TraceIdLo } - return n + return 0 } -func sovWire(x uint64) (n int) { - for { - n++ - x >>= 7 - if x == 0 { - break - } +func (m *TracerState) GetSpanId() uint64 { + if m != nil { + return m.SpanId } - return n + return 0 } -func sozWire(x uint64) (n int) { - return sovWire(uint64((x << 1) ^ uint64((int64(x) >> 63)))) -} -func (m *TracerState) Unmarshal(data []byte) error { - l := len(data) - iNdEx := 0 - for iNdEx < l { - preIndex := iNdEx - var wire uint64 - for shift := uint(0); ; shift += 7 { - if shift >= 64 { - return ErrIntOverflowWire - } - if iNdEx >= l { - return io.ErrUnexpectedEOF - } - b := data[iNdEx] - iNdEx++ - wire |= (uint64(b) & 0x7F) << shift - if b < 0x80 { - break - } - } - fieldNum := int32(wire >> 3) - wireType := int(wire & 0x7) - if wireType == 4 { - return fmt.Errorf("proto: TracerState: wiretype end group for non-group") - } - if fieldNum <= 0 { - return fmt.Errorf("proto: TracerState: illegal tag %d (wire type %d)", fieldNum, wire) - } - switch fieldNum { - case 1: - if wireType != 1 { - return fmt.Errorf("proto: wrong wireType = %d for field TraceId", wireType) - } - m.TraceId = 0 - if (iNdEx + 8) > l { - return io.ErrUnexpectedEOF - } - iNdEx += 8 - m.TraceId = uint64(data[iNdEx-8]) - m.TraceId |= uint64(data[iNdEx-7]) << 8 - m.TraceId |= uint64(data[iNdEx-6]) << 16 - m.TraceId |= uint64(data[iNdEx-5]) << 24 - m.TraceId |= uint64(data[iNdEx-4]) << 32 - m.TraceId |= uint64(data[iNdEx-3]) << 40 - m.TraceId |= uint64(data[iNdEx-2]) << 48 - m.TraceId |= uint64(data[iNdEx-1]) << 56 - case 2: - if wireType != 1 { - return fmt.Errorf("proto: wrong wireType = %d for field SpanId", wireType) - } - m.SpanId = 0 - if (iNdEx + 8) > l { - return io.ErrUnexpectedEOF - } - iNdEx += 8 - m.SpanId = uint64(data[iNdEx-8]) - m.SpanId |= uint64(data[iNdEx-7]) << 8 - m.SpanId |= uint64(data[iNdEx-6]) << 16 - m.SpanId |= uint64(data[iNdEx-5]) << 24 - m.SpanId |= uint64(data[iNdEx-4]) << 32 - m.SpanId |= uint64(data[iNdEx-3]) << 40 - m.SpanId |= uint64(data[iNdEx-2]) << 48 - m.SpanId |= uint64(data[iNdEx-1]) << 56 - case 3: - if wireType != 0 { - return fmt.Errorf("proto: wrong wireType = %d for field Sampled", wireType) - } - var v int - for shift := uint(0); ; shift += 7 { - if shift >= 64 { - return ErrIntOverflowWire - } - if iNdEx >= l { - return io.ErrUnexpectedEOF - } - b := data[iNdEx] - iNdEx++ - v |= (int(b) & 0x7F) << shift - if b < 0x80 { - break - } - } - m.Sampled = bool(v != 0) - case 4: - if wireType != 2 { - return fmt.Errorf("proto: wrong wireType = %d for field BaggageItems", wireType) - } - var msglen int - for shift := uint(0); ; shift += 7 { - if shift >= 64 { - return ErrIntOverflowWire - } - if iNdEx >= l { - return io.ErrUnexpectedEOF - } - b := data[iNdEx] - iNdEx++ - msglen |= (int(b) & 0x7F) << shift - if b < 0x80 { - break - } - } - if msglen < 0 { - return ErrInvalidLengthWire - } - postIndex := iNdEx + msglen - if postIndex > l { - return io.ErrUnexpectedEOF - } - var keykey uint64 - for shift := uint(0); ; shift += 7 { - if shift >= 64 { - return ErrIntOverflowWire - } - if iNdEx >= l { - return io.ErrUnexpectedEOF - } - b := data[iNdEx] - iNdEx++ - keykey |= (uint64(b) & 0x7F) << shift - if b < 0x80 { - break - } - } - var stringLenmapkey uint64 - for shift := uint(0); ; shift += 7 { - if shift >= 64 { - return ErrIntOverflowWire - } - if iNdEx >= l { - return io.ErrUnexpectedEOF - } - b := data[iNdEx] - iNdEx++ - stringLenmapkey |= (uint64(b) & 0x7F) << shift - if b < 0x80 { - break - } - } - intStringLenmapkey := int(stringLenmapkey) - if intStringLenmapkey < 0 { - return ErrInvalidLengthWire - } - postStringIndexmapkey := iNdEx + intStringLenmapkey - if postStringIndexmapkey > l { - return io.ErrUnexpectedEOF - } - mapkey := string(data[iNdEx:postStringIndexmapkey]) - iNdEx = postStringIndexmapkey - var valuekey uint64 - for shift := uint(0); ; shift += 7 { - if shift >= 64 { - return ErrIntOverflowWire - } - if iNdEx >= l { - return io.ErrUnexpectedEOF - } - b := data[iNdEx] - iNdEx++ - valuekey |= (uint64(b) & 0x7F) << shift - if b < 0x80 { - break - } - } - var stringLenmapvalue uint64 - for shift := uint(0); ; shift += 7 { - if shift >= 64 { - return ErrIntOverflowWire - } - if iNdEx >= l { - return io.ErrUnexpectedEOF - } - b := data[iNdEx] - iNdEx++ - stringLenmapvalue |= (uint64(b) & 0x7F) << shift - if b < 0x80 { - break - } - } - intStringLenmapvalue := int(stringLenmapvalue) - if intStringLenmapvalue < 0 { - return ErrInvalidLengthWire - } - postStringIndexmapvalue := iNdEx + intStringLenmapvalue - if postStringIndexmapvalue > l { - return io.ErrUnexpectedEOF - } - mapvalue := string(data[iNdEx:postStringIndexmapvalue]) - iNdEx = postStringIndexmapvalue - if m.BaggageItems == nil { - m.BaggageItems = make(map[string]string) - } - m.BaggageItems[mapkey] = mapvalue - iNdEx = postIndex - default: - iNdEx = preIndex - skippy, err := skipWire(data[iNdEx:]) - if err != nil { - return err - } - if skippy < 0 { - return ErrInvalidLengthWire - } - if (iNdEx + skippy) > l { - return io.ErrUnexpectedEOF - } - iNdEx += skippy - } + +func (m *TracerState) GetSampled() bool { + if m != nil { + return m.Sampled } + return false +} - if iNdEx > l { - return io.ErrUnexpectedEOF +func (m *TracerState) GetBaggageItems() map[string]string { + if m != nil { + return m.BaggageItems } return nil } -func skipWire(data []byte) (n int, err error) { - l := len(data) - iNdEx := 0 - for iNdEx < l { - var wire uint64 - for shift := uint(0); ; shift += 7 { - if shift >= 64 { - return 0, ErrIntOverflowWire - } - if iNdEx >= l { - return 0, io.ErrUnexpectedEOF - } - b := data[iNdEx] - iNdEx++ - wire |= (uint64(b) & 0x7F) << shift - if b < 0x80 { - break - } - } - wireType := int(wire & 0x7) - switch wireType { - case 0: - for shift := uint(0); ; shift += 7 { - if shift >= 64 { - return 0, ErrIntOverflowWire - } - if iNdEx >= l { - return 0, io.ErrUnexpectedEOF - } - iNdEx++ - if data[iNdEx-1] < 0x80 { - break - } - } - return iNdEx, nil - case 1: - iNdEx += 8 - return iNdEx, nil - case 2: - var length int - for shift := uint(0); ; shift += 7 { - if shift >= 64 { - return 0, ErrIntOverflowWire - } - if iNdEx >= l { - return 0, io.ErrUnexpectedEOF - } - b := data[iNdEx] - iNdEx++ - length |= (int(b) & 0x7F) << shift - if b < 0x80 { - break - } - } - iNdEx += length - if length < 0 { - return 0, ErrInvalidLengthWire - } - return iNdEx, nil - case 3: - for { - var innerWire uint64 - var start int = iNdEx - for shift := uint(0); ; shift += 7 { - if shift >= 64 { - return 0, ErrIntOverflowWire - } - if iNdEx >= l { - return 0, io.ErrUnexpectedEOF - } - b := data[iNdEx] - iNdEx++ - innerWire |= (uint64(b) & 0x7F) << shift - if b < 0x80 { - break - } - } - innerWireType := int(innerWire & 0x7) - if innerWireType == 4 { - break - } - next, err := skipWire(data[start:]) - if err != nil { - return 0, err - } - iNdEx = start + next - } - return iNdEx, nil - case 4: - return iNdEx, nil - case 5: - iNdEx += 4 - return iNdEx, nil - default: - return 0, fmt.Errorf("proto: illegal wireType %d", wireType) - } - } - panic("unreachable") + +func init() { + proto.RegisterType((*TracerState)(nil), "basictracer_go.wire.TracerState") + proto.RegisterMapType((map[string]string)(nil), "basictracer_go.wire.TracerState.BaggageItemsEntry") } -var ( - ErrInvalidLengthWire = fmt.Errorf("proto: negative length found during unmarshaling") - ErrIntOverflowWire = fmt.Errorf("proto: integer overflow") -) +func init() { proto.RegisterFile("wire.proto", fileDescriptor_f2dcdddcdf68d8e0) } -var fileDescriptorWire = []byte{ - // 234 bytes of a gzipped FileDescriptorProto - 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x09, 0x6e, 0x88, 0x02, 0xff, 0xe2, 0xe2, 0x2a, 0xcf, 0x2c, 0x4a, +var fileDescriptor_f2dcdddcdf68d8e0 = []byte{ + // 242 bytes of a gzipped FileDescriptorProto + 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xe2, 0xe2, 0x2a, 0xcf, 0x2c, 0x4a, 0xd5, 0x2b, 0x28, 0xca, 0x2f, 0xc9, 0x17, 0x12, 0x4e, 0x4a, 0x2c, 0xce, 0x4c, 0x2e, 0x29, 0x4a, - 0x4c, 0x4e, 0x2d, 0x8a, 0x4f, 0xcf, 0xd7, 0x03, 0x49, 0x29, 0x7d, 0x65, 0xe4, 0xe2, 0x0e, 0x01, - 0x0b, 0x05, 0x97, 0x24, 0x96, 0xa4, 0x0a, 0x49, 0x72, 0x71, 0x80, 0x55, 0xc4, 0x67, 0xa6, 0x48, - 0x30, 0x2a, 0x30, 0x6a, 0xb0, 0x05, 0xb1, 0x83, 0xf9, 0x9e, 0x29, 0x42, 0xe2, 0x5c, 0xec, 0xc5, - 0x05, 0x89, 0x79, 0x20, 0x19, 0x26, 0xb0, 0x0c, 0x1b, 0x88, 0x0b, 0x94, 0x90, 0x00, 0x4a, 0x24, - 0xe6, 0x16, 0xe4, 0xa4, 0xa6, 0x48, 0x30, 0x03, 0x25, 0x38, 0x82, 0x60, 0x5c, 0xa1, 0x70, 0x2e, - 0xde, 0xa4, 0xc4, 0xf4, 0xf4, 0xc4, 0x74, 0xa0, 0x79, 0x25, 0xa9, 0xb9, 0xc5, 0x12, 0x2c, 0x0a, - 0xcc, 0x1a, 0xdc, 0x46, 0x46, 0x7a, 0x58, 0x9c, 0xa2, 0x87, 0xe4, 0x0c, 0x3d, 0x27, 0x88, 0x2e, - 0x4f, 0x90, 0x26, 0xd7, 0xbc, 0x92, 0xa2, 0xca, 0x20, 0x9e, 0x24, 0x24, 0x21, 0x29, 0x7b, 0x2e, - 0x41, 0x0c, 0x25, 0x42, 0x02, 0x5c, 0xcc, 0xd9, 0xa9, 0x95, 0x60, 0x67, 0x73, 0x06, 0x81, 0x98, - 0x42, 0x22, 0x5c, 0xac, 0x65, 0x89, 0x39, 0xa5, 0xa9, 0x60, 0x07, 0x73, 0x06, 0x41, 0x38, 0x56, - 0x4c, 0x16, 0x8c, 0x4e, 0x62, 0x27, 0x1e, 0xc9, 0x31, 0x5e, 0x00, 0xe2, 0x07, 0x40, 0x3c, 0xe1, - 0xb1, 0x1c, 0x43, 0x14, 0x0b, 0xc8, 0x11, 0x49, 0x6c, 0xe0, 0xb0, 0x32, 0x06, 0x04, 0x00, 0x00, - 0xff, 0xff, 0x0a, 0x20, 0x89, 0x38, 0x39, 0x01, 0x00, 0x00, + 0x4c, 0x4e, 0x2d, 0x8a, 0x4f, 0xcf, 0xd7, 0x03, 0x49, 0x29, 0xcd, 0x62, 0xe2, 0xe2, 0x0e, 0x01, + 0x0b, 0x05, 0x97, 0x24, 0x96, 0xa4, 0x0a, 0xc9, 0x71, 0x71, 0x83, 0x55, 0xc4, 0x67, 0xa6, 0xc4, + 0x67, 0x64, 0x4a, 0x30, 0x2a, 0x30, 0x6a, 0xb0, 0x05, 0x71, 0x82, 0x85, 0x3c, 0x53, 0x3c, 0x32, + 0x51, 0xe4, 0x73, 0xf2, 0x25, 0x98, 0x50, 0xe4, 0x7d, 0xf2, 0x85, 0xc4, 0xb9, 0xd8, 0x8b, 0x0b, + 0x12, 0xf3, 0xe2, 0x33, 0x53, 0x24, 0x98, 0xc1, 0x72, 0x6c, 0x20, 0xae, 0x67, 0x8a, 0x90, 0x04, + 0x17, 0x7b, 0x71, 0x62, 0x6e, 0x41, 0x4e, 0x6a, 0x8a, 0x04, 0x8b, 0x02, 0xa3, 0x06, 0x47, 0x10, + 0x8c, 0x2b, 0x14, 0xce, 0xc5, 0x9b, 0x94, 0x98, 0x9e, 0x9e, 0x98, 0x9e, 0x1a, 0x9f, 0x59, 0x92, + 0x9a, 0x5b, 0x2c, 0xc1, 0xaa, 0xc0, 0xac, 0xc1, 0x6d, 0x64, 0xa4, 0x87, 0xc5, 0xbd, 0x7a, 0x48, + 0x6e, 0xd5, 0x73, 0x82, 0xe8, 0xf2, 0x04, 0x69, 0x72, 0xcd, 0x2b, 0x29, 0xaa, 0x0c, 0xe2, 0x49, + 0x42, 0x12, 0x92, 0xb2, 0xe7, 0x12, 0xc4, 0x50, 0x22, 0x24, 0xc0, 0xc5, 0x9c, 0x9d, 0x5a, 0x09, + 0xf6, 0x18, 0x67, 0x10, 0x88, 0x29, 0x24, 0xc2, 0xc5, 0x5a, 0x96, 0x98, 0x53, 0x9a, 0x0a, 0xf6, + 0x0c, 0x67, 0x10, 0x84, 0x63, 0xc5, 0x64, 0xc1, 0xe8, 0xc4, 0x16, 0xc5, 0x02, 0xb2, 0x34, 0x89, + 0x0d, 0x1c, 0x80, 0xc6, 0x80, 0x00, 0x00, 0x00, 0xff, 0xff, 0xcb, 0xda, 0x54, 0x8b, 0x4e, 0x01, + 0x00, 0x00, } diff --git a/tracer/wire/wire.proto b/tracer/wire/wire.proto index 7f73a1f1..d736fdb6 100644 --- a/tracer/wire/wire.proto +++ b/tracer/wire/wire.proto @@ -3,8 +3,9 @@ package basictracer_go.wire; option go_package = "wire"; message TracerState { - fixed64 trace_id = 1; - fixed64 span_id = 2; - bool sampled = 3; - map baggage_items = 4; + fixed64 trace_id_hi = 1; + fixed64 trace_id_lo = 2; + fixed64 span_id = 3; + bool sampled = 4; + map baggage_items = 5; } From df0281dc332ef16940e75a72b83c7b94ca79f9c2 Mon Sep 17 00:00:00 2001 From: Daniel Redondo Date: Wed, 6 May 2020 17:41:36 +0200 Subject: [PATCH 2/3] import formatting --- tracer/api_test.go | 2 +- tracer/bench_test.go | 2 +- tracer/propagation_env_var.go | 5 +++-- tracer/propagation_ot.go | 2 +- tracer/span_test.go | 2 +- 5 files changed, 7 insertions(+), 6 deletions(-) diff --git a/tracer/api_test.go b/tracer/api_test.go index a762c6bf..6431042f 100644 --- a/tracer/api_test.go +++ b/tracer/api_test.go @@ -1,9 +1,9 @@ package tracer import ( - "github.com/google/uuid" "testing" + "github.com/google/uuid" ot "github.com/opentracing/opentracing-go" "github.com/opentracing/opentracing-go/harness" ) diff --git a/tracer/bench_test.go b/tracer/bench_test.go index dc7bee19..d726f012 100644 --- a/tracer/bench_test.go +++ b/tracer/bench_test.go @@ -3,10 +3,10 @@ package tracer import ( "bytes" "fmt" - "github.com/google/uuid" "net/http" "testing" + "github.com/google/uuid" "github.com/opentracing/opentracing-go" ) diff --git a/tracer/propagation_env_var.go b/tracer/propagation_env_var.go index a09a3141..40519c71 100644 --- a/tracer/propagation_env_var.go +++ b/tracer/propagation_env_var.go @@ -2,11 +2,12 @@ package tracer import ( "fmt" - "github.com/google/uuid" - "github.com/opentracing/opentracing-go" "os" "strconv" "strings" + + "github.com/google/uuid" + "github.com/opentracing/opentracing-go" ) const ( diff --git a/tracer/propagation_ot.go b/tracer/propagation_ot.go index 2028924a..f20e254a 100644 --- a/tracer/propagation_ot.go +++ b/tracer/propagation_ot.go @@ -2,12 +2,12 @@ package tracer import ( "encoding/binary" - "github.com/google/uuid" "io" "strconv" "strings" "github.com/gogo/protobuf/proto" + "github.com/google/uuid" opentracing "github.com/opentracing/opentracing-go" "go.undefinedlabs.com/scopeagent/tracer/wire" ) diff --git a/tracer/span_test.go b/tracer/span_test.go index ab8a952d..7ee608f1 100644 --- a/tracer/span_test.go +++ b/tracer/span_test.go @@ -1,11 +1,11 @@ package tracer import ( - "github.com/google/uuid" "reflect" "strconv" "testing" + "github.com/google/uuid" opentracing "github.com/opentracing/opentracing-go" "github.com/opentracing/opentracing-go/ext" "github.com/opentracing/opentracing-go/log" From b4c9fb5feeaf81b3754a7c15c61620d7a1dbfae5 Mon Sep 17 00:00:00 2001 From: Daniel Redondo Date: Thu, 7 May 2020 11:11:47 +0200 Subject: [PATCH 3/3] requested changes --- tracer/util.go | 10 ++-------- tracer/util_test.go | 20 ++++++++++++++++++++ 2 files changed, 22 insertions(+), 8 deletions(-) create mode 100644 tracer/util_test.go diff --git a/tracer/util.go b/tracer/util.go index 2015d605..530bebfe 100644 --- a/tracer/util.go +++ b/tracer/util.go @@ -87,19 +87,13 @@ func (rs *safeSource) Seed(seed int64) { } func UUIDToString(uuid uuid.UUID) string { - if val, err := uuid.MarshalBinary(); err != nil { - panic(err) - } else { - return hex.EncodeToString(val) - } + return hex.EncodeToString(uuid[:]) } func StringToUUID(val string) (uuid.UUID, error) { if data, err := hex.DecodeString(val); err != nil { return uuid.UUID{}, err } else { - res := uuid.UUID{} - err := res.UnmarshalBinary(data) - return res, err + return uuid.FromBytes(data) } } diff --git a/tracer/util_test.go b/tracer/util_test.go new file mode 100644 index 00000000..11945ce3 --- /dev/null +++ b/tracer/util_test.go @@ -0,0 +1,20 @@ +package tracer + +import ( + "testing" + + "github.com/google/uuid" + "github.com/stretchr/testify/assert" +) + +func TestUUIDStringConverter(t *testing.T) { + for i := 0; i < 10000; i++ { + id := uuid.New() + val := UUIDToString(id) + idRes, err := StringToUUID(val) + if err != nil { + t.Fatal(err) + } + assert.Equal(t, id.String(), idRes.String()) + } +}