Skip to content

fix(metrics): Do not flush when no metrics were added to avoid printing root-level _aws dict #1891

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 16, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,9 @@
emfDimensionSet.addDimension(key, val);
// Update our local copy of default dimensions
defaultDimensions.put(key, val);
} catch (Exception e) {
// Ignore dimension errors
}

Check failure on line 86 in powertools-metrics/src/main/java/software/amazon/lambda/powertools/metrics/internal/EmfMetricsLogger.java

View workflow job for this annotation

GitHub Actions / pmd_analyse

Avoid empty catch blocks

Empty Catch Block finds instances where an exception is caught, but nothing is done. In most circumstances, this swallows an exception which should either be acted on or reported. EmptyCatchBlock (Priority: 1, Ruleset: Error Prone) https://docs.pmd-code.org/pmd-doc-7.14.0/pmd_rules_java_errorprone.html#emptycatchblock
});

emfLogger.putDimensions(emfDimensionSet);
Expand All @@ -105,9 +105,9 @@
dimensions.forEach((key, value) -> {
try {
emfDimensionSet.addDimension(key, value);
} catch (Exception e) {
// Ignore dimension errors
}

Check failure on line 110 in powertools-metrics/src/main/java/software/amazon/lambda/powertools/metrics/internal/EmfMetricsLogger.java

View workflow job for this annotation

GitHub Actions / pmd_analyse

Avoid empty catch blocks

Empty Catch Block finds instances where an exception is caught, but nothing is done. In most circumstances, this swallows an exception which should either be acted on or reported. EmptyCatchBlock (Priority: 1, Ruleset: Error Prone) https://docs.pmd-code.org/pmd-doc-7.14.0/pmd_rules_java_errorprone.html#emptycatchblock
});
emfLogger.setDimensions(emfDimensionSet);
// Store a copy of the default dimensions
Expand Down Expand Up @@ -164,8 +164,9 @@
} else {
LOGGER.warn("No metrics were emitted");
}
} else {
emfLogger.flush();
}
emfLogger.flush();
}

@Override
Expand Down Expand Up @@ -195,9 +196,9 @@
dimensions.getDimensions().forEach((key, val) -> {
try {
emfDimensionSet.addDimension(key, val);
} catch (Exception e) {
// Ignore dimension errors
}

Check failure on line 201 in powertools-metrics/src/main/java/software/amazon/lambda/powertools/metrics/internal/EmfMetricsLogger.java

View workflow job for this annotation

GitHub Actions / pmd_analyse

Avoid empty catch blocks

Empty Catch Block finds instances where an exception is caught, but nothing is done. In most circumstances, this swallows an exception which should either be acted on or reported. EmptyCatchBlock (Priority: 1, Ruleset: Error Prone) https://docs.pmd-code.org/pmd-doc-7.14.0/pmd_rules_java_errorprone.html#emptycatchblock
});
coldStartLogger.setDimensions(emfDimensionSet);
}
Expand Down Expand Up @@ -248,9 +249,9 @@
dimensions.getDimensions().forEach((key, val) -> {
try {
emfDimensionSet.addDimension(key, val);
} catch (Exception e) {
// Ignore dimension errors
}

Check failure on line 254 in powertools-metrics/src/main/java/software/amazon/lambda/powertools/metrics/internal/EmfMetricsLogger.java

View workflow job for this annotation

GitHub Actions / pmd_analyse

Avoid empty catch blocks

Empty Catch Block finds instances where an exception is caught, but nothing is done. In most circumstances, this swallows an exception which should either be acted on or reported. EmptyCatchBlock (Priority: 1, Ruleset: Error Prone) https://docs.pmd-code.org/pmd-doc-7.14.0/pmd_rules_java_errorprone.html#emptycatchblock
});
singleMetricLogger.setDimensions(emfDimensionSet);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ class EmfMetricsLoggerTest {

private Metrics metrics;
private final ObjectMapper objectMapper = new ObjectMapper();
private final PrintStream standardOut = System.out;
private static final PrintStream standardOut = System.out;
private final ByteArrayOutputStream outputStreamCaptor = new ByteArrayOutputStream();

@BeforeEach
Expand Down Expand Up @@ -180,7 +180,7 @@ void shouldAddDimension() throws Exception {
JsonNode dimensions = rootNode.get("_aws").get("CloudWatchMetrics").get(0).get("Dimensions").get(0);
boolean hasDimension = false;
for (JsonNode dimension : dimensions) {
if (dimension.asText().equals("CustomDimension")) {
if ("CustomDimension".equals(dimension.asText())) {
hasDimension = true;
break;
}
Expand Down Expand Up @@ -233,9 +233,9 @@ void shouldAddDimensionSet() throws Exception {
boolean hasDim2 = false;
for (JsonNode dimension : dimensions) {
String dimName = dimension.asText();
if (dimName.equals("Dim1")) {
if ("Dim1".equals(dimName)) {
hasDim1 = true;
} else if (dimName.equals("Dim2")) {
} else if ("Dim2".equals(dimName)) {
hasDim2 = true;
}
}
Expand Down Expand Up @@ -348,6 +348,8 @@ void shouldLogWarningOnEmptyMetrics() throws Exception {
// Read the log file and check for the warning
String logContent = new String(Files.readAllBytes(logFile.toPath()), StandardCharsets.UTF_8);
assertThat(logContent).contains("No metrics were emitted");
// No EMF output should be generated
assertThat(outputStreamCaptor.toString().trim()).isEmpty();
}

@Test
Expand Down
Loading