Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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: 3 additions & 1 deletion packages/metrics/src/Metrics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,9 @@ class Metrics extends Utility implements MetricsInterface {
public publishStoredMetrics(): void {
const target = this.serializeMetrics();
console.log(JSON.stringify(target));
this.storedMetrics = {};
this.clearMetrics();
this.clearDimensions();
this.clearMetadata();
}

/**
Expand Down
56 changes: 56 additions & 0 deletions packages/metrics/tests/unit/Metrics.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,35 @@ describe('Class: Metrics', () => {
expect(loggedData[additionalDimension.name]).toEqual(additionalDimension.value);
});

test('Publish Stored Metrics should clear added dimensions', async () => {
const metrics = new Metrics({ namespace: 'test' });
const dimensionItem = { name: 'dimensionName', value: 'dimensionValue' };

class LambdaFunction implements LambdaInterface {
@metrics.logMetrics()
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
public handler<TEvent, TResult>(
_event: TEvent,
_context: Context,
_callback: Callback<TResult>,
): void | Promise<TResult> {
metrics.addMetric('test_name_1', MetricUnits.Count, 1);
metrics.addDimension(dimensionItem.name, dimensionItem.value);
metrics.publishStoredMetrics();
}
}

await new LambdaFunction().handler(dummyEvent, dummyContext.helloworldContext, () => console.log('Lambda invoked!'));
const loggedData = [ JSON.parse(consoleSpy.mock.calls[0][0]), JSON.parse(consoleSpy.mock.calls[1][0]) ];

expect(console.log).toBeCalledTimes(2);
expect(loggedData[0][dimensionItem.name]).toEqual(dimensionItem.value);
expect(loggedData[0]._aws.CloudWatchMetrics[0].Dimensions[0].length).toEqual(1);
expect(loggedData[1][dimensionItem.name]).toBeUndefined();
expect(loggedData[1]._aws.CloudWatchMetrics[0].Dimensions[0].length).toEqual(0);
});

test('Adding more than max dimensions should throw error', () => {
expect.assertions(1);
const metrics = new Metrics();
Expand Down Expand Up @@ -144,6 +173,33 @@ describe('Class: Metrics', () => {
expect(loggedData[metadataItem.name]).toEqual(metadataItem.value);
expect(postClearLoggedData[metadataItem.name]).toBeUndefined();
});

test('Publish Stored Metrics should clear metadata', async () => {
const metrics = new Metrics({ namespace: 'test' });
const metadataItem = { name: 'metaName', value: 'metaValue' };

class LambdaFunction implements LambdaInterface {
@metrics.logMetrics()
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
public handler<TEvent, TResult>(
_event: TEvent,
_context: Context,
_callback: Callback<TResult>,
): void | Promise<TResult> {
metrics.addMetric('test_name_1', MetricUnits.Count, 1);
metrics.addMetadata(metadataItem.name, metadataItem.value);
metrics.publishStoredMetrics();
}
}

await new LambdaFunction().handler(dummyEvent, dummyContext.helloworldContext, () => console.log('Lambda invoked!'));
const loggedData = [ JSON.parse(consoleSpy.mock.calls[0][0]), JSON.parse(consoleSpy.mock.calls[1][0]) ];

expect(console.log).toBeCalledTimes(2);
expect(loggedData[0][metadataItem.name]).toEqual(metadataItem.value);
expect(loggedData[1][metadataItem.name]).toBeUndefined();
});
});

describe('Feature: Default Dimensions', () => {
Expand Down