@@ -88,7 +88,7 @@ class Metrics implements MetricsInterface {
88
88
private isSingleMetric : boolean = false ;
89
89
private metadata : { [ key : string ] : string } = { } ;
90
90
private namespace ?: string ;
91
- private raiseOnEmptyMetrics : boolean = false ;
91
+ private shouldRaiseOnEmptyMetrics : boolean = false ;
92
92
private storedMetrics : StoredMetrics = { } ;
93
93
94
94
public constructor ( options : MetricsOptions = { } ) {
@@ -165,6 +165,28 @@ class Metrics implements MetricsInterface {
165
165
this . storedMetrics = { } ;
166
166
}
167
167
168
+
169
+ /**
170
+ * Throw an Error if the metrics buffer is empty.
171
+ *
172
+ * @example
173
+ *
174
+ * ```typescript
175
+ * import { Metrics, MetricUnits } from '@aws-lambda-powertools/metrics';
176
+ * import { Context } from 'aws-lambda';
177
+ *
178
+ * const metrics = new Metrics({namespace:"ServerlessAirline", service:"orders"});
179
+ *
180
+ * export const handler = async (event: any, context: Context) => {
181
+ * metrics.raiseOnEmptyMetrics();
182
+ * metrics.purgeStoredMetrics(); // will throw since no metrics added.
183
+ * }
184
+ * ```
185
+ */
186
+ public raiseOnEmptyMetrics ( ) : void {
187
+ this . shouldRaiseOnEmptyMetrics = true ;
188
+ }
189
+
168
190
/**
169
191
* A decorator automating coldstart capture, raise on empty metrics and publishing metrics on handler exit.
170
192
*
@@ -192,7 +214,9 @@ class Metrics implements MetricsInterface {
192
214
*/
193
215
public logMetrics ( options : DecoratorOptions = { } ) : HandlerMethodDecorator {
194
216
const { raiseOnEmptyMetrics, defaultDimensions, captureColdStartMetric } = options ;
195
- this . raiseOnEmptyMetrics = raiseOnEmptyMetrics || false ;
217
+ if ( raiseOnEmptyMetrics ) {
218
+ this . raiseOnEmptyMetrics ( ) ;
219
+ }
196
220
if ( defaultDimensions !== undefined ) {
197
221
this . setDefaultDimensions ( defaultDimensions ) ;
198
222
}
@@ -202,7 +226,7 @@ class Metrics implements MetricsInterface {
202
226
descriptor . value = ( event , context , callback ) => {
203
227
this . functionName = context . functionName ;
204
228
205
- if ( captureColdStartMetric ) this . captureColdStart ( ) ;
229
+ if ( captureColdStartMetric ) this . captureColdStartMetric ( ) ;
206
230
try {
207
231
const result = originalMethod ?. apply ( this , [ event , context , callback ] ) ;
208
232
return result ;
@@ -245,7 +269,7 @@ class Metrics implements MetricsInterface {
245
269
Name : metricDefinition . name ,
246
270
Unit : metricDefinition . unit ,
247
271
} ) ) ;
248
- if ( metricDefinitions . length === 0 && this . raiseOnEmptyMetrics ) {
272
+ if ( metricDefinitions . length === 0 && this . shouldRaiseOnEmptyMetrics ) {
249
273
throw new RangeError ( 'The number of metrics recorded must be higher than zero' ) ;
250
274
}
251
275
@@ -321,8 +345,21 @@ class Metrics implements MetricsInterface {
321
345
* * Add function_name and service dimensions
322
346
*
323
347
* This has the advantage of keeping cold start metric separate from your application metrics, where you might have unrelated dimensions.
348
+ *
349
+ * @example
350
+ *
351
+ * ```typescript
352
+ * import { Metrics, MetricUnits } from '@aws-lambda-powertools/metrics';
353
+ * import { Context } from 'aws-lambda';
354
+ *
355
+ * const metrics = new Metrics({namespace:"ServerlessAirline", service:"orders"});
356
+ *
357
+ * export const handler = async (event: any, context: Context) => {
358
+ * metrics.captureColdStartMetric();
359
+ * }
360
+ * ```
324
361
*/
325
- public captureColdStart ( ) : void {
362
+ public captureColdStartMetric ( ) : void {
326
363
if ( ! this . isColdStart ) return ;
327
364
this . isColdStart = false ;
328
365
const singleMetric = this . singleMetric ( ) ;
0 commit comments