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

Commit

Permalink
update plugin loader (#386)
Browse files Browse the repository at this point in the history
  • Loading branch information
mayurkale22 committed Apr 1, 2019
1 parent cbd1d99 commit 27de0b1
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 34 deletions.
27 changes: 13 additions & 14 deletions examples/http/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,19 @@
* limitations under the License.
*/

const path = require('path');
const http = require('http');
const tracing = require('@opencensus/nodejs');
const { plugin } = require('@opencensus/instrumentation-http');
const { ZipkinTraceExporter } = require('@opencensus/exporter-zipkin');
const { TraceContextFormat } = require('@opencensus/propagation-tracecontext');

/**
* The trace instance needs to be initialized first, if you want to enable
* automatic tracing for built-in plugins (HTTP in this case).
* https://github.com/census-instrumentation/opencensus-node#plugins
*/
const tracer = setupTracerAndExporters();

const http = require('http');

/** A function which makes requests and handles response. */
function makeRequest () {
// Root spans typically correspond to incoming requests, while child spans
Expand All @@ -38,7 +42,7 @@ function makeRequest () {
let body = [];
response.on('data', chunk => body.push(chunk));
response.on('end', () => {
console.log(body);
console.log(body.toString());
rootSpan.end();
});
});
Expand All @@ -54,19 +58,14 @@ function setupTracerAndExporters () {
// Creates Zipkin exporter
const exporter = new ZipkinTraceExporter(zipkinOptions);

// Starts tracing and set sampling rate
const tracer = tracing.registerExporter(exporter).start({
// Starts tracing and set sampling rate, exporter and propagation
const tracer = tracing.start({
exporter,
samplingRate: 1, // For demo purposes, always sample
propagation: new TraceContextFormat()
propagation: new TraceContextFormat(),
logLevel: 1 // show errors, if any
}).tracer;

// Defines basedir and version
const basedir = path.dirname(require.resolve('http'));
const version = process.versions.node;

// Enables HTTP plugin: Method that enables the instrumentation patch.
plugin.enable(http, tracer, version, /** plugin options */{}, basedir);

return tracer;
}

Expand Down
1 change: 0 additions & 1 deletion examples/http/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
},
"dependencies": {
"@opencensus/exporter-zipkin": "^0.0.9",
"@opencensus/instrumentation-http": "^0.0.9",
"@opencensus/nodejs": "^0.0.9",
"@opencensus/propagation-tracecontext": "^0.0.9",
"http": "*"
Expand Down
25 changes: 12 additions & 13 deletions examples/http/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,19 @@
* limitations under the License.
*/

const path = require('path');
const http = require('http');
const tracing = require('@opencensus/nodejs');
const { plugin } = require('@opencensus/instrumentation-http');
const { ZipkinTraceExporter } = require('@opencensus/exporter-zipkin');
const { TraceContextFormat } = require('@opencensus/propagation-tracecontext');

/**
* The trace instance needs to be initialized first, if you want to enable
* automatic tracing for built-in plugins (HTTP in this case).
* https://github.com/census-instrumentation/opencensus-node#plugins
*/
const tracer = setupTracerAndExporters();

const http = require('http');

/** Starts a HTTP server that receives requests on sample server port. */
function startServer (port) {
// Creates a server
Expand Down Expand Up @@ -65,19 +69,14 @@ function setupTracerAndExporters () {
// Creates Zipkin exporter
const exporter = new ZipkinTraceExporter(zipkinOptions);

// Starts tracing and set sampling rate
const tracer = tracing.registerExporter(exporter).start({
// Starts tracing and set sampling rate, exporter and propagation
const tracer = tracing.start({
exporter,
samplingRate: 1, // For demo purposes, always sample
propagation: new TraceContextFormat()
propagation: new TraceContextFormat(),
logLevel: 1 // show errors, if any
}).tracer;

// Defines basedir and version
const basedir = path.dirname(require.resolve('http'));
const version = process.versions.node;

// Enables HTTP plugin: Method that enables the instrumentation patch.
plugin.enable(http, tracer, version, /** plugin options */{}, basedir);

return tracer;
}

Expand Down
3 changes: 3 additions & 0 deletions packages/opencensus-core/src/trace/config/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

import {Logger} from '../../common/types';
import {Exporter} from '../../exporters/types';
import {Stats} from '../../stats/types';
import {PluginNames} from '../instrumentation/types';
import {Propagation} from '../propagation/types';

Expand Down Expand Up @@ -70,6 +71,8 @@ export interface TracingConfig {
exporter?: Exporter;
/** An instance of a logger */
logger?: Logger;
/** An instance of a stats */
stats?: Stats;
}

/** Global configuration of trace service */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

import {Logger, Plugin, PluginConfig, PluginNames, Tracer} from '@opencensus/core';
import {Logger, Plugin, PluginConfig, PluginNames, Stats, Tracer} from '@opencensus/core';
import * as fs from 'fs';
import * as path from 'path';
import * as hook from 'require-in-the-middle';
Expand All @@ -36,6 +36,8 @@ export class PluginLoader {
private tracer: Tracer;
/** logger */
private logger: Logger;
/** The stats */
private stats?: Stats;
/** A list of loaded plugins. */
plugins: Plugin[] = [];
/**
Expand All @@ -48,9 +50,10 @@ export class PluginLoader {
* Constructs a new PluginLoader instance.
* @param tracer The tracer.
*/
constructor(logger: Logger, tracer: Tracer) {
constructor(logger: Logger, tracer: Tracer, stats?: Stats) {
this.tracer = tracer;
this.logger = logger;
this.stats = stats;
}

/**
Expand All @@ -64,7 +67,6 @@ export class PluginLoader {
Constants.DEFAULT_PLUGIN_PACKAGE_NAME_PREFIX}-${moduleName}`;
}


/**
* Returns a PluginNames object, build from a string array of target modules
* names, using the defaultPackageName.
Expand Down Expand Up @@ -140,7 +142,7 @@ export class PluginLoader {
const plugin: Plugin = require(moduleName as string).plugin;
this.plugins.push(plugin);
return plugin.enable(
exports, this.tracer, version, moduleConfig, basedir);
exports, this.tracer, version, moduleConfig, basedir, this.stats);
} catch (e) {
this.logger.error(
'could not load plugin %s of module %s. Error: %s', moduleName,
Expand All @@ -152,7 +154,6 @@ export class PluginLoader {
this.hookState = HookState.ENABLED;
}


/** Unloads plugins. */
unloadPlugins() {
for (const plugin of this.plugins) {
Expand Down
3 changes: 2 additions & 1 deletion packages/opencensus-nodejs/src/trace/tracing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,8 @@ export class Tracing implements core.Tracing {
this.configLocal.logger || logger.logger(this.configLocal.logLevel);
this.configLocal.logger = this.logger;
this.logger.debug('config: %o', this.configLocal);
this.pluginLoader = new PluginLoader(this.logger, this.tracer);
this.pluginLoader =
new PluginLoader(this.logger, this.tracer, this.configLocal.stats);
this.pluginLoader.loadPlugins(this.configLocal.plugins as core.PluginNames);

if (!this.configLocal.exporter) {
Expand Down

0 comments on commit 27de0b1

Please sign in to comment.