Skip to content

Commit 34b8c65

Browse files
authored
remove async storage from memcached instrumentation (#5936)
* remove async storage from memcached
1 parent 9ff68ff commit 34b8c65

File tree

3 files changed

+25
-28
lines changed

3 files changed

+25
-28
lines changed

packages/datadog-instrumentations/src/memcached.js

Lines changed: 17 additions & 24 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

@@ -17,35 +16,29 @@ addHook({ name: 'memcached', versions: ['>=2.2'] }, Memcached => {
1716
return command.apply(this, arguments)
1817
}
1918

20-
const callbackResource = new AsyncResource('bound-anonymous-fn')
21-
const asyncResource = new AsyncResource('bound-anonymous-fn')
22-
2319
const client = this
2420

25-
const wrappedQueryCompiler = asyncResource.bind(function () {
21+
const wrappedQueryCompiler = function () {
2622
const query = queryCompiler.apply(this, arguments)
27-
const callback = callbackResource.bind(query.callback)
28-
29-
query.callback = shimmer.wrapFunction(callback, callback => asyncResource.bind(function (err) {
30-
if (err) {
31-
errorCh.publish(err)
32-
}
33-
finishCh.publish()
34-
35-
return callback.apply(this, arguments)
36-
}))
37-
startCh.publish({ client, server, query })
3823

24+
const ctx = { client, server, query }
25+
startCh.runStores(ctx, () => {
26+
query.callback = shimmer.wrapFunction(query.callback, callback => function (err) {
27+
if (err) {
28+
ctx.error = err
29+
errorCh.publish(ctx)
30+
}
31+
finishCh.publish(ctx)
32+
33+
return finishCh.runStores(ctx, callback, this, ...arguments)
34+
})
35+
})
3936
return query
40-
})
41-
42-
return asyncResource.runInAsyncScope(() => {
43-
arguments[0] = wrappedQueryCompiler
37+
}
4438

45-
const result = command.apply(this, arguments)
39+
arguments[0] = wrappedQueryCompiler
4640

47-
return result
48-
})
41+
return command.apply(this, arguments)
4942
})
5043

5144
return Memcached

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@ const CachePlugin = require('../../dd-trace/src/plugins/cache')
66
class MemcachedPlugin extends CachePlugin {
77
static get id () { return 'memcached' }
88

9-
start ({ client, server, query }) {
9+
bindStart (ctx) {
10+
const { client, server, query } = ctx
11+
1012
const address = getAddress(client, server, query)
1113

1214
const meta = {
@@ -23,7 +25,9 @@ class MemcachedPlugin extends CachePlugin {
2325
resource: query.type,
2426
type: 'memcached',
2527
meta
26-
})
28+
}, ctx)
29+
30+
return ctx.currentStore
2731
}
2832
}
2933

packages/dd-trace/src/plugins/cache.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@ const StoragePlugin = require('./storage')
55
class CachePlugin extends StoragePlugin {
66
static get operation () { return 'command' }
77

8-
startSpan (options) {
8+
startSpan (options, ctx) {
99
if (!options.kind) {
1010
options.kind = this.constructor.kind
1111
}
12-
return super.startSpan(this.operationName(), options)
12+
return super.startSpan(this.operationName(), options, ctx)
1313
}
1414
}
1515

0 commit comments

Comments
 (0)