Skip to content

Commit a97906c

Browse files
committed
fix addDimension wrongly adding to defaultDimensions
1 parent 4730be4 commit a97906c

File tree

2 files changed

+5
-3
lines changed

2 files changed

+5
-3
lines changed

powertools-metrics/src/main/java/software/amazon/lambda/powertools/metrics/internal/EmfMetricsLogger.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,6 @@ public void addDimension(software.amazon.lambda.powertools.metrics.model.Dimensi
8181
dimensionSet.getDimensions().forEach((key, val) -> {
8282
try {
8383
emfDimensionSet.addDimension(key, val);
84-
// Update our local copy of default dimensions
85-
defaultDimensions.put(key, val);
8684
} catch (Exception e) {
8785
// Ignore dimension errors
8886
}
@@ -234,7 +232,9 @@ public void flushMetrics(Consumer<Metrics> metricsConsumer) {
234232
if (namespace != null) {
235233
metrics.setNamespace(this.namespace);
236234
}
237-
defaultDimensions.forEach(metrics::addDimension);
235+
if (!defaultDimensions.isEmpty()) {
236+
metrics.setDefaultDimensions(software.amazon.lambda.powertools.metrics.model.DimensionSet.of(defaultDimensions));
237+
}
238238
metadata.forEach(metrics::addMetadata);
239239

240240
metricsConsumer.accept(metrics);

powertools-metrics/src/test/java/software/amazon/lambda/powertools/metrics/internal/EmfMetricsLoggerTest.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -451,6 +451,7 @@ void shouldFlushMetrics() throws Exception {
451451
// Given
452452
metrics.setNamespace("MainNamespace");
453453
metrics.setDefaultDimensions(DimensionSet.of("CustomDim", "CustomValue"));
454+
metrics.addDimension(DimensionSet.of("CustomDim2", "CustomValue2"));
454455
metrics.addMetadata("CustomMetadata", "MetadataValue");
455456

456457
// When
@@ -469,6 +470,7 @@ void shouldFlushMetrics() throws Exception {
469470
assertThat(rootNode.get("metric-two").asDouble()).isEqualTo(100);
470471
assertThat(rootNode.has("CustomDim")).isTrue();
471472
assertThat(rootNode.get("CustomDim").asText()).isEqualTo("CustomValue");
473+
assertThat(rootNode.get("CustomDim2")).isNull();
472474
assertThat(rootNode.get("_aws").get("CloudWatchMetrics").get(0).get("Namespace").asText())
473475
.isEqualTo("MainNamespace");
474476
assertThat(rootNode.get("_aws").has("CustomMetadata")).isTrue();

0 commit comments

Comments
 (0)