Skip to content

Commit

Permalink
revised version of open-telemetry#2640
Browse files Browse the repository at this point in the history
  • Loading branch information
lizthegrey committed Feb 4, 2022
1 parent ee2342b commit 7f872a6
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 23 deletions.
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';
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);
}
}

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

0 comments on commit 7f872a6

Please sign in to comment.