Skip to content

Commit 471eaca

Browse files
authored
refactor(metrics): rename option property from raiseOnEmptyMetrics to throwOnEmptyMetrics (#416)
1 parent f0c3161 commit 471eaca

12 files changed

+35
-35
lines changed

Diff for: docs/core/metrics.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -274,12 +274,12 @@ You can flush metrics automatically using one of the following methods:
274274
* [Middy-compatible](https://github.com/middyjs/middy){target=_blank} middleware
275275
* class decorator
276276

277-
Using the Middy middleware or decorator will **automatically validate, serialize, and flush** all your metrics. During metrics validation, if no metrics are provided then a warning will be logged, but no exception will be raised.
277+
Using the Middy middleware or decorator will **automatically validate, serialize, and flush** all your metrics. During metrics validation, if no metrics are provided then a warning will be logged, but no exception will be thrown.
278278
If you do not use the middleware or decorator, you have to flush your metrics manually.
279279

280280

281281
!!! warning "Metric validation"
282-
If metrics are provided, and any of the following criteria are not met, a **`RangeError`** exception will be raised:
282+
If metrics are provided, and any of the following criteria are not met, a **`RangeError`** exception will be thrown:
283283

284284
* Maximum of 9 dimensions
285285
* Namespace is set only once (or none)
@@ -433,7 +433,7 @@ The `logMetrics` decorator of the metrics utility can be used when your Lambda h
433433

434434
#### Throwing a RangeError when no metrics are emitted
435435

436-
If you want to ensure that at least one metric is emitted before you flush them, you can use the `raiseOnEmptyMetrics` parameter and pass it to the middleware or decorator:
436+
If you want to ensure that at least one metric is emitted before you flush them, you can use the `throwOnEmptyMetrics` parameter and pass it to the middleware or decorator:
437437

438438
```typescript hl_lines="11"
439439
import { Metrics, MetricUnits, logMetrics } from '@aws-lambda-powertools/metrics';
@@ -446,7 +446,7 @@ If you want to ensure that at least one metric is emitted before you flush them,
446446
}
447447

448448
export const handler = middy(lambdaHandler)
449-
.use(logMetrics(metrics, { raiseOnEmptyMetrics: true }));
449+
.use(logMetrics(metrics, { throwOnEmptyMetrics: true }));
450450
```
451451

452452
### Capturing a cold start invocation as metric

Diff for: examples/cdk/lib/example-function.MyFunction.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ export const handler = async (_event: unknown, context: Context): Promise<void>
2929

3030
// ### Experiment metrics
3131
metrics.captureColdStartMetric();
32-
metrics.raiseOnEmptyMetrics();
32+
metrics.throwOnEmptyMetrics();
3333
metrics.setDefaultDimensions({ environment: 'example', type: 'standardFunction' });
3434
metrics.addMetric('test-metric', MetricUnits.Count, 10);
3535

Diff for: examples/cdk/lib/example-function.MyFunctionWithDecorator.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ export class MyFunctionWithDecorator {
1515
@logger.injectLambdaContext()
1616
@metrics.logMetrics({
1717
captureColdStartMetric: true,
18-
raiseOnEmptyMetrics: true,
18+
throwOnEmptyMetrics: true,
1919
defaultDimensions: { environment: 'example', type: 'withDecorator' },
2020
})
2121
public handler(_event: unknown, _context: Context, _callback: Callback<unknown>): void | Promise<unknown> {

Diff for: packages/metrics/examples/decorator/empty-metrics.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@ const metrics = new Metrics();
1616
class Lambda implements LambdaInterface {
1717

1818
// Be default, we will not throw any error if there is no metrics. Use this property to override and throw an exception
19-
@metrics.logMetrics({ raiseOnEmptyMetrics: true })
19+
@metrics.logMetrics({ throwOnEmptyMetrics: true })
2020
public handler<TEvent, TResult>(_event: TEvent, _context: Context, _callback: Callback<TResult>): void | Promise<TResult> {
2121
// Notice that no metrics are added
22-
// Since the raiseOnEmptyMetrics parameter is set to true, the Powertool throw an Error
22+
// Since the throwOnEmptyMetrics parameter is set to true, the Powertool throws an Error
2323
}
2424

2525
}

Diff for: packages/metrics/examples/empty-metrics.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@ const metrics = new Metrics();
1414

1515
const lambdaHandler = async (): Promise<void> => {
1616
// Notice that no metrics are added
17-
// Since the raiseOnEmptyMetrics parameter is set to true, the Powertool throw an Error
17+
// Since the throwOnEmptyMetrics parameter is set to true, the Powertool throws an Error
1818
};
1919

2020
const handlerWithMiddleware = middy(lambdaHandler)
21-
.use(logMetrics(metrics, { raiseOnEmptyMetrics: true }));
21+
.use(logMetrics(metrics, { throwOnEmptyMetrics: true }));
2222

2323
handlerWithMiddleware(dummyEvent, dummyContext, () => console.log('Lambda invoked!'));

Diff for: packages/metrics/src/Metrics.ts

+11-11
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ const DEFAULT_NAMESPACE = 'default_namespace';
3535
* If you are used to TypeScript Class usage to encapsulate your Lambda handler you can leverage the [@metrics.logMetrics()](./_aws_lambda_powertools_metrics.Metrics.html#logMetrics) decorator to automatically:
3636
* * create cold start metric
3737
* * flush buffered metrics
38-
* * raise on empty metrics
38+
* * throw on empty metrics
3939
*
4040
* @example
4141
*
@@ -49,7 +49,7 @@ const DEFAULT_NAMESPACE = 'default_namespace';
4949
*
5050
* // FYI: Decorator might not render properly in VSCode mouse over due to https://github.com/microsoft/TypeScript/issues/39371 and might show as *@metrics* instead of `@metrics.logMetrics`
5151
*
52-
* @metrics.logMetrics({captureColdStartMetric: true, raiseOnEmptyMetrics: true, })
52+
* @metrics.logMetrics({captureColdStartMetric: true, throwOnEmptyMetrics: true, })
5353
* public handler(_event: any, _context: Context, _callback: Callback<any>): void | Promise<any> {
5454
* // ...
5555
* metrics.addMetric('test-metric', MetricUnits.Count, 10);
@@ -89,7 +89,7 @@ class Metrics implements MetricsInterface {
8989
private isSingleMetric: boolean = false;
9090
private metadata: { [key: string]: string } = {};
9191
private namespace?: string;
92-
private shouldRaiseOnEmptyMetrics: boolean = false;
92+
private shouldThrowOnEmptyMetrics: boolean = false;
9393
private storedMetrics: StoredMetrics = {};
9494

9595
public constructor(options: MetricsOptions = {}) {
@@ -202,7 +202,7 @@ class Metrics implements MetricsInterface {
202202
}
203203

204204
/**
205-
* A decorator automating coldstart capture, raise on empty metrics and publishing metrics on handler exit.
205+
* A decorator automating coldstart capture, throw on empty metrics and publishing metrics on handler exit.
206206
*
207207
* @example
208208
*
@@ -227,9 +227,9 @@ class Metrics implements MetricsInterface {
227227
* @decorator Class
228228
*/
229229
public logMetrics(options: ExtraOptions = {}): HandlerMethodDecorator {
230-
const { raiseOnEmptyMetrics, defaultDimensions, captureColdStartMetric } = options;
231-
if (raiseOnEmptyMetrics) {
232-
this.raiseOnEmptyMetrics();
230+
const { throwOnEmptyMetrics, defaultDimensions, captureColdStartMetric } = options;
231+
if (throwOnEmptyMetrics) {
232+
this.throwOnEmptyMetrics();
233233
}
234234
if (defaultDimensions !== undefined) {
235235
this.setDefaultDimensions(defaultDimensions);
@@ -293,13 +293,13 @@ class Metrics implements MetricsInterface {
293293
* const metrics = new Metrics({namespace:"serverlessAirline", serviceName:"orders"});
294294
*
295295
* export const handler = async (event: any, context: Context) => {
296-
* metrics.raiseOnEmptyMetrics();
296+
* metrics.throwOnEmptyMetrics();
297297
* metrics.publishStoredMetrics(); // will throw since no metrics added.
298298
* }
299299
* ```
300300
*/
301-
public raiseOnEmptyMetrics(): void {
302-
this.shouldRaiseOnEmptyMetrics = true;
301+
public throwOnEmptyMetrics(): void {
302+
this.shouldThrowOnEmptyMetrics = true;
303303
}
304304

305305
/**
@@ -312,7 +312,7 @@ class Metrics implements MetricsInterface {
312312
Name: metricDefinition.name,
313313
Unit: metricDefinition.unit,
314314
}));
315-
if (metricDefinitions.length === 0 && this.shouldRaiseOnEmptyMetrics) {
315+
if (metricDefinitions.length === 0 && this.shouldThrowOnEmptyMetrics) {
316316
throw new RangeError('The number of metrics recorded must be higher than zero');
317317
}
318318

Diff for: packages/metrics/src/middleware/middy.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ const logMetrics = (target: Metrics | Metrics[], options: ExtraOptions = {}): mi
88
const logMetricsBefore = async (request: middy.Request): Promise<void> => {
99
metricsInstances.forEach((metrics: Metrics) => {
1010
metrics.setFunctionName(request.context.functionName);
11-
const { raiseOnEmptyMetrics, defaultDimensions, captureColdStartMetric } = options;
12-
if (raiseOnEmptyMetrics !== undefined) {
13-
metrics.raiseOnEmptyMetrics();
11+
const { throwOnEmptyMetrics, defaultDimensions, captureColdStartMetric } = options;
12+
if (throwOnEmptyMetrics !== undefined) {
13+
metrics.throwOnEmptyMetrics();
1414
}
1515
if (defaultDimensions !== undefined) {
1616
metrics.setDefaultDimensions(defaultDimensions);

Diff for: packages/metrics/src/types/Metrics.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ type HandlerMethodDecorator = (
3939
* ```typescript
4040
*
4141
* const metricsOptions: MetricsOptions = {
42-
* raiseOnEmptyMetrics: true,
42+
* throwOnEmptyMetrics: true,
4343
* defaultDimensions: {'environment': 'dev'},
4444
* captureColdStartMetric: true,
4545
* }
@@ -51,7 +51,7 @@ type HandlerMethodDecorator = (
5151
* ```
5252
*/
5353
type ExtraOptions = {
54-
raiseOnEmptyMetrics?: boolean
54+
throwOnEmptyMetrics?: boolean
5555
defaultDimensions?: Dimensions
5656
captureColdStartMetric?: boolean
5757
};

Diff for: packages/metrics/tests/e2e/decorator.test.MyFunction.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ const metrics = new Metrics({ namespace: namespace, service: serviceName });
1818

1919
class Lambda implements LambdaInterface {
2020

21-
@metrics.logMetrics({ captureColdStartMetric: true, defaultDimensions: JSON.parse(defaultDimensions), raiseOnEmptyMetrics: true })
21+
@metrics.logMetrics({ captureColdStartMetric: true, defaultDimensions: JSON.parse(defaultDimensions), throwOnEmptyMetrics: true })
2222
public async handler(_event: unknown, _context: Context): Promise<void> {
2323
metrics.addMetric(metricName, metricUnit, parseInt(metricValue));
2424
metrics.addDimension(

Diff for: packages/metrics/tests/e2e/standardFunctions.test.MyFunction.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ const metrics = new Metrics({ namespace: namespace, serviceName: serviceName });
1717

1818
export const handler = async (_event: unknown, _context: Context): Promise<void> => {
1919
metrics.captureColdStartMetric();
20-
metrics.raiseOnEmptyMetrics();
20+
metrics.throwOnEmptyMetrics();
2121
metrics.setDefaultDimensions(JSON.parse(defaultDimensions));
2222
metrics.addMetric(metricName, metricUnit, parseInt(metricValue));
2323
metrics.addDimension(

Diff for: packages/metrics/tests/unit/Metrics.test.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -346,13 +346,13 @@ describe('Class: Metrics', () => {
346346
});
347347
});
348348

349-
describe('Feature: raiseOnEmptyMetrics', () => {
350-
test('Error should be thrown on empty metrics when raiseOnEmptyMetrics is passed', async () => {
349+
describe('Feature: throwOnEmptyMetrics', () => {
350+
test('Error should be thrown on empty metrics when throwOnEmptyMetrics is passed', async () => {
351351
expect.assertions(1);
352352

353353
const metrics = new Metrics({ namespace: 'test' });
354354
class LambdaFunction implements LambdaInterface {
355-
@metrics.logMetrics({ raiseOnEmptyMetrics: true })
355+
@metrics.logMetrics({ throwOnEmptyMetrics: true })
356356
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
357357
// @ts-ignore
358358
public handler<TEvent, TResult>(
@@ -371,12 +371,12 @@ describe('Class: Metrics', () => {
371371
}
372372
});
373373

374-
test('Error should be thrown on empty metrics when raiseOnEmptyMetrics() is callse', async () => {
374+
test('Error should be thrown on empty metrics when throwOnEmptyMetrics() is callse', async () => {
375375
expect.assertions(1);
376376

377377
const metrics = new Metrics({ namespace: 'test' });
378378
const handler = async (_event: DummyEvent, _context: Context): Promise<void> => {
379-
metrics.raiseOnEmptyMetrics();
379+
metrics.throwOnEmptyMetrics();
380380
// Logic goes here
381381
metrics.publishStoredMetrics();
382382
};

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ describe('Middy middleware', () => {
8686
metrics.addMetric('successfulBooking', MetricUnits.Count, 1);
8787
};
8888
const metricsOptions: ExtraOptions = {
89-
raiseOnEmptyMetrics: true,
89+
throwOnEmptyMetrics: true,
9090
defaultDimensions: { environment : 'prod', aws_region: 'eu-central-1' },
9191
captureColdStartMetric: true
9292
};
@@ -173,7 +173,7 @@ describe('Middy middleware', () => {
173173
metrics.addMetric('successfulBooking', MetricUnits.Count, 1);
174174
};
175175
const metricsOptions: ExtraOptions = {
176-
raiseOnEmptyMetrics: true
176+
throwOnEmptyMetrics: true
177177
};
178178
const handler = middy(lambdaHandler).use(logMetrics([metrics], metricsOptions));
179179

0 commit comments

Comments
 (0)