Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(instrumentation): support ESM in node via import-in-the-middle #2763

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ All notable changes to this project will be documented in this file.

### :rocket: (Enhancement)

* `opentelemetry-instrumentation`
* [#2763](https://github.com/open-telemetry/opentelemetry-js/pull/2763) feat: support ESM in node via import-in-the-middle ([@lizthegrey](https://github.com/lizthegrey), [@bengl](https://github.com/bengl), and [@JamieDanielson](https://github.com/JamieDanielson))

### :bug: (Bug Fix)

### :books: (Refine Doc)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@
},
"dependencies": {
"@opentelemetry/api-metrics": "0.27.0",
"import-in-the-middle": "^1.2.1",
"require-in-the-middle": "^5.0.3",
"semver": "^7.3.2",
"shimmer": "^1.2.1"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
import * as types from '../../types';
import * as path from 'path';
import * as RequireInTheMiddle from 'require-in-the-middle';
import { HookFn } from 'import-in-the-middle';
import * as ImportInTheMiddle from 'import-in-the-middle';
import { satisfies } from 'semver';
import { InstrumentationAbstract } from '../../instrumentation';
import { InstrumentationModuleDefinition } from './types';
Expand All @@ -29,7 +31,7 @@ export abstract class InstrumentationBase<T = any>
extends InstrumentationAbstract
implements types.Instrumentation {
private _modules: InstrumentationModuleDefinition<T>[];
private _hooks: RequireInTheMiddle.Hooked[] = [];
private _hooks = 0;
private _enabled = false;

constructor(
Expand Down Expand Up @@ -71,12 +73,12 @@ export abstract class InstrumentationBase<T = any>
return undefined;
}

private _onRequire<T>(
module: InstrumentationModuleDefinition<T>,
exports: T,
private _onRequire<V>(
module: InstrumentationModuleDefinition<V>,
exports: V,
name: string,
baseDir?: string
): T {
): V {
if (!baseDir) {
if (typeof module.patch === 'function') {
module.moduleExports = exports;
Expand Down Expand Up @@ -120,7 +122,7 @@ export abstract class InstrumentationBase<T = any>
this._enabled = true;

// already hooked, just call patch again
if (this._hooks.length > 0) {
if (this._hooks > 0) {
for (const module of this._modules) {
if (typeof module.patch === 'function' && module.moduleExports) {
module.patch(module.moduleExports, module.moduleVersion);
Expand All @@ -135,22 +137,19 @@ export abstract class InstrumentationBase<T = any>
}

for (const module of this._modules) {
this._hooks.push(
RequireInTheMiddle(
[module.name],
{ internals: true },
(exports, name, baseDir) => {
return this._onRequire<typeof exports>(
(module as unknown) as InstrumentationModuleDefinition<
typeof exports
>,
exports,
name,
baseDir
);
}
)
);
this._hooks++;
const hookFn: RequireInTheMiddle.OnRequireFn = (exports, name, baseDir) => {
return this._onRequire<typeof exports>(
(module as unknown) as InstrumentationModuleDefinition<
typeof exports
>,
exports,
name,
baseDir
);
};
RequireInTheMiddle([module.name], { internals: true }, hookFn);
new (ImportInTheMiddle as unknown as typeof ImportInTheMiddle.default) ([module.name], { internals: true }, <HookFn>hookFn);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ declare module 'require-in-the-middle' {
type Options = {
internals?: boolean;
};
type OnRequireFn = <T>(exports: T, name: string, basedir?: string) => T;
export type OnRequireFn = <T>(exports: T, name: string, basedir?: string) => T;
type Hooked = { unhook(): void };
}
function hook(
Expand Down