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

Commit

Permalink
Thundra Log Plugin NodeJS console integration
Browse files Browse the repository at this point in the history
  • Loading branch information
İbrahim Gürses committed Dec 7, 2018
1 parent 2f25aff commit a8a1068
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 8 deletions.
1 change: 1 addition & 0 deletions README.md
Expand Up @@ -71,6 +71,7 @@ Check out the [configuration part](https://thundra.readme.io/docs/nodejs-configu
| thundra_agent_lambda_trace_instrument_integrations_disable | array | [] |
| thundra_agent_lambda_metric_sample_sampler_timeAware_timeFreq | number | 300000 |
| thundra_agent_lambda_metric_sample_sampler_countAware_countFreq | number | 10 |
| thundra_agent_lambda_log_console_shim_disable | bool | false |

#### 2. Module initialization parameters

Expand Down
7 changes: 6 additions & 1 deletion src/Constants.ts
Expand Up @@ -27,7 +27,8 @@ export const envVariableKeys = {
THUNDRA_LAMBDA_TRACE_INSTRUMENT_CONFIG: 'thundra_agent_lambda_trace_instrument_traceableConfig',
THUNDRA_LAMBDA_TRACE_INSTRUMENT_FILE_PREFIX: 'thundra_agent_lambda_trace_instrument_file_prefix',
THUNDRA_LAMBDA_TRACE_INTEGRATIONS_DISABLE: 'thundra_agent_lambda_trace_instrument_integrations_disable',
THUNDRRA_LAMBDA_LOG_LOGLEVEL: 'thundra_agent_lambda_log_loglevel',
THUNDRA_LAMBDA_LOG_CONSOLE_SHIM_DISABLE: 'thundra_agent_lambda_log_console_shim_disable',
THUNDRA_LAMBDA_LOG_LOGLEVEL: 'thundra_agent_lambda_log_loglevel',
THUNDRA_AGENT_LAMBDA_AGENT_DEBUG_ENABLE: 'thundra_agent_lambda_debug_enable',

AWS_LAMBDA_APPLICATION_ID: 'AWS_LAMBDA_APPLICATION_ID',
Expand Down Expand Up @@ -460,3 +461,7 @@ export const SQLQueryOperationTypes: any = {
UPDATE: 'WRITE',
DELETE: 'WRITE',
};

export const ConsoleShimmedMethods = ['log', 'debug', 'info', 'warn', 'error'];

export const ConsoleLogContext = 'Console';
33 changes: 29 additions & 4 deletions src/plugins/Log.ts
Expand Up @@ -4,7 +4,8 @@ import LogData from './data/log/LogData';
import PluginContext from './PluginContext';
import MonitoringDataType from './data/base/MonitoringDataType';
import ThundraTracer from '../opentracing/Tracer';
import LogManager from './LogManager';
import { ConsoleShimmedMethods, logLevels, ConsoleLogContext, envVariableKeys} from '../Constants';
import * as util from 'util';

class Log {
static instance: Log;
Expand All @@ -29,19 +30,23 @@ class Log {
this.tracer = ThundraTracer.getInstance();

Log.instance = this;

if (Utils.getConfiguration(envVariableKeys.THUNDRA_LAMBDA_LOG_CONSOLE_SHIM_DISABLE) !== 'true') {
this.shimConsole();
}
}

static getInstance(): Log {
return Log.instance;
}

report = (logReport: any) => {
report(logReport: any): void {
if (this.reporter) {
this.reporter.addReport(logReport);
}
}

setPluginContext = (pluginContext: PluginContext) => {
setPluginContext(pluginContext: PluginContext): void {
this.pluginContext = pluginContext;
this.apiKey = pluginContext.apiKey;
}
Expand Down Expand Up @@ -90,14 +95,34 @@ class Log {
}
}

reportLog = (logInfo: any) => {
reportLog(logInfo: any): void {
const logData = new LogData();
const activeSpan = this.tracer ? this.tracer.getActiveSpan() : undefined;
const spanId = activeSpan ? activeSpan.spanContext.spanId : '';
logData.initWithLogDataValues(this.logData, spanId, logInfo);

this.logs.push(logData);
}

shimConsole(): void {
ConsoleShimmedMethods.forEach((method) => {
const consoleReference: any = console;
const originalConsoleMethod = consoleReference[method].bind(console);

consoleReference[method] = (...args: any[]) => {
const logInfo = {
logMessage: util.format.apply(util, args),
logLevel: method ? method.toUpperCase() : 'INFO',
logLevelCode: logLevels[method],
logContextName: ConsoleLogContext,
logTimestamp: Date.now(),
};

this.reportLog(logInfo);
originalConsoleMethod.apply(console, args);
};
});
}
}

export default Log;
2 changes: 1 addition & 1 deletion src/plugins/Logger.ts
Expand Up @@ -13,7 +13,7 @@ class Logger {
constructor(options: { loggerName: any; }) {
this.options = options;
this.loggerName = options && options.loggerName ? options.loggerName : 'default';
const levelConfig = Utils.getConfiguration(envVariableKeys.THUNDRRA_LAMBDA_LOG_LOGLEVEL);
const levelConfig = Utils.getConfiguration(envVariableKeys.THUNDRA_LAMBDA_LOG_LOGLEVEL);
this.logLevel = levelConfig && logLevels[levelConfig] ? logLevels[levelConfig] : 0;
this.levels = { // higher number = higher priority
trace: this.trace, // 0
Expand Down
34 changes: 34 additions & 0 deletions test/plugins/console.shim.test.js
@@ -0,0 +1,34 @@
import LogPlugin from '../../dist/plugins/Log';
import { createMockPluginContext, createMockBeforeInvocationData } from '../mocks/mocks';

describe('Log plugin shim console', () => {
const logPlugin = new LogPlugin();
const pluginContext = createMockPluginContext();
const beforeInvocationData = createMockBeforeInvocationData();
logPlugin.setPluginContext(pluginContext);
logPlugin.beforeInvocation(beforeInvocationData);

console.log('log');
console.debug('debug');
console.info('info');
console.warn('warn');
console.error('error');

it('should capture console.log statements', () => {
expect(logPlugin.logs.length).toBe(5);
expect(logPlugin.logs[0].logLevel).toBe('LOG');
expect(logPlugin.logs[0].logMessage).toBe('log');

expect(logPlugin.logs[1].logLevel).toBe('DEBUG');
expect(logPlugin.logs[1].logMessage).toBe('debug');

expect(logPlugin.logs[2].logLevel).toBe('INFO');
expect(logPlugin.logs[2].logMessage).toBe('info');

expect(logPlugin.logs[3].logLevel).toBe('WARN');
expect(logPlugin.logs[3].logMessage).toBe('warn');

expect(logPlugin.logs[4].logLevel).toBe('ERROR');
expect(logPlugin.logs[4].logMessage).toBe('error');
});
});
4 changes: 2 additions & 2 deletions test/plugins/log.test.js
@@ -1,5 +1,5 @@
import LogPlugin from '../../dist/plugins/Log';
import { createMockReporter, createMockPluginContext, createMockBeforeInvocationData, createMockContext } from '../mocks/mocks';
import { createMockReporter, createMockPluginContext, createMockBeforeInvocationData} from '../mocks/mocks';

describe('LogPlugin', () => {
describe('constructor', () => {
Expand Down Expand Up @@ -66,4 +66,4 @@ describe('LogPlugin', () => {
expect(logPlugin.logs.length).toBe(1);
});
});
});
});

0 comments on commit a8a1068

Please sign in to comment.