Skip to content

Commit

Permalink
fix: fix mongodb imports + global improve
Browse files Browse the repository at this point in the history
  • Loading branch information
Dori Aviram committed Aug 12, 2020
1 parent 88e2faa commit 4e438b5
Show file tree
Hide file tree
Showing 8 changed files with 47 additions and 57 deletions.
1 change: 1 addition & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ module.exports = {
'**/src/**/**/**/**.js',
'!./src/tools/xmlToJson.js',
'!./src/testdata/functions/**/**.js',
'!./d_example/**/**',
],
coverageThreshold: {
global: {
Expand Down
8 changes: 2 additions & 6 deletions src/globals.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,13 +110,9 @@ export const TracerGlobals = (() => {
}) =>
Object.assign(tracerInputs, {
token: token || process.env.LUMIGO_TRACER_TOKEN,
debug:
debug ||
!!(process.env['LUMIGO_DEBUG'] && process.env.LUMIGO_DEBUG.toUpperCase() === 'TRUE'),
debug: debug,
edgeHost: edgeHost || process.env.LUMIGO_TRACER_HOST,
switchOff:
switchOff ||
!!(process.env['LUMIGO_SWITCH_OFF'] && process.env.LUMIGO_SWITCH_OFF === 'TRUE'),
switchOff: switchOff,
isStepFunction:
stepFunction ||
!!(
Expand Down
36 changes: 0 additions & 36 deletions src/globals.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,54 +132,18 @@ describe('globals', () => {
});

test('setGlobals debug', () => {
process.env.LUMIGO_DEBUG = 'TRUE';
globals.TracerGlobals.setTracerInputs({ debug: true });
expect(globals.TracerGlobals.getTracerInputs().debug).toEqual(true);

process.env.LUMIGO_DEBUG = 'TRUE';
globals.TracerGlobals.setTracerInputs({ debug: false });
expect(globals.TracerGlobals.getTracerInputs().debug).toEqual(true);

process.env.LUMIGO_DEBUG = 'TRUE';
globals.TracerGlobals.setTracerInputs({});
expect(globals.TracerGlobals.getTracerInputs().debug).toEqual(true);

process.env.LUMIGO_DEBUG = '';
globals.TracerGlobals.setTracerInputs({ debug: true });
expect(globals.TracerGlobals.getTracerInputs().debug).toEqual(true);

process.env.LUMIGO_DEBUG = '';
globals.TracerGlobals.setTracerInputs({ debug: false });
expect(globals.TracerGlobals.getTracerInputs().debug).toEqual(false);

process.env.LUMIGO_DEBUG = '';
globals.TracerGlobals.setTracerInputs({});
expect(globals.TracerGlobals.getTracerInputs().debug).toEqual(false);
});

test('setGlobals switchOff', () => {
process.env.LUMIGO_SWITCH_OFF = 'TRUE';
globals.TracerGlobals.setTracerInputs({ switchOff: true });
expect(globals.TracerGlobals.getTracerInputs().switchOff).toEqual(true);

process.env.LUMIGO_SWITCH_OFF = 'TRUE';
globals.TracerGlobals.setTracerInputs({ switchOff: false });
expect(globals.TracerGlobals.getTracerInputs().switchOff).toEqual(true);

process.env.LUMIGO_SWITCH_OFF = 'TRUE';
globals.TracerGlobals.setTracerInputs({});
expect(globals.TracerGlobals.getTracerInputs().switchOff).toEqual(true);

process.env.LUMIGO_SWITCH_OFF = '';
globals.TracerGlobals.setTracerInputs({ switchOff: true });
expect(globals.TracerGlobals.getTracerInputs().switchOff).toEqual(true);

process.env.LUMIGO_SWITCH_OFF = '';
globals.TracerGlobals.setTracerInputs({ switchOff: false });
expect(globals.TracerGlobals.getTracerInputs().switchOff).toEqual(false);

process.env.LUMIGO_SWITCH_OFF = '';
globals.TracerGlobals.setTracerInputs({});
expect(globals.TracerGlobals.getTracerInputs().switchOff).toEqual(false);
});

Expand Down
3 changes: 3 additions & 0 deletions src/hooks/mongodb.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,5 +82,8 @@ export const hookMongoDb = mongoLib => {
listener.on('succeeded', safeSucceededHook);
listener.on('failed', safeFailedHook);
})();
logger.info('MongoDB instrumentation done');
} else {
logger.debug('MongoDB SDK not found');
}
};
10 changes: 2 additions & 8 deletions src/logger.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { TracerGlobals } from './globals';
import { isStoreLogs } from './utils';
import { isDebug, isStoreLogs } from './utils';
const LOG_PREFIX = '#LUMIGO#';
const WARN_CLIENT_PREFIX = 'Lumigo Warning';

Expand Down Expand Up @@ -42,11 +41,6 @@ export const LogStore = (() => {
return { addLog, clean };
})();

export const isDebug = () => {
let tracerInputs = TracerGlobals.getTracerInputs();
return tracerInputs.debug;
};

const invokeLog = type => (msg, obj = undefined) => log(type, msg, obj);

export const info = invokeLog('INFO');
Expand All @@ -60,7 +54,7 @@ export const debug = invokeLog('DEBUG');
export const log = (levelname, message, obj) => {
const storeLogsIsOn = isStoreLogs();
storeLogsIsOn && LogStore.addLog(levelname, message, obj);
if (exports.isDebug() && !storeLogsIsOn) {
if (isDebug() && !storeLogsIsOn) {
forceLog(levelname, message, obj);
}
};
Expand Down
18 changes: 13 additions & 5 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,9 @@ const TIMEOUT_MIN_DURATION = 'LUMIGO_TIMEOUT_MIN_DURATION';
const TIMEOUT_BUFFER_FLAG_MS = 'LUMIGO_TIMEOUT_BUFFER_MS';
const AGENT_KEEPALIVE = 'LUMIGO_AGENT_KEEPALIVE_MS';
const REUSE_CONNECTION = 'LUMIGO_REUSE_HTTP_CONNECTION';
const DEBUG_FLAG = 'LUMIGO_DEBUG';
const SWITCH_OFF_FLAG = 'LUMIGO_SWITCH_OFF';
const IS_STEP_FUNCTION_FLAG = 'LUMIGO_STEP_FUNCTION';

const validateEnvVar = (envVar, value = 'TRUE') =>
!!(process.env[envVar] && process.env[envVar].toUpperCase() === value.toUpperCase());
Expand Down Expand Up @@ -187,6 +190,8 @@ export const isVerboseMode = () => validateEnvVar(VERBOSE_FLAG);

export const isWarm = () => validateEnvVar(WARM_FLAG);

export const isDebug = () => validateEnvVar(DEBUG_FLAG) || TracerGlobals.getTracerInputs().debug;

export const isLambdaWrapped = () => validateEnvVar(WRAPPED_FLAG);

export const setLambdaWrapped = () => (process.env[WRAPPED_FLAG] = 'TRUE');
Expand All @@ -200,12 +205,15 @@ export const isSendOnlyIfErrors = () => validateEnvVar(SEND_ONLY_IF_ERROR_FLAG);
export const isPruneTraceOff = () => validateEnvVar(PRUNE_TRACE_OFF_FLAG);

export const isSwitchedOff = () =>
safeExecute(() => {
return TracerGlobals.getTracerInputs().switchOff || !isValidAlias();
})();
safeExecute(
() =>
validateEnvVar(SWITCH_OFF_FLAG) ||
TracerGlobals.getTracerInputs().switchOff ||
!isValidAlias()
)();

export const isStepFunction = () =>
safeExecute(() => TracerGlobals.getTracerInputs().isStepFunction)();
validateEnvVar(IS_STEP_FUNCTION_FLAG) || TracerGlobals.getTracerInputs().isStepFunction;

export const getValidAliases = () =>
safeExecute(() => {
Expand All @@ -228,7 +236,7 @@ export const isValidAlias = () => {
const validAlias =
!currentAlias || validAliases.length === 0 || validAliases.includes(currentAlias);
if (!validAlias) {
logger.info(`Alias is invalid, alias: ${currentAlias}`);
logger.debug(`Alias is invalid, alias: ${currentAlias}`);
}
return validAlias;
};
Expand Down
23 changes: 22 additions & 1 deletion src/utils.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,15 @@ import {
recursiveGetKey,
md5Hash,
safeGet,
isDebug,
} from './utils';
import { TracerGlobals } from './globals';
import crypto from 'crypto';
import { isDebug } from './logger';
import { GET_KEY_DEPTH_ENV_KEY } from './utils';
import { ConsoleWritesForTesting } from '../testUtils/consoleMocker';
import { getEnvVarAsList, isEncodingType, isEmptyString, runOneTimeWrapper } from './utils';
import { DEFAULT_TIMEOUT_MIN_DURATION } from './utils';
import * as globals from './globals';

describe('utils', () => {
const spies = {};
Expand Down Expand Up @@ -158,6 +159,18 @@ describe('utils', () => {
expect(utils.isWarm()).toBe(true);
});

test('isDebug -> ENV VAR', () => {
expect(utils.isDebug()).toBe(false);
process.env.LUMIGO_DEBUG = 'TRUE';
expect(utils.isDebug()).toBe(true);
});

test('isDebug -> TracerInputs', () => {
expect(utils.isDebug()).toBe(false);
globals.TracerGlobals.setTracerInputs({ debug: true });
expect(utils.isDebug()).toBe(true);
});

test('isLambdaWrapped', () => {
expect(utils.isLambdaWrapped()).toBe(false);
process.env.LUMIGO_IS_WRAPPED = 'TRUE';
Expand Down Expand Up @@ -245,6 +258,14 @@ describe('utils', () => {
TracerGlobals.clearHandlerInputs();
});

test('isSwitchedOff -> TracerInputs', () => {
expect(utils.isSwitchedOff()).toBe(false);
TracerGlobals.setTracerInputs({
switchOff: true,
});
expect(utils.isSwitchedOff()).toBe(true);
});

test('isSwitchedOffInvalidAlias', () => {
expect(utils.isSwitchedOff()).toBe(false);
TracerGlobals.setHandlerInputs({
Expand Down
5 changes: 4 additions & 1 deletion webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ module.exports = () => {
return {
target: 'node',
mode: 'production',
output: { libraryTarget: 'umd' },
output: { libraryTarget: 'commonjs2' },
externals: {
mongodb: 'mongodb',
},
};
};

0 comments on commit 4e438b5

Please sign in to comment.