Skip to content

Commit

Permalink
feat(opentelemetry-instrumentation): getConfig and setConfig (#2224)
Browse files Browse the repository at this point in the history
* feat(opentelemetry-instrumentation): config

* chore: remove any type from method signature

* chore: setConfig describe block and rename tests
  • Loading branch information
mottibec authored May 25, 2021
1 parent a968646 commit eb9353a
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 3 deletions.
13 changes: 13 additions & 0 deletions packages/opentelemetry-instrumentation/src/instrumentation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,19 @@ export abstract class InstrumentationAbstract<T = any>
);
}

/* 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
Expand Down
6 changes: 6 additions & 0 deletions packages/opentelemetry-instrumentation/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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() {}
Expand Down Expand Up @@ -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);
});
});
});

0 comments on commit eb9353a

Please sign in to comment.