-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Expand file tree
/
Copy pathspan_v05.go
More file actions
311 lines (300 loc) · 9.93 KB
/
Copy pathspan_v05.go
File metadata and controls
311 lines (300 loc) · 9.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
// 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-present Datadog, Inc.
// Package idx is used to unmarshal v1.0 Trace payloads
package idx
import (
"errors"
"fmt"
"github.com/tinylib/msgp/msgp"
)
// buildStringTable builds a string table from a list of unique v05 strings
// However, unlike the v05 array, the string table expects that the 0 index is always the empty string
// To get around this we swap whatever string is at index 0 with the location of the empty string (if present), or append it to the end
// We then return the string table and the new location for string ref `0` (0 if unchanged)
func buildStringTable(v05Strings []string) (*StringTable, uint32) {
if len(v05Strings) == 0 {
return NewStringTable(), 0
}
if v05Strings[0] == "" {
// The empty string is already at index 0, so we can just use the string table as is
return StringTableFromArray(v05Strings), 0
}
emptyStringIndex := uint32(0)
for i, str := range v05Strings {
if str == "" {
emptyStringIndex = uint32(i)
break
}
}
newZeroRef := emptyStringIndex
if emptyStringIndex != 0 {
v05Strings[0], v05Strings[emptyStringIndex] = v05Strings[emptyStringIndex], v05Strings[0]
} else {
// The empty string is not present, so we append the 0th string to the end and set the first index to the empty string
v05Strings = append(v05Strings, v05Strings[0])
v05Strings[0] = ""
newZeroRef = uint32(len(v05Strings) - 1)
}
return StringTableFromArray(v05Strings), newZeroRef
}
// UnmarshalMsgDictionary decodes a InternalTracerPayload using the specification from the v0.5 endpoint.
// For details, see the documentation for endpoint v0.5 in pkg/trace/api/version.go
func (tp *InternalTracerPayload) UnmarshalMsgDictionary(bts []byte) error {
var err error
if _, bts, err = safeReadHeaderBytes(bts, msgp.ReadArrayHeaderBytes); err != nil {
return err
}
// read dictionary
var sz uint32
if sz, bts, err = safeReadHeaderBytes(bts, msgp.ReadArrayHeaderBytes); err != nil {
return err
}
dict := make([]string, sz)
for i := range dict {
var str string
str, bts, err = parseStringBytes(bts)
if err != nil {
return err
}
dict[i] = str
}
dictSize := sz
stringTable, newZeroRef := buildStringTable(dict)
tp.Strings = stringTable
// read num chunks
sz, bts, err = safeReadHeaderBytes(bts, msgp.ReadArrayHeaderBytes)
if err != nil {
return err
}
if cap(tp.Chunks) >= int(sz) {
tp.Chunks = tp.Chunks[:sz]
} else {
tp.Chunks = make([]*InternalTraceChunk, sz)
}
chunkConvertedFields := ChunkConvertedFields{}
convertedTagSet := false
for i := range tp.Chunks {
sz, bts, err = safeReadHeaderBytes(bts, msgp.ReadArrayHeaderBytes)
if err != nil {
return err
}
if tp.Chunks[i] == nil {
tp.Chunks[i] = &InternalTraceChunk{Strings: stringTable}
}
if cap(tp.Chunks[i].Spans) >= int(sz) {
tp.Chunks[i].Spans = tp.Chunks[i].Spans[:sz]
} else {
tp.Chunks[i].Spans = make([]*InternalSpan, sz)
}
convertedFields := NewSpanConvertedFields()
var rootSampling RootSamplingMergeState
for j := range tp.Chunks[i].Spans {
if tp.Chunks[i].Spans[j] == nil {
tp.Chunks[i].Spans[j] = NewInternalSpan(stringTable, &Span{})
}
if bts, err = tp.Chunks[i].Spans[j].UnmarshalMsgDictionaryConverted(bts, convertedFields, dictSize, newZeroRef); err != nil {
return err
}
if !convertedTagSet {
// _dd.convertedv1 marks that this payload was converted from the v0.5
// wire format. It is a debugging aid, so we only tag the first span of
// the payload rather than paying the allocation on every span.
tp.Chunks[i].Spans[j].SetStringAttribute("_dd.convertedv1", "v05")
convertedTagSet = true
}
rootSampling.ReconcileSamplingPriorityAfterChunkSpan(convertedFields, tp.Chunks[i].Spans[j].ParentID())
}
tp.Chunks[i].ApplyPromotedFields(convertedFields, &chunkConvertedFields)
}
tp.ApplyPromotedFields(&chunkConvertedFields)
return nil
}
// spanPropertyCount specifies the number of top-level properties that a span
// has.
const spanPropertyCount = 12
func readV05StringRef(dictSize uint32, newZeroRef uint32, bts []byte) (uint32, []byte, error) {
var parsedRef uint32
var err error
parsedRef, bts, err = msgp.ReadUint32Bytes(bts)
if err != nil {
return 0, bts, err
}
if parsedRef >= dictSize {
return 0, bts, fmt.Errorf("dictionary index %d out of range", parsedRef)
}
if parsedRef == 0 && newZeroRef != 0 {
// This string was moved from index 0 to index newZeroRef, so we return the new index
return newZeroRef, bts, nil
}
return parsedRef, bts, nil
}
// UnmarshalMsgDictionaryConverted decodes a v05 span directly into an InternalSpan, for details, see the documentation for endpoint v0.5
// in pkg/trace/api/version.go
// The provided InternalSpan must have a pre-populated Strings field
// newZeroRef is the new location for string ref `0` (0 if unchanged) see buildStringTable for more details
func (s *InternalSpan) UnmarshalMsgDictionaryConverted(bts []byte, convertedFields *SpanConvertedFields, dictSize uint32, newZeroRef uint32) ([]byte, error) {
var (
sz uint32
err error
)
sz, bts, err = safeReadHeaderBytes(bts, msgp.ReadArrayHeaderBytes)
if err != nil {
return bts, err
}
if sz != spanPropertyCount {
return bts, errors.New("encoded span needs exactly 12 elements in array")
}
// Service (0)
s.span.ServiceRef, bts, err = readV05StringRef(dictSize, newZeroRef, bts)
if err != nil {
return bts, err
}
// Name (1)
s.span.NameRef, bts, err = readV05StringRef(dictSize, newZeroRef, bts)
if err != nil {
return bts, err
}
// Resource (2)
s.span.ResourceRef, bts, err = readV05StringRef(dictSize, newZeroRef, bts)
if err != nil {
return bts, err
}
// TraceID (3)
convertedFields.TraceIDLower, bts, err = parseUint64Bytes(bts)
if err != nil {
return bts, err
}
// SpanID (4)
s.span.SpanID, bts, err = parseUint64Bytes(bts)
if err != nil {
return bts, err
}
// ParentID (5)
s.span.ParentID, bts, err = parseUint64Bytes(bts)
if err != nil {
return bts, err
}
// Start (6)
var spanStart int64
spanStart, bts, err = parseInt64Bytes(bts)
s.span.Start = uint64(spanStart)
if err != nil {
return bts, err
}
// Duration (7)
var spanDuration int64
spanDuration, bts, err = parseInt64Bytes(bts)
s.span.Duration = uint64(spanDuration)
if err != nil {
return bts, err
}
// Error (8)
var spanError int32
spanError, bts, err = parseInt32Bytes(bts)
s.span.Error = spanError != 0
if err != nil {
return bts, err
}
// Meta (9)
sz, bts, err = safeReadHeaderBytes(bts, msgp.ReadMapHeaderBytes)
if err != nil {
return bts, err
}
if s.span.Attributes == nil && sz > 0 {
s.span.Attributes = make(map[uint32]*AnyValue, sz)
}
if sz > 0 {
if err = checkSlabCount(sz, bts); err != nil {
return bts, err
}
// Slab-allocate the AnyValue containers and their string-ref oneof wrappers
// for every meta entry in two allocations, rather than two per entry. The map
// holds pointers into these backing arrays, which live as long as the span.
values := make([]AnyValue, sz)
refs := make([]AnyValue_StringValueRef, sz)
for i := uint32(0); i < sz; i++ {
var key, val uint32
key, bts, err = readV05StringRef(dictSize, newZeroRef, bts)
if err != nil {
return bts, err
}
val, bts, err = readV05StringRef(dictSize, newZeroRef, bts)
if err != nil {
return bts, err
}
s.handlePromotedMetaFields(key, val, convertedFields)
refs[i].StringValueRef = val
values[i].Value = &refs[i]
s.span.Attributes[key] = &values[i]
}
}
// Metrics (10)
sz, bts, err = safeReadHeaderBytes(bts, msgp.ReadMapHeaderBytes)
if err != nil {
return bts, err
}
if s.span.Attributes == nil && sz > 0 {
s.span.Attributes = make(map[uint32]*AnyValue, sz)
}
if sz > 0 {
if err = checkSlabCount(sz, bts); err != nil {
return bts, err
}
// Slab-allocate the AnyValue containers and their double oneof wrappers for
// every metric in two allocations, rather than two per metric.
values := make([]AnyValue, sz)
doubles := make([]AnyValue_DoubleValue, sz)
for i := uint32(0); i < sz; i++ {
var (
key uint32
val float64
)
key, bts, err = readV05StringRef(dictSize, newZeroRef, bts)
if err != nil {
return bts, err
}
val, bts, err = parseFloat64Bytes(bts)
if err != nil {
return bts, err
}
s.handlePromotedMetricsFields(key, val, convertedFields)
doubles[i].DoubleValue = val
values[i].Value = &doubles[i]
s.span.Attributes[key] = &values[i]
}
}
// Type (11)
s.span.TypeRef, bts, err = readV05StringRef(dictSize, newZeroRef, bts)
if err != nil {
return bts, err
}
return bts, nil
}
// safeReadHeaderBytes wraps msgp header readers (typically ReadArrayHeaderBytes and ReadMapHeaderBytes).
// It enforces the dictionary max size of 25MB and protects the caller from making unbounded allocations through `make(any, sz)`.
func safeReadHeaderBytes(b []byte, read func([]byte) (uint32, []byte, error)) (uint32, []byte, error) {
sz, bts, err := read(b)
if err != nil {
return 0, nil, err
}
if sz > 25*1e6 {
// Dictionary can't be larger than 25 MB
return 0, nil, errors.New("too long payload")
}
return sz, bts, err
}
// minBytesPerSlabEntry is a conservative lower bound on the wire size of one meta/metrics/meta_struct
// entry: every entry reads at least two msgpack values (e.g. two 1-byte nils).
const minBytesPerSlabEntry = 2
// checkSlabCount guards a slab pre-allocation (make([]AnyValue, n) and its oneof-wrapper sibling)
// against a claimed entry count that couldn't possibly be backed by the remaining bytes. Without this,
// a tiny malicious payload could set a map header to millions of entries and force a large allocation
// before decoding ever reaches the missing bytes and fails naturally.
func checkSlabCount(n uint32, remaining []byte) error {
if uint64(n)*minBytesPerSlabEntry > uint64(len(remaining)) {
return fmt.Errorf("not enough data for %d entries", n)
}
return nil
}