Skip to content

Commit 79126f0

Browse files
author
Alan Churley
committed
Adding bulk dimension add
1 parent f3295b3 commit 79126f0

File tree

3 files changed

+54
-5
lines changed

3 files changed

+54
-5
lines changed

packages/metrics/src/Metrics.ts

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,23 @@ class Metrics implements MetricsInterface {
3232
}
3333

3434
public addDimension(name: string, value: string): void {
35-
if (MAX_DIMENSION_COUNT <= (Object.keys(this.dimensions).length + Object.keys(this.defaultDimensions).length)) {
35+
if (MAX_DIMENSION_COUNT <= this.getCurrentDimensionsCount()) {
3636
throw new Error(`Max dimension count of ${MAX_DIMENSION_COUNT} hit`);
3737
}
3838
this.dimensions[name] = value;
3939
}
4040

41+
public addDimensions(dimensions: {[key: string]: string}): void {
42+
const newDimensions = { ...this.dimensions };
43+
Object.keys(dimensions).forEach((dimensionName) => {
44+
newDimensions[dimensionName] = dimensions[dimensionName];
45+
});
46+
if (Object.keys(newDimensions).length > MAX_DIMENSION_COUNT) {
47+
throw new Error(`Adding ${Object.keys(dimensions).length} dimensions would exceed max dimension count of ${MAX_DIMENSION_COUNT}`);
48+
}
49+
this.dimensions = newDimensions;
50+
}
51+
4152
public addMetadata(key: string, value: string): void {
4253
this.metadata[key] = value;
4354
}
@@ -154,6 +165,10 @@ class Metrics implements MetricsInterface {
154165
singleMetric.addMetric('ColdStart', MetricUnits.Count, 1);
155166
}
156167

168+
private getCurrentDimensionsCount(): number {
169+
return Object.keys(this.dimensions).length + Object.keys(this.defaultDimensions).length;
170+
}
171+
157172
private getCustomConfigService(): ConfigServiceInterface | undefined {
158173
return this.customConfigService;
159174
}

packages/metrics/src/MetricsInterface.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ interface MetricsInterface {
66
clearMetrics(): void
77
logMetrics(options?: DecoratorOptions): HandlerMethodDecorator
88
addDimension(name: string, value: string): void
9+
addDimensions(dimensions: {[key: string]: string}): void
910
addMetadata(key: string, value: string): void
1011
clearMetadata(): void
1112
clearDimensions(): void

packages/metrics/tests/unit/Metrics.test.ts

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ const MAX_DIMENSION_COUNT = 9;
99

1010
const consoleSpy = jest.spyOn(console, 'log').mockImplementation();
1111

12+
interface LooseObject {
13+
[key: string]: string
14+
}
15+
1216
describe('Class: Metrics', () => {
1317

1418
const originalEnvironmentVariables = process.env;
@@ -75,6 +79,39 @@ describe('Class: Metrics', () => {
7579
expect(e.message).toBe(`Max dimension count of ${MAX_DIMENSION_COUNT} hit`);
7680
}
7781
});
82+
83+
test('Additional bulk dimensions should be added correctly', () => {
84+
const additionalDimensions: LooseObject = { 'dimension2': 'dimension2Value', 'dimension3': 'dimension3Value' };
85+
const metrics = new Metrics({ namespace: 'test' });
86+
87+
metrics.addMetric('test_name', MetricUnits.Seconds, 10);
88+
metrics.addDimensions(additionalDimensions);
89+
const loggedData = metrics.serializeMetrics();
90+
91+
expect(loggedData._aws.CloudWatchMetrics[0].Dimensions[0].length).toEqual(2);
92+
Object.keys(additionalDimensions).forEach((key) => {
93+
94+
expect(loggedData[key]).toEqual(additionalDimensions[key]);
95+
});
96+
});
97+
98+
test('Bulk Adding more than max dimensions should throw error', () => {
99+
expect.assertions(1);
100+
const metrics = new Metrics();
101+
const additionalDimensions: LooseObject = {};
102+
103+
metrics.addDimension(`Dimension-Initial`, `Dimension-InitialValue`);
104+
for (let x =0; x < MAX_DIMENSION_COUNT ; x++) {
105+
additionalDimensions[`dimension${x}`] = `dimension${x}Value`;
106+
}
107+
108+
try {
109+
metrics.addDimensions(additionalDimensions);
110+
}
111+
catch (e) {
112+
expect(e.message).toBe(`Adding ${Object.keys(additionalDimensions).length} dimensions would exceed max dimension count of ${MAX_DIMENSION_COUNT}`);
113+
}
114+
});
78115
});
79116

80117
describe('Feature: Metadata', () => {
@@ -100,10 +137,6 @@ describe('Class: Metrics', () => {
100137
test('Adding more than max default dimensions should throw error', () => {
101138
expect.assertions(1);
102139

103-
interface LooseObject {
104-
[key: string]: string
105-
}
106-
107140
const defaultDimensions: LooseObject = {};
108141
for (let x = 0; x < MAX_DIMENSION_COUNT + 1; x ++) {
109142
defaultDimensions[`dimension-${x}`] = `value-${x}`;

0 commit comments

Comments
 (0)