Skip to content

Commit

Permalink
feat(): try using process env for cold start check; add tests; cleanu…
Browse files Browse the repository at this point in the history
…p tests
  • Loading branch information
mikaelvesavuori committed Nov 14, 2022
1 parent baaa7f2 commit e5f5953
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 109 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "mikrolog",
"description": "The JSON logger you always wanted for Lambda.",
"version": "2.1.7",
"version": "2.1.8",
"author": "Mikael Vesavuori",
"license": "MIT",
"main": "./lib/index.js",
Expand Down
12 changes: 6 additions & 6 deletions src/entities/MikroLog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ export class MikroLog {
private static correlationId: string;
private static debugSamplingLevel: number;
private static isDebugLogSampled: boolean;
private coldStart = true;

private constructor() {
MikroLog.metadataConfig = {};
Expand All @@ -50,6 +49,8 @@ export class MikroLog {
MikroLog.correlationId = '';
MikroLog.debugSamplingLevel = this.initDebugSampleLevel();
MikroLog.isDebugLogSampled = true;

process.env.IS_COLD_START = 'true';
}

/**
Expand Down Expand Up @@ -93,13 +94,12 @@ export class MikroLog {

/**
* @description Is this a Lambda cold start?
*
* Setting the value in the process environment makes it possible
* to persist the value to subsequent calls, also by other libraries.
*/
public isColdStart(): boolean {
if (this.coldStart) {
this.coldStart = false;
return true;
}

if (process.env.IS_COLD_START === 'true') return true;
return false;
}

Expand Down
164 changes: 64 additions & 100 deletions tests/MikroLog.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,27 @@ import { MikroLog } from '../src/entities/MikroLog';

import { metadataConfig } from '../testdata/config';

const fullLog = {
version: 1,
owner: 'MyCompany',
hostPlatform: 'aws',
domain: 'CustomerAcquisition',
system: 'ShowroomActivities',
service: 'UserSignUp',
team: 'MyDemoTeam',
tags: [''],
dataSensitivity: 'public',
message: 'Hello World',
error: false,
httpStatusCode: 200,
isColdStart: true,
level: 'INFO',
id: '1256767f-c875-4d82-813d-bc260bd0ba07',
timestamp: '2022-07-25T08:52:21.121Z',
timestampEpoch: '1656438566041',
jurisdiction: 'EU'
};

/**
* POSITIVE TESTS
*/
Expand Down Expand Up @@ -64,26 +85,7 @@ test.serial('It should return (print out) a structured log when given a string m
const logger = MikroLog.start({ metadataConfig });
const response: any = logger.log(message);

const expected: any = {
version: 1,
owner: 'MyCompany',
hostPlatform: 'aws',
domain: 'CustomerAcquisition',
system: 'ShowroomActivities',
service: 'UserSignUp',
team: 'MyDemoTeam',
tags: [''],
dataSensitivity: 'public',
message: 'Hello World',
error: false,
httpStatusCode: 200,
isColdStart: true,
level: 'INFO',
id: '1256767f-c875-4d82-813d-bc260bd0ba07',
timestamp: '2022-07-25T08:52:21.121Z',
timestampEpoch: '1656438566041',
jurisdiction: 'EU'
};
const expected: any = JSON.parse(JSON.stringify(fullLog));

// Ensure exactness of message field
t.is(response['message'], message);
Expand Down Expand Up @@ -114,26 +116,7 @@ test.serial(
const logger = MikroLog.start({ metadataConfig });
const response: any = logger.info(message);

const expected: any = {
version: 1,
owner: 'MyCompany',
hostPlatform: 'aws',
domain: 'CustomerAcquisition',
system: 'ShowroomActivities',
service: 'UserSignUp',
team: 'MyDemoTeam',
tags: [''],
dataSensitivity: 'public',
message: 'Hello World',
error: false,
httpStatusCode: 200,
isColdStart: true,
level: 'INFO',
id: '1256767f-c875-4d82-813d-bc260bd0ba07',
timestamp: '2022-07-25T08:52:21.121Z',
timestampEpoch: '1656438566041',
jurisdiction: 'EU'
};
const expected: any = JSON.parse(JSON.stringify(fullLog));

// Ensure exactness of message field
t.is(response['message'], message);
Expand Down Expand Up @@ -165,26 +148,8 @@ test.serial(
const logger = MikroLog.start({ metadataConfig });
const response: any = logger.debug(message);

const expected: any = {
version: 1,
owner: 'MyCompany',
hostPlatform: 'aws',
domain: 'CustomerAcquisition',
system: 'ShowroomActivities',
service: 'UserSignUp',
team: 'MyDemoTeam',
tags: [''],
dataSensitivity: 'public',
message: 'Hello World',
error: false,
httpStatusCode: 200,
isColdStart: true,
level: 'DEBUG',
id: '1256767f-c875-4d82-813d-bc260bd0ba07',
timestamp: '2022-07-25T08:52:21.121Z',
timestampEpoch: '1656438566041',
jurisdiction: 'EU'
};
const expected: any = JSON.parse(JSON.stringify(fullLog));
expected['level'] = 'DEBUG';

// Ensure exactness of message field
t.is(response['message'], message);
Expand Down Expand Up @@ -216,26 +181,8 @@ test.serial(
const logger = MikroLog.start({ metadataConfig });
const response: any = logger.warn(message);

const expected: any = {
version: 1,
owner: 'MyCompany',
hostPlatform: 'aws',
domain: 'CustomerAcquisition',
system: 'ShowroomActivities',
service: 'UserSignUp',
team: 'MyDemoTeam',
tags: [''],
dataSensitivity: 'public',
message: 'Hello World',
error: false,
httpStatusCode: 200,
isColdStart: true,
level: 'WARN',
id: '1256767f-c875-4d82-813d-bc260bd0ba07',
timestamp: '2022-07-25T08:52:21.121Z',
timestampEpoch: '1656438566041',
jurisdiction: 'EU'
};
const expected: any = JSON.parse(JSON.stringify(fullLog));
expected['level'] = 'WARN';

// Ensure exactness of message field
t.is(response['message'], message);
Expand Down Expand Up @@ -267,26 +214,10 @@ test.serial(
const logger = MikroLog.start({ metadataConfig });
const response: any = logger.error(message);

const expected: any = {
version: 1,
owner: 'MyCompany',
hostPlatform: 'aws',
domain: 'CustomerAcquisition',
system: 'ShowroomActivities',
service: 'UserSignUp',
team: 'MyDemoTeam',
tags: [''],
dataSensitivity: 'public',
message: 'Hello World',
error: true,
httpStatusCode: 400,
isColdStart: true,
level: 'ERROR',
id: '1256767f-c875-4d82-813d-bc260bd0ba07',
timestamp: '2022-07-25T08:52:21.121Z',
timestampEpoch: '1656438566041',
jurisdiction: 'EU'
};
const expected: any = JSON.parse(JSON.stringify(fullLog));
expected['level'] = 'ERROR';
expected['error'] = true;
expected['httpStatusCode'] = 400;

// Ensure exactness of message field
t.is(response['message'], message);
Expand Down Expand Up @@ -658,3 +589,36 @@ test.serial('It should be able to merge enrichment even if input is essentially
// @ts-ignore
t.deepEqual(response, expected);
});

test.serial(
'It should retain the cold start status between multiple calls in the same process',
(t) => {
MikroLog.reset();

const logger = MikroLog.start({ metadataConfig });
const response1: any = logger.log('');
const response2: any = logger.log('');

const expected = response1.isColdStart === true && response2.isColdStart === true;

// @ts-ignore
t.is(expected, true);
}
);

test.serial(
'It should not reuse the cold start status between calls when "IS_COLD_START" is not set',
(t) => {
MikroLog.reset();

const logger = MikroLog.start({ metadataConfig });
const response1: any = logger.log('');
process.env.IS_COLD_START = 'false';
const response2: any = logger.log('');

const expected = response1.isColdStart === true && response2.isColdStart === false;

// @ts-ignore
t.is(expected, true);
}
);

0 comments on commit e5f5953

Please sign in to comment.