@@ -106,9 +106,7 @@ import type {
106
106
*/
107
107
class Logger implements ClassThatLogs {
108
108
109
- private static coldStart ?: boolean = undefined ;
110
-
111
- private static coldStartEvaluated : boolean = false ;
109
+ private static coldStartRequestId ?: string ;
112
110
113
111
private customConfigService ?: ConfigServiceInterface ;
114
112
@@ -152,10 +150,9 @@ class Logger implements ClassThatLogs {
152
150
* @returns {void }
153
151
*/
154
152
public addContext ( context : Context ) : void {
155
- Logger . evaluateColdStartOnce ( ) ;
156
153
const lambdaContext : Partial < LambdaFunctionContext > = {
157
154
invokedFunctionArn : context . invokedFunctionArn ,
158
- coldStart : Logger . getColdStartValue ( ) ,
155
+ coldStart : Logger . getColdStartValue ( context . awsRequestId ) ,
159
156
awsRequestId : context . awsRequestId ,
160
157
memoryLimitInMB : Number ( context . memoryLimitInMB ) ,
161
158
functionName : context . functionName ,
@@ -187,6 +184,15 @@ class Logger implements ClassThatLogs {
187
184
this . addPersistentLogAttributes ( attributes ) ;
188
185
}
189
186
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
+
190
196
/**
191
197
* It creates a separate Logger instance, identical to the current one
192
198
* It's possible to overwrite the new instance options by passing them.
@@ -224,32 +230,13 @@ class Logger implements ClassThatLogs {
224
230
* It evaluates whether the current Lambda function invocation has a cold start or not.
225
231
*
226
232
* @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
239
233
* @returns {boolean }
240
234
*/
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 ;
244
238
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 ;
253
240
}
254
241
255
242
/**
@@ -305,30 +292,6 @@ class Logger implements ClassThatLogs {
305
292
this . setLogsSampled ( ) ;
306
293
}
307
294
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
-
332
295
/**
333
296
* It sets the user-provided sample rate value.
334
297
*
@@ -402,27 +365,6 @@ class Logger implements ClassThatLogs {
402
365
return logItem ;
403
366
}
404
367
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
-
426
368
/**
427
369
* It returns the custom config service, an abstraction used to fetch environment variables.
428
370
*
0 commit comments