Skip to content

Commit 9099b48

Browse files
authored
remove async storage from moleculer (#5956)
* remove async storage from moleculer
1 parent a118cce commit 9099b48

File tree

4 files changed

+44
-36
lines changed

4 files changed

+44
-36
lines changed

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

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use strict'
22

3-
const { channel, addHook, AsyncResource } = require('../helpers/instrument')
3+
const { channel, addHook } = require('../helpers/instrument')
44
const shimmer = require('../../../datadog-shimmer')
55

66
const startChannel = channel('apm:moleculer:call:start')
@@ -9,29 +9,28 @@ const errorChannel = channel('apm:moleculer:call:error')
99

1010
function wrapCall (call) {
1111
return function (actionName, params, opts) {
12-
const callResource = new AsyncResource('bound-anonymous-fn')
13-
1412
opts = arguments[2] = opts || {}
1513
opts.meta = opts.meta || {}
1614

1715
arguments.length = Math.max(3, arguments.length)
1816

19-
return callResource.runInAsyncScope(() => {
20-
startChannel.publish({ actionName, params, opts })
21-
17+
const ctx = { actionName, params, opts }
18+
return startChannel.runStores(ctx, () => {
2219
const promise = call.apply(this, arguments)
2320
const broker = this
24-
const ctx = promise.ctx
21+
ctx.promiseCtx = promise.ctx
22+
ctx.broker = broker
2523

2624
return promise
2725
.then(
2826
result => {
29-
finishChannel.publish({ broker, ctx })
27+
finishChannel.publish(ctx)
3028
return result
3129
},
3230
error => {
33-
errorChannel.publish(error)
34-
finishChannel.publish({ broker, ctx })
31+
ctx.error = error
32+
errorChannel.publish(ctx)
33+
finishChannel.publish(ctx)
3534
throw error
3635
}
3736
)

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

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

3-
const { channel, addHook, AsyncResource } = require('../helpers/instrument')
3+
const { channel, addHook } = require('../helpers/instrument')
44
const shimmer = require('../../../datadog-shimmer')
55

66
const startChannel = channel('apm:moleculer:action:start')
@@ -24,27 +24,26 @@ function createMiddleware () {
2424
localAction (next, action) {
2525
const broker = this
2626

27-
return shimmer.wrapFunction(next, next => function datadogMiddleware (ctx) {
28-
const actionResource = new AsyncResource('bound-anonymous-fn')
29-
30-
return actionResource.runInAsyncScope(() => {
31-
startChannel.publish({ action, ctx, broker })
32-
27+
return shimmer.wrapFunction(next, next => function datadogMiddleware (middlewareCtx) {
28+
const ctx = { action, middlewareCtx, broker }
29+
return startChannel.runStores(ctx, () => {
3330
try {
34-
return next(ctx).then(
31+
return next(middlewareCtx).then(
3532
result => {
36-
finishChannel.publish()
33+
finishChannel.publish(ctx)
3734
return result
3835
},
3936
error => {
40-
errorChannel.publish(error)
41-
finishChannel.publish()
37+
ctx.error = error
38+
errorChannel.publish(ctx)
39+
finishChannel.publish(ctx)
4240
throw error
4341
}
4442
)
4543
} catch (e) {
46-
errorChannel.publish(e)
47-
finishChannel.publish()
44+
ctx.error = e
45+
errorChannel.publish(ctx)
46+
finishChannel.publish(ctx)
4847
}
4948
})
5049
})

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

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,29 +7,35 @@ class MoleculerClientPlugin extends ClientPlugin {
77
static get id () { return 'moleculer' }
88
static get operation () { return 'call' }
99

10-
start ({ actionName, opts }) {
10+
bindStart (ctx) {
11+
const { actionName, opts } = ctx
12+
1113
const span = this.startSpan(this.operationName(), {
1214
service: this.config.service || this.serviceName(),
1315
resource: actionName,
1416
kind: 'client'
15-
})
17+
}, ctx)
1618

1719
this.tracer.inject(span, 'text_map', opts.meta)
20+
21+
return ctx.currentStore
1822
}
1923

20-
finish ({ broker, ctx }) {
21-
const span = this.activeSpan
24+
finish (ctx) {
25+
const { promiseCtx, broker } = ctx
26+
27+
const span = ctx.currentStore.span || this.activeSpan
2228

23-
if (ctx) {
24-
const endpoint = ctx.endpoint || {}
29+
if (promiseCtx) {
30+
const endpoint = promiseCtx.endpoint || {}
2531
const node = endpoint.node || {}
2632

2733
this.addHost(node.hostname, node.port)
2834

29-
span.addTags(moleculerTags(broker, ctx, this.config))
35+
span.addTags(moleculerTags(broker, promiseCtx, this.config))
3036
}
3137

32-
super.finish()
38+
super.finish(ctx)
3339
}
3440
}
3541

packages/datadog-plugin-moleculer/src/server.js

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,23 @@ class MoleculerServerPlugin extends ServerPlugin {
77
static get id () { return 'moleculer' }
88
static get operation () { return 'action' }
99

10-
start ({ action, ctx, broker }) {
11-
const followsFrom = this.tracer.extract('text_map', ctx.meta)
10+
bindStart (ctx) {
11+
const { action, middlewareCtx, broker } = ctx
12+
13+
const followsFrom = this.tracer.extract('text_map', middlewareCtx.meta)
1214
this.startSpan(this.operationName(), {
13-
childOf: followsFrom || this.activeSpan,
15+
childOf: followsFrom || ctx?.currentStore?.span || this.activeSpan,
1416
service: this.config.service || this.serviceName(),
1517
resource: action.name,
1618
kind: 'server',
1719
type: 'web',
1820
meta: {
1921
'resource.name': action.name,
20-
...moleculerTags(broker, ctx, this.config)
22+
...moleculerTags(broker, middlewareCtx, this.config)
2123
}
22-
})
24+
}, ctx)
25+
26+
return ctx.currentStore
2327
}
2428
}
2529

0 commit comments

Comments
 (0)