Skip to content

Commit

Permalink
feat(): ensure cold start check is only done in start method; add tes…
Browse files Browse the repository at this point in the history
…ts; cleanup tests; add isColdStart to Metadata type; allow 0 and false to be retained in filterMetadata()
  • Loading branch information
mikaelvesavuori committed Nov 16, 2022
1 parent b6f69be commit df9c532
Show file tree
Hide file tree
Showing 7 changed files with 141 additions and 123 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.10",
"version": "2.1.11",
"author": "Mikael Vesavuori",
"license": "MIT",
"main": "./lib/index.js",
Expand Down
34 changes: 19 additions & 15 deletions src/entities/MikroLog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export class MikroLog {
private static correlationId: string;
private static debugSamplingLevel: number;
private static isDebugLogSampled: boolean;
private coldStart = true;
private static isColdStart = true;

private constructor() {
MikroLog.metadataConfig = {};
Expand All @@ -60,6 +60,8 @@ export class MikroLog {
* If the `start` method receives any input, that input will
* overwrite any existing metadata, event, and context.
*
* It will also, consequently, wipe the Lambda cold start state.
*
* If you want to "add" to these, you should instead call
* `enrich()` and pass in your additional data there.
*/
Expand All @@ -68,10 +70,23 @@ export class MikroLog {
MikroLog.metadataConfig = input?.metadataConfig || {};
MikroLog.event = input?.event || {};
MikroLog.context = input?.context || {};
MikroLog.context.isColdStart = MikroLog.getColdStart();
MikroLog.correlationId = input?.correlationId || '';
return MikroLog.instance;
}

/**
* @description Is this a Lambda cold start?
*/
private static getColdStart(): boolean {
if (MikroLog.isColdStart) {
MikroLog.isColdStart = false;
return true;
}

return false;
}

/**
* @description An emergency mechanism if you absolutely need to
* reset the instance to its empty default state.
Expand All @@ -89,18 +104,6 @@ export class MikroLog {
MikroLog.context = Object.assign(MikroLog.context, input.context || {});
}

/**
* @description Is this a Lambda cold start?
*/
public isColdStart(): boolean {
if (this.coldStart) {
this.coldStart = false;
return true;
}

return false;
}

/**
* @description Set correlation ID manually, for example for use in cross-boundary calls.
*
Expand Down Expand Up @@ -234,6 +237,7 @@ export class MikroLog {
*/
private getDynamicMetadata() {
const metadata = getMetadata(MikroLog.event, MikroLog.context);

return {
...metadata,
correlationId: MikroLog.correlationId || metadata.correlationId
Expand All @@ -248,7 +252,7 @@ export class MikroLog {

Object.entries(metadata).forEach((entry: any) => {
const [key, value] = entry;
if (value) filteredMetadata[key] = value;
if (value || value === false || value === 0) filteredMetadata[key] = value;
});

return filteredMetadata;
Expand Down Expand Up @@ -297,7 +301,7 @@ export class MikroLog {
error: log.level === 'ERROR',
level: log.level,
httpStatusCode: log.httpStatusCode,
isColdStart: this.isColdStart()
isColdStart: MikroLog.context.isColdStart
};

const filteredOutput = this.filterOutput(logOutput, redactedKeys, maskedValues);
Expand Down
4 changes: 4 additions & 0 deletions src/interfaces/Metadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,10 @@ export interface DynamicMetadataOutput {
* @description The AWS account ID that the system is running in.
*/
accountId: string;
/**
* @description Is this a Lambda cold start?
*/
isColdStart: boolean;
}

/**
Expand Down
20 changes: 20 additions & 0 deletions testdata/fullLog.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"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"
}
Loading

0 comments on commit df9c532

Please sign in to comment.