Skip to content

Commit 4d3f0cd

Browse files
author
Alan Churley
committed
Fixing readme and moving to RangeError for certain conditions
1 parent 984da53 commit 4d3f0cd

File tree

2 files changed

+69
-10
lines changed

2 files changed

+69
-10
lines changed

packages/metrics/README.md

Lines changed: 65 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,7 @@ class Lambda implements LambdaInterface {
258258
> CloudWatch EMF supports a max of 100 metrics per batch. Metrics utility will flush all metrics when adding the 100th metric. Subsequent metrics, e.g. 101th, will be aggregated into a new EMF object, for your convenience.
259259
260260
>### ! Do not create metrics or dimensions outside the handler
261-
> Metrics or dimensions added in the global scope will only be added during cold start. Disregard if you that's the intended behaviour.
261+
> Metrics or dimensions added in the global scope will only be added during cold start. Disregard if that's the intended behaviour.
262262
263263
### Adding default dimensions
264264
You can use either setDefaultDimensions method or by passing a defaultDimensions object to either the decorator or to the constructor
@@ -346,6 +346,8 @@ If you need to log the metrics from within your code or if you do not wish to us
346346

347347
This will serialize, and flush all your metrics.
348348
```typescript
349+
process.env.POWERTOOLS_METRICS_NAMESPACE = 'hello-world';
350+
process.env.POWERTOOLS_SERVICE_NAME = 'hello-world-service';
349351
const metrics = new Metrics();
350352

351353
const lambdaHandler: Handler = async () => {
@@ -361,7 +363,35 @@ const lambdaHandler: Handler = async () => {
361363

362364
};
363365
```
366+
<details>
367+
<summary>Click to expand and see the logs outputs</summary>
368+
369+
```bash
370+
371+
{
372+
"_aws":{
373+
"Timestamp":1625587915573,
374+
"CloudWatchMetrics":
375+
[
376+
{
377+
"Namespace":"hello-world",
378+
"Dimensions":[["service"]],
379+
"Metrics":
380+
[
381+
{
382+
"Name":"test-metric",
383+
"Unit":"Count"
384+
}
385+
]
386+
}
387+
]
388+
},
389+
"service":"hello-world-service",
390+
"test-metric":10
391+
}
364392

393+
```
394+
</details>
365395

366396
If you just need to clear the metrics manually you can do this by calling the clearMetrics method
367397
```typescript
@@ -382,17 +412,48 @@ If you need to get programmatic access to the current stored metrics you can do
382412

383413
This will not clear any metrics that are currently stored, if you need to do this, you can use the clearMetrics method as described above.
384414
```typescript
415+
process.env.POWERTOOLS_METRICS_NAMESPACE = 'hello-world';
416+
process.env.POWERTOOLS_SERVICE_NAME = 'hello-world-service';
385417
class Lambda implements LambdaInterface {
386-
387418
@metrics.logMetrics()
388419
public handler<TEvent, TResult>(_event: TEvent, _context: Context, _callback: Callback<TResult>): void | Promise<TResult> {
389-
metrics.addMetric('test-metric', MetricUnits.Count, 10);
390-
const metricsObject = metrics.clearMetrics();
420+
metrics.addMetric('test-metric', MetricUnits.Count, 15);
421+
const metricsObject = metrics.serializeMetrics();
391422
console.log(JSON.stringify(metricsObject));
392423
}
393424

394425
}
395426
```
427+
<details>
428+
<summary>Click to expand and see the logs outputs</summary>
429+
430+
```bash
431+
432+
{
433+
"_aws":{
434+
"Timestamp":1625587915573,
435+
"CloudWatchMetrics":
436+
[
437+
{
438+
"Namespace":"hello-world",
439+
"Dimensions":[["service"]],
440+
"Metrics":
441+
[
442+
{
443+
"Name":"test-metric",
444+
"Unit":"Count"
445+
}
446+
]
447+
}
448+
]
449+
},
450+
"service":"hello-world-service",
451+
"test-metric":15
452+
}
453+
454+
```
455+
</details>
456+
396457
### Raising SchemaValidationError on empty metrics
397458

398459
If you want to ensure that at least one metric is emitted, you can pass raiseOnEmptyMetrics to the logMetrics decorator:

packages/metrics/src/Metrics.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ class Metrics implements MetricsInterface {
3333

3434
public addDimension(name: string, value: string): void {
3535
if (MAX_DIMENSION_COUNT <= this.getCurrentDimensionsCount()) {
36-
throw new Error(`Max dimension count of ${MAX_DIMENSION_COUNT} hit`);
36+
throw new RangeError(`The number of metric dimensions must be lower than ${MAX_DIMENSION_COUNT}`);
3737
}
3838
this.dimensions[name] = value;
3939
}
@@ -44,7 +44,7 @@ class Metrics implements MetricsInterface {
4444
newDimensions[dimensionName] = dimensions[dimensionName];
4545
});
4646
if (Object.keys(newDimensions).length > MAX_DIMENSION_COUNT) {
47-
throw new Error(`Adding ${Object.keys(dimensions).length} dimensions would exceed max dimension count of ${MAX_DIMENSION_COUNT}`);
47+
throw new RangeError(`Unable to add ${Object.keys(dimensions).length} dimensions: the number of metric dimensions must be lower than ${MAX_DIMENSION_COUNT}`);
4848
}
4949
this.dimensions = newDimensions;
5050
}
@@ -106,10 +106,8 @@ class Metrics implements MetricsInterface {
106106
Name: metricDefinition.name,
107107
Unit: metricDefinition.unit
108108
}));
109-
if (metricDefinitions.length === 0) {
110-
if (this.raiseOnEmptyMetrics) {
111-
throw new Error('Must contain at least one metric');
112-
}
109+
if (metricDefinitions.length === 0 && this.raiseOnEmptyMetrics) {
110+
throw new RangeError('The number of metrics recorded must be higher than zero');
113111
}
114112
if (!this.namespace) throw new Error('Namespace must be defined');
115113

0 commit comments

Comments
 (0)