-
Notifications
You must be signed in to change notification settings - Fork 407
Expand file tree
/
Copy pathagentless-ci-visibility.js
More file actions
438 lines (377 loc) · 14.4 KB
/
Copy pathagentless-ci-visibility.js
File metadata and controls
438 lines (377 loc) · 14.4 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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
'use strict'
const { version: ddTraceVersion } = require('../../../../package.json')
const { ITR_CORRELATION_ID, TEST_LEVELS_METADATA } = require('../../src/plugins/util/test')
const id = require('../../src/id')
const {
distributionMetric,
TELEMETRY_ENDPOINT_PAYLOAD_SERIALIZATION_MS,
TELEMETRY_ENDPOINT_PAYLOAD_EVENTS_COUNT,
} = require('../ci-visibility/telemetry')
const { MsgpackChunk, MAX_SIZE, OverflowError } = require('../msgpack')
const { AgentEncoder } = require('./0.4')
const {
truncateSpanTestOpt,
normalizeSpan,
MAX_META_VALUE_LENGTH_TEST_OPTIMIZATION,
} = require('./tags-processors')
const ENCODING_VERSION = 1
const ALLOWED_CONTENT_TYPES = new Set(['test_session_end', 'test_module_end', 'test_suite_end', 'test'])
const ALLOWED_METADATA_TARGETS = new Set([...ALLOWED_CONTENT_TYPES, TEST_LEVELS_METADATA])
const TEST_SUITE_KEYS_LENGTH = 12
const TEST_MODULE_KEYS_LENGTH = 11
const TEST_SESSION_KEYS_LENGTH = 10
const TEST_AND_SPAN_KEYS_LENGTH = 11
const INTAKE_SOFT_LIMIT = 2 * 1024 * 1024 // 2MB
// Prefix is ~1 KB in practice; `MsgpackChunk` resizes on overflow.
const PREFIX_CHUNK_INITIAL_SIZE = 2048
function formatSpan (span) {
let encodingVersion = ENCODING_VERSION
if (span.type === 'test' && span.meta && span.meta.test_session_id) {
encodingVersion = 2
}
return {
type: ALLOWED_CONTENT_TYPES.has(span.type) ? span.type : 'span',
version: encodingVersion,
content: normalizeSpan(truncateSpanTestOpt(span)),
}
}
function isAllowedMetadataTarget (target) {
return ALLOWED_METADATA_TARGETS.has(target)
}
function isEncodableMetadataValue (value) {
return typeof value === 'string' || typeof value === 'number'
}
function truncateTestLevelMetadataValue (value) {
if (typeof value !== 'string' || value.length <= MAX_META_VALUE_LENGTH_TEST_OPTIMIZATION) {
return value
}
return `${value.slice(0, MAX_META_VALUE_LENGTH_TEST_OPTIMIZATION)}...`
}
function truncateTestLevelMetadataTags (tags) {
const truncatedTags = {}
let hasTags = false
for (const key of Object.keys(tags)) {
const value = truncateTestLevelMetadataValue(tags[key])
if (!isEncodableMetadataValue(value)) continue
truncatedTags[key] = value
hasTags = true
}
return hasTags ? truncatedTags : undefined
}
class AgentlessCiVisibilityEncoder extends AgentEncoder {
constructor (writer, { tags }) {
super(writer, INTAKE_SOFT_LIMIT)
// Holds a reference to the live `tags` object (instead of copying `env`/`runtime-id` out of it)
// so a later change (e.g. a MicroVM clone resume) is picked up at flush time.
this.tags = tags
// Used to keep track of the number of encoded events to update the
// length of `payload.events` when calling `makePayload`
this._eventCount = 0
this.metadataTags = {}
this.wildcardMetadataTags = {}
this.testLevelsMetadataKeys = []
this.reset()
}
addMetadataTags (tags) {
if (tags['*']) {
this.wildcardMetadataTags = {
...this.wildcardMetadataTags,
...tags['*'],
}
}
for (const target of Object.keys(tags)) {
if (target === '*' || !tags[target] || !isAllowedMetadataTarget(target)) continue
const targetTags = target === TEST_LEVELS_METADATA
? truncateTestLevelMetadataTags(tags[target])
: tags[target]
if (!targetTags) continue
this.metadataTags[target] = {
...this.metadataTags[target],
...targetTags,
}
if (target === TEST_LEVELS_METADATA) {
this.testLevelsMetadataKeys = Object.keys(this.metadataTags[target])
}
}
}
_removeDuplicateTestLevelsMetadata (event) {
if (!ALLOWED_CONTENT_TYPES.has(event.type)) return
const meta = event.content.meta
if (!meta) return
const testLevelsMetadataTags = this.metadataTags[TEST_LEVELS_METADATA]
const testLevelsMetadataKeys = this.testLevelsMetadataKeys
for (let i = 0; i < testLevelsMetadataKeys.length; i++) {
const key = testLevelsMetadataKeys[i]
if (meta[key] === testLevelsMetadataTags[key]) {
delete meta[key]
}
}
}
_encodeTestSuite (bytes, content) {
let keysLength = TEST_SUITE_KEYS_LENGTH
const itrCorrelationId = content.meta[ITR_CORRELATION_ID]
if (itrCorrelationId) {
keysLength++
}
bytes.writeMapPrefix(keysLength)
this._encodeString(bytes, 'type')
this._encodeString(bytes, content.type)
this._encodeString(bytes, 'test_session_id')
this._encodeId(bytes, content.trace_id)
this._encodeString(bytes, 'test_module_id')
this._encodeId(bytes, content.parent_id)
this._encodeString(bytes, 'test_suite_id')
this._encodeId(bytes, content.span_id)
if (itrCorrelationId) {
this._encodeString(bytes, ITR_CORRELATION_ID)
this._encodeString(bytes, itrCorrelationId)
delete content.meta[ITR_CORRELATION_ID]
}
this._encodeString(bytes, 'error')
bytes.writeNumber(content.error)
this._encodeString(bytes, 'name')
this._encodeString(bytes, content.name)
this._encodeString(bytes, 'service')
this._encodeString(bytes, content.service)
this._encodeString(bytes, 'resource')
this._encodeString(bytes, content.resource)
this._encodeString(bytes, 'start')
bytes.writeNumber(content.start)
this._encodeString(bytes, 'duration')
bytes.writeNumber(content.duration)
this._encodeString(bytes, 'meta')
this._encodeMap(bytes, content.meta)
this._encodeString(bytes, 'metrics')
this._encodeMap(bytes, content.metrics)
}
_encodeTestModule (bytes, content) {
bytes.writeMapPrefix(TEST_MODULE_KEYS_LENGTH)
this._encodeString(bytes, 'type')
this._encodeString(bytes, content.type)
this._encodeString(bytes, 'test_session_id')
this._encodeId(bytes, content.trace_id)
this._encodeString(bytes, 'test_module_id')
this._encodeId(bytes, content.span_id)
this._encodeString(bytes, 'error')
bytes.writeNumber(content.error)
this._encodeString(bytes, 'name')
this._encodeString(bytes, content.name)
this._encodeString(bytes, 'service')
this._encodeString(bytes, content.service)
this._encodeString(bytes, 'resource')
this._encodeString(bytes, content.resource)
this._encodeString(bytes, 'start')
bytes.writeNumber(content.start)
this._encodeString(bytes, 'duration')
bytes.writeNumber(content.duration)
this._encodeString(bytes, 'meta')
this._encodeMap(bytes, content.meta)
this._encodeString(bytes, 'metrics')
this._encodeMap(bytes, content.metrics)
}
_encodeTestSession (bytes, content) {
bytes.writeMapPrefix(TEST_SESSION_KEYS_LENGTH)
this._encodeString(bytes, 'type')
this._encodeString(bytes, content.type)
this._encodeString(bytes, 'test_session_id')
this._encodeId(bytes, content.trace_id)
this._encodeString(bytes, 'error')
bytes.writeNumber(content.error)
this._encodeString(bytes, 'name')
this._encodeString(bytes, content.name)
this._encodeString(bytes, 'service')
this._encodeString(bytes, content.service)
this._encodeString(bytes, 'resource')
this._encodeString(bytes, content.resource)
this._encodeString(bytes, 'start')
bytes.writeNumber(content.start)
this._encodeString(bytes, 'duration')
bytes.writeNumber(content.duration)
this._encodeString(bytes, 'meta')
this._encodeMap(bytes, content.meta)
this._encodeString(bytes, 'metrics')
this._encodeMap(bytes, content.metrics)
}
_encodeEventContent (bytes, content) {
let totalKeysLength = TEST_AND_SPAN_KEYS_LENGTH
if (content.meta.test_session_id) {
totalKeysLength += 1
}
if (content.meta.test_module_id) {
totalKeysLength += 1
}
if (content.meta.test_suite_id) {
totalKeysLength += 1
}
const itrCorrelationId = content.meta[ITR_CORRELATION_ID]
if (itrCorrelationId) {
totalKeysLength += 1
}
if (content.type) {
totalKeysLength += 1
}
bytes.writeMapPrefix(totalKeysLength)
if (content.type) {
this._encodeString(bytes, 'type')
this._encodeString(bytes, content.type)
}
this._encodeString(bytes, 'trace_id')
this._encodeId(bytes, content.trace_id)
this._encodeString(bytes, 'span_id')
this._encodeId(bytes, content.span_id)
this._encodeString(bytes, 'parent_id')
this._encodeId(bytes, content.parent_id)
this._encodeString(bytes, 'name')
this._encodeString(bytes, content.name)
this._encodeString(bytes, 'resource')
this._encodeString(bytes, content.resource)
this._encodeString(bytes, 'service')
this._encodeString(bytes, content.service)
this._encodeString(bytes, 'error')
bytes.writeNumber(content.error)
this._encodeString(bytes, 'start')
bytes.writeNumber(content.start)
this._encodeString(bytes, 'duration')
bytes.writeNumber(content.duration)
/**
* We include `test_session_id` and `test_suite_id`
* in the root of the event by passing them via the `meta` dict.
* This is to avoid changing the span format in packages/dd-trace/src/format.js,
* which can have undesired side effects in other products.
* But `test_session_id` and `test_suite_id` are *not* supposed to be in `meta`,
* so we delete them before enconding the dictionary.
* TODO: find a better way to do this.
*/
if (content.meta.test_session_id) {
this._encodeString(bytes, 'test_session_id')
this._encodeId(bytes, id(content.meta.test_session_id, 10))
delete content.meta.test_session_id
}
if (content.meta.test_module_id) {
this._encodeString(bytes, 'test_module_id')
this._encodeId(bytes, id(content.meta.test_module_id, 10))
delete content.meta.test_module_id
}
if (content.meta.test_suite_id) {
this._encodeString(bytes, 'test_suite_id')
this._encodeId(bytes, id(content.meta.test_suite_id, 10))
delete content.meta.test_suite_id
}
if (itrCorrelationId) {
this._encodeString(bytes, ITR_CORRELATION_ID)
this._encodeString(bytes, itrCorrelationId)
delete content.meta[ITR_CORRELATION_ID]
}
this._encodeString(bytes, 'meta')
this._encodeMap(bytes, content.meta)
this._encodeString(bytes, 'metrics')
this._encodeMap(bytes, content.metrics)
}
_encodeEvent (bytes, event) {
bytes.writeMapPrefix(Object.keys(event).length)
this._encodeString(bytes, 'type')
this._encodeString(bytes, event.type)
this._encodeString(bytes, 'version')
bytes.writeNumber(event.version)
this._encodeString(bytes, 'content')
if (event.type === 'span' || event.type === 'test') {
this._encodeEventContent(bytes, event.content)
} else if (event.type === 'test_suite_end') {
this._encodeTestSuite(bytes, event.content)
} else if (event.type === 'test_module_end') {
this._encodeTestModule(bytes, event.content)
} else if (event.type === 'test_session_end') {
this._encodeTestSession(bytes, event.content)
}
}
_encode (bytes, trace) {
const startTime = Date.now()
const events = trace.map(formatSpan)
this._eventCount += events.length
const hasTestLevelsMetadata = this.testLevelsMetadataKeys.length !== 0
for (const event of events) {
if (hasTestLevelsMetadata) {
this._removeDuplicateTestLevelsMetadata(event)
}
this._encodeEvent(bytes, event)
}
distributionMetric(
TELEMETRY_ENDPOINT_PAYLOAD_SERIALIZATION_MS,
{ endpoint: 'test_cycle' },
Date.now() - startTime
)
}
makePayload () {
distributionMetric(TELEMETRY_ENDPOINT_PAYLOAD_EVENTS_COUNT, { endpoint: 'test_cycle' }, this._eventCount)
// Encode the payload prefix (version + metadata + events-array header) at flush time,
// not on the first `_encode`. The CI Visibility flow adds metadata across multiple
// diagnostic channels (`session:start` adds `test_session.name`, the async
// `library-configuration` callback adds capability tags). Any span finished between
// those calls would otherwise freeze the prefix with stale metadata.
const prefixBytes = new MsgpackChunk(PREFIX_CHUNK_INITIAL_SIZE)
this._encodePayloadStart(prefixBytes)
const eventsOffset = this._eventsOffset
const eventsCount = this._eventCount
prefixBytes.buffer[eventsOffset] = 0xDD
prefixBytes.buffer[eventsOffset + 1] = eventsCount >> 24
prefixBytes.buffer[eventsOffset + 2] = eventsCount >> 16
prefixBytes.buffer[eventsOffset + 3] = eventsCount >> 8
prefixBytes.buffer[eventsOffset + 4] = eventsCount
const eventsBytes = this._traceBytes
const totalSize = prefixBytes.length + eventsBytes.length
// The metadata prefix (built here, not during `encode`) and the events are
// capped independently, so both can stay under the cap while the assembled
// payload crosses it. An oversized metadata tag also overflows the prefix
// chunk itself inside `_encodePayloadStart` above; either way the tagged
// error propagates to the writer's flush-time catch to drop the payload.
if (totalSize > MAX_SIZE) {
throw new OverflowError(totalSize)
}
const buffer = Buffer.allocUnsafe(totalSize)
prefixBytes.buffer.copy(buffer, 0, 0, prefixBytes.length)
eventsBytes.buffer.copy(buffer, prefixBytes.length, 0, eventsBytes.length)
this.reset()
return buffer
}
_encodePayloadStart (bytes) {
// Encodes the payload up to (and including) the `events` array prefix. The 5 reserved
// bytes for the array length are patched in `makePayload`.
const payload = {
version: ENCODING_VERSION,
metadata: {
'*': {
language: 'javascript',
library_version: ddTraceVersion,
...this.wildcardMetadataTags,
},
...this.metadataTags,
},
events: [],
}
if (this.tags.env) {
payload.metadata['*'].env = this.tags.env
}
const runtimeId = this.tags['runtime-id']
if (runtimeId) {
payload.metadata['*']['runtime-id'] = runtimeId
}
bytes.writeMapPrefix(Object.keys(payload).length)
this._encodeString(bytes, 'version')
bytes.writeNumber(payload.version)
this._encodeString(bytes, 'metadata')
const metadataKeys = Object.keys(payload.metadata)
bytes.writeMapPrefix(metadataKeys.length)
for (const metadataKey of metadataKeys) {
this._encodeString(bytes, metadataKey)
this._encodeMap(bytes, payload.metadata[metadataKey])
}
this._encodeString(bytes, 'events')
this._eventsOffset = bytes.length
bytes.reserve(5)
}
reset () {
this._reset()
this._eventCount = 0
}
}
module.exports = { AgentlessCiVisibilityEncoder }