Skip to content

Commit 3f74911

Browse files
committed
Optimizations
1 parent edeb4a1 commit 3f74911

File tree

1 file changed

+24
-20
lines changed

1 file changed

+24
-20
lines changed

packages/metrics/src/Metrics.ts

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -273,8 +273,12 @@ class Metrics extends Utility implements MetricsInterface {
273273
* @param dimensions - An object with key-value pairs of dimensions
274274
*/
275275
public addDimensions(dimensions: Dimensions): void {
276+
const newDimensionKeys = Object.keys(dimensions).filter(
277+
(key) => !this.defaultDimensions[key] && !this.dimensions[key]
278+
);
279+
276280
if (
277-
Object.keys(dimensions).length + this.getCurrentDimensionsCount() >=
281+
newDimensionKeys.length + this.getCurrentDimensionsCount() >=
278282
MAX_DIMENSION_COUNT
279283
) {
280284
throw new RangeError(
@@ -707,31 +711,28 @@ class Metrics extends Utility implements MetricsInterface {
707711

708712
const dimensionNames = [];
709713

714+
const allDimensionKeys = new Set([
715+
...Object.keys(this.defaultDimensions),
716+
...Object.keys(this.dimensions),
717+
]);
718+
710719
if (Object.keys(this.dimensions).length > 0) {
711-
dimensionNames.push([
712-
...new Set([
713-
...Object.keys(this.defaultDimensions),
714-
...Object.keys(this.dimensions),
715-
]),
716-
]);
720+
dimensionNames.push([...allDimensionKeys]);
717721
}
718722

719723
for (const dimensionSet of this.dimensionSets) {
720-
dimensionNames.push([
721-
...new Set([
722-
...Object.keys(this.defaultDimensions),
723-
...Object.keys(dimensionSet),
724-
]),
724+
const dimensionSetKeys = new Set([
725+
...Object.keys(this.defaultDimensions),
726+
...Object.keys(dimensionSet),
725727
]);
728+
dimensionNames.push([...dimensionSetKeys]);
726729
}
727730

728731
if (
729732
dimensionNames.length === 0 &&
730733
Object.keys(this.defaultDimensions).length > 0
731734
) {
732-
dimensionNames.push([
733-
...new Set([...Object.keys(this.defaultDimensions)]),
734-
]);
735+
dimensionNames.push([...Object.keys(this.defaultDimensions)]);
735736
}
736737

737738
return {
@@ -745,11 +746,14 @@ class Metrics extends Utility implements MetricsInterface {
745746
},
746747
],
747748
},
748-
...this.defaultDimensions,
749-
...this.dimensions,
750-
...this.dimensionSets.reduce((acc, dims) => Object.assign(acc, dims), {}),
751-
...metricValues,
752-
...this.metadata,
749+
...Object.assign(
750+
{},
751+
this.defaultDimensions,
752+
this.dimensions,
753+
this.dimensionSets.reduce((acc, dims) => Object.assign(acc, dims), {}),
754+
metricValues,
755+
this.metadata
756+
),
753757
};
754758
}
755759

0 commit comments

Comments
 (0)