Skip to content

Commit

Permalink
fix(metrics): metadata and dimensions not cleared on publish (#1129)
Browse files Browse the repository at this point in the history
* test(metrics): add tests to cover clearing metadata and dimensions after publishing stored metrics

* fix(metrics): fix metadata and dimensions not cleared on publish
  • Loading branch information
shdq authored Oct 26, 2022
1 parent c25279c commit b209c30
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 1 deletion.
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

0 comments on commit b209c30

Please sign in to comment.