diff --git a/agent/agent.go b/agent/agent.go index ae75c340..86a2e44a 100644 --- a/agent/agent.go +++ b/agent/agent.go @@ -352,7 +352,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 400e085a..54eab15d 100644 --- a/agent/recorder.go +++ b/agent/recorder.go @@ -342,7 +342,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": span.Context.TraceID.String(), "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 c9c7d57b..56c2c9fc 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..65c1c94d 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,7 @@ func (p *envVarPropagator) Inject( if carrier == nil { return opentracing.ErrInvalidCarrier } - carrier.Set(fieldNameTraceID, strconv.FormatUint(sc.TraceID, 16)) + carrier.Set(fieldNameTraceID, sc.TraceID.String()) carrier.Set(fieldNameSpanID, strconv.FormatUint(sc.SpanID, 16)) carrier.Set(fieldNameSampled, strconv.FormatBool(sc.Sampled)) for k, v := range sc.Baggage { @@ -50,14 +51,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 = uuid.Parse(v) if err != nil { return opentracing.ErrSpanContextCorrupted } diff --git a/tracer/propagation_ot.go b/tracer/propagation_ot.go index e42b3afe..e797b8d4 100644 --- a/tracer/propagation_ot.go +++ b/tracer/propagation_ot.go @@ -2,6 +2,8 @@ package tracer import ( "encoding/binary" + "fmt" + "github.com/google/uuid" "io" "strconv" "strings" @@ -26,6 +28,8 @@ const ( fieldNameTraceID = prefixTracerState + "traceid" fieldNameSpanID = prefixTracerState + "spanid" fieldNameSampled = prefixTracerState + "sampled" + + traceParentKey = "traceparent" ) func (p *textMapPropagator) Inject( @@ -40,13 +44,29 @@ func (p *textMapPropagator) Inject( if !ok { return opentracing.ErrInvalidCarrier } - carrier.Set(fieldNameTraceID, strconv.FormatUint(sc.TraceID, 16)) + + traceId := strings.Replace(sc.TraceID.String(), "-", "", -1) + + carrier.Set(fieldNameTraceID, traceId) carrier.Set(fieldNameSpanID, strconv.FormatUint(sc.SpanID, 16)) carrier.Set(fieldNameSampled, strconv.FormatBool(sc.Sampled)) + tpSampled := "00" + if sc.Sampled { + tpSampled = "01" + } + traceParentValue := fmt.Sprintf("%v-%v-%016x-%v", + "00", // Version 0 + traceId, // 16bytes TraceId + sc.SpanID, // 8bytes SpanId + tpSampled, // 00 for not sampled, 01 for sampled + ) + carrier.Set(traceParentKey, traceParentValue) + for k, v := range sc.Baggage { carrier.Set(prefixBaggage+k, v) } + return nil } @@ -58,14 +78,46 @@ 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 traceParentKey: + if len(v) < 55 { + return opentracing.ErrSpanContextCorrupted + } + traceParentArray := strings.Split(v, "-") + if len(traceParentArray) < 4 || traceParentArray[0] != "00" || len(traceParentArray[1]) != 32 || len(traceParentArray[2]) != 16 { + return opentracing.ErrSpanContextCorrupted + } + + traceID, err = uuid.Parse(traceParentArray[1]) + if err != nil { + return opentracing.ErrSpanContextCorrupted + } + spanID, err = strconv.ParseUint(traceParentArray[2], 16, 64) + if err != nil { + return opentracing.ErrSpanContextCorrupted + } + if traceParentArray[3] == "01" { + sampled = true + } + requiredFieldCount = requiredFieldCount + 3 + default: + // Balance off the requiredFieldCount++ just below... + requiredFieldCount-- + } + requiredFieldCount++ + return nil + }) err = carrier.ForeachKey(func(k, v string) error { switch strings.ToLower(k) { case fieldNameTraceID: - traceID, err = strconv.ParseUint(v, 16, 64) + traceID, err = uuid.Parse(v) if err != nil { return opentracing.ErrSpanContextCorrupted } @@ -93,6 +145,7 @@ func (p *textMapPropagator) Extract( if err != nil { return nil, err } + if requiredFieldCount < tracerStateFieldCount { if requiredFieldCount == 0 { return nil, opentracing.ErrSpanContextNotFound @@ -122,7 +175,9 @@ func (p *binaryPropagator) Inject( } state := wire.TracerState{} - state.TraceId = sc.TraceID + bytes, _ := sc.TraceID.MarshalBinary() + state.TraceIdHi = binary.BigEndian.Uint64(bytes[:8]) + state.TraceIdLo = binary.BigEndian.Uint64(bytes[8:]) state.SpanId = sc.SpanID state.Sampled = sc.Sampled state.BaggageItems = sc.Baggage @@ -171,8 +226,12 @@ func (p *binaryPropagator) Extract( return nil, opentracing.ErrSpanContextCorrupted } + traceIdBytes := make([]byte, 16) + binary.BigEndian.PutUint64(traceIdBytes[:8], ctx.TraceIdHi) + binary.BigEndian.PutUint64(traceIdBytes[8:], ctx.TraceIdLo) + traceID, _ := uuid.FromBytes(traceIdBytes) 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..72334914 100644 --- a/tracer/propagation_test.go +++ b/tracer/propagation_test.go @@ -2,6 +2,7 @@ package tracer_test import ( "bytes" + "github.com/google/uuid" "net/http" "reflect" "testing" @@ -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 a6c05c7e..7986f111 100644 --- a/tracer/tracer.go +++ b/tracer/tracer.go @@ -1,6 +1,8 @@ package tracer import ( + "encoding/binary" + "github.com/google/uuid" "time" "github.com/go-errors/errors" @@ -27,7 +29,7 @@ type Options struct { // func(traceID uint64) { return traceID % 64 == 0 } // // 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,13 @@ 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 { + bytes, err := traceID.MarshalBinary() + if err != nil { + return false + } + return binary.LittleEndian.Uint64(bytes[8:])%64 == 0 + }, MaxLogsPerSpan: 100, } } @@ -195,7 +203,7 @@ ReferencesLoop: break ReferencesLoop } } - if sp.raw.Context.TraceID == 0 { + if sp.raw.Context.TraceID == uuid.Nil { // No parent Span found; allocate new trace and span ids and determine // the Sampled status. sp.raw.Context.TraceID, sp.raw.Context.SpanID = randomID2() diff --git a/tracer/util.go b/tracer/util.go index 06490ed5..a109f7cb 100644 --- a/tracer/util.go +++ b/tracer/util.go @@ -1,6 +1,7 @@ package tracer import ( + "github.com/google/uuid" "math/rand" "sync" "time" @@ -18,8 +19,12 @@ func randomID() uint64 { return uint64(seededIDGen.Int63()) } -func randomID2() (uint64, uint64) { +func randomID2() (uuid.UUID, uint64) { seededIDLock.Lock() defer seededIDLock.Unlock() - return uint64(seededIDGen.Int63()), uint64(seededIDGen.Int63()) + rndBytes := make([]byte, 8) + seededIDGen.Read(rndBytes) + uuidBytes := append(make([]byte, 8), rndBytes...) + tid, _ := uuid.FromBytes(uuidBytes) + return tid, uint64(seededIDGen.Int63()) } diff --git a/tracer/wire/carrier.go b/tracer/wire/carrier.go index 12ec98e9..59eae569 100644 --- a/tracer/wire/carrier.go +++ b/tracer/wire/carrier.go @@ -1,5 +1,10 @@ package wire +import ( + "encoding/binary" + "github.com/google/uuid" +) + // ProtobufCarrier is a DelegatingCarrier that uses protocol buffers as the // the underlying datastructure. The reason for implementing DelagatingCarrier // is to allow for end users to serialize the underlying protocol buffers using @@ -7,15 +12,21 @@ 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(traceID uuid.UUID, spanID uint64, sampled bool) { + bytes, _ := traceID.MarshalBinary() + p.TraceIdHi = binary.LittleEndian.Uint64(bytes[:8]) + p.TraceIdLo = binary.LittleEndian.Uint64(bytes[8:]) 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() (traceID uuid.UUID, spanID uint64, sampled bool) { + traceIdBytes := make([]byte, 16) + binary.LittleEndian.PutUint64(traceIdBytes[:8], p.TraceIdHi) + binary.LittleEndian.PutUint64(traceIdBytes[8:], p.TraceIdLo) + tId, _ := uuid.FromBytes(traceIdBytes) + traceID = tId spanID = p.SpanId sampled = p.Sampled return traceID, spanID, sampled diff --git a/tracer/wire/wire.pb.go b/tracer/wire/wire.pb.go index b78d7ea9..03f40dc4 100644 --- a/tracer/wire/wire.pb.go +++ b/tracer/wire/wire.pb.go @@ -1,23 +1,16 @@ -// 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 ( + encoding_binary "encoding/binary" + fmt "fmt" + proto "github.com/gogo/protobuf/proto" + io "io" + math "math" + math_bits "math/bits" +) // Reference imports to suppress errors if they are not otherwise used. var _ = proto.Marshal @@ -26,19 +19,78 @@ 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"` +} + +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 (m *TracerState) XXX_Unmarshal(b []byte) error { + return m.Unmarshal(b) +} +func (m *TracerState) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) { + if deterministic { + return xxx_messageInfo_TracerState.Marshal(b, m, deterministic) + } else { + b = b[:cap(b)] + n, err := m.MarshalToSizedBuffer(b) + if err != nil { + return nil, err + } + return b[:n], nil + } +} +func (m *TracerState) XXX_Merge(src proto.Message) { + xxx_messageInfo_TracerState.Merge(m, src) +} +func (m *TracerState) XXX_Size() int { + return m.Size() +} +func (m *TracerState) XXX_DiscardUnknown() { + xxx_messageInfo_TracerState.DiscardUnknown(m) +} + +var xxx_messageInfo_TracerState proto.InternalMessageInfo + +func (m *TracerState) GetTraceIdHi() uint64 { + if m != nil { + return m.TraceIdHi + } + return 0 +} + +func (m *TracerState) GetTraceIdLo() uint64 { + if m != nil { + return m.TraceIdLo + } + return 0 } -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) GetSpanId() uint64 { + if m != nil { + return m.SpanId + } + return 0 +} + +func (m *TracerState) GetSampled() bool { + if m != nil { + return m.Sampled + } + return false +} func (m *TracerState) GetBaggageItems() map[string]string { if m != nil { @@ -49,93 +101,123 @@ func (m *TracerState) GetBaggageItems() map[string]string { func init() { proto.RegisterType((*TracerState)(nil), "basictracer_go.wire.TracerState") + proto.RegisterMapType((map[string]string)(nil), "basictracer_go.wire.TracerState.BaggageItemsEntry") +} + +func init() { proto.RegisterFile("wire.proto", fileDescriptor_f2dcdddcdf68d8e0) } + +var fileDescriptor_f2dcdddcdf68d8e0 = []byte{ + // 267 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, 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, 0x24, 0x77, 0xe2, 0x91, 0x1c, 0xe3, 0x85, + 0x47, 0x72, 0x8c, 0x0f, 0x1e, 0xc9, 0x31, 0x4e, 0x78, 0x2c, 0xc7, 0x70, 0xe1, 0xb1, 0x1c, 0xc3, + 0x8d, 0xc7, 0x72, 0x0c, 0x51, 0x2c, 0x20, 0xc7, 0x24, 0xb1, 0x81, 0x03, 0xd6, 0x18, 0x10, 0x00, + 0x00, 0xff, 0xff, 0xc7, 0x0c, 0x4c, 0x66, 0x66, 0x01, 0x00, 0x00, } -func (m *TracerState) Marshal() (data []byte, err error) { + +func (m *TracerState) Marshal() (dAtA []byte, err error) { size := m.Size() - data = make([]byte, size) - n, err := m.MarshalTo(data) + dAtA = make([]byte, size) + n, err := m.MarshalToSizedBuffer(dAtA[:size]) if err != nil { return nil, err } - return data[:n], nil + return dAtA[:n], nil +} + +func (m *TracerState) MarshalTo(dAtA []byte) (int, error) { + size := m.Size() + return m.MarshalToSizedBuffer(dAtA[:size]) } -func (m *TracerState) MarshalTo(data []byte) (int, error) { - var i int +func (m *TracerState) MarshalToSizedBuffer(dAtA []byte) (int, error) { + i := len(dAtA) _ = 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 len(m.BaggageItems) > 0 { + for k := range m.BaggageItems { + v := m.BaggageItems[k] + baseI := i + i -= len(v) + copy(dAtA[i:], v) + i = encodeVarintWire(dAtA, i, uint64(len(v))) + i-- + dAtA[i] = 0x12 + i -= len(k) + copy(dAtA[i:], k) + i = encodeVarintWire(dAtA, i, uint64(len(k))) + i-- + dAtA[i] = 0xa + i = encodeVarintWire(dAtA, i, uint64(baseI-i)) + i-- + dAtA[i] = 0x2a + } } if m.Sampled { - data[i] = 0x18 - i++ + i-- if m.Sampled { - data[i] = 1 + dAtA[i] = 1 } else { - data[i] = 0 + dAtA[i] = 0 } - i++ + i-- + dAtA[i] = 0x20 } - 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) - } + if m.SpanId != 0 { + i -= 8 + encoding_binary.LittleEndian.PutUint64(dAtA[i:], uint64(m.SpanId)) + i-- + dAtA[i] = 0x19 + } + if m.TraceIdLo != 0 { + i -= 8 + encoding_binary.LittleEndian.PutUint64(dAtA[i:], uint64(m.TraceIdLo)) + i-- + dAtA[i] = 0x11 } - return i, nil + if m.TraceIdHi != 0 { + i -= 8 + encoding_binary.LittleEndian.PutUint64(dAtA[i:], uint64(m.TraceIdHi)) + i-- + dAtA[i] = 0x9 + } + return len(dAtA) - i, nil } -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 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 encodeVarintWire(data []byte, offset int, v uint64) int { +func encodeVarintWire(dAtA []byte, offset int, v uint64) int { + offset -= sovWire(v) + base := offset for v >= 1<<7 { - data[offset] = uint8(v&0x7f | 0x80) + dAtA[offset] = uint8(v&0x7f | 0x80) v >>= 7 offset++ } - data[offset] = uint8(v) - return offset + 1 + dAtA[offset] = uint8(v) + return base } func (m *TracerState) Size() (n int) { + if m == nil { + return 0 + } var l int _ = l - if m.TraceId != 0 { + if m.TraceIdHi != 0 { + n += 9 + } + if m.TraceIdLo != 0 { n += 9 } if m.SpanId != 0 { @@ -156,20 +238,13 @@ func (m *TracerState) Size() (n int) { } func sovWire(x uint64) (n int) { - for { - n++ - x >>= 7 - if x == 0 { - break - } - } - return n + return (math_bits.Len64(x|1) + 6) / 7 } 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) +func (m *TracerState) Unmarshal(dAtA []byte) error { + l := len(dAtA) iNdEx := 0 for iNdEx < l { preIndex := iNdEx @@ -181,9 +256,9 @@ func (m *TracerState) Unmarshal(data []byte) error { if iNdEx >= l { return io.ErrUnexpectedEOF } - b := data[iNdEx] + b := dAtA[iNdEx] iNdEx++ - wire |= (uint64(b) & 0x7F) << shift + wire |= uint64(b&0x7F) << shift if b < 0x80 { break } @@ -199,22 +274,25 @@ func (m *TracerState) Unmarshal(data []byte) error { switch fieldNum { case 1: if wireType != 1 { - return fmt.Errorf("proto: wrong wireType = %d for field TraceId", wireType) + return fmt.Errorf("proto: wrong wireType = %d for field TraceIdHi", wireType) } - m.TraceId = 0 + m.TraceIdHi = 0 if (iNdEx + 8) > l { return io.ErrUnexpectedEOF } + m.TraceIdHi = uint64(encoding_binary.LittleEndian.Uint64(dAtA[iNdEx:])) 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 TraceIdLo", wireType) + } + m.TraceIdLo = 0 + if (iNdEx + 8) > l { + return io.ErrUnexpectedEOF + } + m.TraceIdLo = uint64(encoding_binary.LittleEndian.Uint64(dAtA[iNdEx:])) + iNdEx += 8 + case 3: if wireType != 1 { return fmt.Errorf("proto: wrong wireType = %d for field SpanId", wireType) } @@ -222,16 +300,9 @@ func (m *TracerState) Unmarshal(data []byte) error { if (iNdEx + 8) > l { return io.ErrUnexpectedEOF } + m.SpanId = uint64(encoding_binary.LittleEndian.Uint64(dAtA[iNdEx:])) 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: + case 4: if wireType != 0 { return fmt.Errorf("proto: wrong wireType = %d for field Sampled", wireType) } @@ -243,15 +314,15 @@ func (m *TracerState) Unmarshal(data []byte) error { if iNdEx >= l { return io.ErrUnexpectedEOF } - b := data[iNdEx] + b := dAtA[iNdEx] iNdEx++ - v |= (int(b) & 0x7F) << shift + v |= int(b&0x7F) << shift if b < 0x80 { break } } m.Sampled = bool(v != 0) - case 4: + case 5: if wireType != 2 { return fmt.Errorf("proto: wrong wireType = %d for field BaggageItems", wireType) } @@ -263,9 +334,9 @@ func (m *TracerState) Unmarshal(data []byte) error { if iNdEx >= l { return io.ErrUnexpectedEOF } - b := data[iNdEx] + b := dAtA[iNdEx] iNdEx++ - msglen |= (int(b) & 0x7F) << shift + msglen |= int(b&0x7F) << shift if b < 0x80 { break } @@ -274,103 +345,122 @@ func (m *TracerState) Unmarshal(data []byte) error { 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 { + if postIndex < 0 { return ErrInvalidLengthWire } - postStringIndexmapkey := iNdEx + intStringLenmapkey - if postStringIndexmapkey > l { + if postIndex > 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 - } + if m.BaggageItems == nil { + m.BaggageItems = make(map[string]string) } - var stringLenmapvalue uint64 - for shift := uint(0); ; shift += 7 { - if shift >= 64 { - return ErrIntOverflowWire - } - if iNdEx >= l { - return io.ErrUnexpectedEOF + var mapkey string + var mapvalue string + for iNdEx < postIndex { + entryPreIndex := 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 + } } - b := data[iNdEx] - iNdEx++ - stringLenmapvalue |= (uint64(b) & 0x7F) << shift - if b < 0x80 { - break + fieldNum := int32(wire >> 3) + if fieldNum == 1 { + 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 < 0 { + return ErrInvalidLengthWire + } + if postStringIndexmapkey > l { + return io.ErrUnexpectedEOF + } + mapkey = string(dAtA[iNdEx:postStringIndexmapkey]) + iNdEx = postStringIndexmapkey + } else if fieldNum == 2 { + 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 < 0 { + return ErrInvalidLengthWire + } + if postStringIndexmapvalue > l { + return io.ErrUnexpectedEOF + } + mapvalue = string(dAtA[iNdEx:postStringIndexmapvalue]) + iNdEx = postStringIndexmapvalue + } else { + iNdEx = entryPreIndex + skippy, err := skipWire(dAtA[iNdEx:]) + if err != nil { + return err + } + if skippy < 0 { + return ErrInvalidLengthWire + } + if (iNdEx + skippy) > postIndex { + return io.ErrUnexpectedEOF + } + iNdEx += skippy } } - 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:]) + skippy, err := skipWire(dAtA[iNdEx:]) if err != nil { return err } if skippy < 0 { return ErrInvalidLengthWire } + if (iNdEx + skippy) < 0 { + return ErrInvalidLengthWire + } if (iNdEx + skippy) > l { return io.ErrUnexpectedEOF } @@ -383,9 +473,10 @@ func (m *TracerState) Unmarshal(data []byte) error { } return nil } -func skipWire(data []byte) (n int, err error) { - l := len(data) +func skipWire(dAtA []byte) (n int, err error) { + l := len(dAtA) iNdEx := 0 + depth := 0 for iNdEx < l { var wire uint64 for shift := uint(0); ; shift += 7 { @@ -395,7 +486,7 @@ func skipWire(data []byte) (n int, err error) { if iNdEx >= l { return 0, io.ErrUnexpectedEOF } - b := data[iNdEx] + b := dAtA[iNdEx] iNdEx++ wire |= (uint64(b) & 0x7F) << shift if b < 0x80 { @@ -413,14 +504,12 @@ func skipWire(data []byte) (n int, err error) { return 0, io.ErrUnexpectedEOF } iNdEx++ - if data[iNdEx-1] < 0x80 { + 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 { @@ -430,79 +519,41 @@ func skipWire(data []byte) (n int, err error) { if iNdEx >= l { return 0, io.ErrUnexpectedEOF } - b := data[iNdEx] + b := dAtA[iNdEx] iNdEx++ length |= (int(b) & 0x7F) << shift if b < 0x80 { break } } - iNdEx += length if length < 0 { return 0, ErrInvalidLengthWire } - return iNdEx, nil + iNdEx += length 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 + depth++ case 4: - return iNdEx, nil + if depth == 0 { + return 0, ErrUnexpectedEndOfGroupWire + } + depth-- case 5: iNdEx += 4 - return iNdEx, nil default: return 0, fmt.Errorf("proto: illegal wireType %d", wireType) } + if iNdEx < 0 { + return 0, ErrInvalidLengthWire + } + if depth == 0 { + return iNdEx, nil + } } - panic("unreachable") + return 0, io.ErrUnexpectedEOF } var ( - ErrInvalidLengthWire = fmt.Errorf("proto: negative length found during unmarshaling") - ErrIntOverflowWire = fmt.Errorf("proto: integer overflow") + ErrInvalidLengthWire = fmt.Errorf("proto: negative length found during unmarshaling") + ErrIntOverflowWire = fmt.Errorf("proto: integer overflow") + ErrUnexpectedEndOfGroupWire = fmt.Errorf("proto: unexpected end of group") ) - -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, - 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, -} 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; }