Skip to content

Commit 72fd332

Browse files
authored
Merge pull request #154 from lumigo-io/fix-mongo-imports
fix: fix Mongodb imports + global improve
2 parents d722904 + eac2bb3 commit 72fd332

8 files changed

+47
-57
lines changed

jest.config.js

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ module.exports = {
77
'**/src/**/**/**/**.js',
88
'!./src/tools/xmlToJson.js',
99
'!./src/testdata/functions/**/**.js',
10+
'!./d_example/**/**',
1011
],
1112
coverageThreshold: {
1213
global: {

src/globals.js

+2-6
Original file line numberDiff line numberDiff line change
@@ -110,13 +110,9 @@ export const TracerGlobals = (() => {
110110
}) =>
111111
Object.assign(tracerInputs, {
112112
token: token || process.env.LUMIGO_TRACER_TOKEN,
113-
debug:
114-
debug ||
115-
!!(process.env['LUMIGO_DEBUG'] && process.env.LUMIGO_DEBUG.toUpperCase() === 'TRUE'),
113+
debug: debug,
116114
edgeHost: edgeHost || process.env.LUMIGO_TRACER_HOST,
117-
switchOff:
118-
switchOff ||
119-
!!(process.env['LUMIGO_SWITCH_OFF'] && process.env.LUMIGO_SWITCH_OFF === 'TRUE'),
115+
switchOff: switchOff,
120116
isStepFunction:
121117
stepFunction ||
122118
!!(

src/globals.test.js

-36
Original file line numberDiff line numberDiff line change
@@ -132,54 +132,18 @@ describe('globals', () => {
132132
});
133133

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

139-
process.env.LUMIGO_DEBUG = 'TRUE';
140138
globals.TracerGlobals.setTracerInputs({ debug: false });
141-
expect(globals.TracerGlobals.getTracerInputs().debug).toEqual(true);
142-
143-
process.env.LUMIGO_DEBUG = 'TRUE';
144-
globals.TracerGlobals.setTracerInputs({});
145-
expect(globals.TracerGlobals.getTracerInputs().debug).toEqual(true);
146-
147-
process.env.LUMIGO_DEBUG = '';
148-
globals.TracerGlobals.setTracerInputs({ debug: true });
149-
expect(globals.TracerGlobals.getTracerInputs().debug).toEqual(true);
150-
151-
process.env.LUMIGO_DEBUG = '';
152-
globals.TracerGlobals.setTracerInputs({ debug: false });
153-
expect(globals.TracerGlobals.getTracerInputs().debug).toEqual(false);
154-
155-
process.env.LUMIGO_DEBUG = '';
156-
globals.TracerGlobals.setTracerInputs({});
157139
expect(globals.TracerGlobals.getTracerInputs().debug).toEqual(false);
158140
});
159141

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

165-
process.env.LUMIGO_SWITCH_OFF = 'TRUE';
166146
globals.TracerGlobals.setTracerInputs({ switchOff: false });
167-
expect(globals.TracerGlobals.getTracerInputs().switchOff).toEqual(true);
168-
169-
process.env.LUMIGO_SWITCH_OFF = 'TRUE';
170-
globals.TracerGlobals.setTracerInputs({});
171-
expect(globals.TracerGlobals.getTracerInputs().switchOff).toEqual(true);
172-
173-
process.env.LUMIGO_SWITCH_OFF = '';
174-
globals.TracerGlobals.setTracerInputs({ switchOff: true });
175-
expect(globals.TracerGlobals.getTracerInputs().switchOff).toEqual(true);
176-
177-
process.env.LUMIGO_SWITCH_OFF = '';
178-
globals.TracerGlobals.setTracerInputs({ switchOff: false });
179-
expect(globals.TracerGlobals.getTracerInputs().switchOff).toEqual(false);
180-
181-
process.env.LUMIGO_SWITCH_OFF = '';
182-
globals.TracerGlobals.setTracerInputs({});
183147
expect(globals.TracerGlobals.getTracerInputs().switchOff).toEqual(false);
184148
});
185149

src/hooks/mongodb.js

+3
Original file line numberDiff line numberDiff line change
@@ -82,5 +82,8 @@ export const hookMongoDb = mongoLib => {
8282
listener.on('succeeded', safeSucceededHook);
8383
listener.on('failed', safeFailedHook);
8484
})();
85+
logger.info('MongoDB instrumentation done');
86+
} else {
87+
logger.debug('MongoDB SDK not found');
8588
}
8689
};

src/logger.js

+2-8
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
import { TracerGlobals } from './globals';
2-
import { isStoreLogs } from './utils';
1+
import { isDebug, isStoreLogs } from './utils';
32
const LOG_PREFIX = '#LUMIGO#';
43
const WARN_CLIENT_PREFIX = 'Lumigo Warning';
54

@@ -42,11 +41,6 @@ export const LogStore = (() => {
4241
return { addLog, clean };
4342
})();
4443

45-
export const isDebug = () => {
46-
let tracerInputs = TracerGlobals.getTracerInputs();
47-
return tracerInputs.debug;
48-
};
49-
5044
const invokeLog = type => (msg, obj = undefined) => log(type, msg, obj);
5145

