Skip to content

Commit 8bbebfd

Browse files
authored
feat(instrumentation): add getModuleDefinitions() instead of making init() public (#4475)
* feat(instrumentation): add getModuleDefinitions() instead of making init() public * test(instrumetation): add tests for getModuleDefinitions() * chore: changelog
1 parent 44b0b29 commit 8bbebfd

File tree

3 files changed

+70
-5
lines changed

3 files changed

+70
-5
lines changed

experimental/CHANGELOG.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ All notable changes to experimental packages in this project will be documented
99
### :rocket: (Enhancement)
1010

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

experimental/packages/opentelemetry-instrumentation/src/instrumentation.ts

+18-4
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,23 @@ export abstract class InstrumentationAbstract<T = any>
108108
);
109109
}
110110

111+
/**
112+
* @experimental
113+
*
114+
* Get module definitions defined by {@link init}.
115+
* This can be used for experimental compile-time instrumentation.
116+
*
117+
* @returns an array of {@link InstrumentationModuleDefinition}
118+
*/
119+
public getModuleDefinitions(): InstrumentationModuleDefinition<T>[] {
120+
const initResult = this.init() ?? [];
121+
if (!Array.isArray(initResult)) {
122+
return [initResult];
123+
}
124+
125+
return initResult;
126+
}
127+
111128
/**
112129
* Sets the new metric instruments with the current Meter.
113130
*/
@@ -153,11 +170,8 @@ export abstract class InstrumentationAbstract<T = any>
153170
/**
154171
* Init method in which plugin should define _modules and patches for
155172
* methods.
156-
* Use `enable()` if you are trying to turn on this plugin. This method
157-
* will return objects to patch specific modules with the appropriate
158-
* instrumentation (or not return anything).
159173
*/
160-
abstract init():
174+
protected abstract init():
161175
| InstrumentationModuleDefinition<T>
162176
| InstrumentationModuleDefinition<T>[]
163177
| void;

experimental/packages/opentelemetry-instrumentation/test/common/Instrumentation.test.ts

+51
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import {
1919
Instrumentation,
2020
InstrumentationBase,
2121
InstrumentationConfig,
22+
InstrumentationModuleDefinition,
2223
} from '../../src';
2324

2425
import { MeterProvider } from '@opentelemetry/sdk-metrics';
@@ -132,4 +133,54 @@ describe('BaseInstrumentation', () => {
132133
assert.strictEqual(configuration.isActive, true);
133134
});
134135
});
136+
137+
describe('getModuleDefinitions', () => {
138+
const moduleDefinition: InstrumentationModuleDefinition<unknown> = {
139+
name: 'foo',
140+
patch: moduleExports => {},
141+
unpatch: moduleExports => {},
142+
moduleExports: {},
143+
files: [],
144+
supportedVersions: ['*'],
145+
};
146+
147+
it('should return single module definition from init() as array ', () => {
148+
class TestInstrumentation2 extends TestInstrumentation {
149+
override init() {
150+
return moduleDefinition;
151+
}
152+
}
153+
const instrumentation = new TestInstrumentation2();
154+
155+
assert.deepStrictEqual(instrumentation.getModuleDefinitions(), [
156+
moduleDefinition,
157+
]);
158+
});
159+
160+
it('should return multiple module definitions from init() as array ', () => {
161+
class TestInstrumentation2 extends TestInstrumentation {
162+
override init() {
163+
return [moduleDefinition, moduleDefinition, moduleDefinition];
164+
}
165+
}
166+
const instrumentation = new TestInstrumentation2();
167+
168+
assert.deepStrictEqual(instrumentation.getModuleDefinitions(), [
169+
moduleDefinition,
170+
moduleDefinition,
171+
moduleDefinition,
172+
]);
173+
});
174+
175+
it('should return void from init() as empty array ', () => {
176+
class TestInstrumentation2 extends TestInstrumentation {
177+
override init() {
178+
return;
179+
}
180+
}
181+
const instrumentation = new TestInstrumentation2();
182+
183+
assert.deepStrictEqual(instrumentation.getModuleDefinitions(), []);
184+
});
185+
});
135186
});

0 commit comments

Comments
 (0)