Skip to content
Open
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
10 changes: 6 additions & 4 deletions docs/core/metrics.md
Original file line number Diff line number Diff line change
Expand Up @@ -303,9 +303,11 @@ You can add dimensions to your metrics using the `addDimension` method. You can

### Flushing metrics

The `@FlushMetrics` annotation **validates**, **serializes**, and **flushes** all your metrics. During metrics validation,
if no metrics are provided no exception will be raised. If metrics are provided, and any of the following criteria are
not met, `IllegalStateException` or `IllegalArgumentException` exceptions will be raised.
The `@FlushMetrics` annotation **validates**, **serializes**, and **flushes** all your application metrics.
When no application metrics are provided, it logs a warning instead of publishing an empty EMF document.
The `ColdStart` metric may still be published separately when enabled.
If metrics are provided, and any of the following criteria are not met, `IllegalStateException` or
`IllegalArgumentException` exceptions will be raised.

<!-- prettier-ignore-start -->
!!! tip "Metric validation"
Expand All @@ -314,7 +316,7 @@ not met, `IllegalStateException` or `IllegalArgumentException` exceptions will b
- Metric values must be valid numbers
<!-- prettier-ignore-end -->

If you want to ensure that at least one metric is emitted, you can pass `raiseOnEmptyMetrics = true` to the `@FlushMetrics` annotation:
If you want to ensure that at least one application metric is emitted, you can pass `raiseOnEmptyMetrics = true` to the `@FlushMetrics` annotation. The separately published `ColdStart` metric does not satisfy this check:

=== "MetricsRaiseOnEmpty.java"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ public void flush() {
if (raiseOnEmptyMetrics.get()) {
throw new IllegalStateException("No metrics were emitted");
} else {
LOGGER.warn("No metrics were emitted");
LOGGER.warn("No application metrics to publish. The cold-start metric may be published if enabled.");
}
} else {
emfLogger.flush();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,8 @@
import static org.assertj.core.api.Assertions.assertThatThrownBy;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.PrintStream;
import java.lang.reflect.Method;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.time.Instant;
import java.util.stream.Stream;

Expand Down Expand Up @@ -391,22 +388,27 @@ void shouldRaiseExceptionOnEmptyMetrics() {
}

@Test
void shouldLogWarningOnEmptyMetrics() throws Exception {
// Given
File logFile = new File("target/metrics-test.log");

// When
// Flushing without adding metrics
void shouldNotPublishEmptyApplicationMetrics() {
metrics.flush();

// Then
// Read the log file and check for the warning
String logContent = Files.readString(logFile.toPath(), StandardCharsets.UTF_8);
assertThat(logContent).contains("No metrics were emitted");
// No EMF output should be generated
assertThat(outputStreamCaptor.toString().trim()).isEmpty();
}

@Test
void shouldRejectEmptyApplicationMetricsAfterPublishingColdStart() throws Exception {
metrics.setRaiseOnEmptyMetrics(true);
metrics.captureColdStartMetric();

String coldStartOutput = outputStreamCaptor.toString().trim();
JsonNode coldStartMetric = objectMapper.readTree(coldStartOutput);
assertThat(coldStartMetric.get("ColdStart").asDouble()).isEqualTo(1.0);

assertThatThrownBy(metrics::flush)
.isInstanceOf(IllegalStateException.class)
.hasMessage("No metrics were emitted");
assertThat(outputStreamCaptor.toString().trim()).isEqualTo(coldStartOutput);
}

@Test
void shouldClearDefaultDimensions() throws Exception {
// Given
Expand Down