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

Commit

Permalink
refactor: rename TracingImpl to Tracing and use core types and classe…
Browse files Browse the repository at this point in the history
…s module namespaces
  • Loading branch information
silva-fabio authored and kjin committed May 11, 2018
1 parent 0cde438 commit f34fdc3
Show file tree
Hide file tree
Showing 8 changed files with 83 additions and 90 deletions.
8 changes: 4 additions & 4 deletions packages/opencensus-nodejs/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@
* limitations under the License.
*/

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

import {TracingImpl} from './trace/tracing';
const tracing: types.Tracing = Tracing.instance;

const tracing: Tracing = TracingImpl.instance;
export { tracing };

export = tracing;
6 changes: 3 additions & 3 deletions packages/opencensus-nodejs/src/trace/config/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@
* limitations under the License.
*/

import {CONSTANTS} from '../constants';
import {Constants} from '../constants';

/** Defines a default configuration. */
export const defaultConfig = {
logLevel: 1,
maximumLabelValueSize: 150,
plugins: {},
bufferSize: CONSTANTS.DEFAULT_BUFFER_SIZE,
bufferTimeout: CONSTANTS.DEFAULT_BUFFER_TIMEOUT,
bufferSize: Constants.DEFAULT_BUFFER_SIZE,
bufferTimeout: Constants.DEFAULT_BUFFER_TIMEOUT,
samplingRate: 1,
exporter: null,
logger: null
Expand Down
2 changes: 1 addition & 1 deletion packages/opencensus-nodejs/src/trace/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*/

/** General pupose constants. */
export const CONSTANTS = {
export const Constants = {
DEFAULT_BUFFER_SIZE: 3,
DEFAULT_BUFFER_TIMEOUT: 20000,
DEFAULT_INSTRUMENTATION_MODULES: ['http', 'https', 'mongodb-core'],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,29 +14,32 @@
* limitations under the License.
*/

import {Tracer} from '@opencensus/opencensus-core';
import {Plugin} from '@opencensus/opencensus-core';
import {PluginNames} from '@opencensus/opencensus-core';
import {debug} from '@opencensus/opencensus-core';
import {types} from '@opencensus/opencensus-core';
import {classes} from '@opencensus/opencensus-core';
import {logger} from '@opencensus/opencensus-core';

import * as fs from 'fs';
import * as path from 'path';
import * as hook from 'require-in-the-middle';

import {CONSTANTS} from '../constants';
import {Constants} from '../constants';

/** Defines a plugin loader. */
export class PluginLoader {
/** The tracer */
private tracer: Tracer;
private tracer: types.Tracer;
/** A list of plugins. */
private plugins: Plugin[] = [];
private plugins: types.Plugin[] = [];
/** logger */
private logger: types.Logger;

/**
* Constructs a new PluginLoader instance.
* @param tracer The tracer.
*/
constructor(tracer: Tracer) {
constructor(logger: types.Logger, tracer: types.Tracer) {
this.tracer = tracer;
this.logger = logger;
}

/**
Expand All @@ -45,7 +48,7 @@ export class PluginLoader {
* @returns The default name for that package.
*/
private static defaultPackageName(moduleName): string {
return `${CONSTANTS.SCOPE}/${CONSTANTS.PLUGIN_PACKAGE_NAME_PREFIX}-${
return `${Constants.SCOPE}/${Constants.PLUGIN_PACKAGE_NAME_PREFIX}-${
moduleName}`;
}

Expand All @@ -55,7 +58,7 @@ export class PluginLoader {
* @param modulesToPatch A list of modules to patch.
* @returns Plugin names.
*/
static defaultPluginsFromArray(modulesToPatch: string[]): PluginNames {
static defaultPluginsFromArray(modulesToPatch: string[]): types.PluginNames {
const plugins = modulesToPatch.reduce((plugins, moduleName) => {
plugins[moduleName] = PluginLoader.defaultPackageName(moduleName);
return plugins;
Expand Down Expand Up @@ -87,7 +90,7 @@ export class PluginLoader {
try {
version = JSON.parse(fs.readFileSync(pkgJson).toString()).version;
} catch (e) {
debug('could not get version of %s module: %s', name, e.message);
this.logger.error('could not get version of %s module: %s', name, e.message);
}
} else {
version = process.versions.node;
Expand All @@ -100,19 +103,19 @@ export class PluginLoader {
* Loads plugins.
* @param pluginList A list of plugins.
*/
loadPlugins(pluginList: PluginNames) {
loadPlugins(pluginList: types.PluginNames) {
const self = this;

hook(Object.keys(pluginList), (exports, name, basedir) => {
const version = self.getPackageVersion(name, basedir);
if (!version) {
return exports;
} else {
debug('applying patch to %s@%s module', name, version);
debug('using package %s to patch %s', pluginList[name], name);
self.logger.debug('applying patch to %s@%s module', name, version);
self.logger.debug('using package %s to patch %s', pluginList[name], name);
const pluginImportPath =
self.getPlugingImportPath(pluginList[name], name);
const plugin: Plugin = require(pluginImportPath);
const plugin: types.Plugin = require(pluginImportPath);
self.plugins.push(plugin);
return plugin.applyPatch(exports, self.tracer, version);
}
Expand Down
51 changes: 22 additions & 29 deletions packages/opencensus-nodejs/src/trace/tracing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,50 +13,42 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import {PluginNames, RootSpan, SamplerImpl, TracerImpl} from '@opencensus/opencensus-core';
import {Span} from '@opencensus/opencensus-core';
import {debug} from '@opencensus/opencensus-core';
import {Tracer} from '@opencensus/opencensus-core';
import {Tracing} from '@opencensus/opencensus-core';
import {Sampler} from '@opencensus/opencensus-core';
import {Logger} from '@opencensus/opencensus-core';
import {ConsoleExporter, Exporter, NoopExporter} from '@opencensus/opencensus-core';
import {Config} from '@opencensus/opencensus-core';
import * as logger from '@opencensus/opencensus-core';
import * as extend from 'extend';
import {types} from '@opencensus/opencensus-core';
import {classes} from '@opencensus/opencensus-core';
import {logger} from '@opencensus/opencensus-core';

import {defaultConfig} from './config/config';
import {CONSTANTS} from './constants';
import {Constants} from './constants';
import {PluginLoader} from './instrumentation/plugingloader';
import * as extend from 'extend';


/** Implements a Tracing. */
export class TracingImpl implements Tracing {
export class Tracing implements types.Tracing {
/** Indicates if the tracing is active */
private active: boolean;
/** A tracer object */
private tracerLocal: Tracer;
private tracerLocal: types.Tracer;
/** A plugin loader object */
private pluginLoader: PluginLoader;
/** Plugin names */
private defaultPlugins: PluginNames;
private defaultPlugins: types.PluginNames;
/** A configuration object to start the tracing */
private config: Config;
private config: types.Config;
/** An object to log information to */
private logger: Logger;
private logger: types.Logger;
/** Singleton instance */
private static sgltnInstance: Tracing;
private static sgltnInstance: types.Tracing;

/** Constructs a new TracingImpl instance. */
constructor() {
this.tracerLocal = new TracerImpl();
this.pluginLoader = new PluginLoader(this.tracerLocal);
this.tracerLocal = new classes.Tracer();
this.defaultPlugins = PluginLoader.defaultPluginsFromArray(
CONSTANTS.DEFAULT_INSTRUMENTATION_MODULES);
Constants.DEFAULT_INSTRUMENTATION_MODULES);
}

/** Gets the trancing instance. */
static get instance() {
static get instance(): types.Tracing {
return this.sgltnInstance || (this.sgltnInstance = new this());
}

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

if (!this.config.exporter) {
const exporter = new ConsoleExporter(this.config);
const exporter = new classes.ConsoleExporter(this.config);
this.registerExporter(exporter);
}else{
this.registerExporter(this.config.exporter);
Expand All @@ -91,20 +84,20 @@ export class TracingImpl implements Tracing {
}

/** Gets the tracer. */
get tracer(): Tracer {
get tracer(): types.Tracer {
return this.tracerLocal;
}

/** Gets the exporter. */
get exporter(): Exporter {
get exporter(): types.Exporter {
return this.config.exporter;
}

/**
* Registers an exporter to send the collected traces to.
* @param exporter THe exporter to send the traces to.
*/
registerExporter(exporter: Exporter): Tracing {
registerExporter(exporter: types.Exporter): types.Tracing {
this.config.exporter = exporter;
this.tracer.registerEndSpanListener(exporter);
return this;
Expand Down
28 changes: 16 additions & 12 deletions packages/opencensus-nodejs/test/test-plugingloader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,24 @@
* limitations under the License.
*/

import {PluginNames, TracerImpl} from '@opencensus/opencensus-core';
import {classes} from '@opencensus/opencensus-core';
import {logger} from '@opencensus/opencensus-core';

const log = logger.logger();

import * as assert from 'assert';
import {isArray} from 'util';

import {CONSTANTS} from '../src/trace/constants';
import {Constants} from '../src/trace/constants';
import {PluginLoader} from '../src/trace/instrumentation/plugingloader';
import {TracingImpl} from '../src/trace/tracing';
import {Tracing} from '../src/trace/tracing';

describe('PluginLoader', () => {
/** Should create a Tracing instance */
describe('new PluginLoader()', () => {
it('should create a PluginLoader instance', () => {
const tracer = new TracerImpl();
const pluginLoader = new PluginLoader(tracer);
const tracer = new classes.Tracer();
const pluginLoader = new PluginLoader(log, tracer);
assert.ok(pluginLoader instanceof PluginLoader);
});
});
Expand All @@ -36,7 +40,7 @@ describe('PluginLoader', () => {
describe('static defaultPluginsFromArray()', () => {
it('should get the plugins to use', () => {
const plugins = PluginLoader.defaultPluginsFromArray(
CONSTANTS.DEFAULT_INSTRUMENTATION_MODULES);
Constants.DEFAULT_INSTRUMENTATION_MODULES);
assert.ok(plugins['http']);
assert.ok(plugins['https']);
assert.ok(plugins['mongodb-core']);
Expand All @@ -47,9 +51,9 @@ describe('PluginLoader', () => {
describe('loadPlugins()', () => {
it('should load the plugins', () => {
const plugins = PluginLoader.defaultPluginsFromArray(
CONSTANTS.DEFAULT_INSTRUMENTATION_MODULES);
const tracer = new TracerImpl();
const pluginLoader = new PluginLoader(tracer);
Constants.DEFAULT_INSTRUMENTATION_MODULES);
const tracer = new classes.Tracer();
const pluginLoader = new PluginLoader(log, tracer);

assert.equal(pluginLoader.loadPlugins(plugins), null);
});
Expand All @@ -59,9 +63,9 @@ describe('PluginLoader', () => {
describe('unloadPlugins()', () => {
it('should unload the plugins', () => {
const plugins = PluginLoader.defaultPluginsFromArray(
CONSTANTS.DEFAULT_INSTRUMENTATION_MODULES);
const tracer = new TracerImpl();
const pluginLoader = new PluginLoader(tracer);
Constants.DEFAULT_INSTRUMENTATION_MODULES);
const tracer = new classes.Tracer();
const pluginLoader = new PluginLoader(log, tracer);
pluginLoader.loadPlugins(plugins);

assert.equal(pluginLoader.unloadPlugins(), null);
Expand Down
Loading

0 comments on commit f34fdc3

Please sign in to comment.