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

Commit f34fdc3

Browse files
silva-fabiokjin
authored andcommitted
refactor: rename TracingImpl to Tracing and use core types and classes module namespaces
1 parent 0cde438 commit f34fdc3

File tree

8 files changed

+83
-90
lines changed

8 files changed

+83
-90
lines changed

packages/opencensus-nodejs/src/index.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@
1414
* limitations under the License.
1515
*/
1616

17-
import {Tracing} from '@opencensus/opencensus-core';
17+
import {types} from '@opencensus/opencensus-core';
18+
import {Tracing} from './trace/tracing';
1819

19-
import {TracingImpl} from './trace/tracing';
20+
const tracing: types.Tracing = Tracing.instance;
2021

21-
const tracing: Tracing = TracingImpl.instance;
22+
export { tracing };
2223

23-
export = tracing;

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,15 @@
1414
* limitations under the License.
1515
*/
1616

17-
import {CONSTANTS} from '../constants';
17+
import {Constants} from '../constants';
1818

1919
/** Defines a default configuration. */
2020
export const defaultConfig = {
2121
logLevel: 1,
2222
maximumLabelValueSize: 150,
2323
plugins: {},
24-
bufferSize: CONSTANTS.DEFAULT_BUFFER_SIZE,
25-
bufferTimeout: CONSTANTS.DEFAULT_BUFFER_TIMEOUT,
24+
bufferSize: Constants.DEFAULT_BUFFER_SIZE,
25+
bufferTimeout: Constants.DEFAULT_BUFFER_TIMEOUT,
2626
samplingRate: 1,
2727
exporter: null,
2828
logger: null

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
*/
1616

1717
/** General pupose constants. */
18-
export const CONSTANTS = {
18+
export const Constants = {
1919
DEFAULT_BUFFER_SIZE: 3,
2020
DEFAULT_BUFFER_TIMEOUT: 20000,
2121
DEFAULT_INSTRUMENTATION_MODULES: ['http', 'https', 'mongodb-core'],

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

File renamed without changes.

packages/opencensus-nodejs/src/trace/instrumentation/plugingloader.ts

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -14,29 +14,32 @@
1414
* limitations under the License.
1515
*/
1616

17-
import {Tracer} from '@opencensus/opencensus-core';
18-
import {Plugin} from '@opencensus/opencensus-core';
19-
import {PluginNames} from '@opencensus/opencensus-core';
20-
import {debug} from '@opencensus/opencensus-core';
17+
import {types} from '@opencensus/opencensus-core';
18+
import {classes} from '@opencensus/opencensus-core';
19+
import {logger} from '@opencensus/opencensus-core';
20+
2121
import * as fs from 'fs';
2222
import * as path from 'path';
2323
import * as hook from 'require-in-the-middle';
2424

25-
import {CONSTANTS} from '../constants';
25+
import {Constants} from '../constants';
2626

2727
/** Defines a plugin loader. */
2828
export class PluginLoader {
2929
/** The tracer */
30-
private tracer: Tracer;
30+
private tracer: types.Tracer;
3131
/** A list of plugins. */
32-
private plugins: Plugin[] = [];
32+
private plugins: types.Plugin[] = [];
33+
/** logger */
34+
private logger: types.Logger;
3335

3436
/**
3537
* Constructs a new PluginLoader instance.
3638
* @param tracer The tracer.
3739
*/
38-
constructor(tracer: Tracer) {
40+
constructor(logger: types.Logger, tracer: types.Tracer) {
3941
this.tracer = tracer;
42+
this.logger = logger;
4043
}
4144

4245
/**
@@ -45,7 +48,7 @@ export class PluginLoader {
4548
* @returns The default name for that package.
4649
*/
4750
private static defaultPackageName(moduleName): string {
48-
return `${CONSTANTS.SCOPE}/${CONSTANTS.PLUGIN_PACKAGE_NAME_PREFIX}-${
51+
return `${Constants.SCOPE}/${Constants.PLUGIN_PACKAGE_NAME_PREFIX}-${
4952
moduleName}`;
5053
}
5154

@@ -55,7 +58,7 @@ export class PluginLoader {
5558
* @param modulesToPatch A list of modules to patch.
5659
* @returns Plugin names.
5760
*/
58-
static defaultPluginsFromArray(modulesToPatch: string[]): PluginNames {
61+
static defaultPluginsFromArray(modulesToPatch: string[]): types.PluginNames {
5962
const plugins = modulesToPatch.reduce((plugins, moduleName) => {
6063
plugins[moduleName] = PluginLoader.defaultPackageName(moduleName);
6164
return plugins;
@@ -87,7 +90,7 @@ export class PluginLoader {
8790
try {
8891
version = JSON.parse(fs.readFileSync(pkgJson).toString()).version;
8992
} catch (e) {
90-
debug('could not get version of %s module: %s', name, e.message);
93+
this.logger.error('could not get version of %s module: %s', name, e.message);
9194
}
9295
} else {
9396
version = process.versions.node;
@@ -100,19 +103,19 @@ export class PluginLoader {
100103
* Loads plugins.
101104
* @param pluginList A list of plugins.
102105
*/
103-
loadPlugins(pluginList: PluginNames) {
106+
loadPlugins(pluginList: types.PluginNames) {
104107
const self = this;
105108

106109
hook(Object.keys(pluginList), (exports, name, basedir) => {
107110
const version = self.getPackageVersion(name, basedir);
108111
if (!version) {
109112
return exports;
110113
} else {
111-
debug('applying patch to %s@%s module', name, version);
112-
debug('using package %s to patch %s', pluginList[name], name);
114+
self.logger.debug('applying patch to %s@%s module', name, version);
115+
self.logger.debug('using package %s to patch %s', pluginList[name], name);
113116
const pluginImportPath =
114117
self.getPlugingImportPath(pluginList[name], name);
115-
const plugin: Plugin = require(pluginImportPath);
118+
const plugin: types.Plugin = require(pluginImportPath);
116119
self.plugins.push(plugin);
117120
return plugin.applyPatch(exports, self.tracer, version);
118121
}

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

Lines changed: 22 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -13,50 +13,42 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
17-
import {PluginNames, RootSpan, SamplerImpl, TracerImpl} from '@opencensus/opencensus-core';
18-
import {Span} from '@opencensus/opencensus-core';
19-
import {debug} from '@opencensus/opencensus-core';
20-
import {Tracer} from '@opencensus/opencensus-core';
21-
import {Tracing} from '@opencensus/opencensus-core';
22-
import {Sampler} from '@opencensus/opencensus-core';
23-
import {Logger} from '@opencensus/opencensus-core';
24-
import {ConsoleExporter, Exporter, NoopExporter} from '@opencensus/opencensus-core';
25-
import {Config} from '@opencensus/opencensus-core';
26-
import * as logger from '@opencensus/opencensus-core';
16+
import * as extend from 'extend';
17+
import {types} from '@opencensus/opencensus-core';
18+
import {classes} from '@opencensus/opencensus-core';
19+
import {logger} from '@opencensus/opencensus-core';
2720

2821
import {defaultConfig} from './config/config';
29-
import {CONSTANTS} from './constants';
22+
import {Constants} from './constants';
3023
import {PluginLoader} from './instrumentation/plugingloader';
31-
import * as extend from 'extend';
24+
3225

3326
/** Implements a Tracing. */
34-
export class TracingImpl implements Tracing {
27+
export class Tracing implements types.Tracing {
3528
/** Indicates if the tracing is active */
3629
private active: boolean;
3730
/** A tracer object */
38-
private tracerLocal: Tracer;
31+
private tracerLocal: types.Tracer;
3932
/** A plugin loader object */
4033
private pluginLoader: PluginLoader;
4134
/** Plugin names */
42-
private defaultPlugins: PluginNames;
35+
private defaultPlugins: types.PluginNames;
4336
/** A configuration object to start the tracing */
44-
private config: Config;
37+
private config: types.Config;
4538
/** An object to log information to */
46-
private logger: Logger;
39+
private logger: types.Logger;
4740
/** Singleton instance */
48-
private static sgltnInstance: Tracing;
41+
private static sgltnInstance: types.Tracing;
4942

5043
/** Constructs a new TracingImpl instance. */
5144
constructor() {
52-
this.tracerLocal = new TracerImpl();
53-
this.pluginLoader = new PluginLoader(this.tracerLocal);
45+
this.tracerLocal = new classes.Tracer();
5446
this.defaultPlugins = PluginLoader.defaultPluginsFromArray(
55-
CONSTANTS.DEFAULT_INSTRUMENTATION_MODULES);
47+
Constants.DEFAULT_INSTRUMENTATION_MODULES);
5648
}
5749

5850
/** Gets the trancing instance. */
59-
static get instance() {
51+
static get instance(): types.Tracing {
6052
return this.sgltnInstance || (this.sgltnInstance = new this());
6153
}
6254

@@ -65,16 +57,17 @@ export class TracingImpl implements Tracing {
6557
* @param userConfig A configuration object to start the tracing.
6658
* @returns The started tracing.
6759
*/
68-
start(userConfig?: Config): Tracing {
60+
start(userConfig?: types.Config): types.Tracing {
6961
this.config = extend(
7062
true, {}, defaultConfig, {plugins: this.defaultPlugins}, userConfig);
7163
// TODO: Instance logger if no logger was passed
7264
this.logger = this.config.logger || logger.logger();
73-
debug('config: %o', this.config);
65+
this.logger.debug('config: %o', this.config);
66+
this.pluginLoader = new PluginLoader(this.logger, this.tracerLocal);
7467
this.pluginLoader.loadPlugins(this.config.plugins);
7568

7669
if (!this.config.exporter) {
77-
const exporter = new ConsoleExporter(this.config);
70+
const exporter = new classes.ConsoleExporter(this.config);
7871
this.registerExporter(exporter);
7972
}else{
8073
this.registerExporter(this.config.exporter);
@@ -91,20 +84,20 @@ export class TracingImpl implements Tracing {
9184
}
9285

9386
/** Gets the tracer. */
94-
get tracer(): Tracer {
87+
get tracer(): types.Tracer {
9588
return this.tracerLocal;
9689
}
9790

9891
/** Gets the exporter. */
99-
get exporter(): Exporter {
92+
get exporter(): types.Exporter {
10093
return this.config.exporter;
10194
}
10295

10396
/**
10497
* Registers an exporter to send the collected traces to.
10598
* @param exporter THe exporter to send the traces to.
10699
*/
107-
registerExporter(exporter: Exporter): Tracing {
100+
registerExporter(exporter: types.Exporter): types.Tracing {
108101
this.config.exporter = exporter;
109102
this.tracer.registerEndSpanListener(exporter);
110103
return this;

packages/opencensus-nodejs/test/test-plugingloader.ts

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,20 +14,24 @@
1414
* limitations under the License.
1515
*/
1616

17-
import {PluginNames, TracerImpl} from '@opencensus/opencensus-core';
17+
import {classes} from '@opencensus/opencensus-core';
18+
import {logger} from '@opencensus/opencensus-core';
19+
20+
const log = logger.logger();
21+
1822
import * as assert from 'assert';
1923
import {isArray} from 'util';
2024

21-
import {CONSTANTS} from '../src/trace/constants';
25+
import {Constants} from '../src/trace/constants';
2226
import {PluginLoader} from '../src/trace/instrumentation/plugingloader';
23-
import {TracingImpl} from '../src/trace/tracing';
27+
import {Tracing} from '../src/trace/tracing';
2428

2529
describe('PluginLoader', () => {
2630
/** Should create a Tracing instance */
2731
describe('new PluginLoader()', () => {
2832
it('should create a PluginLoader instance', () => {
29-
const tracer = new TracerImpl();
30-
const pluginLoader = new PluginLoader(tracer);
33+
const tracer = new classes.Tracer();
34+
const pluginLoader = new PluginLoader(log, tracer);
3135
assert.ok(pluginLoader instanceof PluginLoader);
3236
});
3337
});
@@ -36,7 +40,7 @@ describe('PluginLoader', () => {
3640
describe('static defaultPluginsFromArray()', () => {
3741
it('should get the plugins to use', () => {
3842
const plugins = PluginLoader.defaultPluginsFromArray(
39-
CONSTANTS.DEFAULT_INSTRUMENTATION_MODULES);
43+
Constants.DEFAULT_INSTRUMENTATION_MODULES);
4044
assert.ok(plugins['http']);
4145
assert.ok(plugins['https']);
4246
assert.ok(plugins['mongodb-core']);
@@ -47,9 +51,9 @@ describe('PluginLoader', () => {
4751
describe('loadPlugins()', () => {
4852
it('should load the plugins', () => {
4953
const plugins = PluginLoader.defaultPluginsFromArray(
50-
CONSTANTS.DEFAULT_INSTRUMENTATION_MODULES);
51-
const tracer = new TracerImpl();
52-
const pluginLoader = new PluginLoader(tracer);
54+
Constants.DEFAULT_INSTRUMENTATION_MODULES);
55+
const tracer = new classes.Tracer();
56+
const pluginLoader = new PluginLoader(log, tracer);
5357

5458
assert.equal(pluginLoader.loadPlugins(plugins), null);
5559
});
@@ -59,9 +63,9 @@ describe('PluginLoader', () => {
5963
describe('unloadPlugins()', () => {
6064
it('should unload the plugins', () => {
6165
const plugins = PluginLoader.defaultPluginsFromArray(
62-
CONSTANTS.DEFAULT_INSTRUMENTATION_MODULES);
63-
const tracer = new TracerImpl();
64-
const pluginLoader = new PluginLoader(tracer);
66+
Constants.DEFAULT_INSTRUMENTATION_MODULES);
67+
const tracer = new classes.Tracer();
68+
const pluginLoader = new PluginLoader(log, tracer);
6569
pluginLoader.loadPlugins(plugins);
6670

6771
assert.equal(pluginLoader.unloadPlugins(), null);

0 commit comments

Comments
 (0)