Skip to content

Commit a9705f2

Browse files
author
Aaron Michael Lamb
committed
fix: change putDimensions to update/sort existing dimension sets when duplicate
This change ensures new dimension key-values are used for the EMF root node by removing duplicate dimension sets and adding input dimension set to the end of the collection. Example: ``` [ { "keyA": "value1" }, { "keyA": "value2" }, ] // expected EMF target member: { "keyA": "value2 } ``` [TESTING] Updated unit tests to check for this chase wherein putDimensions may be triggered using various dimension set lengths, values, and order.
1 parent 8a07c48 commit a9705f2

File tree

2 files changed

+44
-13
lines changed

2 files changed

+44
-13
lines changed

src/logger/MetricsContext.ts

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -111,23 +111,20 @@ export class MetricsContext {
111111
* @param dimensions
112112
*/
113113
public putDimensions(incomingDimensionSet: Record<string, string>): void {
114-
const incomingDimensionSetKeys = Object.keys(incomingDimensionSet);
115-
114+
// Duplicate dimensions sets are removed before being added to the end of the collection.
115+
// This ensures the latest dimension key-value is used as a target member on the root EMF node.
116116
// This operation is O(n^2), but acceptable given sets are capped at 10 dimensions
117-
const doesDimensionSetExist = this.dimensions.some(existingDimensionSet => {
117+
const incomingDimensionSetKeys = Object.keys(incomingDimensionSet);
118+
this.dimensions = this.dimensions.filter(existingDimensionSet => {
118119
const existingDimensionSetKeys = Object.keys(existingDimensionSet);
119120
if (existingDimensionSetKeys.length !== incomingDimensionSetKeys.length) {
120-
return false;
121+
return true;
121122
}
122-
return existingDimensionSetKeys.every(existingDimensionSetKey =>
123+
return !existingDimensionSetKeys.every(existingDimensionSetKey =>
123124
incomingDimensionSetKeys.includes(existingDimensionSetKey),
124125
);
125126
});
126127

127-
if (doesDimensionSetExist) {
128-
return;
129-
}
130-
131128
this.dimensions.push(incomingDimensionSet);
132129
}
133130

src/logger/__tests__/MetricsContext.test.ts

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,25 +50,59 @@ test('putDimensions will not duplicate dimensions', () => {
5050
const context = MetricsContext.empty();
5151
const dimension1 = faker.random.word();
5252
const dimension2 = faker.random.word();
53-
const expectedDimension1 = { dimension1 };
54-
const expectedDimension2 = { dimension1, dimension2 };
55-
const expectedDimension3 = { dimension2 };
53+
const expectedDimension1 = {};
54+
const expectedDimension2 = { dimension1 };
55+
const expectedDimension3 = { dimension2, dimension1 };
56+
const expectedDimension4 = { dimension2 };
5657

5758
// act
59+
context.putDimensions({});
5860
context.putDimensions({ dimension1 });
5961
context.putDimensions({ dimension1, dimension2 });
6062
context.putDimensions({ dimension2, dimension1 });
6163
context.putDimensions({ dimension2 });
64+
context.putDimensions({});
6265
context.putDimensions({ dimension1 });
6366
context.putDimensions({ dimension1, dimension2 });
6467
context.putDimensions({ dimension2, dimension1 });
6568
context.putDimensions({ dimension2 });
6669

6770
// assert
68-
expect(context.getDimensions().length).toBe(3);
71+
expect(context.getDimensions().length).toBe(4);
6972
expect(context.getDimensions()[0]).toStrictEqual(expectedDimension1);
7073
expect(context.getDimensions()[1]).toStrictEqual(expectedDimension2);
7174
expect(context.getDimensions()[2]).toStrictEqual(expectedDimension3);
75+
expect(context.getDimensions()[3]).toStrictEqual(expectedDimension4);
76+
});
77+
78+
test('putDimensions will sort dimensions correctly', () => {
79+
// arrange
80+
const context = MetricsContext.empty();
81+
const dimension1 = faker.random.word();
82+
const dimension2 = faker.random.word();
83+
const expectedDimension1 = { dimension2, dimension1 };
84+
const expectedDimension2 = { dimension2 };
85+
const expectedDimension3 = { dimension1 };
86+
const expectedDimension4 = {};
87+
88+
// act
89+
context.putDimensions({});
90+
context.putDimensions({ dimension1 });
91+
context.putDimensions({ dimension1, dimension2 });
92+
context.putDimensions({ dimension2, dimension1 });
93+
context.putDimensions({ dimension2 });
94+
context.putDimensions({ dimension1, dimension2 });
95+
context.putDimensions({ dimension2, dimension1 });
96+
context.putDimensions({ dimension2 });
97+
context.putDimensions({ dimension1 });
98+
context.putDimensions({});
99+
100+
// assert
101+
expect(context.getDimensions().length).toBe(4);
102+
expect(context.getDimensions()[0]).toStrictEqual(expectedDimension1);
103+
expect(context.getDimensions()[1]).toStrictEqual(expectedDimension2);
104+
expect(context.getDimensions()[2]).toStrictEqual(expectedDimension3);
105+
expect(context.getDimensions()[3]).toStrictEqual(expectedDimension4);
72106
});
73107

74108
test('getDimensions returns default dimensions if custom dimensions not set', () => {

0 commit comments

Comments
 (0)