Skip to content

Commit 5b35410

Browse files
authored
chore: small improvements to v1 protocol (#4090)
Co-authored-by: hannahs.kim <[email protected]>
1 parent 15e5d9c commit 5b35410

File tree

2 files changed

+46
-56
lines changed

2 files changed

+46
-56
lines changed

ddtrace/tracer/api.txt

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -130,16 +130,6 @@ type UserMonitoringConfig struct {
130130

131131
type UserMonitoringOption func(*UserMonitoringConfig)()
132132

133-
// File: payload_v1.go
134-
135-
// Package Functions
136-
func DecodeAttributes([]byte, *stringTable) (map[string]anyValue, []byte, error)
137-
func DecodeKeyValueList([]byte, *stringTable) (map[string]anyValue, []byte, error)
138-
func DecodeSpanEvents([]byte, *stringTable) ([]spanEvent, []byte, error)
139-
func DecodeSpanLinks([]byte, *stringTable) ([]SpanLink, []byte, error)
140-
func DecodeSpans([]byte, *stringTable) (spanList, []byte, error)
141-
func DecodeTraceChunks([]byte, *stringTable) ([]traceChunk, []byte, error)
142-
143133
// File: propagator.go
144134

145135
// Types

ddtrace/tracer/payload_v1.go

Lines changed: 46 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,7 @@ func encodeField[F fieldValue](buf []byte, bm bitmap, fieldID uint32, a F, st *s
318318
switch value := any(a).(type) {
319319
case string:
320320
// encode msgp value, either by pulling from string table or writing it directly
321-
buf = st.Serialize(value, buf)
321+
buf = st.serialize(value, buf)
322322
case bool:
323323
buf = msgp.AppendBool(buf, value)
324324
case float64:
@@ -352,7 +352,7 @@ func (p *payloadV1) encodeAttributes(bm bitmap, fieldID int, kv map[string]anyVa
352352

353353
for k, v := range kv {
354354
// encode msgp key
355-
p.buf = st.Serialize(k, p.buf)
355+
p.buf = st.serialize(k, p.buf)
356356

357357
// encode value
358358
p.buf = v.encode(p.buf, st)
@@ -663,7 +663,7 @@ func (p *payloadV1) decodeBuffer() ([]byte, error) {
663663

664664
// handle attributes
665665
if idx == 10 {
666-
p.attributes, o, err = DecodeAttributes(o, st)
666+
p.attributes, o, err = decodeAttributes(o, st)
667667
if err != nil {
668668
break
669669
}
@@ -672,7 +672,7 @@ func (p *payloadV1) decodeBuffer() ([]byte, error) {
672672

673673
// handle trace chunks
674674
if idx == 11 {
675-
p.chunks, o, err = DecodeTraceChunks(o, st)
675+
p.chunks, o, err = decodeTraceChunks(o, st)
676676
if err != nil {
677677
break
678678
}
@@ -682,7 +682,7 @@ func (p *payloadV1) decodeBuffer() ([]byte, error) {
682682
// read msgp string value
683683
var value string
684684
var ok bool
685-
value, o, ok = st.Read(o)
685+
value, o, ok = st.read(o)
686686
if !ok {
687687
err = errUnableDecodeString
688688
break
@@ -762,7 +762,7 @@ func (a anyValue) encode(buf []byte, st *stringTable) []byte {
762762
switch a.valueType {
763763
case StringValueType:
764764
s := a.value.(string)
765-
buf = st.Serialize(s, buf)
765+
buf = st.serialize(s, buf)
766766
case BoolValueType:
767767
buf = msgp.AppendBool(buf, a.value.(bool))
768768
case FloatValueType:
@@ -866,7 +866,7 @@ func newStringTable() *stringTable {
866866
}
867867

868868
// Adds a string to the string table if it does not already exist. Returns the index of the string.
869-
func (s *stringTable) Add(str string) (idx index) {
869+
func (s *stringTable) add(str string) (idx index) {
870870
sv := stringValue(str)
871871
if _, ok := s.indices[sv]; ok {
872872
return s.indices[sv]
@@ -879,28 +879,28 @@ func (s *stringTable) Add(str string) (idx index) {
879879
}
880880

881881
// Get returns the index of a string in the string table if it exists. Returns false if the string does not exist.
882-
func (s *stringTable) Get(str string) (index, bool) {
882+
func (s *stringTable) get(str string) (index, bool) {
883883
sv := stringValue(str)
884884
if idx, ok := s.indices[sv]; ok {
885885
return idx, true
886886
}
887887
return -1, false
888888
}
889889

890-
func (st *stringTable) Serialize(value string, buf []byte) []byte {
891-
if idx, ok := st.Get(value); ok {
890+
func (st *stringTable) serialize(value string, buf []byte) []byte {
891+
if idx, ok := st.get(value); ok {
892892
buf = idx.encode(buf)
893893
} else {
894894
s := stringValue(value)
895895
buf = s.encode(buf)
896-
st.Add(value)
896+
st.add(value)
897897
}
898898
return buf
899899
}
900900

901901
// Reads a string from a byte slice and returns it from the string table if it exists.
902902
// Returns false if the string does not exist.
903-
func (s *stringTable) Read(b []byte) (string, []byte, bool) {
903+
func (s *stringTable) read(b []byte) (string, []byte, bool) {
904904
sType := getStreamingType(b[0])
905905
if sType == -1 {
906906
return "", b, false
@@ -913,7 +913,7 @@ func (s *stringTable) Read(b []byte) (string, []byte, bool) {
913913
return "", b, false
914914
}
915915
str := string(sv)
916-
s.Add(str)
916+
s.add(str)
917917
return str, o, true
918918
}
919919
// if b is an index
@@ -973,8 +973,8 @@ type traceChunk struct {
973973
samplingMechanism uint32
974974
}
975975

976-
// DecodeTraceChunks decodes a list of trace chunks from a byte slice.
977-
func DecodeTraceChunks(b []byte, st *stringTable) ([]traceChunk, []byte, error) {
976+
// decodeTraceChunks decodes a list of trace chunks from a byte slice.
977+
func decodeTraceChunks(b []byte, st *stringTable) ([]traceChunk, []byte, error) {
978978
out := []traceChunk{}
979979
numChunks, o, err := msgp.ReadArrayHeaderBytes(b)
980980
if err != nil {
@@ -1013,14 +1013,14 @@ func (tc *traceChunk) decode(b []byte, st *stringTable) ([]byte, error) {
10131013
case 1:
10141014
tc.priority, o, err = msgp.ReadInt32Bytes(o)
10151015
case 2:
1016-
tc.origin, o, ok = st.Read(o)
1016+
tc.origin, o, ok = st.read(o)
10171017
if !ok {
10181018
err = errUnableDecodeString
10191019
}
10201020
case 3:
1021-
tc.attributes, o, err = DecodeAttributes(o, st)
1021+
tc.attributes, o, err = decodeAttributes(o, st)
10221022
case 4:
1023-
tc.spans, o, err = DecodeSpans(o, st)
1023+
tc.spans, o, err = decodeSpans(o, st)
10241024
case 5:
10251025
tc.droppedTrace, o, err = msgp.ReadBoolBytes(o)
10261026
case 6:
@@ -1034,8 +1034,8 @@ func (tc *traceChunk) decode(b []byte, st *stringTable) ([]byte, error) {
10341034
return o, err
10351035
}
10361036

1037-
// DecodeSpans decodes a list of spans from a byte slice.
1038-
func DecodeSpans(b []byte, st *stringTable) (spanList, []byte, error) {
1037+
// decodeSpans decodes a list of spans from a byte slice.
1038+
func decodeSpans(b []byte, st *stringTable) (spanList, []byte, error) {
10391039
out := spanList{}
10401040
numSpans, o, err := msgp.ReadArrayHeaderBytes(b)
10411041
if err != nil {
@@ -1073,17 +1073,17 @@ func (span *Span) decode(b []byte, st *stringTable) ([]byte, error) {
10731073
// read msgp value
10741074
switch idx {
10751075
case 1:
1076-
span.service, o, ok = st.Read(o)
1076+
span.service, o, ok = st.read(o)
10771077
if !ok {
10781078
err = errUnableDecodeString
10791079
}
10801080
case 2:
1081-
span.name, o, ok = st.Read(o)
1081+
span.name, o, ok = st.read(o)
10821082
if !ok {
10831083
err = errUnableDecodeString
10841084
}
10851085
case 3:
1086-
span.resource, o, ok = st.Read(o)
1086+
span.resource, o, ok = st.read(o)
10871087
if !ok {
10881088
err = errUnableDecodeString
10891089
}
@@ -1105,22 +1105,22 @@ func (span *Span) decode(b []byte, st *stringTable) ([]byte, error) {
11051105
}
11061106
case 9:
11071107
var attr map[string]anyValue
1108-
attr, o, err = DecodeAttributes(o, st)
1108+
attr, o, err = decodeAttributes(o, st)
11091109
for k, v := range attr {
11101110
span.SetTag(k, v.value)
11111111
}
11121112
case 10:
1113-
span.spanType, o, ok = st.Read(o)
1113+
span.spanType, o, ok = st.read(o)
11141114
if !ok {
11151115
err = errUnableDecodeString
11161116
}
11171117
case 11:
1118-
span.spanLinks, o, err = DecodeSpanLinks(o, st)
1118+
span.spanLinks, o, err = decodeSpanLinks(o, st)
11191119
case 12:
1120-
span.spanEvents, o, err = DecodeSpanEvents(o, st)
1120+
span.spanEvents, o, err = decodeSpanEvents(o, st)
11211121
case 13:
11221122
var env string
1123-
env, o, ok = st.Read(o)
1123+
env, o, ok = st.read(o)
11241124
if !ok {
11251125
err = errUnableDecodeString
11261126
break
@@ -1130,7 +1130,7 @@ func (span *Span) decode(b []byte, st *stringTable) ([]byte, error) {
11301130
}
11311131
case 14:
11321132
var ver string
1133-
ver, o, ok = st.Read(o)
1133+
ver, o, ok = st.read(o)
11341134
if !ok {
11351135
err = errUnableDecodeString
11361136
break
@@ -1140,7 +1140,7 @@ func (span *Span) decode(b []byte, st *stringTable) ([]byte, error) {
11401140
}
11411141
case 15:
11421142
var component string
1143-
component, o, ok = st.Read(o)
1143+
component, o, ok = st.read(o)
11441144
if !ok {
11451145
err = errUnableDecodeString
11461146
break
@@ -1162,8 +1162,8 @@ func (span *Span) decode(b []byte, st *stringTable) ([]byte, error) {
11621162
return o, err
11631163
}
11641164

1165-
// DecodeSpanLinks decodes a list of span links from a byte slice.
1166-
func DecodeSpanLinks(b []byte, st *stringTable) ([]SpanLink, []byte, error) {
1165+
// decodeSpanLinks decodes a list of span links from a byte slice.
1166+
func decodeSpanLinks(b []byte, st *stringTable) ([]SpanLink, []byte, error) {
11671167
out := []SpanLink{}
11681168
numLinks, o, err := msgp.ReadArrayHeaderBytes(b)
11691169
if err != nil {
@@ -1206,7 +1206,7 @@ func (link *SpanLink) decode(b []byte, st *stringTable) ([]byte, error) {
12061206
link.SpanID, o, err = msgp.ReadUint64Bytes(o)
12071207
case 3:
12081208
var attr map[string]anyValue
1209-
attr, o, err = DecodeAttributes(o, st)
1209+
attr, o, err = decodeAttributes(o, st)
12101210
for k, v := range attr {
12111211
if v.valueType != StringValueType {
12121212
return o, fmt.Errorf("unexpected value type: %d", v.valueType)
@@ -1216,7 +1216,7 @@ func (link *SpanLink) decode(b []byte, st *stringTable) ([]byte, error) {
12161216
case 4:
12171217
var state string
12181218
var ok bool
1219-
state, o, ok = st.Read(o)
1219+
state, o, ok = st.read(o)
12201220
if !ok {
12211221
err = errUnableDecodeString
12221222
break
@@ -1231,8 +1231,8 @@ func (link *SpanLink) decode(b []byte, st *stringTable) ([]byte, error) {
12311231
return o, err
12321232
}
12331233

1234-
// DecodeSpanEvents decodes a list of span events from a byte slice.
1235-
func DecodeSpanEvents(b []byte, st *stringTable) ([]spanEvent, []byte, error) {
1234+
// decodeSpanEvents decodes a list of span events from a byte slice.
1235+
func decodeSpanEvents(b []byte, st *stringTable) ([]spanEvent, []byte, error) {
12361236
out := []spanEvent{}
12371237
numEvents, o, err := msgp.ReadArrayHeaderBytes(b)
12381238
if err != nil {
@@ -1272,15 +1272,15 @@ func (event *spanEvent) decode(b []byte, st *stringTable) ([]byte, error) {
12721272
case 2:
12731273
var name string
12741274
var ok bool
1275-
name, o, ok = st.Read(o)
1275+
name, o, ok = st.read(o)
12761276
if !ok {
12771277
err = errUnableDecodeString
12781278
break
12791279
}
12801280
event.Name = name
12811281
case 3:
12821282
var attr map[string]anyValue
1283-
attr, o, err = DecodeAttributes(o, st)
1283+
attr, o, err = decodeAttributes(o, st)
12841284
if err != nil {
12851285
break
12861286
}
@@ -1307,7 +1307,7 @@ func decodeAnyValue(b []byte, strings *stringTable) (anyValue, []byte, error) {
13071307
str string
13081308
ok bool
13091309
)
1310-
str, o, ok = strings.Read(o)
1310+
str, o, ok = strings.read(o)
13111311
if !ok {
13121312
return anyValue{}, o, errUnableDecodeString
13131313
}
@@ -1357,7 +1357,7 @@ func decodeAnyValue(b []byte, strings *stringTable) (anyValue, []byte, error) {
13571357
return anyValue{valueType: ArrayValueType, value: arrayValue}, o, nil
13581358
case keyValueListType:
13591359
var kv map[string]anyValue
1360-
kv, o, err = DecodeKeyValueList(o, strings)
1360+
kv, o, err = decodeKeyValueList(o, strings)
13611361
if err != nil {
13621362
return anyValue{}, o, err
13631363
}
@@ -1367,8 +1367,8 @@ func decodeAnyValue(b []byte, strings *stringTable) (anyValue, []byte, error) {
13671367
}
13681368
}
13691369

1370-
// DecodeKeyValueList decodes a map of string to anyValue from a byte slice.
1371-
func DecodeKeyValueList(b []byte, strings *stringTable) (map[string]anyValue, []byte, error) {
1370+
// decodeKeyValueList decodes a map of string to anyValue from a byte slice.
1371+
func decodeKeyValueList(b []byte, strings *stringTable) (map[string]anyValue, []byte, error) {
13721372
numFields, o, err := msgp.ReadMapHeaderBytes(b)
13731373
if err != nil {
13741374
return nil, b, err
@@ -1381,7 +1381,7 @@ func DecodeKeyValueList(b []byte, strings *stringTable) (map[string]anyValue, []
13811381
ok bool
13821382
av anyValue
13831383
)
1384-
key, o, ok = strings.Read(o)
1384+
key, o, ok = strings.read(o)
13851385
if !ok {
13861386
return nil, o, fmt.Errorf("unable to read key of field %d", i)
13871387
}
@@ -1394,9 +1394,9 @@ func DecodeKeyValueList(b []byte, strings *stringTable) (map[string]anyValue, []
13941394
return kv, o, nil
13951395
}
13961396

1397-
// DecodeAttributes decodes a map of string to anyValue from a byte slice
1397+
// decodeAttributes decodes a map of string to anyValue from a byte slice
13981398
// Attributes are encoded as an array of key, valueType, and value.
1399-
func DecodeAttributes(b []byte, strings *stringTable) (map[string]anyValue, []byte, error) {
1399+
func decodeAttributes(b []byte, strings *stringTable) (map[string]anyValue, []byte, error) {
14001400
n, o, err := msgp.ReadArrayHeaderBytes(b)
14011401
numFields := n / 3
14021402
if err != nil {
@@ -1410,7 +1410,7 @@ func DecodeAttributes(b []byte, strings *stringTable) (map[string]anyValue, []by
14101410
ok bool
14111411
av anyValue
14121412
)
1413-
key, o, ok = strings.Read(o)
1413+
key, o, ok = strings.read(o)
14141414
if !ok {
14151415
return nil, o, fmt.Errorf("unable to read key of field %d", i)
14161416
}

0 commit comments

Comments
 (0)