Skip to content

Commit

Permalink
feat(cold-start): add check for cold start check
Browse files Browse the repository at this point in the history
  • Loading branch information
mikaelvesavuori committed Nov 10, 2022
1 parent 8758113 commit f09ef8d
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 20 deletions.
35 changes: 18 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -266,23 +266,24 @@ The dynamic metadata fields are picked up automatically if you pass them in duri

If these values are not available, they will be dropped at the time of log output. In effect, this means you won't have to deal with them (being empty or otherwise) if you use MikroLog in another type of context.

| Field | Type | Description |
| -------------------- | ------ | --------------------------------------------------------- |
| `accountId` | string | The AWS account ID that the system is running in. |
| `correlationId` | string | Correlation ID for this function call. |
| `functionMemorySize` | string | Memory size of the current function. |
| `functionName` | string | The name of the function. |
| `functionVersion` | string | The version of the function. |
| `id` | string | ID of the log. |
| `region` | string | The region of the responding function/system. |
| `resource` | string | The resource (channel, URL path...) that is responding. |
| `runtime` | string | What runtime is used? |
| `stage` | string | What AWS stage are we in? |
| `timestamp` | string | Timestamp of this message in ISO 8601 (RFC 3339) format. |
| `timestampEpoch` | string | Timestamp of this message in Unix epoch. |
| `timestampRequest` | string | Request time in Unix epoch of the incoming request. |
| `user` | string | The user in this log context. |
| `viewerCountry` | string | Which country did AWS CloudFront infer the user to be in? |
| Field | Type | Description |
| -------------------- | ------- | --------------------------------------------------------- |
| `accountId` | string | The AWS account ID that the system is running in. |
| `correlationId` | string | Correlation ID for this function call. |
| `functionMemorySize` | string | Memory size of the current function. |
| `functionName` | string | The name of the function. |
| `functionVersion` | string | The version of the function. |
| `id` | string | ID of the log. |
| `isColdStart` | boolean | Is this a Lambda cold start? |
| `region` | string | The region of the responding function/system. |
| `resource` | string | The resource (channel, URL path...) that is responding. |
| `runtime` | string | What runtime is used? |
| `stage` | string | What AWS stage are we in? |
| `timestamp` | string | Timestamp of this message in ISO 8601 (RFC 3339) format. |
| `timestampEpoch` | string | Timestamp of this message in Unix epoch. |
| `timestampRequest` | string | Request time in Unix epoch of the incoming request. |
| `user` | string | The user in this log context. |
| `viewerCountry` | string | Which country did AWS CloudFront infer the user to be in? |

## Redacting keys or masking values

Expand Down
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.4",
"version": "2.1.5",
"author": "Mikael Vesavuori",
"license": "MIT",
"main": "./lib/index.js",
Expand Down
14 changes: 14 additions & 0 deletions src/entities/MikroLog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ export class MikroLog {
private static correlationId: string;
private static debugSamplingLevel: number;
private static isDebugLogSampled: boolean;
private coldStart = true;

private constructor() {
MikroLog.metadataConfig = {};
Expand Down Expand Up @@ -103,6 +104,18 @@ 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 @@ -217,6 +230,7 @@ export class MikroLog {
*/
private loadEnrichedEnvironment() {
return {
isColdStart: this.isColdStart(),
timestampRequest: produceTimestampRequest(MikroLog.event),
accountId: produceAccountId(MikroLog.event),
region: produceRegion(MikroLog.context),
Expand Down
11 changes: 11 additions & 0 deletions tests/MikroLog.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ test.serial(
message: 'Hello World',
error: false,
httpStatusCode: 200,
isColdStart: true,
level: 'INFO',
id: '1256767f-c875-4d82-813d-bc260bd0ba07',
timestamp: '2022-07-25T08:52:21.121Z',
Expand Down Expand Up @@ -76,6 +77,7 @@ test.serial('It should return (print out) a structured log when given a string m
message: 'Hello World',
error: false,
httpStatusCode: 200,
isColdStart: true,
level: 'INFO',
id: '1256767f-c875-4d82-813d-bc260bd0ba07',
timestamp: '2022-07-25T08:52:21.121Z',
Expand Down Expand Up @@ -125,6 +127,7 @@ test.serial(
message: 'Hello World',
error: false,
httpStatusCode: 200,
isColdStart: true,
level: 'INFO',
id: '1256767f-c875-4d82-813d-bc260bd0ba07',
timestamp: '2022-07-25T08:52:21.121Z',
Expand Down Expand Up @@ -175,6 +178,7 @@ test.serial(
message: 'Hello World',
error: false,
httpStatusCode: 200,
isColdStart: true,
level: 'DEBUG',
id: '1256767f-c875-4d82-813d-bc260bd0ba07',
timestamp: '2022-07-25T08:52:21.121Z',
Expand Down Expand Up @@ -225,6 +229,7 @@ test.serial(
message: 'Hello World',
error: false,
httpStatusCode: 200,
isColdStart: true,
level: 'WARN',
id: '1256767f-c875-4d82-813d-bc260bd0ba07',
timestamp: '2022-07-25T08:52:21.121Z',
Expand Down Expand Up @@ -275,6 +280,7 @@ test.serial(
message: 'Hello World',
error: true,
httpStatusCode: 400,
isColdStart: true,
level: 'ERROR',
id: '1256767f-c875-4d82-813d-bc260bd0ba07',
timestamp: '2022-07-25T08:52:21.121Z',
Expand Down Expand Up @@ -456,6 +462,7 @@ test.serial('It should redact keys when given a "redactedKeys" list', (t) => {
message: 'Hello World',
error: true,
httpStatusCode: 400,
isColdStart: true,
level: 'ERROR',
id: '1256767f-c875-4d82-813d-bc260bd0ba07',
timestamp: '2022-07-25T08:52:21.121Z',
Expand Down Expand Up @@ -499,6 +506,7 @@ test.serial('It should mask values when given a "maskedValues" list', (t) => {
error: true,
hostPlatform: 'aws',
httpStatusCode: 400,
isColdStart: true,
level: 'ERROR',
message: 'Hello World',
owner: 'MyCompany',
Expand Down Expand Up @@ -551,6 +559,7 @@ test.serial('It should accept a custom metadata configuration', (t) => {
},
error: false,
httpStatusCode: 200,
isColdStart: true,
level: 'INFO',
message: 'Hello World'
};
Expand Down Expand Up @@ -590,6 +599,7 @@ test.serial('It should retain falsy but defined values in logs', (t) => {
const expected: any = {
error: false,
httpStatusCode: 200,
isColdStart: true,
falsyTest1: false,
falsyTest2: 0,
level: 'INFO',
Expand Down Expand Up @@ -627,6 +637,7 @@ test.serial('It should be able to merge enrichment even if input is essentially
const expected: any = {
error: false,
httpStatusCode: 200,
isColdStart: true,
level: 'INFO',
message: 'Hello World'
};
Expand Down
1 change: 1 addition & 0 deletions tests/metadataUtils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,7 @@ test.serial('It should emit a full log and filter out any empty fields', (t) =>
functionName: 'somestack-FunctionName',
functionVersion: '$LATEST',
httpStatusCode: 200,
isColdStart: true,
level: 'INFO',
message: 'something',
region: 'eu-north-1',
Expand Down

0 comments on commit f09ef8d

Please sign in to comment.