Skip to content

Commit 0e7ac4b

Browse files
committed
add fake db to benchmark and update event support
1 parent 10dc79b commit 0e7ac4b

File tree

5 files changed

+263
-155
lines changed

5 files changed

+263
-155
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
'use strict'
2+
3+
const { id, zeroId } = require('./id')
4+
5+
class TraceContext {
6+
constructor (childOf) {
7+
if (childOf) {
8+
this.traceId = childOf.traceId
9+
this.spanId = id()
10+
this.parentId = childOf.spanId
11+
} else {
12+
this.traceId = id()
13+
this.spanId = this.traceId
14+
this.parentId = zeroId
15+
}
16+
}
17+
}
18+
19+
module.exports = { TraceContext }

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

Lines changed: 84 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
const Chunk = require('../../../../packages/dd-trace/src/encode/chunk')
44
const { storage } = require('../../../../packages/datadog-core')
55
const { Client } = require('./client')
6+
const { zeroId } = require('./id')
67

78
const processStartTime = BigInt(Date.now() * 1e6)
89
const processStartTicks = process.hrtime.bigint()
@@ -13,12 +14,13 @@ const SOFT_LIMIT = 8 * 1024 * 1024 // 8MB
1314
const flushInterval = 2000
1415
const noop = () => {}
1516
const eventTypes = {
16-
KOA_REQUEST_START: 1,
17+
WEB_REQUEST_START: 1,
1718
ERROR: 2,
18-
KOA_REQUEST_FINISH: 3,
19+
WEB_REQUEST_FINISH: 3,
1920
START_SPAN: 4,
2021
FINISH_SPAN: 5,
21-
ADD_TAGS: 6
22+
ADD_TAGS: 6,
23+
MYSQL_START_SPAN: 8
2224
}
2325

2426
const float64Array = new Float64Array(1)
@@ -44,90 +46,80 @@ class Encoder {
4446
return this._eventCount
4547
}
4648

