From 2a8a2f957e9f4f98c18333102938f13982ca74e6 Mon Sep 17 00:00:00 2001 From: Marc Pichler Date: Mon, 12 Feb 2024 14:32:57 +0100 Subject: [PATCH 1/3] feat(instrumentation): add getModuleDefinitions() instead of making init() public --- .../src/instrumentation.ts | 22 +++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/experimental/packages/opentelemetry-instrumentation/src/instrumentation.ts b/experimental/packages/opentelemetry-instrumentation/src/instrumentation.ts index 88a6b03887..ed619bd139 100644 --- a/experimental/packages/opentelemetry-instrumentation/src/instrumentation.ts +++ b/experimental/packages/opentelemetry-instrumentation/src/instrumentation.ts @@ -108,6 +108,23 @@ export abstract class InstrumentationAbstract ); } + /** + * @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[] { + const initResult = this.init() ?? []; + if (!Array.isArray(initResult)) { + return [initResult]; + } + + return initResult; + } + /** * Sets the new metric instruments with the current Meter. */ @@ -153,11 +170,8 @@ export abstract class InstrumentationAbstract /** * 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 | InstrumentationModuleDefinition[] | void; From 5817d893fb37add304407c27761af66767171c71 Mon Sep 17 00:00:00 2001 From: Marc Pichler Date: Mon, 12 Feb 2024 14:52:28 +0100 Subject: [PATCH 2/3] test(instrumetation): add tests for getModuleDefinitions() --- .../test/common/Instrumentation.test.ts | 51 +++++++++++++++++++ 1 file changed, 51 insertions(+) diff --git a/experimental/packages/opentelemetry-instrumentation/test/common/Instrumentation.test.ts b/experimental/packages/opentelemetry-instrumentation/test/common/Instrumentation.test.ts index 126f4c0944..fde0c2c34e 100644 --- a/experimental/packages/opentelemetry-instrumentation/test/common/Instrumentation.test.ts +++ b/experimental/packages/opentelemetry-instrumentation/test/common/Instrumentation.test.ts @@ -19,6 +19,7 @@ import { Instrumentation, InstrumentationBase, InstrumentationConfig, + InstrumentationModuleDefinition, } from '../../src'; import { MeterProvider } from '@opentelemetry/sdk-metrics'; @@ -132,4 +133,54 @@ describe('BaseInstrumentation', () => { assert.strictEqual(configuration.isActive, true); }); }); + + describe('getModuleDefinitions', () => { + const moduleDefinition: InstrumentationModuleDefinition = { + 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(), []); + }); + }); }); From 8f9a11d74d52723fbb2fe6d70da225634e7d5b9e Mon Sep 17 00:00:00 2001 From: Marc Pichler Date: Mon, 12 Feb 2024 15:05:48 +0100 Subject: [PATCH 3/3] chore: changelog --- experimental/CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/experimental/CHANGELOG.md b/experimental/CHANGELOG.md index 01b1b3d688..ab698812ef 100644 --- a/experimental/CHANGELOG.md +++ b/experimental/CHANGELOG.md @@ -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