Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add support for disabling individual plugins #471

Merged
merged 1 commit into from
Mar 1, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 8 additions & 2 deletions docs/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,9 @@ export declare interface Tracer extends opentracing.Tracer {
/**
* Enable and optionally configure a plugin.
* @param plugin The name of a built-in plugin.
* @param config Configuration options.
* @param config Configuration options. Can also be `false` to disable the plugin.
*/
use<P extends keyof Plugins>(plugin: P, config?: Plugins[P]): this;
use<P extends keyof Plugins>(plugin: P, config?: Plugins[P] | boolean): this;

/**
* Returns a reference to the current scope.
Expand Down Expand Up @@ -294,6 +294,12 @@ declare namespace plugins {
* The service name to be used for this plugin.
*/
service?: string;

/**
* Whether to enable the plugin.
* @default true
*/
enabled?: boolean;
}

/** @hidden */
Expand Down
3 changes: 3 additions & 0 deletions docs/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,9 @@ tracer.use('router');
tracer.use('when');
tracer.use('winston');

tracer.use('express', false)
tracer.use('express', { enabled: false })

span = tracer.startSpan('test');
span = tracer.startSpan('test', {});
span = tracer.startSpan('test', {
Expand Down
10 changes: 9 additions & 1 deletion src/instrumenter.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ class Instrumenter {
}

use (name, config) {
if (typeof config === 'boolean') {
config = { enabled: config }
}

config = config || {}

try {
Expand Down Expand Up @@ -130,7 +134,11 @@ class Instrumenter {
.filter(instrumentation => moduleName === filename(instrumentation))
.filter(instrumentation => matchVersion(moduleVersion, instrumentation.versions))
.forEach(instrumentation => {
this._patch(instrumentation, moduleExports, this._plugins.get(plugin).config)
const config = this._plugins.get(plugin).config

if (config.enabled !== false) {
this._patch(instrumentation, moduleExports, config)
}
})
} catch (e) {
log.error(e)
Expand Down
16 changes: 16 additions & 0 deletions test/instrumenter.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,22 @@ describe('Instrumenter', () => {

expect(integrations.express.patch).to.have.been.calledOnce
})

it('should not patch disabled plugins', () => {
instrumenter.use('express-mock', { enabled: false })

require('express-mock')

expect(integrations.express.patch).to.not.have.been.called
})

it('should not patch disabled plugins using shorthand', () => {
instrumenter.use('express-mock', false)

require('express-mock')

expect(integrations.express.patch).to.not.have.been.called
})
})

describe('patch', () => {
Expand Down