Skip to content

Commit

Permalink
feat: support context propagation in bluebird
Browse files Browse the repository at this point in the history
  • Loading branch information
kjin committed Dec 18, 2018
1 parent 04c15d9 commit 7f411ef
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,7 @@ export const defaultConfig = {
maximumLabelValueSize: 512,
plugins: {
// enable all by default
'bluebird': path.join(pluginDirectory, 'plugin-bluebird.js'),
'connect': path.join(pluginDirectory, 'plugin-connect.js'),
'express': path.join(pluginDirectory, 'plugin-express.js'),
'generic-pool': path.join(pluginDirectory, 'plugin-generic-pool.js'),
Expand Down
30 changes: 30 additions & 0 deletions src/plugins/plugin-bluebird.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import * as shimmer from 'shimmer';

import {PluginTypes} from '..';

import {bluebird_3} from './types';

type BluebirdModule = typeof bluebird_3&{prototype: {_then: Function;}};

const plugin: PluginTypes.Plugin = [{
// Bluebird is a class.
// tslint:disable-next-line:variable-name
patch: (Bluebird, tracer) => {
// any is a type arg; args are type checked when read directly, otherwise
// passed through to a function with the same type signature.
// tslint:disable:no-any
const wrapIfFunction = (fn: any) =>
typeof fn === 'function' ? tracer.wrap(fn) : fn;
shimmer.wrap(Bluebird.prototype, '_then', (thenFn: Function) => {
// Inherit context from the call site of .then().
return function<T>(this: bluebird_3<T>, ...args: any[]) {
return thenFn.apply(this, [
wrapIfFunction(args[0]), wrapIfFunction(args[1]), ...args.slice(2)
]);
};
});
// tslint:enable:no-any
}
} as PluginTypes.Monkeypatch<BluebirdModule>];

export = plugin;
1 change: 1 addition & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"include": [
"src/*.ts",
"src/cls/*.ts",
"src/plugins/plugin-bluebird.ts",
"src/plugins/plugin-connect.ts",
"src/plugins/plugin-express.ts",
"src/plugins/plugin-grpc.ts",
Expand Down

0 comments on commit 7f411ef

Please sign in to comment.