@@ -88,7 +88,7 @@ class Metrics implements MetricsInterface {
8888 private isSingleMetric : boolean = false ;
8989 private metadata : { [ key : string ] : string } = { } ;
9090 private namespace ?: string ;
91- private raiseOnEmptyMetrics : boolean = false ;
91+ private shouldRaiseOnEmptyMetrics : boolean = false ;
9292 private storedMetrics : StoredMetrics = { } ;
9393
9494 public constructor ( options : MetricsOptions = { } ) {
@@ -165,6 +165,28 @@ class Metrics implements MetricsInterface {
165165 this . storedMetrics = { } ;
166166 }
167167
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+
168190 /**
169191 * A decorator automating coldstart capture, raise on empty metrics and publishing metrics on handler exit.
170192 *
@@ -192,7 +214,9 @@ class Metrics implements MetricsInterface {
192214 */
193215 public logMetrics ( options : DecoratorOptions = { } ) : HandlerMethodDecorator {
194216 const { raiseOnEmptyMetrics, defaultDimensions, captureColdStartMetric } = options ;
195- this . raiseOnEmptyMetrics = raiseOnEmptyMetrics || false ;
217+ if ( raiseOnEmptyMetrics ) {
218+ this . raiseOnEmptyMetrics ( ) ;
219+ }
196220 if ( defaultDimensions !== undefined ) {
197221 this . setDefaultDimensions ( defaultDimensions ) ;
198222 }
@@ -202,7 +226,7 @@ class Metrics implements MetricsInterface {
202226 descriptor . value = ( event , context , callback ) => {
203227 this . functionName = context . functionName ;
204228
205- if ( captureColdStartMetric ) this . captureColdStart ( ) ;
229+ if ( captureColdStartMetric ) this . captureColdStartMetric ( ) ;
206230 try {
207231 const result = originalMethod ?. apply ( this , [ event , context , callback ] ) ;
208232 return result ;
@@ -245,7 +269,7 @@ class Metrics implements MetricsInterface {
245269 Name : metricDefinition . name ,
246270 Unit : metricDefinition . unit ,
247271 } ) ) ;
248- if ( metricDefinitions . length === 0 && this . raiseOnEmptyMetrics ) {
272+ if ( metricDefinitions . length === 0 && this . shouldRaiseOnEmptyMetrics ) {
249273 throw new RangeError ( 'The number of metrics recorded must be higher than zero' ) ;
250274 }
251275
@@ -321,8 +345,21 @@ class Metrics implements MetricsInterface {
321345 * * Add function_name and service dimensions
322346 *
323347 * 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+ * ```
324361 */
325- public captureColdStart ( ) : void {
362+ public captureColdStartMetric ( ) : void {
326363 if ( ! this . isColdStart ) return ;
327364 this . isColdStart = false ;
328365 const singleMetric = this . singleMetric ( ) ;
0 commit comments