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

Commit d4185b0

Browse files
silva-fabiokjin
authored andcommitted
refactor(fix): changes to address review comments
1 parent f32d3f7 commit d4185b0

File tree

16 files changed

+309
-158
lines changed

16 files changed

+309
-158
lines changed

packages/opencensus-nodejs/AUTHORS

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@
55
# of contributors, see the revision history in source control.
66

77
Google LLC
8-
CESAR Team (www.cesar.org.br)
8+
CESAR Team (www.cesar.org.br)

packages/opencensus-nodejs/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# OpenCensus Core Node.js
1+
# OpenCensus Main Package
22
[![Gitter chat][gitter-image]][gitter-url]
33

44
OpenCensus for Node.js is an implementation of OpenCensus, a toolkit for collecting application performance and behavior monitoring data.

packages/opencensus-nodejs/package-lock.json

Lines changed: 61 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/opencensus-nodejs/package.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,15 @@
66
"types": "build/src/index.d.ts",
77
"repository": "census-instrumentation/opencensus-node",
88
"scripts": {
9-
"build": "npm run compile",
109
"test": "nyc -x '**/test/**' --reporter=html --reporter=text mocha 'build/test/**/*.js'",
1110
"clean": "rimraf build/*",
1211
"check": "gts check",
13-
"compile": "tsc -p .; npm run cp-test-support-files",
12+
"compile": "tsc -p .",
1413
"fix": "gts fix",
1514
"prepare": "npm run compile",
16-
"pretest": "npm run compile",
15+
"pretest": "npm run compile; npm run copy-fixtures",
1716
"posttest": "npm run check",
18-
"cp-test-support-files": "find ./test -name 'package.json' -type f -exec rsync -R {} ./build \\;"
17+
"copy-fixtures": "./scripts/copy-fixtures.sh"
1918
},
2019
"keywords": [
2120
"opencensus",
@@ -45,6 +44,7 @@
4544
"@types/node": "^9.4.7",
4645
"@types/shimmer": "^1.0.1",
4746
"gts": "^0.5.4",
47+
"intercept-stdout": "^0.1.2",
4848
"mocha": "^5.0.4",
4949
"ncp": "^2.0.0",
5050
"nyc": "11.6.0",
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#!/bin/bash
2+
3+
find ./test -type d -exec mkdir -p ./build/{} \;
4+
find ./test -name 'package.json' -type f -exec cp {} ./build/{} \;

packages/opencensus-nodejs/src/trace/constants.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* limitations under the License.
1515
*/
1616

17-
/** General pupose constants. */
17+
/** General purpose constants. */
1818
const constants = {
1919
/** Default maximum size of a buffer. */
2020
DEFAULT_BUFFER_SIZE: 100,

packages/opencensus-nodejs/src/trace/instrumentation/ext-types.d.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,3 @@ declare module 'require-in-the-middle' {
2626
function hook(onRequire: hook.OnRequireFn): void;
2727
export = hook;
2828
}
29-

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

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ export class PluginLoader {
5656

5757

5858
/**
59-
* Returns a PlunginNames object, build from a string array of target modules
59+
* Returns a PluginNames object, build from a string array of target modules
6060
* names, using the defaultPackageName.
6161
* @param modulesToPatch A list of modules to patch.
6262
* @returns Plugin names.
@@ -98,26 +98,33 @@ export class PluginLoader {
9898
* Loads a list of plugins (using a map of the target module name
9999
* and its instrumentation plugin package name). Each plugin module
100100
* should implement the core Plugin interface and export an instance
101-
* named as "plugin".
101+
* named as "plugin". This function will attach a hook to be called
102+
* the first time the module is loaded.
102103
* @param pluginList A list of plugins.
103104
*/
104105
loadPlugins(pluginList: types.PluginNames) {
105-
const self = this;
106-
107106
// tslint:disable:no-any
108107
hook(Object.keys(pluginList), (exports, name, basedir) => {
109-
const version = self.getPackageVersion(name, basedir as string);
110-
self.logger.info('trying loading %s.%s', name, version);
108+
const version = this.getPackageVersion(name, basedir as string);
109+
this.logger.info('trying loading %s.%s', name, version);
110+
let result = exports;
111111
if (!version) {
112-
return exports;
112+
return result;
113113
} else {
114-
self.logger.debug('applying patch to %s@%s module', name, version);
115-
self.logger.debug(
114+
this.logger.debug('applying patch to %s@%s module', name, version);
115+
this.logger.debug(
116116
'using package %s to patch %s', pluginList[name], name);
117117
// Expecting a plugin from module;
118-
const plugin: types.Plugin = require(pluginList[name]).plugin;
119-
self.plugins.push(plugin);
120-
return plugin.applyPatch(exports, self.tracer, version);
118+
try {
119+
const plugin: types.Plugin = require(pluginList[name]).plugin;
120+
this.plugins.push(plugin);
121+
result = plugin.applyPatch(exports, this.tracer, version);
122+
} catch (e) {
123+
this.logger.error(
124+
'could not load plugin %s of module %s. Error: %s',
125+
pluginList[name], name, e.message);
126+
}
127+
return result;
121128
}
122129
});
123130
}

packages/opencensus-nodejs/src/trace/tracing.ts

Lines changed: 34 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import {PluginLoader} from './instrumentation/plugin-loader';
2424
/** Implements a Tracing. */
2525
export class Tracing implements types.Tracing {
2626
/** A tracer object */
27-
private tracerLocal: types.Tracer;
27+
readonly tracer: types.Tracer;
2828
/** A plugin loader object */
2929
private pluginLoader: PluginLoader;
3030
/** Plugin names */
@@ -34,20 +34,20 @@ export class Tracing implements types.Tracing {
3434
/** An object to log information to */
3535
private logger: types.Logger = null;
3636
/** Singleton instance */
37-
private static sgltnInstance: types.Tracing;
37+
private static singletonInstance: types.Tracing;
3838
/** Indicates if the tracing is active */
3939
private activeLocal: boolean;
4040

4141
/** Constructs a new TracingImpl instance. */
4242
constructor() {
43-
this.tracerLocal = new classes.Tracer();
43+
this.tracer = new classes.Tracer();
4444
this.defaultPlugins = PluginLoader.defaultPluginsFromArray(
4545
Constants.DEFAULT_INSTRUMENTATION_MODULES);
4646
}
4747

4848
/** Gets the trancing instance. */
4949
static get instance(): types.Tracing {
50-
return this.sgltnInstance || (this.sgltnInstance = new this());
50+
return this.singletonInstance || (this.singletonInstance = new this());
5151
}
5252

5353
// TODO: tracing interface should be updated
@@ -74,7 +74,7 @@ export class Tracing implements types.Tracing {
7474
this.configLocal.logger || logger.logger(this.configLocal.logLevel);
7575
this.configLocal.logger = this.logger;
7676
this.logger.debug('config: %o', this.configLocal);
77-
this.pluginLoader = new PluginLoader(this.logger, this.tracerLocal);
77+
this.pluginLoader = new PluginLoader(this.logger, this.tracer);
7878
this.pluginLoader.loadPlugins(
7979
this.configLocal.plugins as types.PluginNames);
8080

@@ -85,24 +85,20 @@ export class Tracing implements types.Tracing {
8585
this.registerExporter(this.configLocal.exporter);
8686
}
8787
this.activeLocal = true;
88-
this.tracerLocal.start(this.configLocal);
88+
this.tracer.start(this.configLocal);
8989
return this;
9090
}
9191

9292
/** Stops the tracing. */
9393
stop() {
9494
this.activeLocal = false;
95-
this.tracerLocal.stop();
95+
this.tracer.stop();
9696
this.pluginLoader.unloadPlugins();
9797
this.configLocal = null;
9898
this.logger = null;
9999
// TODO: maybe some exporter logic when stop tracing
100100
}
101101

102-
/** Gets the tracer. */
103-
get tracer(): types.Tracer {
104-
return this.tracerLocal;
105-
}
106102

107103
/** Gets the exporter. */
108104
get exporter(): types.Exporter {
@@ -115,14 +111,35 @@ export class Tracing implements types.Tracing {
115111
* @param exporter THe exporter to send the traces to.
116112
*/
117113
registerExporter(exporter: types.Exporter): types.Tracing {
118-
this.configLocal.exporter = exporter;
119-
120-
// TODO: review this logic. Tracer.registerEndSpanListener
121-
// should allow only one exporter listener
122-
// an unregister method on Tracer is needed or a clear EndSpanListener
123-
if (this.tracer.eventListeners.indexOf(exporter) < 0) {
114+
if (exporter) {
115+
if (this.configLocal.exporter) {
116+
this.unRegisterExporter(this.configLocal.exporter);
117+
}
118+
this.configLocal.exporter = exporter;
124119
this.tracer.registerEndSpanListener(exporter);
120+
} else {
121+
// TODO: if unRegisterExporter go public, this logic may not be
122+
// necessary - register a null to unRegister
123+
if (this.configLocal.exporter) {
124+
this.unRegisterExporter(this.configLocal.exporter);
125+
}
126+
}
127+
return this;
128+
}
129+
130+
131+
/**
132+
* Registers an exporter to send the collected traces to.
133+
* @param exporter THe exporter to send the traces to.
134+
*/
135+
// TODO: maybe this method should be added to Tracing interface
136+
private unRegisterExporter(exporter: types.Exporter): types.Tracing {
137+
// TODO: maybe an unRegisterEndSpanListener method should be added to Tracer
138+
const index = this.tracer.eventListeners.indexOf(exporter, 0);
139+
if (index > -1) {
140+
this.tracer.eventListeners.splice(index, 1);
125141
}
142+
this.configLocal.exporter = null;
126143
return this;
127144
}
128145
}

packages/opencensus-nodejs/test/instrumentation/node_modules/@opencensus/opencensus-instrumentation-simple-module/simple-module.ts

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)