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 1 commit
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
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
},
"dependencies": {
"@opentelemetry/api-metrics": "0.27.0",
"import-in-the-middle": "^1.2.0",
"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,7 @@
import * as types from '../../types';
import * as path from 'path';
import * as RequireInTheMiddle from 'require-in-the-middle';
import ImportInTheMiddle, { HookFn } from 'import-in-the-middle';
lizthegrey marked this conversation as resolved.
Show resolved Hide resolved
import { satisfies } from 'semver';
import { InstrumentationAbstract } from '../../instrumentation';
import { InstrumentationModuleDefinition } from './types';
Expand All @@ -29,7 +30,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 +72,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 +121,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 +136,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([module.name], { internals: true }, <HookFn>hookFn);
lizthegrey marked this conversation as resolved.
Show resolved Hide resolved
}
}

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