Skip to content

Commit

Permalink
feat(instrumentation): add getModuleDefinitions() instead of making…
Browse files Browse the repository at this point in the history
… `init()` public (#4475)

* feat(instrumentation): add getModuleDefinitions() instead of making init() public

* test(instrumetation): add tests for getModuleDefinitions()

* chore: changelog
  • Loading branch information
pichlermarc authored Feb 15, 2024
1 parent 44b0b29 commit 8bbebfd
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 5 deletions.
2 changes: 1 addition & 1 deletion experimental/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ All notable changes to experimental packages in this project will be documented
### :rocket: (Enhancement)

* feat(instrumentation): allow LoggerProvider to be specified in Instrumentations [#4314](https://github.com/open-telemetry/opentelemetry-js/pull/4314) @hectorhdzg
* feat(instrumentation): Make `init()` method public [#4418](https://github.com/open-telemetry/opentelemetry-js/pull/4418)
* feat(instrumentation): add getModuleDefinitions() to InstrumentationBase [#4475](https://github.com/open-telemetry/opentelemetry-js/pull/4475) @pichlermarc
* feat(exporter-metrics-otlp-http): add option to set the exporter aggregation preference [#4409](https://github.com/open-telemetry/opentelemetry-js/pull/4409) @AkselAllas
* feat(node-sdk): add spanProcessors option [#4454](https://github.com/open-telemetry/opentelemetry-js/pull/4454) @naseemkullah

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,23 @@ export abstract class InstrumentationAbstract<T = any>
);
}

/**
* @experimental
*
* Get module definitions defined by {@link init}.
* This can be used for experimental compile-time instrumentation.
*
* @returns an array of {@link InstrumentationModuleDefinition}
*/
public getModuleDefinitions(): InstrumentationModuleDefinition<T>[] {
const initResult = this.init() ?? [];
if (!Array.isArray(initResult)) {
return [initResult];
}

return initResult;
}

/**
* Sets the new metric instruments with the current Meter.
*/
Expand Down Expand Up @@ -153,11 +170,8 @@ export abstract class InstrumentationAbstract<T = any>
/**
* Init method in which plugin should define _modules and patches for
* methods.
* Use `enable()` if you are trying to turn on this plugin. This method
* will return objects to patch specific modules with the appropriate
* instrumentation (or not return anything).
*/
abstract init():
protected abstract init():
| InstrumentationModuleDefinition<T>
| InstrumentationModuleDefinition<T>[]
| void;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {
Instrumentation,
InstrumentationBase,
InstrumentationConfig,
InstrumentationModuleDefinition,
} from '../../src';

import { MeterProvider } from '@opentelemetry/sdk-metrics';
Expand Down Expand Up @@ -132,4 +133,54 @@ describe('BaseInstrumentation', () => {
assert.strictEqual(configuration.isActive, true);
});
});

describe('getModuleDefinitions', () => {
const moduleDefinition: InstrumentationModuleDefinition<unknown> = {
name: 'foo',
patch: moduleExports => {},
unpatch: moduleExports => {},
moduleExports: {},
files: [],
supportedVersions: ['*'],
};

it('should return single module definition from init() as array ', () => {
class TestInstrumentation2 extends TestInstrumentation {
override init() {
return moduleDefinition;
}
}
const instrumentation = new TestInstrumentation2();

assert.deepStrictEqual(instrumentation.getModuleDefinitions(), [
moduleDefinition,
]);
});

it('should return multiple module definitions from init() as array ', () => {
class TestInstrumentation2 extends TestInstrumentation {
override init() {
return [moduleDefinition, moduleDefinition, moduleDefinition];
}
}
const instrumentation = new TestInstrumentation2();

assert.deepStrictEqual(instrumentation.getModuleDefinitions(), [
moduleDefinition,
moduleDefinition,
moduleDefinition,
]);
});

it('should return void from init() as empty array ', () => {
class TestInstrumentation2 extends TestInstrumentation {
override init() {
return;
}
}
const instrumentation = new TestInstrumentation2();

assert.deepStrictEqual(instrumentation.getModuleDefinitions(), []);
});
});
});

0 comments on commit 8bbebfd

Please sign in to comment.