47-
// encodeKoaRequestStart (req) {
48-
// const bytes = this._eventBytes
49-
// const store = storage.getStore()
50-
51-
// if (!store || !store.traceContext) return
52-
53-
// this._encodeFixArray(bytes, 2)
54-
// this._encodeShort(bytes, eventTypes.START_SPAN)
55-
// this._encodeFixArray(bytes, 10)
56-
// this._encodeLong(bytes, now())
57-
// this._encodeId(bytes, store.traceContext.traceId)
58-
// this._encodeId(bytes, store.traceContext.spanId)
59-
// this._encodeId(bytes, store.traceContext.parentId)
60-
// this._encodeString(bytes, service)
61-
// this._encodeString(bytes, 'koa.request')
62-
// this._encodeString(bytes, `${req.method} ${req.url}`)
63-
// this._encodeFixMap(bytes, 2)
64-
// this._encodeString(bytes, 'http.method')
65-
// this._encodeString(bytes, req.method)
66-
// this._encodeString(bytes, 'http.url')
67-
// this._encodeString(bytes, req.url)
68-
// this._encodeFixMap(bytes, 0)
69-
// this._encodeString(bytes, 'web')
70-
71-
// this._afterEncode()
72-
// }
73-
74-
encodeKoaRequestStart (req) {
49+
encodeWebRequestStart (req, component) {
7550
const bytes = this._eventBytes
7651
const store = storage.getStore()
7752

7853
if (!store || !store.traceContext) return
7954

80-
// service name comes from process
81-
// span name comes from event type
82-
// resource comes from mixing http method and route
83-
// error will be its own event
84-
8555
this._encodeFixArray(bytes, 2)
86-
this._encodeUnsigned(bytes, eventTypes.KOA_REQUEST_START) // implied: name
87-
this._encodeFixArray(bytes, 6)
56+
this._encodeByte(bytes, eventTypes.WEB_REQUEST_START)
57+
this._encodeFixArray(bytes, 8)
8858
this._encodeLong(bytes, now())
8959
this._encodeId(bytes, store.traceContext.traceId)
9060
this._encodeId(bytes, store.traceContext.spanId)
91-
this._encodeId(bytes, store.traceContext.parentId) // ???
61+
this._encodeId(bytes, store.traceContext.parentId)
62+
this._encodeString(bytes, component)
9263
this._encodeString(bytes, req.method)
9364
this._encodeString(bytes, req.url)
65+
this._encodeString(bytes, req.url) // route
9466

9567
this._afterEncode()
9668
}
9769

98-
// encodeKoaRequestFinish (res) {
99-
// const bytes = this._eventBytes
100-
// const store = storage.getStore()
70+
encodeWebRequestFinish (res) {
71+
const bytes = this._eventBytes
72+
const store = storage.getStore()
10173

102-
// if (!store || !store.traceContext) return
74+
if (!store || !store.traceContext) return
10375

104-
// this._encodeFixArray(bytes, 2)
105-
// this._encodeShort(bytes, eventTypes.FINISH_SPAN)
106-
// this._encodeFixArray(bytes, 5)
107-
// this._encodeLong(bytes, now())
108-
// this._encodeId(bytes, store.traceContext.traceId)
109-
// this._encodeId(bytes, store.traceContext.spanId)
110-
// this._encodeFixMap(bytes, 1)
111-
// this._encodeString(bytes, 'http.status_code')
112-
// this._encodeString(bytes, String(res.statusCode || 0))
113-
// this._encodeFixMap(bytes, 0)
76+
this._encodeFixArray(bytes, 2)
77+
this._encodeByte(bytes, eventTypes.WEB_REQUEST_FINISH)
78+
this._encodeFixArray(bytes, 3)
79+
this._encodeLong(bytes, now())
80+
this._encodeId(bytes, store.traceContext.traceId)
81+
this._encodeId(bytes, store.traceContext.spanId)
82+
this._encodeShort(bytes, res.statusCode)
11483

115-
// this._afterEncode()
116-
// }
84+
this._afterEncode()
85+
}
11786

118-
encodeKoaRequestFinish (res) {
87+
encodeMysqlQueryStart (query) {
11988
const bytes = this._eventBytes
12089
const store = storage.getStore()
12190

12291
if (!store || !store.traceContext) return
12392

12493
this._encodeFixArray(bytes, 2)
125-
this._encodeUnsigned(bytes, eventTypes.KOA_REQUEST_FINISH) // implied: name
126-
this._encodeFixArray(bytes, 4)
94+
this._encodeByte(bytes, eventTypes.MYSQL_START_SPAN)
95+
this._encodeFixArray(bytes, 9)
12796
this._encodeLong(bytes, now())
12897
this._encodeId(bytes, store.traceContext.traceId)
12998
this._encodeId(bytes, store.traceContext.spanId)
130-
this._encodeUnsigned(bytes, res.statusCode)
99+
this._encodeId(bytes, store.traceContext.parentId)
100+
this._encodeString(bytes, query.sql)
101+
this._encodeString(bytes, query.conf.database)
102+
this._encodeString(bytes, query.conf.user)
103+
this._encodeString(bytes, query.conf.host)
104+
this._encodeString(bytes, query.conf.port)
105+
106+
this._afterEncode()
107+
}
108+
109+
encodeFinish () {
110+
const bytes = this._eventBytes
111+
const store = storage.getStore()
112+
113+
if (!store || !store.traceContext) return
114+
115+
this._encodeFixArray(bytes, 2)
116+
this._encodeByte(bytes, eventTypes.FINISH_SPAN)
117+
this._encodeFixArray(bytes, 5)
118+
this._encodeLong(bytes, now())
119+
this._encodeId(bytes, store.traceContext.traceId)
120+
this._encodeId(bytes, store.traceContext.spanId)
121+
this._encodeFixMap(bytes, 0)
122+
this._encodeFixMap(bytes, 0)
131123

132124
this._afterEncode()
133125
}
@@ -139,14 +131,17 @@ class Encoder {
139131
if (!store || !store.traceContext) return // TODO: support errors without tracing
140132

141133
this._encodeFixArray(bytes, 2)
142-
this._encodeShort(bytes, eventTypes.ERROR) // implied: name
143-
this._encodeFixArray(bytes, 6)
134+
this._encodeByte(bytes, eventTypes.ERROR) // implied: name
135+
this._encodeFixArray(bytes, error ? 6 : 3)
144136
this._encodeLong(bytes, now())
145137
this._encodeId(bytes, store.traceContext.traceId)
146138
this._encodeId(bytes, store.traceContext.spanId)
147-
this._encodeString(bytes, error.name)
148-
this._encodeString(bytes, error.message)
149-
this._encodeString(bytes, error.stack)
139+
140+
if (error) {
141+
this._encodeString(bytes, error.name)
142+
this._encodeString(bytes, error.message)
143+
this._encodeString(bytes, error.stack)
144+
}
150145

151146
this._afterEncode()
152147
}
@@ -261,18 +256,25 @@ class Encoder {
261256
_encodeId (bytes, id) {
262257
const offset = bytes.length
263258

264-
bytes.reserve(9)
265-
bytes.length += 9
259+
if (id === zeroId) {
260+
bytes.reserve(1)
261+
bytes.length += 1
266262

267-
bytes.buffer[offset] = 0xcf
268-
bytes.buffer[offset + 1] = id[0]
269-
bytes.buffer[offset + 2] = id[1]
270-
bytes.buffer[offset + 3] = id[2]
271-
bytes.buffer[offset + 4] = id[3]
272-
bytes.buffer[offset + 5] = id[4]
273-
bytes.buffer[offset + 6] = id[5]
274-
bytes.buffer[offset + 7] = id[6]
275-
bytes.buffer[offset + 8] = id[7]
263+
bytes.buffer[offset] = 0x00
264+
} else {
265+
bytes.reserve(9)
266+
bytes.length += 9
267+
268+
bytes.buffer[offset] = 0xcf
269+
bytes.buffer[offset + 1] = id[0]
270+
bytes.buffer[offset + 2] = id[1]
271+
bytes.buffer[offset + 3] = id[2]
272+
bytes.buffer[offset + 4] = id[3]
273+
bytes.buffer[offset + 5] = id[4]
274+
bytes.buffer[offset + 6] = id[5]
275+
bytes.buffer[offset + 7] = id[6]
276+
bytes.buffer[offset + 8] = id[7]
277+
}
276278
}
277279

278280
_encodeInteger (bytes, value) {
@@ -321,7 +323,12 @@ class Encoder {
321323
_encodeUnsigned (bytes, value) {
322324
const offset = bytes.length
323325

324-
if (value <= 0xff) {
326+
if (value <= 0x7f) {
327+
bytes.reserve(1)
328+
bytes.length += 1
329+
330+
bytes.buffer[offset] = value
331+
} else if (value <= 0xff) {
325332
bytes.reserve(2)
326333
bytes.length += 2
327334

@@ -446,4 +453,4 @@ class Encoder {
446453
}
447454
}
448455

449-
module.exports = { Encoder }
456+
module.exports = { Encoder, encoder: new Encoder() }
Lines changed: 4 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,29 @@
11
'use strict'
22

33
const { channel } = require('diagnostics_channel')
4-
const { Encoder } = require('./encoder')
4+
const { encoder } = require('./encoder')
5+
const { TraceContext } = require('./context')
56
const { storage } = require('../../../../packages/datadog-core')
6-
const { id, zeroId } = require('./id')
77

88
const startChannel = channel('apm:koa:request:start')
99
const errorChannel = channel('apm:koa:request:error')
1010
const asyncEndChannel = channel('apm:koa:request:async-end')
1111

12-
const encoder = new Encoder()
13-
14-
class TraceContext {
15-
constructor (childOf) {
16-
if (childOf) {
17-
this.traceId = childOf.traceId
18-
this.spanId = id()
19-
this.parentId = childOf.spanId
20-
} else {
21-
this.traceId = id()
22-
this.spanId = this.traceId
23-
this.parentId = zeroId
24-
}
25-
}
26-
}
27-
2812
startChannel.subscribe(({ req }) => {
2913
const store = storage.getStore()
3014
const traceContext = new TraceContext(store.traceContext)
3115

3216
store.traceContext = traceContext
3317

34-
encoder.encodeKoaRequestStart(req)
18+
encoder.encodeWebRequestStart(req, 'koa')
3519
})
3620

3721
errorChannel.subscribe(error => {
3822
encoder.encodeError(error)
3923
})
4024

4125
asyncEndChannel.subscribe(({ res }) => {
42-
encoder.encodeKoaRequestFinish(res)
26+
encoder.encodeWebRequestFinish(res, 'koa')
4327

4428
// TODO: restore parent context
4529
})

0 commit comments

Comments
 (0)