Skip to content

Commit 4515ee4

Browse files
committed
Add warning if not emitting metrics and raiseOnEmptyMetrics is false.
1 parent a178128 commit 4515ee4

File tree

3 files changed

+35
-2
lines changed

3 files changed

+35
-2
lines changed

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

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@
2222
import java.util.Map;
2323
import java.util.concurrent.atomic.AtomicBoolean;
2424

25+
import org.slf4j.Logger;
26+
import org.slf4j.LoggerFactory;
27+
2528
import com.amazonaws.services.lambda.runtime.Context;
2629

2730
import software.amazon.cloudwatchlogs.emf.environment.EnvironmentProvider;
@@ -38,6 +41,7 @@
3841
* library {@link software.amazon.cloudwatchlogs.emf.logger.MetricsLogger}.
3942
*/
4043
public class EmfMetricsLogger implements MetricsLogger {
44+
private static final Logger LOGGER = LoggerFactory.getLogger(EmfMetricsLogger.class);
4145
private static final String TRACE_ID_PROPERTY = "xray_trace_id";
4246
private static final String REQUEST_ID_PROPERTY = "function_request_id";
4347
private static final String COLD_START_METRIC = "ColdStart";
@@ -136,8 +140,12 @@ public void clearDefaultDimensions() {
136140

137141
@Override
138142
public void flush() {
139-
if (raiseOnEmptyMetrics && !hasMetrics.get()) {
140-
throw new IllegalStateException("No metrics were emitted");
143+
if (!hasMetrics.get()) {
144+
if (raiseOnEmptyMetrics) {
145+
throw new IllegalStateException("No metrics were emitted");
146+
} else {
147+
LOGGER.warn("No metrics were emitted");
148+
}
141149
}
142150
emfLogger.flush();
143151
}

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,11 @@
1818
import static org.assertj.core.api.Assertions.assertThatThrownBy;
1919

2020
import java.io.ByteArrayOutputStream;
21+
import java.io.File;
2122
import java.io.PrintStream;
2223
import java.lang.reflect.Method;
24+
import java.nio.charset.StandardCharsets;
25+
import java.nio.file.Files;
2326
import java.util.stream.Stream;
2427

2528
import org.junit.jupiter.api.AfterEach;
@@ -310,6 +313,21 @@ void shouldRaiseExceptionOnEmptyMetrics() {
310313
.hasMessage("No metrics were emitted");
311314
}
312315

316+
@Test
317+
void shouldLogWarningOnEmptyMetrics() throws Exception {
318+
// Given
319+
File logFile = new File("target/metrics-test.log");
320+
321+
// When
322+
// Flushing without adding metrics
323+
metricsLogger.flush();
324+
325+
// Then
326+
// Read the log file and check for the warning
327+
String logContent = new String(Files.readAllBytes(logFile.toPath()), StandardCharsets.UTF_8);
328+
assertThat(logContent).contains("No metrics were emitted");
329+
}
330+
313331
@Test
314332
void shouldClearDefaultDimensions() throws Exception {
315333
// Given
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
org.slf4j.simpleLogger.logFile=target/metrics-test.log
2+
org.slf4j.simpleLogger.defaultLogLevel=warn
3+
org.slf4j.simpleLogger.showDateTime=true
4+
org.slf4j.simpleLogger.dateTimeFormat=yyyy-MM-dd HH:mm:ss:SSS
5+
org.slf4j.simpleLogger.showThreadName=false
6+
org.slf4j.simpleLogger.showLogName=true
7+
org.slf4j.simpleLogger.showShortLogName=false

0 commit comments

Comments
 (0)