Skip to content

Commit 6ea3d16

Browse files
committed
wip
1 parent 60165d3 commit 6ea3d16

File tree

3 files changed

+401
-986
lines changed

3 files changed

+401
-986
lines changed

ddtrace/tracer/payload.go

Lines changed: 6 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,15 @@ func newPayload(protocol float64) payload {
6060

6161
// https://github.com/msgpack/msgpack/blob/master/spec.md#array-format-family
6262
const (
63+
// arrays
6364
msgpackArrayFix byte = 144 // up to 15 items
6465
msgpackArray16 byte = 0xdc // up to 2^16-1 items, followed by size in 2 bytes
6566
msgpackArray32 byte = 0xdd // up to 2^32-1 items, followed by size in 4 bytes
67+
68+
// maps
69+
msgpackMapFix byte = 0x80 // up to 15 items
70+
msgpackMap16 byte = 0xde // up to 2^16-1 items, followed by size in 2 bytes
71+
msgpackMap32 byte = 0xdf // up to 2^32-1 items, followed by size in 4 bytes
6672
)
6773

6874
// safePayload provides a thread-safe wrapper around payload.
@@ -152,29 +158,3 @@ func (sp *safePayload) protocol() float64 {
152158
// Protocol is immutable after creation - no lock needed
153159
return sp.p.protocol()
154160
}
155-
156-
// traceChunk represents a list of spans with the same trace ID,
157-
// i.e. a chunk of a trace
158-
type traceChunk struct {
159-
// the sampling priority of the trace
160-
priority int32 `msg:"priority"`
161-
162-
// the optional string origin ("lambda", "rum", etc.) of the trace chunk
163-
origin string `msg:"origin,omitempty"`
164-
165-
// a collection of key to value pairs common in all `spans`
166-
attributes keyValueList `msg:"attributes,omitempty"`
167-
168-
// a list of spans in this chunk
169-
spans spanList `msg:"spans,omitempty"`
170-
171-
// whether the trace only contains analyzed spans
172-
// (not required by tracers and set by the agent)
173-
droppedTrace bool `msg:"droppedTrace"`
174-
175-
// the ID of the trace to which all spans in this chunk belong
176-
traceID []byte `msg:"traceID"`
177-
178-
// the optional string decision maker (previously span tag _dd.p.dm)
179-
samplingMechanism string `msg:"samplingMechanism,omitempty"`
180-
}

ddtrace/tracer/payload_test.go

Lines changed: 63 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,15 @@ import (
99
"bytes"
1010
"fmt"
1111
"io"
12+
"math"
1213
"strconv"
1314
"strings"
1415
"sync"
1516
"sync/atomic"
1617
"testing"
1718

19+
"github.com/DataDog/dd-trace-go/v2/internal/globalconfig"
20+
"github.com/DataDog/dd-trace-go/v2/internal/version"
1821
"github.com/stretchr/testify/assert"
1922
"github.com/tinylib/msgp/msgp"
2023
)
@@ -82,17 +85,18 @@ func TestPayloadV04Decode(t *testing.T) {
8285
func TestPayloadV1Decode(t *testing.T) {
8386
for _, n := range []int{10, 1 << 10} {
8487
t.Run(strconv.Itoa(n), func(t *testing.T) {
85-
assert := assert.New(t)
86-
p := newPayloadV1()
87-
88-
p.containerID = "containerID"
89-
p.languageName = "languageName"
90-
p.languageVersion = "languageVersion"
91-
p.tracerVersion = "tracerVersion"
92-
p.runtimeID = "runtimeID"
93-
p.env = "env"
94-
p.hostname = "hostname"
95-
p.appVersion = "appVersion"
88+
var (
89+
assert = assert.New(t)
90+
p = newPayloadV1()
91+
)
92+
p.SetContainerID("containerID")
93+
p.SetLanguageName("go")
94+
p.SetLanguageVersion("1.25")
95+
p.SetTracerVersion(version.Tag)
96+
p.SetRuntimeID(globalconfig.RuntimeID())
97+
p.SetEnv("test")
98+
p.SetHostname("hostname")
99+
p.SetAppVersion("appVersion")
96100

97101
for i := 0; i < n; i++ {
98102
_, _ = p.push(newSpanList(i%5 + 1))
@@ -102,17 +106,56 @@ func TestPayloadV1Decode(t *testing.T) {
102106
assert.NoError(err)
103107

104108
got := newPayloadV1()
105-
_, err = got.Decode(encoded)
109+
buf := bytes.NewBuffer(encoded)
110+
_, err = buf.WriteTo(got)
106111
assert.NoError(err)
107112

108-
assert.Equal(p.containerID, got.containerID)
109-
assert.Equal(p.languageName, got.languageName)
110-
assert.Equal(p.languageVersion, got.languageVersion)
111-
assert.Equal(p.tracerVersion, got.tracerVersion)
112-
assert.Equal(p.runtimeID, got.runtimeID)
113-
assert.Equal(p.env, got.env)
114-
assert.Equal(p.hostname, got.hostname)
115-
assert.Equal(p.appVersion, got.appVersion)
113+
o, err := got.hydrate()
114+
assert.NoError(err)
115+
assert.Empty(o)
116+
assert.Equal(p.strings.strings, []string{
117+
"",
118+
"containerID",
119+
"go",
120+
"1.25",
121+
version.Tag,
122+
globalconfig.RuntimeID(),
123+
"test",
124+
"hostname",
125+
"appVersion",
126+
})
127+
assert.Equal(p.ContainerID(), got.ContainerID())
128+
assert.Equal(p.LanguageName(), got.LanguageName())
129+
assert.Equal(p.LanguageVersion(), got.LanguageVersion())
130+
assert.Equal(p.TracerVersion(), got.TracerVersion())
131+
assert.Equal(p.RuntimeID(), got.RuntimeID())
132+
assert.Equal(p.Env(), got.Env())
133+
assert.Equal(p.Hostname(), got.Hostname())
134+
assert.Equal(p.AppVersion(), got.AppVersion())
135+
})
136+
}
137+
}
138+
139+
func TestPayloadV1UpdateHeader(t *testing.T) {
140+
testCases := []uint32{ // Number of items
141+
15,
142+
math.MaxUint16,
143+
math.MaxUint32,
144+
}
145+
for _, tc := range testCases {
146+
t.Run(fmt.Sprintf("n=%d", tc), func(t *testing.T) {
147+
var (
148+
p = payloadV1{
149+
fields: tc,
150+
header: make([]byte, 8),
151+
}
152+
expected []byte
153+
)
154+
expected = msgp.AppendMapHeader(expected, tc)
155+
p.updateHeader()
156+
if got := p.header[p.off:]; !bytes.Equal(expected, got) {
157+
t.Fatalf("expected %+v, got %+v", expected, got)
158+
}
116159
})
117160
}
118161
}

0 commit comments

Comments
 (0)