Skip to content

Commit 1d0a993

Browse files
wconti27rochdev
authored andcommitted
bug(tracing): fix moleculer client meta propagation (#6270)
* fix moleculer meta propagation
1 parent ff804be commit 1d0a993

File tree

2 files changed

+108
-21
lines changed

2 files changed

+108
-21
lines changed

packages/datadog-instrumentations/src/moleculer/client.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,19 +21,18 @@ function wrapCall (call) {
2121
ctx.promiseCtx = promise.ctx
2222
ctx.broker = broker
2323

24-
return promise
24+
promise
2525
.then(
2626
result => {
2727
finishChannel.publish(ctx)
28-
return result
2928
},
3029
error => {
3130
ctx.error = error
3231
errorChannel.publish(ctx)
3332
finishChannel.publish(ctx)
34-
throw error
3533
}
3634
)
35+
return promise
3736
})
3837
}
3938
}

packages/datadog-plugin-moleculer/test/index.spec.js

Lines changed: 106 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
'use strict'
22

33
const { expect } = require('chai')
4+
const assert = require('node:assert')
45
const getPort = require('get-port')
56
const os = require('node:os')
67
const { withNamingSchema, withPeerService, withVersions } = require('../../dd-trace/test/setup/mocha')
78
const agent = require('../../dd-trace/test/plugins/agent')
89
const { expectedSchema, rawExpectedSchema } = require('./naming')
10+
const { assertObjectContains } = require('../../../integration-tests/helpers')
911

1012
const sort = trace => trace.sort((a, b) => Number(a.start - b.start))
1113

@@ -30,10 +32,10 @@ describe('Plugin', () => {
3032
broker.createService({
3133
name: 'math',
3234
actions: {
33-
add (ctx) {
35+
async add (ctx) {
3436
const numerify = this.actions.numerify
3537

36-
return numerify(ctx.params.a) + numerify(ctx.params.b)
38+
return await numerify(ctx.params.a) + await numerify(ctx.params.b)
3739
},
3840

3941
numerify (ctx) {
@@ -42,6 +44,15 @@ describe('Plugin', () => {
4244
}
4345
})
4446

47+
broker.createService({
48+
name: 'error',
49+
actions: {
50+
async error (ctx) {
51+
throw new Error('Invalid number')
52+
}
53+
}
54+
})
55+
4556
return broker.start()
4657
}
4758

@@ -159,25 +170,61 @@ describe('Plugin', () => {
159170
'out.host'
160171
)
161172

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+
163177
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+
})
165201

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' })
179204

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+
})
181228
})
182229

183230
withNamingSchema(
@@ -331,6 +378,47 @@ describe('Plugin', () => {
331378
expect(spanId.toString()).to.equal(parentId.toString())
332379
})
333380
})
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+
})
334422
})
335423
})
336424
})

0 commit comments

Comments
 (0)