1
1
'use strict'
2
2
3
3
const { expect } = require ( 'chai' )
4
+ const assert = require ( 'node:assert' )
4
5
const getPort = require ( 'get-port' )
5
6
const os = require ( 'node:os' )
6
7
const { withNamingSchema, withPeerService, withVersions } = require ( '../../dd-trace/test/setup/mocha' )
7
8
const agent = require ( '../../dd-trace/test/plugins/agent' )
8
9
const { expectedSchema, rawExpectedSchema } = require ( './naming' )
10
+ const { assertObjectContains } = require ( '../../../integration-tests/helpers' )
9
11
10
12
const sort = trace => trace . sort ( ( a , b ) => Number ( a . start - b . start ) )
11
13
@@ -30,10 +32,10 @@ describe('Plugin', () => {
30
32
broker . createService ( {
31
33
name : 'math' ,
32
34
actions : {
33
- add ( ctx ) {
35
+ async add ( ctx ) {
34
36
const numerify = this . actions . numerify
35
37
36
- return numerify ( ctx . params . a ) + numerify ( ctx . params . b )
38
+ return await numerify ( ctx . params . a ) + await numerify ( ctx . params . b )
37
39
} ,
38
40
39
41
numerify ( ctx ) {
@@ -42,6 +44,15 @@ describe('Plugin', () => {
42
44
}
43
45
} )
44
46
47
+ broker . createService ( {
48
+ name : 'error' ,
49
+ actions : {
50
+ async error ( ctx ) {
51
+ throw new Error ( 'Invalid number' )
52
+ }
53
+ }
54
+ } )
55
+
45
56
return broker . start ( )
46
57
}
47
58
@@ -159,25 +170,61 @@ describe('Plugin', () => {
159
170
'out.host'
160
171
)
161
172
162
- it ( 'should do automatic instrumentation' , done => {
173
+ it ( 'should do automatic instrumentation' , async ( ) => {
174
+ const result = await broker . call ( 'math.add' , { a : 5 , b : 3 } )
175
+ assert . strictEqual ( result , 8 )
176
+
163
177
agent . assertSomeTraces ( traces => {
164
- const spans = sort ( traces [ 0 ] )
178
+ const span = traces [ 0 ] [ 0 ]
179
+
180
+ assertObjectContains ( span , {
181
+ name : expectedSchema . client . opName ,
182
+ service : expectedSchema . client . serviceName ,
183
+ resource : 'math.add' ,
184
+ meta : {
185
+ 'span.kind' : 'client' ,
186
+ 'out.host' : hostname ,
187
+ 'moleculer.context.action' : 'math.add' ,
188
+ 'moleculer.context.node_id' : `server-${ process . pid } ` ,
189
+ 'moleculer.context.service' : 'math' ,
190
+ 'moleculer.namespace' : 'multi' ,
191
+ 'moleculer.node_id' : `server-${ process . pid } ` ,
192
+ } ,
193
+ metrics : {
194
+ 'network.destination.port' : port
195
+ }
196
+ } )
197
+
198
+ assert . strictEqual ( typeof span . meta [ 'moleculer.context.request_id' ] , 'string' )
199
+ } )
200
+ } )
165
201
166
- expect ( spans [ 0 ] ) . to . have . property ( 'name' , expectedSchema . client . opName )
167
- expect ( spans [ 0 ] ) . to . have . property ( 'service' , expectedSchema . client . serviceName )
168
- expect ( spans [ 0 ] ) . to . have . property ( 'resource' , 'math.add' )
169
- expect ( spans [ 0 ] . meta ) . to . have . property ( 'span.kind' , 'client' )
170
- expect ( spans [ 0 ] . meta ) . to . have . property ( 'out.host' , hostname )
171
- expect ( spans [ 0 ] . meta ) . to . have . property ( 'moleculer.context.action' , 'math.add' )
172
- expect ( spans [ 0 ] . meta ) . to . have . property ( 'moleculer.context.node_id' , `server-${ process . pid } ` )
173
- expect ( spans [ 0 ] . meta ) . to . have . property ( 'moleculer.context.request_id' )
174
- expect ( spans [ 0 ] . meta ) . to . have . property ( 'moleculer.context.service' , 'math' )
175
- expect ( spans [ 0 ] . meta ) . to . have . property ( 'moleculer.namespace' , 'multi' )
176
- expect ( spans [ 0 ] . meta ) . to . have . property ( 'moleculer.node_id' , `server-${ process . pid } ` )
177
- expect ( spans [ 0 ] . metrics ) . to . have . property ( 'network.destination.port' , port )
178
- } ) . then ( done , done )
202
+ it ( 'should handle error cases' , async ( ) => {
203
+ await assert . rejects ( broker . call ( 'error.error' ) , { message : 'Invalid number' } )
179
204
180
- broker . call ( 'math.add' , { a : 5 , b : 3 } ) . catch ( done )
205
+ agent . assertSomeTraces ( traces => {
206
+ const span = traces [ 0 ] [ 0 ]
207
+
208
+ assertObjectContains ( span , {
209
+ name : expectedSchema . client . opName ,
210
+ service : expectedSchema . client . serviceName ,
211
+ resource : 'error.error' ,
212
+ meta : {
213
+ 'span.kind' : 'client' ,
214
+ 'out.host' : hostname ,
215
+ 'moleculer.context.action' : 'error.error' ,
216
+ 'moleculer.context.node_id' : `server-${ process . pid } ` ,
217
+ 'moleculer.context.service' : 'error' ,
218
+ 'moleculer.namespace' : 'multi' ,
219
+ 'moleculer.node_id' : `server-${ process . pid } ` ,
220
+ } ,
221
+ metrics : {
222
+ 'network.destination.port' : port
223
+ }
224
+ } )
225
+
226
+ assert . strictEqual ( typeof span . meta [ 'moleculer.context.request_id' ] , 'string' )
227
+ } )
181
228
} )
182
229
183
230
withNamingSchema (
@@ -331,6 +378,47 @@ describe('Plugin', () => {
331
378
expect ( spanId . toString ( ) ) . to . equal ( parentId . toString ( ) )
332
379
} )
333
380
} )
381
+ describe ( 'meta propagation' , ( ) => {
382
+ before ( ( ) => agent . load ( 'moleculer' , {
383
+ meta : true
384
+ } ) )
385
+
386
+ before ( async ( ) => {
387
+ const { ServiceBroker } = require ( `../../../versions/moleculer@${ version } ` ) . get ( )
388
+ broker = new ServiceBroker ( {
389
+ nodeID : `server-${ process . pid } ` ,
390
+ logger : false
391
+ } )
392
+
393
+ broker . createService ( {
394
+ name : 'test' ,
395
+ actions : {
396
+ async first ( ctx ) {
397
+ await ctx . call ( 'test.second' , null , {
398
+ meta : {
399
+ a : 'John'
400
+ }
401
+ } )
402
+ return ctx . meta . a
403
+ } ,
404
+ second ( ctx ) {
405
+ ctx . meta . a = 'Doe'
406
+ }
407
+ }
408
+ } )
409
+
410
+ return broker . start ( )
411
+ } )
412
+
413
+ after ( ( ) => broker . stop ( ) )
414
+
415
+ after ( ( ) => agent . close ( { ritmReset : false } ) )
416
+
417
+ it ( 'should propagate meta from child to parent' , async ( ) => {
418
+ const result = await broker . call ( 'test.first' )
419
+ assert . strictEqual ( result , 'Doe' )
420
+ } )
421
+ } )
334
422
} )
335
423
} )
336
424
} )
0 commit comments