Skip to content

Commit 8cdf312

Browse files
authored
remove async storage from elasticsearch instrumentation (#5935)
* remove async storage from elasticsearch
1 parent f8c0e9c commit 8cdf312

File tree

4 files changed

+51
-44
lines changed

4 files changed

+51
-44
lines changed

packages/datadog-instrumentations/src/elasticsearch.js

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@
22

33
const {
44
channel,
5-
addHook,
6-
AsyncResource
5+
addHook
76
} = require('./helpers/instrument')
87
const shimmer = require('../../datadog-shimmer')
98

@@ -73,33 +72,30 @@ function createWrapRequest (name) {
7372

7473
if (!params) return request.apply(this, arguments)
7574

76-
const parentResource = new AsyncResource('bound-anonymous-fn')
77-
const asyncResource = new AsyncResource('bound-anonymous-fn')
78-
79-
return asyncResource.runInAsyncScope(() => {
80-
startCh.publish({ params })
81-
75+
const ctx = { params }
76+
return startCh.runStores(ctx, () => {
8277
try {
8378
const lastIndex = arguments.length - 1
8479
cb = arguments[lastIndex]
8580

8681
if (typeof cb === 'function') {
87-
cb = parentResource.bind(cb)
88-
89-
arguments[lastIndex] = shimmer.wrapFunction(cb, cb => asyncResource.bind(function (error) {
90-
finish(params, error)
91-
return cb.apply(null, arguments)
92-
}))
82+
arguments[lastIndex] = shimmer.wrapFunction(cb, cb => function (error) {
83+
if (error) {
84+
ctx.error = error
85+
errorCh.publish(ctx)
86+
}
87+
return finishCh.runStores(ctx, cb, null, ...arguments)
88+
})
9389
return request.apply(this, arguments)
9490
}
9591
const promise = request.apply(this, arguments)
9692
if (promise && typeof promise.then === 'function') {
97-
const onResolve = asyncResource.bind(() => finish(params))
98-
const onReject = asyncResource.bind(e => finish(params, e))
93+
const onResolve = () => finish(ctx)
94+
const onReject = e => finish(ctx, e)
9995

10096
promise.then(onResolve, onReject)
10197
} else {
102-
finish(params)
98+
finish(ctx)
10399
}
104100
return promise
105101
} catch (err) {
@@ -112,11 +108,12 @@ function createWrapRequest (name) {
112108
}
113109
}
114110

115-
function finish (params, error) {
111+
function finish (ctx, error) {
116112
if (error) {
113+
ctx.error = error
117114
errorCh.publish(error)
118115
}
119-
finishCh.publish({ params })
116+
return finishCh.runStores(ctx, () => {})
120117
}
121118
}
122119

packages/datadog-instrumentations/src/sharedb.js

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@
22

33
const {
44
channel,
5-
addHook,
6-
AsyncResource
5+
addHook
76
} = require('./helpers/instrument')
87
const shimmer = require('../../datadog-shimmer')
98

@@ -37,30 +36,25 @@ addHook({ name: 'sharedb', versions: ['>=1'], file: 'lib/agent.js' }, Agent => {
3736
const errorCh = channel('apm:sharedb:request:error')
3837

3938
shimmer.wrap(Agent.prototype, '_handleMessage', origHandleMessageFn => function (request, callback) {
40-
const callbackResource = new AsyncResource('bound-anonymous-fn')
41-
const asyncResource = new AsyncResource('bound-anonymous-fn')
42-
4339
const action = request.a
4440
const actionName = getReadableActionName(action)
4541

46-
return asyncResource.runInAsyncScope(() => {
47-
startCh.publish({ actionName, request })
48-
49-
callback = callbackResource.bind(callback)
50-
51-
arguments[1] = shimmer.wrapFunction(callback, callback => asyncResource.bind(function (error, res) {
42+
const ctx = { actionName, request }
43+
return startCh.runStores(ctx, () => {
44+
arguments[1] = shimmer.wrapFunction(callback, callback => function (error, res) {
5245
if (error) {
46+
ctx.error = error
5347
errorCh.publish(error)
5448
}
55-
finishCh.publish({ request, res })
56-
57-
return callback.apply(this, arguments)
58-
}))
49+
ctx.res = res
50+
return finishCh.runStores(ctx, callback, this, ...arguments)
51+
})
5952

6053
try {
6154
return origHandleMessageFn.apply(this, arguments)
6255
} catch (error) {
63-
errorCh.publish(error)
56+
ctx.error = error
57+
errorCh.publish(ctx)
6458

6559
throw error
6660
}

packages/datadog-plugin-elasticsearch/src/index.js

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@ const DatabasePlugin = require('../../dd-trace/src/plugins/database')
55
class ElasticsearchPlugin extends DatabasePlugin {
66
static get id () { return 'elasticsearch' }
77

8-
start ({ params }) {
8+
bindStart (ctx) {
9+
const { params } = ctx
10+
911
const body = getBody(params.body || params.bulkBody)
1012

1113
this.startSpan(this.operationName(), {
@@ -20,13 +22,19 @@ class ElasticsearchPlugin extends DatabasePlugin {
2022
[`${this.system}.body`]: body,
2123
[`${this.system}.params`]: JSON.stringify(params.querystring || params.query)
2224
}
23-
})
25+
}, ctx)
26+
27+
return ctx.currentStore
2428
}
2529

26-
finish ({ params }) {
30+
bindFinish (ctx) {
31+
const { params } = ctx
32+
2733
const span = this.activeSpan
2834
this.config.hooks.query(span, params)
29-
super.finish({ params })
35+
super.finish(ctx)
36+
37+
return ctx.parentStore
3038
}
3139
}
3240

packages/datadog-plugin-sharedb/src/index.js

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,27 +5,35 @@ const ServerPlugin = require('../../dd-trace/src/plugins/server')
55
class SharedbPlugin extends ServerPlugin {
66
static get id () { return 'sharedb' }
77

8-
start ({ actionName, request }) {
8+
bindStart (ctx) {
9+
const { actionName, request } = ctx
10+
911
const span = this.startSpan('sharedb.request', {
1012
service: this.config.service,
1113
resource: getReadableResourceName(actionName, request.c, request.q),
1214
kind: 'server',
1315
meta: {
1416
'sharedb.action': actionName
1517
}
16-
})
18+
}, ctx)
1719

1820
if (this.config.hooks && this.config.hooks.receive) {
1921
this.config.hooks.receive(span, request)
2022
}
23+
24+
return ctx.currentStore
2125
}
2226

23-
finish ({ request, res }) {
24-
const span = this.activeSpan
27+
bindFinish (ctx) {
28+
const { request, res } = ctx
29+
30+
const span = ctx.currentStore.span
2531
if (this.config.hooks && this.config.hooks.reply) {
2632
this.config.hooks.reply(span, request, res)
2733
}
28-
super.finish()
34+
super.finish(ctx)
35+
36+
return ctx.parentStore
2937
}
3038
}
3139

0 commit comments

Comments
 (0)