Skip to content

Commit f617c33

Browse files
authored
fix: usage of AsyncResource.bind in mongodb instrumentation for Node.js v14 and earlier (#3692)
Fixes: #3691
1 parent 6dfc4dd commit f617c33

File tree

3 files changed

+88
-1
lines changed

3 files changed

+88
-1
lines changed

Diff for: NOTICE.md

+29
Original file line numberDiff line numberDiff line change
@@ -411,3 +411,32 @@ The above copyright notice and this permission notice shall be included in all c
411411
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
412412
```
413413

414+
415+
## Node.js
416+
417+
- **path:** [lib/async-hooks-polyfill.js](lib/async-hooks-polyfill.js)
418+
- **project url:** https://github.com/nodejs/node
419+
- **original file:** https://github.com/nodejs/node/blob/v17.8.0/lib/async_hooks.js
420+
- **license:** Node.js license
421+
422+
```
423+
Copyright Node.js contributors. All rights reserved.
424+
425+
Permission is hereby granted, free of charge, to any person obtaining a copy
426+
of this software and associated documentation files (the "Software"), to
427+
deal in the Software without restriction, including without limitation the
428+
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
429+
sell copies of the Software, and to permit persons to whom the Software is
430+
furnished to do so, subject to the following conditions:
431+
432+
The above copyright notice and this permission notice shall be included in
433+
all copies or substantial portions of the Software.
434+
435+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
436+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
437+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
438+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
439+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
440+
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
441+
IN THE SOFTWARE.
442+
```

Diff for: lib/async-hooks-polyfill.js

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and other contributors where applicable.
3+
* Licensed under the BSD 2-Clause License; you may not use this file except in
4+
* compliance with the BSD 2-Clause License.
5+
*/
6+
7+
'use strict';
8+
9+
const async_hooks = require('async_hooks');
10+
const semver = require('semver');
11+
12+
// A polyfilled `AsyncResource.bind` that behaves like Node.js v17.8.0.
13+
// https://nodejs.org/api/async_context.html#asyncresourcebindfn-thisarg
14+
// Adapted from dd-trace and
15+
// https://github.com/nodejs/node/blob/v17.8.0/lib/async_hooks.js#L227-L260
16+
let AsyncResource;
17+
if (semver.satisfies(process.versions.node, '>=17.8.0')) {
18+
AsyncResource = async_hooks.AsyncResource;
19+
} else {
20+
AsyncResource = class extends async_hooks.AsyncResource {
21+
static bind(fn, type, thisArg) {
22+
type = type || fn.name;
23+
return new AsyncResource(type || 'bound-anonymous-fn').bind(fn, thisArg);
24+
}
25+
26+
bind(fn, thisArg) {
27+
let bound;
28+
if (thisArg === undefined) {
29+
const resource = this;
30+
bound = function (...args) {
31+
args.unshift(fn, this);
32+
return Reflect.apply(resource.runInAsyncScope, resource, args);
33+
};
34+
} else {
35+
bound = this.runInAsyncScope.bind(this, fn, thisArg);
36+
}
37+
Object.defineProperties(bound, {
38+
length: {
39+
configurable: true,
40+
enumerable: false,
41+
value: fn.length,
42+
writable: false,
43+
},
44+
asyncResource: {
45+
configurable: true,
46+
enumerable: true,
47+
value: this,
48+
writable: true,
49+
},
50+
});
51+
return bound;
52+
}
53+
};
54+
}
55+
56+
module.exports = {
57+
AsyncResource,
58+
};

Diff for: lib/instrumentation/modules/mongodb/lib/cmap/connection_pool.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
'use strict';
88

9-
const { AsyncResource } = require('async_hooks');
9+
const { AsyncResource } = require('../../../../../async-hooks-polyfill');
1010

1111
const semver = require('semver');
1212

0 commit comments

Comments
 (0)