Skip to content
This repository has been archived by the owner on Jul 10, 2023. It is now read-only.

Commit

Permalink
Added flag to enable/disable (enable by default for now) load time in…
Browse files Browse the repository at this point in the history
…strumentation by `require-in-the-middle`
  • Loading branch information
Serkan ÖZAL committed Mar 27, 2022
1 parent d97a487 commit 39558ea
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 7 deletions.
4 changes: 4 additions & 0 deletions src/config/ConfigMetadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,10 @@ export const ConfigMetadata: {[key: string]: { type: string, defaultValue?: any
[ConfigNames.THUNDRA_TRACE_INSTRUMENT_FILE_PREFIX]: {
type: 'string',
},
[ConfigNames.THUNDRA_TRACE_INSTRUMENT_ONLOAD]: {
type: 'boolean',
defaultValue: true,
},
[ConfigNames.THUNDRA_TRACE_SPAN_LISTENERCONFIG]: {
type: 'string',
},
Expand Down
2 changes: 2 additions & 0 deletions src/config/ConfigNames.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,8 @@ class ConfigNames {
'thundra.agent.trace.instrument.traceableconfig';
public static readonly THUNDRA_TRACE_INSTRUMENT_FILE_PREFIX: string =
'thundra.agent.trace.instrument.file.prefix';
public static readonly THUNDRA_TRACE_INSTRUMENT_ONLOAD: string =
'thundra.agent.trace.instrument.onload';

/////////////////////////////////////////////////////////////////////////////

Expand Down
7 changes: 7 additions & 0 deletions src/init/InitManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
*/
import { INITIALIZERS } from './Initializers';
import ThundraLogger from '../ThundraLogger';
import ModuleUtils from '../utils/ModuleUtils';
import ConfigProvider from '../config/ConfigProvider';
import ConfigNames from '../config/ConfigNames';

/**
* Manages initialization of initializers
Expand All @@ -20,6 +23,10 @@ class InitManager {
static init(): void {
ThundraLogger.debug(`<InitManager> Initializing initializers ...`);
if (!InitManager.initialized) {
const instrumentOnLoad: boolean =
ConfigProvider.get<boolean>(ConfigNames.THUNDRA_TRACE_INSTRUMENT_ONLOAD);
ModuleUtils.setInstrumentOnLoad(instrumentOnLoad);

INITIALIZERS.forEach((initializer: any) => {
ThundraLogger.debug(`<InitManager> Initializing ${initializer.name} ...`);
if (!initializer.initialized) {
Expand Down
33 changes: 26 additions & 7 deletions src/utils/ModuleUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,27 @@ class ModuleUtils {

private static readonly instrumenters: any = [];
private static readonly pendingModulesToInstrument: any = [];
private static instrumentOnLoad: boolean = true;

private constructor() {
}

/**
* Gets the load time instrumentation mode flag
* @return the load time instrumentation mode flag
*/
static isInstrumentOnLoad(): boolean {
return ModuleUtils.instrumentOnLoad;
}

/**
* Sets the load time instrumentation mode flag
* @param instrumentOnLoad the load time instrumentation mode flag to be set
*/
static setInstrumentOnLoad(instrumentOnLoad: boolean): void {
ModuleUtils.instrumentOnLoad = instrumentOnLoad;
}

/**
* Tries to require given module by its name and paths
* @param {string} name the module name
Expand Down Expand Up @@ -145,13 +162,15 @@ class ModuleUtils {
ModuleUtils.doInstrument(requiredLib, libs, null, moduleName, null, wrapper, config);
}
}
const hook = Hook(moduleName, { internals: true }, (lib: any, name: string, basedir: string) => {
if (name === moduleName) {
ModuleUtils.doInstrument(lib, libs, basedir, moduleName, version, wrapper, config);
}
return lib;
});
hooks.push(hook);
if (ModuleUtils.instrumentOnLoad) {
const hook = Hook(moduleName, {internals: true}, (lib: any, name: string, basedir: string) => {
if (name === moduleName) {
ModuleUtils.doInstrument(lib, libs, basedir, moduleName, version, wrapper, config);
}
return lib;
});
hooks.push(hook);
}
}
return {
uninstrument: () => {
Expand Down

0 comments on commit 39558ea

Please sign in to comment.