@@ -149,24 +149,13 @@ class Metrics implements MetricsInterface {
149
149
if ( this . isSingleMetric ) this . purgeStoredMetrics ( ) ;
150
150
}
151
151
152
- public clearDefaultDimensions ( ) : void {
153
- this . defaultDimensions = { } ;
154
- }
155
-
156
- public clearDimensions ( ) : void {
157
- this . dimensions = { } ;
158
- }
159
-
160
- public clearMetadata ( ) : void {
161
- this . metadata = { } ;
162
- }
163
-
164
- public clearMetrics ( ) : void {
165
- this . storedMetrics = { } ;
166
- }
167
-
168
152
/**
169
- * Throw an Error if the metrics buffer is empty.
153
+ * Create a singleMetric to capture cold start.
154
+ * If it's a cold start invocation, this feature will:
155
+ * * Create a separate EMF blob solely containing a metric named ColdStart
156
+ * * Add function_name and service dimensions
157
+ *
158
+ * This has the advantage of keeping cold start metric separate from your application metrics, where you might have unrelated dimensions.
170
159
*
171
160
* @example
172
161
*
@@ -177,13 +166,38 @@ class Metrics implements MetricsInterface {
177
166
* const metrics = new Metrics({namespace:"ServerlessAirline", service:"orders"});
178
167
*
179
168
* export const handler = async (event: any, context: Context) => {
180
- * metrics.raiseOnEmptyMetrics();
181
- * metrics.purgeStoredMetrics(); // will throw since no metrics added.
169
+ * metrics.captureColdStartMetric();
182
170
* }
183
171
* ```
184
172
*/
185
- public raiseOnEmptyMetrics ( ) : void {
186
- this . shouldRaiseOnEmptyMetrics = true ;
173
+ public captureColdStartMetric ( ) : void {
174
+ if ( ! this . isColdStart ) return ;
175
+ this . isColdStart = false ;
176
+ const singleMetric = this . singleMetric ( ) ;
177
+
178
+ if ( this . dimensions . service ) {
179
+ singleMetric . addDimension ( 'service' , this . dimensions . service ) ;
180
+ }
181
+ if ( this . functionName != null ) {
182
+ singleMetric . addDimension ( 'function_name' , this . functionName ) ;
183
+ }
184
+ singleMetric . addMetric ( 'ColdStart' , MetricUnits . Count , 1 ) ;
185
+ }
186
+
187
+ public clearDefaultDimensions ( ) : void {
188
+ this . defaultDimensions = { } ;
189
+ }
190
+
191
+ public clearDimensions ( ) : void {
192
+ this . dimensions = { } ;
193
+ }
194
+
195
+ public clearMetadata ( ) : void {
196
+ this . metadata = { } ;
197
+ }
198
+
199
+ public clearMetrics ( ) : void {
200
+ this . storedMetrics = { } ;
187
201
}
188
202
189
203
/**
@@ -228,7 +242,7 @@ class Metrics implements MetricsInterface {
228
242
if ( captureColdStartMetric ) this . captureColdStartMetric ( ) ;
229
243
try {
230
244
const result = originalMethod ?. apply ( this , [ event , context , callback ] ) ;
231
-
245
+
232
246
return result ;
233
247
} finally {
234
248
this . purgeStoredMetrics ( ) ;
@@ -259,6 +273,27 @@ class Metrics implements MetricsInterface {
259
273
this . storedMetrics = { } ;
260
274
}
261
275
276
+ /**
277
+ * Throw an Error if the metrics buffer is empty.
278
+ *
279
+ * @example
280
+ *
281
+ * ```typescript
282
+ * import { Metrics, MetricUnits } from '@aws-lambda-powertools/metrics';
283
+ * import { Context } from 'aws-lambda';
284
+ *
285
+ * const metrics = new Metrics({namespace:"ServerlessAirline", service:"orders"});
286
+ *
287
+ * export const handler = async (event: any, context: Context) => {
288
+ * metrics.raiseOnEmptyMetrics();
289
+ * metrics.purgeStoredMetrics(); // will throw since no metrics added.
290
+ * }
291
+ * ```
292
+ */
293
+ public raiseOnEmptyMetrics ( ) : void {
294
+ this . shouldRaiseOnEmptyMetrics = true ;
295
+ }
296
+
262
297
/**
263
298
* Function to create the right object compliant with Cloudwatch EMF (Event Metric Format).
264
299
* @see https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/CloudWatch_Embedded_Metric_Format_Specification.html for more details
@@ -315,6 +350,10 @@ class Metrics implements MetricsInterface {
315
350
this . defaultDimensions = targetDimensions ;
316
351
}
317
352
353
+ public setFunctionName ( value : string ) : void {
354
+ this . functionName = value ;
355
+ }
356
+
318
357
/**
319
358
* CloudWatch EMF uses the same dimensions across all your metrics. Use singleMetric if you have a metric that should have different dimensions.
320
359
*
@@ -339,45 +378,6 @@ class Metrics implements MetricsInterface {
339
378
} ) ;
340
379
}
341
380
342
- /**
343
- * Create a singleMetric to capture cold start.
344
- * If it's a cold start invocation, this feature will:
345
- * * Create a separate EMF blob solely containing a metric named ColdStart
346
- * * Add function_name and service dimensions
347
- *
348
- * This has the advantage of keeping cold start metric separate from your application metrics, where you might have unrelated dimensions.
349
- *
350
- * @example
351
- *
352
- * ```typescript
353
- * import { Metrics, MetricUnits } from '@aws-lambda-powertools/metrics';
354
- * import { Context } from 'aws-lambda';
355
- *
356
- * const metrics = new Metrics({namespace:"ServerlessAirline", service:"orders"});
357
- *
358
- * export const handler = async (event: any, context: Context) => {
359
- * metrics.captureColdStartMetric();
360
- * }
361
- * ```
362
- */
363
- public captureColdStartMetric ( ) : void {
364
- if ( ! this . isColdStart ) return ;
365
- this . isColdStart = false ;
366
- const singleMetric = this . singleMetric ( ) ;
367
-
368
- if ( this . dimensions . service ) {
369
- singleMetric . addDimension ( 'service' , this . dimensions . service ) ;
370
- }
371
- if ( this . functionName != null ) {
372
- singleMetric . addDimension ( 'function_name' , this . functionName ) ;
373
- }
374
- singleMetric . addMetric ( 'ColdStart' , MetricUnits . Count , 1 ) ;
375
- }
376
-
377
- public setFunctionName ( value : string ) : void {
378
- this . functionName = value ;
379
- }
380
-
381
381
private getCurrentDimensionsCount ( ) : number {
382
382
return Object . keys ( this . dimensions ) . length + Object . keys ( this . defaultDimensions ) . length ;
383
383
}
0 commit comments