Skip to content

Commit c1e21cb

Browse files
committed
update metrics with async calls
1 parent ffc49db commit c1e21cb

File tree

2 files changed

+17
-16
lines changed

2 files changed

+17
-16
lines changed

src/metrics.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,15 @@ export class MetricPublisher {
2525
this.client = session.client('CloudWatch') as CloudWatch;
2626
}
2727

28-
publishMetric(
28+
async publishMetric(
2929
metricName: MetricTypes,
3030
dimensions: Map<string, string>,
3131
unit: StandardUnit,
3232
value: number,
3333
timestamp: Date,
34-
): void {
34+
): Promise<void> {
3535
try {
36-
this.client.putMetricData({
36+
const metric = await this.client.putMetricData({
3737
Namespace: this.namespace,
3838
MetricData: [{
3939
MetricName: metricName,
@@ -42,7 +42,7 @@ export class MetricPublisher {
4242
Timestamp: timestamp,
4343
Value: value,
4444
}],
45-
});
45+
}).promise();
4646
} catch(err) {
4747
LOGGER.error(`An error occurred while publishing metrics: ${err.message}`);
4848
}
@@ -56,7 +56,7 @@ export class MetricsPublisherProxy {
5656
constructor(public accountId: string, public resourceType: string) {
5757
this.namespace = MetricsPublisherProxy.makeNamespace(accountId, resourceType);
5858
this.resourceType = resourceType;
59-
this.publishers = [];
59+
this.publishers = [];
6060
}
6161

6262
static makeNamespace(accountId: string, resourceType: string): string {

tests/lib/metrics.test.ts

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -64,21 +64,23 @@ describe('when getting metrics', () => {
6464
]);
6565
});
6666

67-
test('put metric catches error', () => {
67+
test('put metric catches error', async () => {
6868
const spyConsoleError: jest.SpyInstance = jest
6969
.spyOn(global.console, 'error').mockImplementation(() => {});
70-
putMetricData.mockImplementationOnce(() => {
71-
throw awsUtil.error(new Error(), {
72-
code: 'InternalServiceError',
73-
message: 'An error occurred (InternalServiceError) when '
74-
+ 'calling the PutMetricData operation: ',
75-
});
70+
putMetricData.mockReturnValueOnce({
71+
promise: jest.fn().mockRejectedValueOnce(
72+
awsUtil.error(new Error(), {
73+
code: 'InternalServiceError',
74+
message: 'An error occurred (InternalServiceError) when '
75+
+ 'calling the PutMetricData operation: ',
76+
})
77+
)
7678
});
7779
const publisher = new MetricPublisher(session, NAMESPACE);
7880
const dimensions = new Map<string, string>();
7981
dimensions.set('DimensionKeyActionType', Action.Create);
8082
dimensions.set('DimensionKeyResourceType', RESOURCE_TYPE);
81-
publisher.publishMetric(
83+
await publisher.publishMetric(
8284
MetricTypes.HandlerInvocationCount,
8385
dimensions,
8486
StandardUnit.Count,
@@ -115,7 +117,7 @@ describe('when getting metrics', () => {
115117
test('publish exception metric', () => {
116118
const proxy = new MetricsPublisherProxy(ACCOUNT_ID, RESOURCE_TYPE);
117119
proxy.addMetricsPublisher(session);
118-
proxy.publishExceptionMetric( MOCK_DATE, Action.Create, new Error('fake-err'));
120+
proxy.publishExceptionMetric(MOCK_DATE, Action.Create, new Error('fake-err'));
119121
expect(putMetricData).toHaveBeenCalledTimes(1);
120122
expect(putMetricData).toHaveBeenCalledWith({
121123
MetricData: [{
@@ -145,7 +147,7 @@ describe('when getting metrics', () => {
145147
test('publish invocation metric', () => {
146148
const proxy = new MetricsPublisherProxy(ACCOUNT_ID, RESOURCE_TYPE);
147149
proxy.addMetricsPublisher(session);
148-
proxy.publishInvocationMetric( MOCK_DATE, Action.Create);
150+
proxy.publishInvocationMetric(MOCK_DATE, Action.Create);
149151
expect(putMetricData).toHaveBeenCalledTimes(1);
150152
expect(putMetricData).toHaveBeenCalledWith({
151153
MetricData: [{
@@ -194,7 +196,6 @@ describe('when getting metrics', () => {
194196
});
195197
});
196198

197-
198199
test('publish log delivery exception metric', () => {
199200
const proxy = new MetricsPublisherProxy(ACCOUNT_ID, RESOURCE_TYPE);
200201
proxy.addMetricsPublisher(session);

0 commit comments

Comments
 (0)