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

Added flag to enable/disable (enable by default for now) load time instrumentation … #341

Merged
merged 1 commit into from
Mar 27, 2022
Merged
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
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