Skip to content

Commit 899207a

Browse files
committed
fix(metrics) use same naming for serviceName
1 parent 83bf7f4 commit 899207a

9 files changed

+52
-52
lines changed

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

+16-16
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ class Metrics implements MetricsInterface {
153153
* Create a singleMetric to capture cold start.
154154
* If it's a cold start invocation, this feature will:
155155
* * Create a separate EMF blob solely containing a metric named ColdStart
156-
* * Add function_name and service dimensions
156+
* * Add function_name and serviceName dimensions
157157
*
158158
* This has the advantage of keeping cold start metric separate from your application metrics, where you might have unrelated dimensions.
159159
*
@@ -163,7 +163,7 @@ class Metrics implements MetricsInterface {
163163
* import { Metrics, MetricUnits } from '@aws-lambda-powertools/metrics';
164164
* import { Context } from 'aws-lambda';
165165
*
166-
* const metrics = new Metrics({namespace:"serverlessAirline", service:"orders"});
166+
* const metrics = new Metrics({namespace:"serverlessAirline", serviceName:"orders"});
167167
*
168168
* export const handler = async (event: any, context: Context) => {
169169
* metrics.captureColdStartMetric();
@@ -175,8 +175,8 @@ class Metrics implements MetricsInterface {
175175
this.isColdStart = false;
176176
const singleMetric = this.singleMetric();
177177

178-
if (this.dimensions.service) {
179-
singleMetric.addDimension('service', this.dimensions.service);
178+
if (this.dimensions.serviceName) {
179+
singleMetric.addDimension('serviceName', this.dimensions.serviceName);
180180
}
181181
if (this.functionName != null) {
182182
singleMetric.addDimension('function_name', this.functionName);
@@ -209,7 +209,7 @@ class Metrics implements MetricsInterface {
209209
* import { Metrics, MetricUnits } from '@aws-lambda-powertools/metrics';
210210
* import { Callback, Context } from 'aws-lambda';
211211
*
212-
* const metrics = new Metrics({namespace:"CDKExample", service:"withDecorator"});
212+
* const metrics = new Metrics({namespace:"CDKExample", serviceName:"withDecorator"});
213213
*
214214
* export class MyFunctionWithDecorator {
215215
*
@@ -260,7 +260,7 @@ class Metrics implements MetricsInterface {
260260
* ```typescript
261261
* import { Metrics, MetricUnits } from '@aws-lambda-powertools/metrics';
262262
*
263-
* const metrics = new Metrics({namespace: "CDKExample", service: "MyFunction"}); // Sets metric namespace, and service as a metric dimension
263+
* const metrics = new Metrics({namespace: "CDKExample", serviceName: "MyFunction"}); // Sets metric namespace, and serviceName as a metric dimension
264264
*
265265
* export const handler = async (_event: any, _context: any) => {
266266
* metrics.addMetric('test-metric', MetricUnits.Count, 10);
@@ -283,7 +283,7 @@ class Metrics implements MetricsInterface {
283283
* import { Metrics, MetricUnits } from '@aws-lambda-powertools/metrics';
284284
* import { Context } from 'aws-lambda';
285285
*
286-
* const metrics = new Metrics({namespace:"serverlessAirline", service:"orders"});
286+
* const metrics = new Metrics({namespace:"serverlessAirline", serviceName:"orders"});
287287
*
288288
* export const handler = async (event: any, context: Context) => {
289289
* metrics.raiseOnEmptyMetrics();
@@ -373,7 +373,7 @@ class Metrics implements MetricsInterface {
373373
public singleMetric(): Metrics {
374374
return new Metrics({
375375
namespace: this.namespace,
376-
service: this.dimensions.service,
376+
serviceName: this.dimensions.serviceName,
377377
defaultDimensions: this.defaultDimensions,
378378
singleMetric: true,
379379
});
@@ -420,24 +420,24 @@ class Metrics implements MetricsInterface {
420420
}
421421

422422
private setOptions(options: MetricsOptions): Metrics {
423-
const { customConfigService, namespace, service, singleMetric, defaultDimensions } = options;
423+
const { customConfigService, namespace, serviceName, singleMetric, defaultDimensions } = options;
424424

425425
this.setEnvVarsService();
426426
this.setCustomConfigService(customConfigService);
427427
this.setNamespace(namespace);
428-
this.setService(service);
428+
this.setServiceName(serviceName);
429429
this.setDefaultDimensions(defaultDimensions);
430430
this.isSingleMetric = singleMetric || false;
431431

432432
return this;
433433
}
434434

435-
private setService(service: string | undefined): void {
436-
const targetService = (service ||
437-
this.getCustomConfigService()?.getService() ||
438-
this.getEnvVarsService().getService()) as string;
439-
if (targetService.length > 0) {
440-
this.addDimension('service', targetService);
435+
private setServiceName(serviceName: string | undefined): void {
436+
const targetServiceName = (serviceName ||
437+
this.getCustomConfigService()?.getServiceName() ||
438+
this.getEnvVarsService().getServiceName()) as string;
439+
if (targetServiceName.length > 0) {
440+
this.addDimension('serviceName', targetServiceName);
441441
}
442442
}
443443

Diff for: packages/metrics/src/config/ConfigService.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ abstract class ConfigService implements ConfigServiceInterface {
44

55
public abstract get(name: string): string;
66
public abstract getNamespace(): string;
7-
public abstract getService(): string;
7+
public abstract getServiceName(): string;
88

99
}
1010

Diff for: packages/metrics/src/config/ConfigServiceInterface.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ interface ConfigServiceInterface {
22

33
get?(name: string): string
44
getNamespace(): string
5-
getService(): string
5+
getServiceName(): string
66

77
}
88

Diff for: packages/metrics/src/config/EnvironmentVariablesService.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { ConfigService } from '.';
33
class EnvironmentVariablesService extends ConfigService {
44

55
private namespaceVariable = 'POWERTOOLS_METRICS_NAMESPACE';
6-
private serviceVariable = 'POWERTOOLS_SERVICE_NAME';
6+
private serviceNameVariable = 'POWERTOOLS_SERVICE_NAME';
77

88
public get(name: string): string {
99
return process.env[name]?.trim() || '';
@@ -12,8 +12,8 @@ class EnvironmentVariablesService extends ConfigService {
1212
public getNamespace(): string {
1313
return this.get(this.namespaceVariable);
1414
}
15-
public getService(): string {
16-
return this.get(this.serviceVariable);
15+
public getServiceName(): string {
16+
return this.get(this.serviceNameVariable);
1717
}
1818

1919
}

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ type Dimensions = { [key: string]: string };
88
type MetricsOptions = {
99
customConfigService?: ConfigServiceInterface
1010
namespace?: string
11-
service?: string
11+
serviceName?: string
1212
singleMetric?: boolean
1313
defaultDimensions?: Dimensions
1414
};

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ const singleMetricName = process.env.EXPECTED_SINGLE_METRIC_NAME ?? 'MySingleMet
1313
const singleMetricUnit = (process.env.EXPECTED_SINGLE_METRIC_UNIT as MetricUnits) ?? MetricUnits.Percent;
1414
const singleMetricValue = process.env.EXPECTED_SINGLE_METRIC_VALUE ?? '2';
1515

16-
const metrics = new Metrics({ namespace: namespace, service: serviceName });
16+
const metrics = new Metrics({ namespace: namespace, serviceName: serviceName });
1717

1818
export const handler = async (_event: unknown, _context: Context): Promise<void> => {
1919
metrics.captureColdStartMetric();

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

+3-3
Original file line numberDiff line numberDiff line change
@@ -97,15 +97,15 @@ describe('happy cases', () => {
9797
.promise();
9898
expect(coldStartMetrics.Metrics?.length).toBe(1);
9999
const coldStartMetric = coldStartMetrics.Metrics?.[0];
100-
expect(coldStartMetric?.Dimensions).toStrictEqual([{ Name: 'service', Value: expectedServiceName }]);
100+
expect(coldStartMetric?.Dimensions).toStrictEqual([{ Name: 'ServiceName', Value: expectedServiceName }]);
101101

102102
// Check coldstart metric value
103103
const coldStartMetricStat = await cloudwatchClient
104104
.getMetricStatistics(
105105
{
106106
Namespace: expectedNamespace,
107107
StartTime: new Date(startTime.getTime() - 60 * 1000), // minus 1 minute,
108-
Dimensions: [{ Name: 'service', Value: expectedServiceName }],
108+
Dimensions: [{ Name: 'ServiceName', Value: expectedServiceName }],
109109
EndTime: new Date(new Date().getTime() + 60 * 1000),
110110
Period: 60,
111111
MetricName: 'ColdStart',
@@ -148,7 +148,7 @@ describe('happy cases', () => {
148148
expect(metrics.Metrics?.length).toBe(1);
149149
const metric = metrics.Metrics?.[0];
150150
const expectedDimensions = [
151-
{ Name: 'service', Value: expectedServiceName },
151+
{ Name: 'ServiceName', Value: expectedServiceName },
152152
{ Name: Object.keys(expectedDefaultDimensions)[0], Value: expectedDefaultDimensions.MyDimension },
153153
{ Name: Object.keys(expectedExtraDimension)[0], Value: expectedExtraDimension.MyExtraDimension },
154154
];

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

+12-12
Original file line numberDiff line numberDiff line change
@@ -48,13 +48,13 @@ describe('Class: Metrics', () => {
4848
test('should log service dimension correctly when passed', () => {
4949
const serviceName = 'testing_name';
5050

51-
const metrics = new Metrics({ namespace: 'test', service: serviceName });
51+
const metrics = new Metrics({ namespace: 'test', serviceName: serviceName });
5252
metrics.addMetric('test_name', MetricUnits.Seconds, 14);
5353
const loggedData = metrics.serializeMetrics();
5454

55-
expect(loggedData.service).toEqual(serviceName);
55+
expect(loggedData.serviceName).toEqual(serviceName);
5656
expect(loggedData._aws.CloudWatchMetrics[0].Dimensions[0].length).toEqual(1);
57-
expect(loggedData._aws.CloudWatchMetrics[0].Dimensions[0][0]).toEqual('service');
57+
expect(loggedData._aws.CloudWatchMetrics[0].Dimensions[0][0]).toEqual('serviceName');
5858
});
5959

6060
test('should log service dimension correctly from env var when not passed', () => {
@@ -65,7 +65,7 @@ describe('Class: Metrics', () => {
6565
metrics.addMetric('test_name', MetricUnits.Seconds, 10);
6666
const loggedData = metrics.serializeMetrics();
6767

68-
expect(loggedData.service).toEqual(serviceName);
68+
expect(loggedData.serviceName).toEqual(serviceName);
6969
});
7070

7171
test('Additional dimensions should be added correctly', () => {
@@ -286,7 +286,7 @@ describe('Class: Metrics', () => {
286286

287287
test('Cold should have service and function name if present', async () => {
288288
const serviceName = 'test-service';
289-
const metrics = new Metrics({ namespace: 'test', service: serviceName });
289+
const metrics = new Metrics({ namespace: 'test', serviceName: serviceName });
290290

291291
class LambdaFunction implements LambdaInterface {
292292
@metrics.logMetrics({ captureColdStartMetric: true })
@@ -307,16 +307,16 @@ describe('Class: Metrics', () => {
307307
expect(loggedData._aws.CloudWatchMetrics[0].Metrics.length).toBe(1);
308308
expect(loggedData._aws.CloudWatchMetrics[0].Metrics[0].Name).toBe('ColdStart');
309309
expect(loggedData._aws.CloudWatchMetrics[0].Metrics[0].Unit).toBe('Count');
310-
expect(loggedData.service).toBe(serviceName);
310+
expect(loggedData.serviceName).toBe(serviceName);
311311
expect(loggedData.function_name).toBe(dummyContext.helloworldContext.functionName);
312-
expect(loggedData._aws.CloudWatchMetrics[0].Dimensions[0]).toContain('service');
312+
expect(loggedData._aws.CloudWatchMetrics[0].Dimensions[0]).toContain('serviceName');
313313
expect(loggedData._aws.CloudWatchMetrics[0].Dimensions[0]).toContain('function_name');
314314
expect(loggedData.ColdStart).toBe(1);
315315
});
316316

317317
test('Cold should still log, without a function name', async () => {
318318
const serviceName = 'test-service';
319-
const metrics = new Metrics({ namespace: 'test', service: serviceName });
319+
const metrics = new Metrics({ namespace: 'test', serviceName: serviceName });
320320
const newDummyContext = JSON.parse(JSON.stringify(dummyContext));
321321
delete newDummyContext.functionName;
322322
class LambdaFunction implements LambdaInterface {
@@ -339,8 +339,8 @@ describe('Class: Metrics', () => {
339339
expect(loggedData._aws.CloudWatchMetrics[0].Metrics.length).toBe(1);
340340
expect(loggedData._aws.CloudWatchMetrics[0].Metrics[0].Name).toBe('ColdStart');
341341
expect(loggedData._aws.CloudWatchMetrics[0].Metrics[0].Unit).toBe('Count');
342-
expect(loggedData.service).toBe(serviceName);
343-
expect(loggedData._aws.CloudWatchMetrics[0].Dimensions[0]).toContain('service');
342+
expect(loggedData.serviceName).toBe(serviceName);
343+
expect(loggedData._aws.CloudWatchMetrics[0].Dimensions[0]).toContain('serviceName');
344344
expect(loggedData._aws.CloudWatchMetrics[0].Dimensions[0]).not.toContain('function_name');
345345
expect(loggedData.ColdStart).toBe(1);
346346
});
@@ -566,14 +566,14 @@ describe('Class: Metrics', () => {
566566
const serviceName = 'Custom Provider Service Name';
567567
const namespace = 'Custom Provider namespace';
568568
const customConfigService = {
569-
getService: () => serviceName,
569+
getServiceName: () => serviceName,
570570
getNamespace: () => namespace,
571571
};
572572

573573
const metrics = new Metrics({ customConfigService: customConfigService });
574574
const loggedData = metrics.serializeMetrics();
575575

576-
expect(loggedData.service).toEqual(serviceName);
576+
expect(loggedData.serviceName).toEqual(serviceName);
577577
expect(loggedData._aws.CloudWatchMetrics[0].Namespace).toEqual(namespace);
578578
});
579579
});

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

+14-14
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ describe('Middy middleware', () => {
4545

4646
test('when a metrics instance receive multiple metrics with the same name, it prints multiple values in an array format', async () => {
4747
// Prepare
48-
const metrics = new Metrics({ namespace:'serverlessAirline', service:'orders' });
48+
const metrics = new Metrics({ namespace:'serverlessAirline', serviceName:'orders' });
4949

5050
const lambdaHandler = (): void => {
5151
metrics.addMetric('successfulBooking', MetricUnits.Count, 2);
@@ -64,12 +64,12 @@ describe('Middy middleware', () => {
6464
'CloudWatchMetrics': [{
6565
'Namespace': 'serverlessAirline',
6666
'Dimensions': [
67-
['service']
67+
['serviceName']
6868
],
6969
'Metrics': [{ 'Name': 'successfulBooking', 'Unit': 'Count' }],
7070
}],
7171
},
72-
'service': 'orders',
72+
'serviceName': 'orders',
7373
'successfulBooking': [
7474
2,
7575
1,
@@ -80,7 +80,7 @@ describe('Middy middleware', () => {
8080
test('when a metrics instance is passed WITH custom options, it prints the metrics in the stdout', async () => {
8181

8282
// Prepare
83-
const metrics = new Metrics({ namespace:'serverlessAirline', service:'orders' });
83+
const metrics = new Metrics({ namespace:'serverlessAirline', serviceName:'orders' });
8484

8585
const lambdaHandler = (): void => {
8686
metrics.addMetric('successfulBooking', MetricUnits.Count, 1);
@@ -102,14 +102,14 @@ describe('Middy middleware', () => {
102102
'CloudWatchMetrics': [{
103103
'Namespace': 'serverlessAirline',
104104
'Dimensions': [
105-
[ 'environment', 'aws_region', 'service', 'function_name' ]
105+
[ 'environment', 'aws_region', 'serviceName', 'function_name' ]
106106
],
107107
'Metrics': [{ 'Name': 'ColdStart', 'Unit': 'Count' }],
108108
}],
109109
},
110110
'environment': 'prod',
111111
'aws_region' : 'eu-central-1',
112-
'service': 'orders',
112+
'serviceName': 'orders',
113113
'function_name': 'foo-bar-function',
114114
'ColdStart': 1,
115115
}));
@@ -119,14 +119,14 @@ describe('Middy middleware', () => {
119119
'CloudWatchMetrics': [{
120120
'Namespace': 'serverlessAirline',
121121
'Dimensions': [
122-
[ 'environment', 'aws_region', 'service' ]
122+
[ 'environment', 'aws_region', 'serviceName' ]
123123
],
124124
'Metrics': [{ 'Name': 'successfulBooking', 'Unit': 'Count' }],
125125
}],
126126
},
127127
'environment': 'prod',
128128
'aws_region' : 'eu-central-1',
129-
'service': 'orders',
129+
'serviceName': 'orders',
130130
'successfulBooking': 1,
131131
}));
132132

@@ -135,7 +135,7 @@ describe('Middy middleware', () => {
135135
test('when a metrics instance is passed WITHOUT custom options, it prints the metrics in the stdout', async () => {
136136

137137
// Prepare
138-
const metrics = new Metrics({ namespace:'serverlessAirline', service:'orders' });
138+
const metrics = new Metrics({ namespace:'serverlessAirline', serviceName:'orders' });
139139

140140
const lambdaHandler = (): void => {
141141
metrics.addMetric('successfulBooking', MetricUnits.Count, 1);
@@ -153,12 +153,12 @@ describe('Middy middleware', () => {
153153
'CloudWatchMetrics': [{
154154
'Namespace': 'serverlessAirline',
155155
'Dimensions': [
156-
['service']
156+
['serviceName']
157157
],
158158
'Metrics': [{ 'Name': 'successfulBooking', 'Unit': 'Count' }],
159159
}],
160160
},
161-
'service': 'orders',
161+
'serviceName': 'orders',
162162
'successfulBooking': 1,
163163
}));
164164

@@ -167,7 +167,7 @@ describe('Middy middleware', () => {
167167
test('when an array of Metrics instances is passed, it prints the metrics in the stdout', async () => {
168168

169169
// Prepare
170-
const metrics = new Metrics({ namespace:'serverlessAirline', service:'orders' });
170+
const metrics = new Metrics({ namespace:'serverlessAirline', serviceName:'orders' });
171171

172172
const lambdaHandler = (): void => {
173173
metrics.addMetric('successfulBooking', MetricUnits.Count, 1);
@@ -187,12 +187,12 @@ describe('Middy middleware', () => {
187187
'CloudWatchMetrics': [{
188188
'Namespace': 'serverlessAirline',
189189
'Dimensions': [
190-
['service']
190+
['serviceName']
191191
],
192192
'Metrics': [{ 'Name': 'successfulBooking', 'Unit': 'Count' }],
193193
}],
194194
},
195-
'service': 'orders',
195+
'serviceName': 'orders',
196196
'successfulBooking': 1,
197197
}));
198198

0 commit comments

Comments
 (0)