Skip to content

fix(metrics): use same naming for serviceName #401

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jan 5, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions packages/metrics/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/metrics/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@
"@typescript-eslint/parser": "^5.4.0",
"aws-cdk": "^1.137.0",
"aws-sdk": "^2.1048.0",
"esbuild": "^0.14.5",
"esbuild": "0.14.5",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(minor) Any particular reason to not auto update this one?

Copy link
Contributor Author

@flochaz flochaz Jan 5, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, upper version are breaking import of enum . I am creating a simple project to reproduce the issue and create one on esbuild repo , probably v0.14.7 https://github.com/evanw/esbuild/releases

"eslint": "^8.3.0",
"eslint-import-resolver-node": "^0.3.6",
"eslint-import-resolver-typescript": "^2.5.0",
Expand Down
18 changes: 9 additions & 9 deletions packages/metrics/src/Metrics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ const DEFAULT_NAMESPACE = 'default_namespace';
* import { Metrics, MetricUnits } from '@aws-lambda-powertools/metrics';
* import { Callback, Context } from 'aws-lambda';
*
* const metrics = new Metrics({namespace:"MyService", service:"withDecorator"});
* const metrics = new Metrics({namespace:"MyService", serviceName:"withDecorator"});
*
* export class MyFunctionWithDecorator {
*
Expand All @@ -69,7 +69,7 @@ const DEFAULT_NAMESPACE = 'default_namespace';
* ```typescript
* import { Metrics, MetricUnits } from '@aws-lambda-powertools/metrics';
*
* const metrics = new Metrics({namespace: "MyService", service: "MyFunction"});
* const metrics = new Metrics({namespace: "MyService", serviceName: "MyFunction"});
*
* export const handler = async (_event: any, _context: any) => {
* metrics.captureColdStart();
Expand Down Expand Up @@ -163,7 +163,7 @@ class Metrics implements MetricsInterface {
* import { Metrics, MetricUnits } from '@aws-lambda-powertools/metrics';
* import { Context } from 'aws-lambda';
*
* const metrics = new Metrics({namespace:"serverlessAirline", service:"orders"});
* const metrics = new Metrics({namespace:"serverlessAirline", serviceName:"orders"});
*
* export const handler = async (event: any, context: Context) => {
* metrics.captureColdStartMetric();
Expand Down Expand Up @@ -209,7 +209,7 @@ class Metrics implements MetricsInterface {
* import { Metrics, MetricUnits } from '@aws-lambda-powertools/metrics';
* import { Callback, Context } from 'aws-lambda';
*
* const metrics = new Metrics({namespace:"CDKExample", service:"withDecorator"});
* const metrics = new Metrics({namespace:"CDKExample", serviceName:"withDecorator"});
*
* export class MyFunctionWithDecorator {
*
Expand Down Expand Up @@ -260,7 +260,7 @@ class Metrics implements MetricsInterface {
* ```typescript
* import { Metrics, MetricUnits } from '@aws-lambda-powertools/metrics';
*
* const metrics = new Metrics({namespace: "CDKExample", service: "MyFunction"}); // Sets metric namespace, and service as a metric dimension
* const metrics = new Metrics({namespace: "CDKExample", serviceName: "MyFunction"}); // Sets metric namespace, and service as a metric dimension
*
* export const handler = async (_event: any, _context: any) => {
* metrics.addMetric('test-metric', MetricUnits.Count, 10);
Expand All @@ -283,7 +283,7 @@ class Metrics implements MetricsInterface {
* import { Metrics, MetricUnits } from '@aws-lambda-powertools/metrics';
* import { Context } from 'aws-lambda';
*
* const metrics = new Metrics({namespace:"serverlessAirline", service:"orders"});
* const metrics = new Metrics({namespace:"serverlessAirline", serviceName:"orders"});
*
* export const handler = async (event: any, context: Context) => {
* metrics.raiseOnEmptyMetrics();
Expand Down Expand Up @@ -373,7 +373,7 @@ class Metrics implements MetricsInterface {
public singleMetric(): Metrics {
return new Metrics({
namespace: this.namespace,
service: this.dimensions.service,
serviceName: this.dimensions.service,
defaultDimensions: this.defaultDimensions,
singleMetric: true,
});
Expand Down Expand Up @@ -420,12 +420,12 @@ class Metrics implements MetricsInterface {
}

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

this.setEnvVarsService();
this.setCustomConfigService(customConfigService);
this.setNamespace(namespace);
this.setService(service);
this.setService(serviceName);
this.setDefaultDimensions(defaultDimensions);
this.isSingleMetric = singleMetric || false;

Expand Down
2 changes: 1 addition & 1 deletion packages/metrics/src/types/Metrics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ type Dimensions = { [key: string]: string };
type MetricsOptions = {
customConfigService?: ConfigServiceInterface
namespace?: string
service?: string
serviceName?: string
singleMetric?: boolean
defaultDimensions?: Dimensions
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const singleMetricName = process.env.EXPECTED_SINGLE_METRIC_NAME ?? 'MySingleMet
const singleMetricUnit = (process.env.EXPECTED_SINGLE_METRIC_UNIT as MetricUnits) ?? MetricUnits.Percent;
const singleMetricValue = process.env.EXPECTED_SINGLE_METRIC_VALUE ?? '2';

const metrics = new Metrics({ namespace: namespace, service: serviceName });
const metrics = new Metrics({ namespace: namespace, serviceName: serviceName });

export const handler = async (_event: unknown, _context: Context): Promise<void> => {
metrics.captureColdStartMetric();
Expand All @@ -33,5 +33,4 @@ export const handler = async (_event: unknown, _context: Context): Promise<void>
metricWithItsOwnDimensions.addMetric(singleMetricName, singleMetricUnit, parseInt(singleMetricValue));

metrics.publishStoredMetrics();
metrics.raiseOnEmptyMetrics();
};
1 change: 1 addition & 0 deletions packages/metrics/tests/e2e/standardFunctions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

import { randomUUID } from 'crypto';
import * as lambda from '@aws-cdk/aws-lambda-nodejs';
import { Tracing } from '@aws-cdk/aws-lambda';
import { App, Stack } from '@aws-cdk/core';
import { SdkProvider } from 'aws-cdk/lib/api/aws-auth';
import { CloudFormationDeployments } from 'aws-cdk/lib/api/cloudformation-deployments';
Expand Down
6 changes: 3 additions & 3 deletions packages/metrics/tests/unit/Metrics.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ describe('Class: Metrics', () => {
test('should log service dimension correctly when passed', () => {
const serviceName = 'testing_name';

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

Expand Down Expand Up @@ -286,7 +286,7 @@ describe('Class: Metrics', () => {

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

class LambdaFunction implements LambdaInterface {
@metrics.logMetrics({ captureColdStartMetric: true })
Expand Down Expand Up @@ -316,7 +316,7 @@ describe('Class: Metrics', () => {

test('Cold should still log, without a function name', async () => {
const serviceName = 'test-service';
const metrics = new Metrics({ namespace: 'test', service: serviceName });
const metrics = new Metrics({ namespace: 'test', serviceName: serviceName });
const newDummyContext = JSON.parse(JSON.stringify(dummyContext));
delete newDummyContext.functionName;
class LambdaFunction implements LambdaInterface {
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 @@ -45,7 +45,7 @@ describe('Middy middleware', () => {

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

const lambdaHandler = (): void => {
metrics.addMetric('successfulBooking', MetricUnits.Count, 2);
Expand Down Expand Up @@ -80,7 +80,7 @@ describe('Middy middleware', () => {
test('when a metrics instance is passed WITH custom options, it prints the metrics in the stdout', async () => {

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

const lambdaHandler = (): void => {
metrics.addMetric('successfulBooking', MetricUnits.Count, 1);
Expand Down Expand Up @@ -135,7 +135,7 @@ describe('Middy middleware', () => {
test('when a metrics instance is passed WITHOUT custom options, it prints the metrics in the stdout', async () => {

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

const lambdaHandler = (): void => {
metrics.addMetric('successfulBooking', MetricUnits.Count, 1);
Expand Down Expand Up @@ -167,7 +167,7 @@ describe('Middy middleware', () => {
test('when an array of Metrics instances is passed, it prints the metrics in the stdout', async () => {

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

const lambdaHandler = (): void => {
metrics.addMetric('successfulBooking', MetricUnits.Count, 1);
Expand Down