File tree Expand file tree Collapse file tree 3 files changed +35
-2
lines changed
main/java/software/amazon/lambda/powertools/metrics/internal
java/software/amazon/lambda/powertools/metrics/internal Expand file tree Collapse file tree 3 files changed +35
-2
lines changed Original file line number Diff line number Diff line change 2222import java .util .Map ;
2323import java .util .concurrent .atomic .AtomicBoolean ;
2424
25+ import org .slf4j .Logger ;
26+ import org .slf4j .LoggerFactory ;
27+
2528import com .amazonaws .services .lambda .runtime .Context ;
2629
2730import software .amazon .cloudwatchlogs .emf .environment .EnvironmentProvider ;
3841 * library {@link software.amazon.cloudwatchlogs.emf.logger.MetricsLogger}.
3942 */
4043public 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 }
Original file line number Diff line number Diff line change 1818import static org .assertj .core .api .Assertions .assertThatThrownBy ;
1919
2020import java .io .ByteArrayOutputStream ;
21+ import java .io .File ;
2122import java .io .PrintStream ;
2223import java .lang .reflect .Method ;
24+ import java .nio .charset .StandardCharsets ;
25+ import java .nio .file .Files ;
2326import java .util .stream .Stream ;
2427
2528import 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
Original file line number Diff line number Diff line change 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
You can’t perform that action at this time.
0 commit comments