5246
export const info = invokeLog('INFO');
@@ -60,7 +54,7 @@ export const debug = invokeLog('DEBUG');
6054
export const log = (levelname, message, obj) => {
6155
const storeLogsIsOn = isStoreLogs();
6256
storeLogsIsOn && LogStore.addLog(levelname, message, obj);
63-
if (exports.isDebug() && !storeLogsIsOn) {
57+
if (isDebug() && !storeLogsIsOn) {
6458
forceLog(levelname, message, obj);
6559
}
6660
};

src/utils.js

+13-5
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,9 @@ const TIMEOUT_MIN_DURATION = 'LUMIGO_TIMEOUT_MIN_DURATION';
139139
const TIMEOUT_BUFFER_FLAG_MS = 'LUMIGO_TIMEOUT_BUFFER_MS';
140140
const AGENT_KEEPALIVE = 'LUMIGO_AGENT_KEEPALIVE_MS';
141141
const REUSE_CONNECTION = 'LUMIGO_REUSE_HTTP_CONNECTION';
142+
const DEBUG_FLAG = 'LUMIGO_DEBUG';
143+
const SWITCH_OFF_FLAG = 'LUMIGO_SWITCH_OFF';
144+
const IS_STEP_FUNCTION_FLAG = 'LUMIGO_STEP_FUNCTION';
142145

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

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

193+
export const isDebug = () => validateEnvVar(DEBUG_FLAG) || TracerGlobals.getTracerInputs().debug;
194+
190195
export const isLambdaWrapped = () => validateEnvVar(WRAPPED_FLAG);
191196

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

202207
export const isSwitchedOff = () =>
203-
safeExecute(() => {
204-
return TracerGlobals.getTracerInputs().switchOff || !isValidAlias();
205-
})();
208+
safeExecute(
209+
() =>
210+
validateEnvVar(SWITCH_OFF_FLAG) ||
211+
TracerGlobals.getTracerInputs().switchOff ||
212+
!isValidAlias()
213+
)();
206214

207215
export const isStepFunction = () =>
208-
safeExecute(() => TracerGlobals.getTracerInputs().isStepFunction)();
216+
validateEnvVar(IS_STEP_FUNCTION_FLAG) || TracerGlobals.getTracerInputs().isStepFunction;
209217

210218
export const getValidAliases = () =>
211219
safeExecute(() => {
@@ -228,7 +236,7 @@ export const isValidAlias = () => {
228236
const validAlias =
229237
!currentAlias || validAliases.length === 0 || validAliases.includes(currentAlias);
230238
if (!validAlias) {
231-
logger.info(`Alias is invalid, alias: ${currentAlias}`);
239+
logger.debug(`Alias is invalid, alias: ${currentAlias}`);
232240
}
233241
return validAlias;
234242
};

src/utils.test.js

+22-1
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,15 @@ import {
99
recursiveGetKey,
1010
md5Hash,
1111
safeGet,
12+
isDebug,
1213
} from './utils';
1314
import { TracerGlobals } from './globals';
1415
import crypto from 'crypto';
15-
import { isDebug } from './logger';
1616
import { GET_KEY_DEPTH_ENV_KEY } from './utils';
1717
import { ConsoleWritesForTesting } from '../testUtils/consoleMocker';
1818
import { getEnvVarAsList, isEncodingType, isEmptyString, runOneTimeWrapper } from './utils';
1919
import { DEFAULT_TIMEOUT_MIN_DURATION } from './utils';
20+
import * as globals from './globals';
2021

2122
describe('utils', () => {
2223
const spies = {};
@@ -158,6 +159,18 @@ describe('utils', () => {
158159
expect(utils.isWarm()).toBe(true);
159160
});
160161

162+
test('isDebug -> ENV VAR', () => {
163+
expect(utils.isDebug()).toBe(false);
164+
process.env.LUMIGO_DEBUG = 'TRUE';
165+
expect(utils.isDebug()).toBe(true);
166+
});
167+
168+
test('isDebug -> TracerInputs', () => {
169+
expect(utils.isDebug()).toBe(false);
170+
globals.TracerGlobals.setTracerInputs({ debug: true });
171+
expect(utils.isDebug()).toBe(true);
172+
});
173+
161174
test('isLambdaWrapped', () => {
162175
expect(utils.isLambdaWrapped()).toBe(false);
163176
process.env.LUMIGO_IS_WRAPPED = 'TRUE';
@@ -245,6 +258,14 @@ describe('utils', () => {
245258
TracerGlobals.clearHandlerInputs();
246259
});
247260

261+
test('isSwitchedOff -> TracerInputs', () => {
262+
expect(utils.isSwitchedOff()).toBe(false);
263+
TracerGlobals.setTracerInputs({
264+
switchOff: true,
265+
});
266+
expect(utils.isSwitchedOff()).toBe(true);
267+
});
268+
248269
test('isSwitchedOffInvalidAlias', () => {
249270
expect(utils.isSwitchedOff()).toBe(false);
250271
TracerGlobals.setHandlerInputs({

webpack.config.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@ module.exports = () => {
22
return {
33
target: 'node',
44
mode: 'production',
5-
output: { libraryTarget: 'umd' },
5+
output: { libraryTarget: 'commonjs2' },
6+
externals: {
7+
mongodb: 'mongodb',
8+
},
69
};
710
};

0 commit comments

Comments
 (0)