From a979202ae0563f8ce00dee98bbf15d0bcfcfd3cc Mon Sep 17 00:00:00 2001 From: Andrea Amorosi Date: Wed, 9 Nov 2022 13:48:26 +0100 Subject: [PATCH] fix(metrics): store service name in defaultDimensions to avoid clearing it (#1146) * fix: store service name in defaultDimensions * fix: changed behavior also in singleMetric * chore: unit test --- packages/metrics/src/Metrics.ts | 6 +++--- packages/metrics/tests/unit/middleware/middy.test.ts | 8 ++++---- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/packages/metrics/src/Metrics.ts b/packages/metrics/src/Metrics.ts index f8e2571fe3..10e500b5f7 100644 --- a/packages/metrics/src/Metrics.ts +++ b/packages/metrics/src/Metrics.ts @@ -198,8 +198,8 @@ class Metrics extends Utility implements MetricsInterface { if (!this.isColdStart()) return; const singleMetric = this.singleMetric(); - if (this.dimensions.service) { - singleMetric.addDimension('service', this.dimensions.service); + if (this.defaultDimensions.service) { + singleMetric.setDefaultDimensions({ service: this.defaultDimensions.service }); } if (this.functionName != null) { singleMetric.addDimension('function_name', this.functionName); @@ -475,7 +475,7 @@ class Metrics extends Utility implements MetricsInterface { this.getCustomConfigService()?.getServiceName() || this.getEnvVarsService().getServiceName()) as string; if (targetService.length > 0) { - this.addDimension('service', targetService); + this.setDefaultDimensions({ service: targetService }); } } diff --git a/packages/metrics/tests/unit/middleware/middy.test.ts b/packages/metrics/tests/unit/middleware/middy.test.ts index a31cb20d5e..ced312f1e3 100644 --- a/packages/metrics/tests/unit/middleware/middy.test.ts +++ b/packages/metrics/tests/unit/middleware/middy.test.ts @@ -247,14 +247,14 @@ describe('Middy middleware', () => { CloudWatchMetrics: [ { Namespace: 'serverlessAirline', - Dimensions: [[ 'environment', 'aws_region', 'service', 'function_name' ]], + Dimensions: [[ 'service', 'environment', 'aws_region', 'function_name' ]], Metrics: [{ Name: 'ColdStart', Unit: 'Count' }], }, ], }, + service: 'orders', environment: 'prod', aws_region: 'eu-west-1', - service: 'orders', function_name: 'foo-bar-function', ColdStart: 1, }) @@ -267,14 +267,14 @@ describe('Middy middleware', () => { CloudWatchMetrics: [ { Namespace: 'serverlessAirline', - Dimensions: [[ 'environment', 'aws_region', 'service' ]], + Dimensions: [[ 'service', 'environment', 'aws_region' ]], Metrics: [{ Name: 'successfulBooking', Unit: 'Count' }], }, ], }, + service: 'orders', environment: 'prod', aws_region: 'eu-west-1', - service: 'orders', successfulBooking: 1, }) );