@@ -4,6 +4,10 @@ const Chunk = require('../../../../packages/dd-trace/src/encode/chunk')
4
4
const { storage } = require ( '../../../../packages/datadog-core' )
5
5
const { Client } = require ( './client' )
6
6
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'
7
11
const ARRAY_OF_TWO = 0x92
8
12
const SOFT_LIMIT = 8 * 1024 * 1024 // 8MB
9
13
const flushInterval = 2000
@@ -49,7 +53,7 @@ class Encoder {
49
53
50
54
this . _encodeFixArray ( bytes , 7 )
51
55
this . _encodeShort ( bytes , eventTypes . KOA_REQUEST_START ) // implied: name
52
- this . _encodeLong ( bytes , 0 ) // figure out timestamp
56
+ this . _encodeLong ( bytes , now ( ) )
53
57
this . _encodeId ( bytes , store . traceContext . traceId )
54
58
this . _encodeId ( bytes , store . traceContext . spanId )
55
59
this . _encodeId ( bytes , store . traceContext . parentId ) // ???
@@ -67,7 +71,7 @@ class Encoder {
67
71
68
72
this . _encodeFixArray ( bytes , 5 )
69
73
this . _encodeShort ( bytes , eventTypes . KOA_REQUEST_FINISH ) // implied: name
70
- this . _encodeLong ( bytes , 0 ) // figure out timestamp
74
+ this . _encodeLong ( bytes , now ( ) )
71
75
this . _encodeId ( bytes , store . traceContext . traceId )
72
76
this . _encodeId ( bytes , store . traceContext . spanId )
73
77
this . _encodeShort ( bytes , res . statusCode )
@@ -83,7 +87,7 @@ class Encoder {
83
87
84
88
this . _encodeFixArray ( bytes , 7 )
85
89
this . _encodeShort ( bytes , eventTypes . ERROR ) // implied: name
86
- this . _encodeLong ( bytes , 0 ) // figure out timestamp
90
+ this . _encodeLong ( bytes , now ( ) )
87
91
this . _encodeId ( bytes , store . traceContext . traceId )
88
92
this . _encodeId ( bytes , store . traceContext . spanId )
89
93
this . _encodeString ( bytes , error . name )
@@ -93,7 +97,8 @@ class Encoder {
93
97
this . _afterEncode ( )
94
98
}
95
99
96
- makePayload ( ) {
100
+ // TODO: support new payload format
101
+ makePayload05 ( ) {
97
102
const prefixSize = 1
98
103
const stringSize = this . _stringBytes . length + 5
99
104
const eventSize = this . _eventBytes . length + 5
@@ -111,11 +116,23 @@ class Encoder {
111
116
return buffer
112
117
}
113
118
114
- makePayload04 ( ) {
119
+ makePayload ( ) {
115
120
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)
117
127
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 )
119
136
120
137
this . _reset ( )
121
138
@@ -128,7 +145,7 @@ class Encoder {
128
145
if ( count === 0 ) return
129
146
130
147
const data = this . makePayload ( )
131
- const path = `/v1.0 /events`
148
+ const path = `/v0.1 /events`
132
149
133
150
this . _timer = clearTimeout ( this . _timer )
134
151
this . _client . request ( { data, path, count } , done )
@@ -287,12 +304,12 @@ class Encoder {
287
304
}
288
305
}
289
306
290
- _encodeString ( bytes , value = '' ) {
307
+ _encodeString05 ( bytes , value = '' ) {
291
308
this . _cacheString ( value )
292
309
this . _encodeInteger ( bytes , this . _stringMap [ value ] )
293
310
}
294
311
295
- _encodeString04 ( bytes , value = '' ) {
312
+ _encodeString ( bytes , value = '' ) {
296
313
this . _cacheString ( value )
297
314
298
315
const { start, end } = this . _stringMap [ value ]
@@ -320,14 +337,14 @@ class Encoder {
320
337
}
321
338
}
322
339
323
- _cacheString ( value ) {
340
+ _cacheString05 ( value ) {
324
341
if ( ! ( value in this . _stringMap ) ) {
325
342
this . _stringMap [ value ] = this . _stringCount ++
326
343
this . _stringBytes . write ( value )
327
344
}
328
345
}
329
346
330
- _cacheString04 ( value ) {
347
+ _cacheString ( value ) {
331
348
if ( ! ( value in this . _stringMap ) ) {
332
349
this . _stringCount ++
333
350
this . _stringMap [ value ] = {
0 commit comments