Skip to content
This repository was archived by the owner on Oct 3, 2023. It is now read-only.

Commit fcd82df

Browse files
authored
fix: add states to plugin loader mechanism (#72)
* fix: add states to plugin loader mechanism * fix: address comments * chore(instrumentation-grpc): pin gRPC to 1.12
1 parent 44b68f7 commit fcd82df

3 files changed

Lines changed: 32 additions & 14 deletions

File tree

packages/opencensus-instrumentation-grpc/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@
6060
},
6161
"dependencies": {
6262
"@opencensus/core": "^0.0.2",
63-
"grpc": "^1.12.2",
63+
"grpc": "~1.12.2",
6464
"lodash": "^4.17.10",
6565
"semver": "^5.5.0",
6666
"shimmer": "^1.2.0"

packages/opencensus-nodejs/src/trace/instrumentation/plugin-loader.ts

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,12 @@ import * as hook from 'require-in-the-middle';
2121

2222
import {Constants} from '../constants';
2323

24+
enum HookState {
25+
UNINITIALIZED,
26+
ENABLED,
27+
DISABLED
28+
}
29+
2430
/**
2531
* The PluginLoader class can load instrumentation plugins that
2632
* use a patch mechanism to enable automatic tracing for
@@ -33,6 +39,11 @@ export class PluginLoader {
3339
private logger: Logger;
3440
/** A list of loaded plugins. */
3541
plugins: Plugin[] = [];
42+
/**
43+
* A field that tracks whether the r-i-t-m hook has been loaded for the
44+
* first time, as well as whether the hook body is enabled or not.
45+
*/
46+
private hookState = HookState.UNINITIALIZED;
3647

3748
/**
3849
* Constructs a new PluginLoader instance.
@@ -102,30 +113,33 @@ export class PluginLoader {
102113
* @param pluginList A list of plugins.
103114
*/
104115
loadPlugins(pluginList: PluginNames) {
105-
// tslint:disable:no-any
106-
hook(Object.keys(pluginList), (exports, name, basedir) => {
107-
const version = this.getPackageVersion(name, basedir as string);
108-
this.logger.info('trying loading %s.%s', name, version);
109-
let moduleExports = exports;
110-
if (!version) {
111-
return moduleExports;
112-
} else {
116+
if (this.hookState === HookState.UNINITIALIZED) {
117+
hook(Object.keys(pluginList), (exports, name, basedir) => {
118+
if (this.hookState !== HookState.ENABLED) {
119+
return exports;
120+
}
121+
const version = this.getPackageVersion(name, basedir as string);
122+
this.logger.info('trying loading %s.%s', name, version);
123+
if (!version) {
124+
return exports;
125+
}
113126
this.logger.debug('applying patch to %s@%s module', name, version);
114127
this.logger.debug(
115128
'using package %s to patch %s', pluginList[name], name);
116129
// Expecting a plugin from module;
117130
try {
118131
const plugin: Plugin = require(pluginList[name]).plugin;
119132
this.plugins.push(plugin);
120-
moduleExports = plugin.enable(exports, this.tracer, version, basedir);
133+
return plugin.enable(exports, this.tracer, version, basedir);
121134
} catch (e) {
122135
this.logger.error(
123136
'could not load plugin %s of module %s. Error: %s',
124137
pluginList[name], name, e.message);
138+
return exports;
125139
}
126-
return moduleExports;
127-
}
128-
});
140+
});
141+
}
142+
this.hookState = HookState.ENABLED;
129143
}
130144

131145

@@ -135,6 +149,7 @@ export class PluginLoader {
135149
plugin.disable();
136150
}
137151
this.plugins = [];
152+
this.hookState = HookState.DISABLED;
138153
}
139154

140155
/**

packages/opencensus-nodejs/test/test-plugin-loader.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ describe('Plugin Loader', () => {
102102
assert.strictEqual(pluginLoader.plugins.length, 1);
103103
assert.strictEqual(simpleModule.name(), 'patched-' + TEST_MODULES[0]);
104104
assert.strictEqual(simpleModule.value(), 101);
105+
pluginLoader.unloadPlugins();
105106
});
106107

107108
it('should load and patch extra plugin file', () => {
@@ -119,6 +120,7 @@ describe('Plugin Loader', () => {
119120
assert.strictEqual(
120121
loadInternalFileModule.extraName(), 'patched-' + extraModuleName);
121122
assert.strictEqual(loadInternalFileModule.extraValue(), 121);
123+
pluginLoader.unloadPlugins();
122124
});
123125

124126

@@ -133,6 +135,7 @@ describe('Plugin Loader', () => {
133135
assert.ok(txt.indexOf('error') >= 0);
134136
})();
135137
assert.strictEqual(pluginLoader.plugins.length, 0);
138+
pluginLoader.unloadPlugins();
136139
});
137140
});
138141

@@ -154,7 +157,7 @@ describe('Plugin Loader', () => {
154157
});
155158

156159
// Should load/unload end-user (non-default named) plugin.
157-
describe('load/unload end-user pluging', () => {
160+
describe('load/unload end-user plugin', () => {
158161
it('should load/unload patch/unpatch end-user plugins', () => {
159162
const pluginLoader = new PluginLoader(log, tracer);
160163
assert.strictEqual(pluginLoader.plugins.length, 0);

0 commit comments

Comments
 (0)