From 297ef1f1cd591645daa00658addef19197fe7bfe Mon Sep 17 00:00:00 2001 From: Nicholas Hulston Date: Mon, 27 Jan 2025 13:58:28 -0500 Subject: [PATCH] Move `SpanLink` struct to separate `span_link.go` file so msgp generation works properly --- ddtrace/ddtrace.go | 19 ------ ddtrace/span_link.go | 25 ++++++++ ddtrace/span_link_msgp.go | 126 +++++++++++++++++++------------------- 3 files changed, 89 insertions(+), 81 deletions(-) create mode 100644 ddtrace/span_link.go diff --git a/ddtrace/ddtrace.go b/ddtrace/ddtrace.go index 928816ba6c..fc645aff88 100644 --- a/ddtrace/ddtrace.go +++ b/ddtrace/ddtrace.go @@ -103,25 +103,6 @@ type SpanContext interface { ForeachBaggageItem(handler func(k, v string) bool) } -// SpanLink represents a reference to a span that exists outside of the trace. -// -//go:generate msgp -unexported -marshal=false -o=span_link_msgp.go -tests=false - -type SpanLink struct { - // TraceID represents the low 64 bits of the linked span's trace id. This field is required. - TraceID uint64 `msg:"trace_id" json:"trace_id"` - // TraceIDHigh represents the high 64 bits of the linked span's trace id. This field is only set if the linked span's trace id is 128 bits. - TraceIDHigh uint64 `msg:"trace_id_high,omitempty" json:"trace_id_high"` - // SpanID represents the linked span's span id. - SpanID uint64 `msg:"span_id" json:"span_id"` - // Attributes is a mapping of keys to string values. These values are used to add additional context to the span link. - Attributes map[string]string `msg:"attributes,omitempty" json:"attributes"` - // Tracestate is the tracestate of the linked span. This field is optional. - Tracestate string `msg:"tracestate,omitempty" json:"tracestate"` - // Flags represents the W3C trace flags of the linked span. This field is optional. - Flags uint32 `msg:"flags,omitempty" json:"flags"` -} - // StartSpanOption is a configuration option that can be used with a Tracer's StartSpan method. type StartSpanOption func(cfg *StartSpanConfig) diff --git a/ddtrace/span_link.go b/ddtrace/span_link.go new file mode 100644 index 0000000000..22e7013e72 --- /dev/null +++ b/ddtrace/span_link.go @@ -0,0 +1,25 @@ +// Unless explicitly stated otherwise all files in this repository are licensed +// under the Apache License Version 2.0. +// This product includes software developed at Datadog (https://www.datadoghq.com/). +// Copyright 2016 Datadog, Inc. + +package ddtrace + +// SpanLink represents a reference to a span that exists outside of the trace. +// +//go:generate msgp -unexported -marshal=false -o=span_link_msgp.go -tests=false + +type SpanLink struct { + // TraceID represents the low 64 bits of the linked span's trace id. This field is required. + TraceID uint64 `msg:"trace_id" json:"trace_id"` + // TraceIDHigh represents the high 64 bits of the linked span's trace id. This field is only set if the linked span's trace id is 128 bits. + TraceIDHigh uint64 `msg:"trace_id_high,omitempty" json:"trace_id_high"` + // SpanID represents the linked span's span id. + SpanID uint64 `msg:"span_id" json:"span_id"` + // Attributes is a mapping of keys to string values. These values are used to add additional context to the span link. + Attributes map[string]string `msg:"attributes,omitempty" json:"attributes"` + // Tracestate is the tracestate of the linked span. This field is optional. + Tracestate string `msg:"tracestate,omitempty" json:"tracestate"` + // Flags represents the W3C trace flags of the linked span. This field is optional. + Flags uint32 `msg:"flags,omitempty" json:"flags"` +} diff --git a/ddtrace/span_link_msgp.go b/ddtrace/span_link_msgp.go index c2b90ec516..5c2579713d 100644 --- a/ddtrace/span_link_msgp.go +++ b/ddtrace/span_link_msgp.go @@ -97,9 +97,10 @@ func (z *SpanLink) DecodeMsg(dc *msgp.Reader) (err error) { // EncodeMsg implements msgp.Encodable func (z *SpanLink) EncodeMsg(en *msgp.Writer) (err error) { - // omitempty: check for empty values + // check for omitted fields zb0001Len := uint32(6) var zb0001Mask uint8 /* 6 bits */ + _ = zb0001Mask if z.TraceIDHigh == 0 { zb0001Len-- zb0001Mask |= 0x2 @@ -121,87 +122,88 @@ func (z *SpanLink) EncodeMsg(en *msgp.Writer) (err error) { if err != nil { return } - if zb0001Len == 0 { - return - } - // write "trace_id" - err = en.Append(0xa8, 0x74, 0x72, 0x61, 0x63, 0x65, 0x5f, 0x69, 0x64) - if err != nil { - return - } - err = en.WriteUint64(z.TraceID) - if err != nil { - err = msgp.WrapError(err, "TraceID") - return - } - if (zb0001Mask & 0x2) == 0 { // if not empty - // write "trace_id_high" - err = en.Append(0xad, 0x74, 0x72, 0x61, 0x63, 0x65, 0x5f, 0x69, 0x64, 0x5f, 0x68, 0x69, 0x67, 0x68) - if err != nil { - return - } - err = en.WriteUint64(z.TraceIDHigh) - if err != nil { - err = msgp.WrapError(err, "TraceIDHigh") - return - } - } - // write "span_id" - err = en.Append(0xa7, 0x73, 0x70, 0x61, 0x6e, 0x5f, 0x69, 0x64) - if err != nil { - return - } - err = en.WriteUint64(z.SpanID) - if err != nil { - err = msgp.WrapError(err, "SpanID") - return - } - if (zb0001Mask & 0x8) == 0 { // if not empty - // write "attributes" - err = en.Append(0xaa, 0x61, 0x74, 0x74, 0x72, 0x69, 0x62, 0x75, 0x74, 0x65, 0x73) + + // skip if no fields are to be emitted + if zb0001Len != 0 { + // write "trace_id" + err = en.Append(0xa8, 0x74, 0x72, 0x61, 0x63, 0x65, 0x5f, 0x69, 0x64) if err != nil { return } - err = en.WriteMapHeader(uint32(len(z.Attributes))) + err = en.WriteUint64(z.TraceID) if err != nil { - err = msgp.WrapError(err, "Attributes") + err = msgp.WrapError(err, "TraceID") return } - for za0001, za0002 := range z.Attributes { - err = en.WriteString(za0001) + if (zb0001Mask & 0x2) == 0 { // if not omitted + // write "trace_id_high" + err = en.Append(0xad, 0x74, 0x72, 0x61, 0x63, 0x65, 0x5f, 0x69, 0x64, 0x5f, 0x68, 0x69, 0x67, 0x68) if err != nil { - err = msgp.WrapError(err, "Attributes") return } - err = en.WriteString(za0002) + err = en.WriteUint64(z.TraceIDHigh) if err != nil { - err = msgp.WrapError(err, "Attributes", za0001) + err = msgp.WrapError(err, "TraceIDHigh") return } } - } - if (zb0001Mask & 0x10) == 0 { // if not empty - // write "tracestate" - err = en.Append(0xaa, 0x74, 0x72, 0x61, 0x63, 0x65, 0x73, 0x74, 0x61, 0x74, 0x65) + // write "span_id" + err = en.Append(0xa7, 0x73, 0x70, 0x61, 0x6e, 0x5f, 0x69, 0x64) if err != nil { return } - err = en.WriteString(z.Tracestate) + err = en.WriteUint64(z.SpanID) if err != nil { - err = msgp.WrapError(err, "Tracestate") + err = msgp.WrapError(err, "SpanID") return } - } - if (zb0001Mask & 0x20) == 0 { // if not empty - // write "flags" - err = en.Append(0xa5, 0x66, 0x6c, 0x61, 0x67, 0x73) - if err != nil { - return + if (zb0001Mask & 0x8) == 0 { // if not omitted + // write "attributes" + err = en.Append(0xaa, 0x61, 0x74, 0x74, 0x72, 0x69, 0x62, 0x75, 0x74, 0x65, 0x73) + if err != nil { + return + } + err = en.WriteMapHeader(uint32(len(z.Attributes))) + if err != nil { + err = msgp.WrapError(err, "Attributes") + return + } + for za0001, za0002 := range z.Attributes { + err = en.WriteString(za0001) + if err != nil { + err = msgp.WrapError(err, "Attributes") + return + } + err = en.WriteString(za0002) + if err != nil { + err = msgp.WrapError(err, "Attributes", za0001) + return + } + } } - err = en.WriteUint32(z.Flags) - if err != nil { - err = msgp.WrapError(err, "Flags") - return + if (zb0001Mask & 0x10) == 0 { // if not omitted + // write "tracestate" + err = en.Append(0xaa, 0x74, 0x72, 0x61, 0x63, 0x65, 0x73, 0x74, 0x61, 0x74, 0x65) + if err != nil { + return + } + err = en.WriteString(z.Tracestate) + if err != nil { + err = msgp.WrapError(err, "Tracestate") + return + } + } + if (zb0001Mask & 0x20) == 0 { // if not omitted + // write "flags" + err = en.Append(0xa5, 0x66, 0x6c, 0x61, 0x67, 0x73) + if err != nil { + return + } + err = en.WriteUint32(z.Flags) + if err != nil { + err = msgp.WrapError(err, "Flags") + return + } } } return