3
3
const Chunk = require ( '../../../../packages/dd-trace/src/encode/chunk' )
4
4
const { storage } = require ( '../../../../packages/datadog-core' )
5
5
const { Client } = require ( './client' )
6
+ const { zeroId } = require ( './id' )
6
7
7
8
const processStartTime = BigInt ( Date . now ( ) * 1e6 )
8
9
const processStartTicks = process . hrtime . bigint ( )
@@ -13,12 +14,13 @@ const SOFT_LIMIT = 8 * 1024 * 1024 // 8MB
13
14
const flushInterval = 2000
14
15
const noop = ( ) => { }
15
16
const eventTypes = {
16
- KOA_REQUEST_START : 1 ,
17
+ WEB_REQUEST_START : 1 ,
17
18
ERROR : 2 ,
18
- KOA_REQUEST_FINISH : 3 ,
19
+ WEB_REQUEST_FINISH : 3 ,
19
20
START_SPAN : 4 ,
20
21
FINISH_SPAN : 5 ,
21
- ADD_TAGS : 6
22
+ ADD_TAGS : 6 ,
23
+ MYSQL_START_SPAN : 8
22
24
}
23
25
24
26
const float64Array = new Float64Array ( 1 )
@@ -44,90 +46,80 @@ class Encoder {
44
46
return this . _eventCount
45
47
}
46
48
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 ) {
75
50
const bytes = this . _eventBytes
76
51
const store = storage . getStore ( )
77
52
78
53
if ( ! store || ! store . traceContext ) return
79
54
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
-
85
55
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 )
88
58
this . _encodeLong ( bytes , now ( ) )
89
59
this . _encodeId ( bytes , store . traceContext . traceId )
90
60
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 )
92
63
this . _encodeString ( bytes , req . method )
93
64
this . _encodeString ( bytes , req . url )
65
+ this . _encodeString ( bytes , req . url ) // route
94
66
95
67
this . _afterEncode ( )
96
68
}
97
69
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 ( )
101
73
102
- // if (!store || !store.traceContext) return
74
+ if ( ! store || ! store . traceContext ) return
103
75
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 )
114
83
115
- // this._afterEncode()
116
- // }
84
+ this . _afterEncode ( )
85
+ }
117
86
118
- encodeKoaRequestFinish ( res ) {
87
+ encodeMysqlQueryStart ( query ) {
119
88
const bytes = this . _eventBytes
120
89
const store = storage . getStore ( )
121
90
122
91
if ( ! store || ! store . traceContext ) return
123
92
124
93
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 )
127
96
this . _encodeLong ( bytes , now ( ) )
128
97
this . _encodeId ( bytes , store . traceContext . traceId )
129
98
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 )
131
123
132
124
this . _afterEncode ( )
133
125
}
@@ -139,14 +131,17 @@ class Encoder {
139
131
if ( ! store || ! store . traceContext ) return // TODO: support errors without tracing
140
132
141
133
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 )
144
136
this . _encodeLong ( bytes , now ( ) )
145
137
this . _encodeId ( bytes , store . traceContext . traceId )
146
138
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
+ }
150
145
151
146
this . _afterEncode ( )
152
147
}
@@ -261,18 +256,25 @@ class Encoder {
261
256
_encodeId ( bytes , id ) {
262
257
const offset = bytes . length
263
258
264
- bytes . reserve ( 9 )
265
- bytes . length += 9
259
+ if ( id === zeroId ) {
260
+ bytes . reserve ( 1 )
261
+ bytes . length += 1
266
262
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
+ }
276
278
}
277
279
278
280
_encodeInteger ( bytes , value ) {
@@ -321,7 +323,12 @@ class Encoder {
321
323
_encodeUnsigned ( bytes , value ) {
322
324
const offset = bytes . length
323
325
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 ) {
325
332
bytes . reserve ( 2 )
326
333
bytes . length += 2
327
334
@@ -446,4 +453,4 @@ class Encoder {
446
453
}
447
454
}
448
455
449
- module . exports = { Encoder }
456
+ module . exports = { Encoder, encoder : new Encoder ( ) }
0 commit comments