diff --git a/src/utils.test.ts b/src/utils.test.ts index 27be7283..55850d9a 100644 --- a/src/utils.test.ts +++ b/src/utils.test.ts @@ -1,6 +1,44 @@ -import { extractEnvVars } from './utils'; +import { extractEnvVars, logger, safeRequire } from './utils'; +import http from 'http'; describe('utils tests', () => { + afterEach(() => { + jest.clearAllMocks(); + }); + + test('safeRequire -> simple flow', () => { + const http = require('http'); + + const result = safeRequire('http'); + + expect(http).toEqual(result); + }); + + test('safeRequire -> simple flow', () => { + delete process.env.NODE_PATH; + jest.spyOn(logger, 'warn'); + const result = safeRequire('BlaBlaBlaBla'); + + expect(result).toEqual(undefined); + expect(logger.warn).toBeCalledTimes(0); + }); + + test('safeRequire -> not exist', () => { + const result = safeRequire('BlaBlaBlaBla'); + + expect(result).toBeFalsy(); + }); + + test('safeRequire -> other error', () => { + jest.doMock('fs', () => { + throw Error('RandomError'); + }); + + const result = safeRequire('fs'); + + expect(result).toBeFalsy(); + }); + describe('when evn vars are less then max size', () => { it('extractEnvVars should not filter anything', () => { process.env = { diff --git a/src/utils.ts b/src/utils.ts index 9e681085..dc2d1e85 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -146,3 +146,32 @@ export const removeDuplicates = (arr) => Array.from(new Set(arr)); export const logger: DiagLogger = diag.createComponentLogger({ namespace: '@lumigo/opentelemetry:', }); + +export const safeRequire = (libId) => { + try { + const customReq = + // eslint-disable-next-line no-undef,camelcase + // @ts-ignore __non_webpack_require__ not available at compile time + typeof __non_webpack_require__ !== 'undefined' ? __non_webpack_require__ : require; + return customReq(libId); + } catch (e) { + try { + const customReq = + // eslint-disable-next-line no-undef,camelcase + // @ts-ignore __non_webpack_require__ not available at compile time + typeof __non_webpack_require__ !== 'undefined' ? __non_webpack_require__ : require; + const path = customReq.resolve(libId, { + paths: (process.env.NODE_PATH || '').split(':'), + }); + return customReq(path); + } catch (e) { + if (e.code !== 'MODULE_NOT_FOUND') { + logger.warn('Unable to load module', { + error: e, + libId: libId, + }); + } + } + } + return undefined; +}; diff --git a/src/wrapper.ts b/src/wrapper.ts index 97ceaaa3..cb1921d7 100644 --- a/src/wrapper.ts +++ b/src/wrapper.ts @@ -12,7 +12,7 @@ import { NodeTracerProvider } from '@opentelemetry/sdk-trace-node'; import { FileSpanExporter } from './exporters'; import LumigoExpressInstrumentation from './instrumentors/LumigoExpressInstrumentation'; import LumigoHttpInstrumentation from './instrumentors/LumigoHttpInstrumentation'; -import { extractEnvVars, isEnvVarTrue, logger } from './utils'; +import { extractEnvVars, isEnvVarTrue, logger, safeRequire } from './utils'; import * as awsResourceDetectors from '@opentelemetry/resource-detector-aws'; import { AwsEcsDetector, LumigoDistroDetector } from './resources/detectors'; @@ -31,35 +31,6 @@ let isTraceInitialized = false; const externalInstrumentations = []; -const safeRequire = (libId) => { - try { - const customReq = - // eslint-disable-next-line no-undef,camelcase - // @ts-ignore __non_webpack_require__ not available at compile time - typeof __non_webpack_require__ !== 'undefined' ? __non_webpack_require__ : require; - return customReq(libId); - } catch (e) { - try { - const customReq = - // eslint-disable-next-line no-undef,camelcase - // @ts-ignore __non_webpack_require__ not available at compile time - typeof __non_webpack_require__ !== 'undefined' ? __non_webpack_require__ : require; - const path = customReq.resolve(libId, { - paths: [...process.env.NODE_PATH.split(':'), '/var/task/node_modules/'], - }); - return customReq(path); - } catch (e) { - if (e.code !== 'MODULE_NOT_FOUND') { - logger.warn('Unable to load module', { - error: e, - libId: libId, - }); - } - } - } - return undefined; -}; - function requireIfAvailable(names: string[]) { names.forEach((name) => safeRequire(name)); }