diff --git a/packages/opentelemetry-instrumentation/src/instrumentation.ts b/packages/opentelemetry-instrumentation/src/instrumentation.ts index 091271d9521..c1389ee12f1 100644 --- a/packages/opentelemetry-instrumentation/src/instrumentation.ts +++ b/packages/opentelemetry-instrumentation/src/instrumentation.ts @@ -70,6 +70,19 @@ export abstract class InstrumentationAbstract ); } + /* Returns InstrumentationConfig */ + public getConfig() { + return this._config; + } + + /** + * Sets InstrumentationConfig to this plugin + * @param InstrumentationConfig + */ + public setConfig(config: types.InstrumentationConfig = {}) { + this._config = Object.assign({}, config); + } + /** * Sets TraceProvider to this plugin * @param tracerProvider diff --git a/packages/opentelemetry-instrumentation/src/types.ts b/packages/opentelemetry-instrumentation/src/types.ts index 4d54f7d02e8..4ed6febf10a 100644 --- a/packages/opentelemetry-instrumentation/src/types.ts +++ b/packages/opentelemetry-instrumentation/src/types.ts @@ -44,6 +44,12 @@ export interface Instrumentation { /** Method to set meter provider */ setMeterProvider(meterProvider: MeterProvider): void; + /** Method to set instrumentation config */ + setConfig(config: InstrumentationConfig): void; + + /** Method to get instrumentation config */ + getConfig(): InstrumentationConfig; + /** * Contains all supported versions. * All versions must be compatible with [semver](https://semver.org/spec/v2.0.0.html) format. diff --git a/packages/opentelemetry-instrumentation/test/common/Instrumentation.test.ts b/packages/opentelemetry-instrumentation/test/common/Instrumentation.test.ts index 8324c01cd04..efd4debb8e8 100644 --- a/packages/opentelemetry-instrumentation/test/common/Instrumentation.test.ts +++ b/packages/opentelemetry-instrumentation/test/common/Instrumentation.test.ts @@ -15,11 +15,19 @@ */ import * as assert from 'assert'; -import { Instrumentation, InstrumentationBase } from '../../src'; +import { + Instrumentation, + InstrumentationBase, + InstrumentationConfig, +} from '../../src'; + +interface TestInstrumentationConfig extends InstrumentationConfig { + isActive?: boolean; +} class TestInstrumentation extends InstrumentationBase { - constructor() { - super('test', '1.0.0'); + constructor(config: TestInstrumentationConfig & InstrumentationConfig = {}) { + super('test', '1.0.0', Object.assign({}, config)); } enable() {} disable() {} @@ -56,4 +64,29 @@ describe('BaseInstrumentation', () => { assert.strictEqual(called, true); }); }); + + describe('getConfig', () => { + it('should return instrumentation config', () => { + const instrumentation: Instrumentation = new TestInstrumentation({ + isActive: false, + }); + const configuration = + instrumentation.getConfig() as TestInstrumentationConfig; + assert.notStrictEqual(configuration, null); + assert.strictEqual(configuration.isActive, false); + }); + }); + + describe('setConfig', () => { + it('should set a new config for instrumentation', () => { + const instrumentation: Instrumentation = new TestInstrumentation(); + const config: TestInstrumentationConfig = { + isActive: true, + }; + instrumentation.setConfig(config); + const configuration = + instrumentation.getConfig() as TestInstrumentationConfig; + assert.strictEqual(configuration.isActive, true); + }); + }); });