Skip to content

Commit

Permalink
fix(metrics): store service name in defaultDimensions to avoid cleari…
Browse files Browse the repository at this point in the history
…ng it (#1146)

* fix: store service name in defaultDimensions

* fix: changed behavior also in singleMetric

* chore: unit test
  • Loading branch information
dreamorosi authored Nov 9, 2022
1 parent cc8bd26 commit a979202
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 7 deletions.
6 changes: 3 additions & 3 deletions packages/metrics/src/Metrics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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 });
}
}

Expand Down
8 changes: 4 additions & 4 deletions packages/metrics/tests/unit/middleware/middy.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
})
Expand All @@ -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,
})
);
Expand Down

0 comments on commit a979202

Please sign in to comment.