Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: usage of AsyncResource.bind in mongodb instrumentation for Node.js v14 and earlier #3692

Merged
merged 1 commit into from
Oct 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions NOTICE.md
Original file line number Diff line number Diff line change
Expand Up @@ -411,3 +411,32 @@ The above copyright notice and this permission notice shall be included in all c
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.
```


## Node.js

- **path:** [lib/async-hooks-polyfill.js](lib/async-hooks-polyfill.js)
- **project url:** https://github.com/nodejs/node
- **original file:** https://github.com/nodejs/node/blob/v17.8.0/lib/async_hooks.js
- **license:** Node.js license

```
Copyright Node.js contributors. All rights reserved.

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to
deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
sell copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

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.
```
58 changes: 58 additions & 0 deletions lib/async-hooks-polyfill.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* Copyright Elasticsearch B.V. and other contributors where applicable.
* Licensed under the BSD 2-Clause License; you may not use this file except in
* compliance with the BSD 2-Clause License.
*/

'use strict';

const async_hooks = require('async_hooks');
const semver = require('semver');

// A polyfilled `AsyncResource.bind` that behaves like Node.js v17.8.0.
// https://nodejs.org/api/async_context.html#asyncresourcebindfn-thisarg
// Adapted from dd-trace and
// https://github.com/nodejs/node/blob/v17.8.0/lib/async_hooks.js#L227-L260
let AsyncResource;
if (semver.satisfies(process.versions.node, '>=17.8.0')) {
AsyncResource = async_hooks.AsyncResource;
} else {
AsyncResource = class extends async_hooks.AsyncResource {
static bind(fn, type, thisArg) {
type = type || fn.name;
return new AsyncResource(type || 'bound-anonymous-fn').bind(fn, thisArg);
}

bind(fn, thisArg) {
let bound;
if (thisArg === undefined) {
const resource = this;
bound = function (...args) {
args.unshift(fn, this);
return Reflect.apply(resource.runInAsyncScope, resource, args);
};
} else {
bound = this.runInAsyncScope.bind(this, fn, thisArg);
}
Object.defineProperties(bound, {
length: {
configurable: true,
enumerable: false,
value: fn.length,
writable: false,
},
asyncResource: {
configurable: true,
enumerable: true,
value: this,
writable: true,
},
});
return bound;
}
};
}

module.exports = {
AsyncResource,
};
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

'use strict';

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

const semver = require('semver');

Expand Down
Loading