Skip to content

Commit 7e90e2c

Browse files
committed
Base cold start heuristic on request id
1 parent c803194 commit 7e90e2c

File tree

3 files changed

+17
-147
lines changed

3 files changed

+17
-147
lines changed

Diff for: packages/logger/src/Logger.ts

+15-73
Original file line numberDiff line numberDiff line change
@@ -106,9 +106,7 @@ import type {
106106
*/
107107
class Logger implements ClassThatLogs {
108108

109-
private static coldStart?: boolean = undefined;
110-
111-
private static coldStartEvaluated: boolean = false;
109+
private static coldStartRequestId?: string;
112110

113111
private customConfigService?: ConfigServiceInterface;
114112

@@ -152,10 +150,9 @@ class Logger implements ClassThatLogs {
152150
* @returns {void}
153151
*/
154152
public addContext(context: Context): void {
155-
Logger.evaluateColdStartOnce();
156153
const lambdaContext: Partial<LambdaFunctionContext> = {
157154
invokedFunctionArn: context.invokedFunctionArn,
158-
coldStart: Logger.getColdStartValue(),
155+
coldStart: Logger.getColdStartValue(context.awsRequestId),
159156
awsRequestId: context.awsRequestId,
160157
memoryLimitInMB: Number(context.memoryLimitInMB),
161158
functionName: context.functionName,
@@ -187,6 +184,15 @@ class Logger implements ClassThatLogs {
187184
this.addPersistentLogAttributes(attributes);
188185
}
189186

187+
/**
188+
* Clears the static field that's used for checking if the request involved a cold start.
189+
* @static
190+
* @returns {void}
191+
*/
192+
public static clearColdStartRequestId(): void {
193+
Logger.coldStartRequestId = undefined;
194+
}
195+
190196
/**
191197
* It creates a separate Logger instance, identical to the current one
192198
* It's possible to overwrite the new instance options by passing them.
@@ -224,32 +230,13 @@ class Logger implements ClassThatLogs {
224230
* It evaluates whether the current Lambda function invocation has a cold start or not.
225231
*
226232
* @static
227-
* @returns {void}
228-
*/
229-
public static evaluateColdStartOnce(): void {
230-
if (!Logger.getColdStartEvaluatedValue()) {
231-
Logger.evaluateColdStart();
232-
}
233-
}
234-
235-
/**
236-
* It returns a boolean value which is true if the current Lambda function cold start has been already evaluated, false otherwise.
237-
*
238-
* @static
239233
* @returns {boolean}
240234
*/
241-
public static getColdStartEvaluatedValue(): boolean {
242-
return Logger.coldStartEvaluated;
243-
}
235+
public static getColdStartValue(currentRequestId: string): boolean {
236+
if (!Logger.coldStartRequestId)
237+
Logger.coldStartRequestId = currentRequestId;
244238

245-
/**
246-
* It returns an optional boolean value, true if the current Lambda function invocation has a cold start, false otherwise.
247-
*
248-
* @static
249-
* @returns {boolean | undefined}
250-
*/
251-
public static getColdStartValue(): boolean | undefined {
252-
return Logger.coldStart;
239+
return currentRequestId === Logger.coldStartRequestId;
253240
}
254241

255242
/**
@@ -305,30 +292,6 @@ class Logger implements ClassThatLogs {
305292
this.setLogsSampled();
306293
}
307294

308-
/**
309-
* It sets the value of a flag static propriety that tracks whether
310-
* the cold start evaluation already took place.
311-
*
312-
* @param {boolean} value
313-
* @static
314-
* @returns {void}
315-
*/
316-
public static setColdStartEvaluatedValue(value: boolean): void {
317-
Logger.coldStartEvaluated = value;
318-
}
319-
320-
/**
321-
* It sets the value of a flag static propriety that tracks whether
322-
* the current Lambda invocation experienced a cold start.
323-
*
324-
* @static
325-
* @param {boolean | undefined} value
326-
* @returns {void}
327-
*/
328-
public static setColdStartValue(value: boolean | undefined): void {
329-
Logger.coldStart = value;
330-
}
331-
332295
/**
333296
* It sets the user-provided sample rate value.
334297
*
@@ -402,27 +365,6 @@ class Logger implements ClassThatLogs {
402365
return logItem;
403366
}
404367

405-
/**
406-
* It evaluates whether the current Lambda invocation experienced a
407-
* cold start.
408-
*
409-
* @private
410-
* @static
411-
* @returns {void}
412-
*/
413-
private static evaluateColdStart(): void {
414-
const coldStartValue = Logger.getColdStartValue();
415-
if (typeof coldStartValue === 'undefined') {
416-
Logger.setColdStartValue(true);
417-
} else if (coldStartValue) {
418-
Logger.setColdStartValue(false);
419-
} else {
420-
Logger.setColdStartValue(false);
421-
}
422-
423-
Logger.setColdStartEvaluatedValue(true);
424-
}
425-
426368
/**
427369
* It returns the custom config service, an abstraction used to fetch environment variables.
428370
*

Diff for: packages/logger/tests/unit/Logger.test.ts

+1-74
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,7 @@ const consoleSpy = {
2828
describe('Class: Logger', () => {
2929

3030
beforeEach(() => {
31-
Logger.setColdStartValue(undefined);
32-
Logger.setColdStartEvaluatedValue(false);
31+
Logger.clearColdStartRequestId();
3332
consoleSpy['debug'].mockClear();
3433
consoleSpy['info'].mockClear();
3534
consoleSpy['warn'].mockClear();
@@ -647,55 +646,6 @@ describe('Class: Logger', () => {
647646

648647
});
649648

650-
describe('Method: evaluateColdStartOnce', () => {
651-
652-
test('when called during the first invocation (cold start), it populates the logger\'s PowertoolLogData object with coldstart set to true', () => {
653-
654-
// Prepare
655-
// This value is undefined at the beginning of the first invocation
656-
Logger.setColdStartValue(undefined);
657-
658-
// Act
659-
Logger.evaluateColdStartOnce();
660-
Logger.evaluateColdStartOnce();
661-
Logger.evaluateColdStartOnce();
662-
663-
// Assess
664-
expect(Logger.getColdStartValue()).toEqual(true);
665-
});
666-
667-
test('when called during the SECOND invocation (warm start), it populates the logger\'s PowertoolLogData object with coldstart set to false', () => {
668-
669-
// Prepare
670-
// This value is set to true at the beginning of the second invocation
671-
Logger.setColdStartValue(true);
672-
673-
// Act
674-
Logger.evaluateColdStartOnce();
675-
Logger.evaluateColdStartOnce();
676-
Logger.evaluateColdStartOnce();
677-
678-
// Assess
679-
expect(Logger.getColdStartValue()).toEqual(false);
680-
});
681-
682-
test('when called during the THIRD invocation (warm start), it populates the logger\'s PowertoolLogData object with coldstart set to false', () => {
683-
684-
// Prepare
685-
// This value is set to false at the beginning of the third invocation
686-
Logger.setColdStartValue(false);
687-
688-
// Act
689-
Logger.evaluateColdStartOnce();
690-
Logger.evaluateColdStartOnce();
691-
Logger.evaluateColdStartOnce();
692-
693-
// Assess
694-
expect(Logger.getColdStartValue()).toEqual(false);
695-
});
696-
697-
});
698-
699649
describe('Method: injectLambdaContext', () => {
700650

701651
beforeEach(() => {
@@ -834,29 +784,6 @@ describe('Class: Logger', () => {
834784

835785
});
836786

837-
describe('Method: setColdStartValue', () => {
838-
839-
test('when called, it sets the value of the static variable coldStart in the same file', async () => {
840-
841-
// Act
842-
Logger.setColdStartValue(undefined);
843-
const undefinedValue = Logger.getColdStartValue();
844-
845-
Logger.setColdStartValue(true);
846-
const trueValue = Logger.getColdStartValue();
847-
848-
Logger.setColdStartValue(false);
849-
const falseValue = Logger.getColdStartValue();
850-
851-
// Assess
852-
expect(undefinedValue).toBe(undefined);
853-
expect(trueValue).toBe(true);
854-
expect(falseValue).toBe(false);
855-
856-
});
857-
858-
});
859-
860787
describe('Method: refreshSampleRateCalculation', () => {
861788

862789
test('when called, it recalculates whether the current Lambda invocation\'s logs will be printed or not', () => {

Diff for: packages/logger/tests/unit/middleware/middy.test.ts

+1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ describe('Middy middleware', () => {
1919
// eslint-disable-next-line @typescript-eslint/no-empty-function
2020
jest.spyOn(console, 'log').mockImplementation(() => {});
2121
process.env = { ...ENVIRONMENT_VARIABLES };
22+
Logger.clearColdStartRequestId();
2223
});
2324

2425
afterAll(() => {

0 commit comments

Comments
 (0)