Skip to content

Commit f5e5ac0

Browse files
committed
chore: align with suggested implementation
1 parent 6b008c2 commit f5e5ac0

File tree

7 files changed

+19
-43
lines changed

7 files changed

+19
-43
lines changed

docs/core/metrics.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -66,12 +66,12 @@ The library requires two settings. You can set them as environment variables, or
6666

6767
These settings will be used across all metrics emitted:
6868

69-
| Setting | Description | Environment variable | Default | Allowed Values | Example | Constructor parameter |
70-
|----------------------|------------------------------------------------------------------|------------------------------------|-------------------|----------------|---------------------|-----------------------|
71-
| **Service** | Optionally, sets **service** metric dimension across all metrics | `POWERTOOLS_SERVICE_NAME` | `service_undefined` | Any string | `serverlessAirline` | `serviceName` |
72-
| **Metric namespace** | Logical container where all metrics will be placed | `POWERTOOLS_METRICS_NAMESPACE` | `default_namespace` | Any string | `serverlessAirline` | `default_namespace` |
73-
| **Function Name** | Function name used as dimension for the `ColdStart` metric | `POWERTOOLS_METRICS_FUNCTION_NAME` | [See docs](#capturing-a-cold-start-invocation-as-metric) | Any string | `my-function-name` | `functionName` |
74-
| **Enabled** | Whether to emit metrics to standard output or not | `POWERTOOLS_METRICS_ENABLED` | `true` | Boolean | `false` | |
69+
| Setting | Description | Environment variable | Default | Allowed Values | Example | Constructor parameter |
70+
|----------------------|------------------------------------------------------------------|------------------------------------|----------------------------------------------------------|----------------|---------------------|-----------------------|
71+
| **Service** | Optionally, sets **service** metric dimension across all metrics | `POWERTOOLS_SERVICE_NAME` | `service_undefined` | Any string | `serverlessAirline` | `serviceName` |
72+
| **Metric namespace** | Logical container where all metrics will be placed | `POWERTOOLS_METRICS_NAMESPACE` | `default_namespace` | Any string | `serverlessAirline` | `default_namespace` |
73+
| **Function Name** | Function name used as dimension for the `ColdStart` metric | `POWERTOOLS_METRICS_FUNCTION_NAME` | [See docs](#capturing-a-cold-start-invocation-as-metric) | Any string | `my-function-name` | `functionName` |
74+
| **Enabled** | Whether to emit metrics to standard output or not | `POWERTOOLS_METRICS_ENABLED` | `true` | Boolean | `false` | |
7575

7676
!!! tip
7777
Use your application name or main service as the metric namespace to easily group all metrics
@@ -425,7 +425,7 @@ The priority of the `function_name` dimension value is defined as:
425425

426426
=== "captureColdStartMetric method"
427427

428-
```typescript hl_lines="8"
428+
```typescript hl_lines="12"
429429
--8<-- "examples/snippets/metrics/captureColdStartMetric.ts"
430430
```
431431

examples/snippets/metrics/captureColdStartMetric.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ export const handler = async (
99
_event: unknown,
1010
_context: unknown
1111
): Promise<void> => {
12-
// Capture cold start metric
1312
metrics.captureColdStartMetric('my-function-name');
1413

1514
metrics.addMetric('successfulBooking', MetricUnit.Count, 1);

examples/snippets/metrics/functionName.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ export const handler = async (
1010
_event: unknown,
1111
_context: unknown
1212
): Promise<void> => {
13-
// Capture cold start metric
1413
metrics.captureColdStartMetric();
1514

1615
metrics.addMetric('successfulBooking', MetricUnit.Count, 1);

packages/metrics/src/Metrics.ts

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ import type {
2929
MetricsOptions,
3030
StoredMetrics,
3131
} from './types/index.js';
32-
import { getFirstDefinedValue } from './utils';
3332

3433
/**
3534
* The Metrics utility creates custom metrics asynchronously by logging metrics to standard output following {@link https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/CloudWatch_Embedded_Metric_Format.html | Amazon CloudWatch Embedded Metric Format (EMF)}.
@@ -384,12 +383,11 @@ class Metrics extends Utility implements MetricsInterface {
384383
service: this.defaultDimensions.service,
385384
});
386385
}
387-
const coldStartFunctionName = getFirstDefinedValue(
388-
this.functionName,
389-
functionName
390-
);
391-
if (coldStartFunctionName) {
392-
singleMetric.addDimension('function_name', coldStartFunctionName);
386+
for (const value of [this.functionName, functionName]) {
387+
if (value && value.trim().length > 0) {
388+
singleMetric.addDimension('function_name', value);
389+
break;
390+
}
393391
}
394392
singleMetric.addMetric(COLD_START_METRIC, MetricUnits.Count, 1);
395393
}
@@ -919,10 +917,12 @@ class Metrics extends Utility implements MetricsInterface {
919917
protected setFunctionNameForColdStartMetric(functionName?: string): void {
920918
const constructorFunctionName = functionName;
921919
const envFunctionName = this.getEnvVarsService().getFunctionName();
922-
this.functionName = getFirstDefinedValue(
923-
constructorFunctionName,
924-
envFunctionName
925-
);
920+
for (const value of [constructorFunctionName, envFunctionName]) {
921+
if (value && value.trim().length > 0) {
922+
this.functionName = value;
923+
break;
924+
}
925+
}
926926
}
927927

928928
/**

packages/metrics/src/config/EnvironmentVariablesService.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ class EnvironmentVariablesService
1313
{
1414
private readonly namespaceVariable = 'POWERTOOLS_METRICS_NAMESPACE';
1515
private readonly functionNameVariable = 'POWERTOOLS_METRICS_FUNCTION_NAME';
16-
1716
private readonly disabledVariable = 'POWERTOOLS_METRICS_DISABLED';
1817

1918
/**

packages/metrics/src/types/Metrics.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -463,7 +463,7 @@ interface MetricsInterface {
463463
* @deprecated Override the function name for `ColdStart` metrics inferred from the context either via:
464464
* - `functionName` constructor parameter
465465
* - `POWERTOOLS_FUNCTION_NAME` environment variable
466-
* - {@link Metrics.captureColdStartMetric | `captureColdStartMetric('myFunctionName')`} method
466+
* - {@link MetricsInterface.captureColdStartMetric | `captureColdStartMetric()`} method
467467
*/
468468
setFunctionName(name: string): void;
469469
/**

packages/metrics/src/utils.ts

Lines changed: 0 additions & 21 deletions
This file was deleted.

0 commit comments

Comments
 (0)