Skip to content

Commit db2ca5c

Browse files
rochdevtlhunter
authored andcommitted
add collector to convert events to spans
1 parent 170793c commit db2ca5c

File tree

9 files changed

+1737
-14
lines changed

9 files changed

+1737
-14
lines changed

.editorconfig

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,8 @@ charset = utf-8
1010
trim_trailing_whitespace = true
1111
insert_final_newline = true
1212

13+
[*.rs]
14+
indent_size = 4
15+
1316
[*.md]
1417
trim_trailing_whitespace = false

benchmark/sirun/plugin-koa/internal-tracer/client.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ class Client {
1414
request (options, done) {
1515
if (options.count === 0) return
1616

17-
const url = new URL(DD_TRACE_AGENT_URL || 'http://127.0.0.1:8126')
17+
const url = new URL(DD_TRACE_AGENT_URL || 'http://127.0.0.1:8127')
1818
const isSecure = url.protocol === 'https:'
1919
const isUnix = url.protocol === 'unix:'
2020
const client = isSecure ? https : http

benchmark/sirun/plugin-koa/internal-tracer/encoder.js

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ const Chunk = require('../../../../packages/dd-trace/src/encode/chunk')
44
const { storage } = require('../../../../packages/datadog-core')
55
const { Client } = require('./client')
66

7+
const processStartTime = BigInt(Date.now() * 1e6)
8+
const processStartTicks = process.hrtime.bigint()
9+
const now = () => Number(processStartTime + process.hrtime.bigint() - processStartTicks)
10+
const service = process.env.DD_SERVICE || 'unnamed-node-app'
711
const ARRAY_OF_TWO = 0x92
812
const SOFT_LIMIT = 8 * 1024 * 1024 // 8MB
913
const flushInterval = 2000
@@ -49,7 +53,7 @@ class Encoder {
4953

5054
this._encodeFixArray(bytes, 7)
5155
this._encodeShort(bytes, eventTypes.KOA_REQUEST_START) // implied: name
52-
this._encodeLong(bytes, 0) // figure out timestamp
56+
this._encodeLong(bytes, now())
5357
this._encodeId(bytes, store.traceContext.traceId)
5458
this._encodeId(bytes, store.traceContext.spanId)
5559
this._encodeId(bytes, store.traceContext.parentId) // ???
@@ -67,7 +71,7 @@ class Encoder {
6771

6872
this._encodeFixArray(bytes, 5)
6973
this._encodeShort(bytes, eventTypes.KOA_REQUEST_FINISH) // implied: name
70-
this._encodeLong(bytes, 0) // figure out timestamp
74+
this._encodeLong(bytes, now())
7175
this._encodeId(bytes, store.traceContext.traceId)
7276
this._encodeId(bytes, store.traceContext.spanId)
7377
this._encodeShort(bytes, res.statusCode)
@@ -83,7 +87,7 @@ class Encoder {
8387

8488
this._encodeFixArray(bytes, 7)
8589
this._encodeShort(bytes, eventTypes.ERROR) // implied: name
86-
this._encodeLong(bytes, 0) // figure out timestamp
90+
this._encodeLong(bytes, now())
8791
this._encodeId(bytes, store.traceContext.traceId)
8892
this._encodeId(bytes, store.traceContext.spanId)
8993
this._encodeString(bytes, error.name)
@@ -93,7 +97,8 @@ class Encoder {
9397
this._afterEncode()
9498
}
9599

96-
makePayload () {
100+
// TODO: support new payload format
101+
makePayload05 () {
97102
const prefixSize = 1
98103
const stringSize = this._stringBytes.length + 5
99104
const eventSize = this._eventBytes.length + 5
@@ -111,11 +116,23 @@ class Encoder {
111116
return buffer
112117
}
113118

114-
makePayload04 () {
119+
makePayload () {
115120
const eventSize = this._eventBytes.length + 5
116-
const buffer = Buffer.allocUnsafe(eventSize)
121+
const serviceLength = Buffer.byteLength(service)
122+
const buffer = Buffer.allocUnsafe(eventSize + 18 + serviceLength)
123+
124+
let offset = 0
125+
126+
buffer[offset++] = 0x82 // fixmap(2)
117127

118-
this._writeEvents(buffer)
128+
buffer[offset++] = 0xa7 // fixstr(7)
129+
offset += buffer.write('service', offset)
130+
buffer[offset++] = 0xd9 // str8
131+
buffer[offset++] = serviceLength
132+
offset += buffer.write(service, offset)
133+
buffer[offset++] = 0xa6 // fixstr(6)
134+
offset += buffer.write('events', offset)
135+
offset = this._writeEvents(buffer, offset)
119136

120137
this._reset()
121138

@@ -128,7 +145,7 @@ class Encoder {
128145
if (count === 0) return
129146

130147
const data = this.makePayload()
131-
const path = `/v1.0/events`
148+
const path = `/v0.1/events`
132149

133150
this._timer = clearTimeout(this._timer)
134151
this._client.request({ data, path, count }, done)
@@ -287,12 +304,12 @@ class Encoder {
287304
}
288305
}
289306

290-
_encodeString (bytes, value = '') {
307+
_encodeString05 (bytes, value = '') {
291308
this._cacheString(value)
292309
this._encodeInteger(bytes, this._stringMap[value])
293310
}
294311

295-
_encodeString04 (bytes, value = '') {
312+
_encodeString (bytes, value = '') {
296313
this._cacheString(value)
297314

298315
const { start, end } = this._stringMap[value]
@@ -320,14 +337,14 @@ class Encoder {
320337
}
321338
}
322339

323-
_cacheString (value) {
340+
_cacheString05 (value) {
324341
if (!(value in this._stringMap)) {
325342
this._stringMap[value] = this._stringCount++
326343
this._stringBytes.write(value)
327344
}
328345
}
329346

330-
_cacheString04 (value) {
347+
_cacheString (value) {
331348
if (!(value in this._stringMap)) {
332349
this._stringCount++
333350
this._stringMap[value] = {

benchmark/sirun/plugin-koa/internal-tracer/trace.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ errorChannel.subscribe(error => {
3838
encoder.encodeError(error)
3939
})
4040

41-
asyncEndChannel.subscribe(res => {
41+
asyncEndChannel.subscribe(({ res }) => {
4242
encoder.encodeKoaRequestFinish(res)
4343

4444
// TODO: restore parent context

collector/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
target

0 commit comments

Comments
 (0)