Skip to content

Commit 09ea473

Browse files
author
Aaron Michael Lamb
committed
fix: change putDimensions to update latest key-value for duplicates
This change ensures new dimension key-values are used for the EMF root node by moving duplicates 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 09ea473

File tree

2 files changed

+46
-13
lines changed

2 files changed

+46
-13
lines changed

src/logger/MetricsContext.ts

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -107,27 +107,26 @@ export class MetricsContext {
107107
/**
108108
* Adds a new set of dimensions. Any time a new dimensions set
109109
* is added, the set is first prepended by the default dimensions.
110+
* Duplicate dimensions sets are removed before being added to the end of the collection.
111+
* This ensures the latest dimension key-value is used as a target member on the root EMF node.
110112
*
111113
* @param dimensions
112114
*/
113115
public putDimensions(incomingDimensionSet: Record<string, string>): void {
114-
const incomingDimensionSetKeys = Object.keys(incomingDimensionSet);
115-
116+
// Duplicate dimensions sets are removed before being added to the end of the collection.
117+
// This ensures the latest dimension key-value is used as a target member on the root EMF node.
116118
// This operation is O(n^2), but acceptable given sets are capped at 10 dimensions
117-
const doesDimensionSetExist = this.dimensions.some(existingDimensionSet => {
119+
const incomingDimensionSetKeys = Object.keys(incomingDimensionSet);
120+
this.dimensions = this.dimensions.filter(existingDimensionSet => {
118121
const existingDimensionSetKeys = Object.keys(existingDimensionSet);
119122
if (existingDimensionSetKeys.length !== incomingDimensionSetKeys.length) {
120-
return false;
123+
return true;
121124
}
122-
return existingDimensionSetKeys.every(existingDimensionSetKey =>
125+
return !existingDimensionSetKeys.every(existingDimensionSetKey =>
123126
incomingDimensionSetKeys.includes(existingDimensionSetKey),
124127
);
125128
});
126129

127-
if (doesDimensionSetExist) {
128-
return;
129-
}
130-
131130
this.dimensions.push(incomingDimensionSet);
132131
}
133132

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 order dimensions collection 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)