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

Commit 27de0b1

Browse files
authored
update plugin loader (#386)
1 parent cbd1d99 commit 27de0b1

File tree

6 files changed

+36
-34
lines changed

6 files changed

+36
-34
lines changed

examples/http/client.js

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

17-
const path = require('path');
18-
const http = require('http');
1917
const tracing = require('@opencensus/nodejs');
20-
const { plugin } = require('@opencensus/instrumentation-http');
2118
const { ZipkinTraceExporter } = require('@opencensus/exporter-zipkin');
2219
const { TraceContextFormat } = require('@opencensus/propagation-tracecontext');
2320

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

28+
const http = require('http');
29+
2630
/** A function which makes requests and handles response. */
2731
function makeRequest () {
2832
// Root spans typically correspond to incoming requests, while child spans
@@ -38,7 +42,7 @@ function makeRequest () {
3842
let body = [];
3943
response.on('data', chunk => body.push(chunk));
4044
response.on('end', () => {
41-
console.log(body);
45+
console.log(body.toString());
4246
rootSpan.end();
4347
});
4448
});
@@ -54,19 +58,14 @@ function setupTracerAndExporters () {
5458
// Creates Zipkin exporter
5559
const exporter = new ZipkinTraceExporter(zipkinOptions);
5660

57-
// Starts tracing and set sampling rate
58-
const tracer = tracing.registerExporter(exporter).start({
61+
// Starts tracing and set sampling rate, exporter and propagation
62+
const tracer = tracing.start({
63+
exporter,
5964
samplingRate: 1, // For demo purposes, always sample
60-
propagation: new TraceContextFormat()
65+
propagation: new TraceContextFormat(),
66+
logLevel: 1 // show errors, if any
6167
}).tracer;
6268

63-
// Defines basedir and version
64-
const basedir = path.dirname(require.resolve('http'));
65-
const version = process.versions.node;
66-
67-
// Enables HTTP plugin: Method that enables the instrumentation patch.
68-
plugin.enable(http, tracer, version, /** plugin options */{}, basedir);
69-
7069
return tracer;
7170
}
7271

examples/http/package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
},
2222
"dependencies": {
2323
"@opencensus/exporter-zipkin": "^0.0.9",
24-
"@opencensus/instrumentation-http": "^0.0.9",
2524
"@opencensus/nodejs": "^0.0.9",
2625
"@opencensus/propagation-tracecontext": "^0.0.9",
2726
"http": "*"

examples/http/server.js

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

17-
const path = require('path');
18-
const http = require('http');
1917
const tracing = require('@opencensus/nodejs');
20-
const { plugin } = require('@opencensus/instrumentation-http');
2118
const { ZipkinTraceExporter } = require('@opencensus/exporter-zipkin');
2219
const { TraceContextFormat } = require('@opencensus/propagation-tracecontext');
2320

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

28+
const http = require('http');
29+
2630
/** Starts a HTTP server that receives requests on sample server port. */
2731
function startServer (port) {
2832
// Creates a server
@@ -65,19 +69,14 @@ function setupTracerAndExporters () {
6569
// Creates Zipkin exporter
6670
const exporter = new ZipkinTraceExporter(zipkinOptions);
6771

68-
// Starts tracing and set sampling rate
69-
const tracer = tracing.registerExporter(exporter).start({
72+
// Starts tracing and set sampling rate, exporter and propagation
73+
const tracer = tracing.start({
74+
exporter,
7075
samplingRate: 1, // For demo purposes, always sample
71-
propagation: new TraceContextFormat()
76+
propagation: new TraceContextFormat(),
77+
logLevel: 1 // show errors, if any
7278
}).tracer;
7379

74-
// Defines basedir and version
75-
const basedir = path.dirname(require.resolve('http'));
76-
const version = process.versions.node;
77-
78-
// Enables HTTP plugin: Method that enables the instrumentation patch.
79-
plugin.enable(http, tracer, version, /** plugin options */{}, basedir);
80-
8180
return tracer;
8281
}
8382

packages/opencensus-core/src/trace/config/types.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
import {Logger} from '../../common/types';
1818
import {Exporter} from '../../exporters/types';
19+
import {Stats} from '../../stats/types';
1920
import {PluginNames} from '../instrumentation/types';
2021
import {Propagation} from '../propagation/types';
2122

@@ -70,6 +71,8 @@ export interface TracingConfig {
7071
exporter?: Exporter;
7172
/** An instance of a logger */
7273
logger?: Logger;
74+
/** An instance of a stats */
75+
stats?: Stats;
7376
}
7477

7578
/** Global configuration of trace service */

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

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

17-
import {Logger, Plugin, PluginConfig, PluginNames, Tracer} from '@opencensus/core';
17+
import {Logger, Plugin, PluginConfig, PluginNames, Stats, Tracer} from '@opencensus/core';
1818
import * as fs from 'fs';
1919
import * as path from 'path';
2020
import * as hook from 'require-in-the-middle';
@@ -36,6 +36,8 @@ export class PluginLoader {
3636
private tracer: Tracer;
3737
/** logger */
3838
private logger: Logger;
39+
/** The stats */
40+
private stats?: Stats;
3941
/** A list of loaded plugins. */
4042
plugins: Plugin[] = [];
4143
/**
@@ -48,9 +50,10 @@ export class PluginLoader {
4850
* Constructs a new PluginLoader instance.
4951
* @param tracer The tracer.
5052
*/
51-
constructor(logger: Logger, tracer: Tracer) {
53+
constructor(logger: Logger, tracer: Tracer, stats?: Stats) {
5254
this.tracer = tracer;
5355
this.logger = logger;
56+
this.stats = stats;
5457
}
5558

5659
/**
@@ -64,7 +67,6 @@ export class PluginLoader {
6467
Constants.DEFAULT_PLUGIN_PACKAGE_NAME_PREFIX}-${moduleName}`;
6568
}
6669

67-
6870
/**
6971
* Returns a PluginNames object, build from a string array of target modules
7072
* names, using the defaultPackageName.
@@ -140,7 +142,7 @@ export class PluginLoader {
140142
const plugin: Plugin = require(moduleName as string).plugin;
141143
this.plugins.push(plugin);
142144
return plugin.enable(
143-
exports, this.tracer, version, moduleConfig, basedir);
145+
exports, this.tracer, version, moduleConfig, basedir, this.stats);
144146
} catch (e) {
145147
this.logger.error(
146148
'could not load plugin %s of module %s. Error: %s', moduleName,
@@ -152,7 +154,6 @@ export class PluginLoader {
152154
this.hookState = HookState.ENABLED;
153155
}
154156

155-
156157
/** Unloads plugins. */
157158
unloadPlugins() {
158159
for (const plugin of this.plugins) {

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,8 @@ export class Tracing implements core.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.tracer);
77+
this.pluginLoader =
78+
new PluginLoader(this.logger, this.tracer, this.configLocal.stats);
7879
this.pluginLoader.loadPlugins(this.configLocal.plugins as core.PluginNames);
7980

8081
if (!this.configLocal.exporter) {

0 commit comments

Comments
 (0)