Skip to content

Commit c7c9ed5

Browse files
committed
Add Testing your code section to docs.
1 parent 4637fef commit c7c9ed5

File tree

1 file changed

+94
-0
lines changed

1 file changed

+94
-0
lines changed

docs/core/metrics.md

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -535,3 +535,97 @@ The following example shows how to configure a custom `MetricsLogger` using the
535535
}
536536
}
537537
```
538+
539+
## Testing your code
540+
541+
### Suppressing metrics output
542+
543+
If you would like to suppress metrics output during your unit tests, you can use the `POWERTOOLS_DISABLE_METRICS` environment variable. For example, using Maven you can set in your build plugins:
544+
545+
```xml
546+
<plugin>
547+
<groupId>org.apache.maven.plugins</groupId>
548+
<artifactId>maven-surefire-plugin</artifactId>
549+
<configuration>
550+
<environmentVariables>
551+
<POWERTOOLS_DISABLE_METRICS>true</POWERTOOLS_DISABLE_METRICS>
552+
</environmentVariables>
553+
</configuration>
554+
</plugin>
555+
```
556+
557+
### Asserting EMF output
558+
559+
When unit testing your code, you can run assertions against the output generated by the `MetricsLogger`. For the `EmfMetricsLogger`, you can assert the generated JSON blob following the [CloudWatch EMF specification](https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/CloudWatch_Embedded_Metric_Format.html) against your expected output.
560+
561+
Consider the following example where we redirect the standard output to a custom `PrintStream`. We use the Jackson library to parse the EMF output into a `JsonNode` and run assertions against that.
562+
563+
```java hl_lines="23 28 33 50-55"
564+
import static org.assertj.core.api.Assertions.assertThat;
565+
566+
import java.io.ByteArrayOutputStream;
567+
import java.io.PrintStream;
568+
import java.util.HashMap;
569+
import java.util.Map;
570+
571+
import org.junit.jupiter.api.AfterEach;
572+
import org.junit.jupiter.api.BeforeEach;
573+
import org.junit.jupiter.api.Test;
574+
575+
import com.amazonaws.services.lambda.runtime.Context;
576+
import com.amazonaws.services.lambda.runtime.RequestHandler;
577+
import com.fasterxml.jackson.databind.JsonNode;
578+
import com.fasterxml.jackson.databind.ObjectMapper;
579+
580+
import software.amazon.lambda.powertools.metrics.model.MetricUnit;
581+
import software.amazon.lambda.powertools.metrics.testutils.TestContext;
582+
583+
class MetricsTestExample {
584+
585+
private final PrintStream standardOut = System.out;
586+
private final ByteArrayOutputStream outputStreamCaptor = new ByteArrayOutputStream();
587+
private final ObjectMapper objectMapper = new ObjectMapper();
588+
589+
@BeforeEach
590+
void setUp() {
591+
System.setOut(new PrintStream(outputStreamCaptor));
592+
}
593+
594+
@AfterEach
595+
void tearDown() {
596+
System.setOut(standardOut);
597+
}
598+
599+
@Test
600+
void shouldCaptureMetricsFromAnnotatedHandler() throws Exception {
601+
// Given
602+
RequestHandler<Map<String, Object>, String> handler = new HandlerWithMetricsAnnotation();
603+
Context context = new TestContext();
604+
Map<String, Object> input = new HashMap<>();
605+
606+
// When
607+
handler.handleRequest(input, context);
608+
609+
// Then
610+
String emfOutput = outputStreamCaptor.toString().trim();
611+
JsonNode rootNode = objectMapper.readTree(emfOutput);
612+
613+
assertThat(rootNode.has("test-metric")).isTrue();
614+
assertThat(rootNode.get("test-metric").asDouble()).isEqualTo(100.0);
615+
assertThat(rootNode.get("_aws").get("CloudWatchMetrics").get(0).get("Namespace").asText())
616+
.isEqualTo("CustomNamespace");
617+
assertThat(rootNode.has("Service")).isTrue();
618+
assertThat(rootNode.get("Service").asText()).isEqualTo("CustomService");
619+
}
620+
621+
static class HandlerWithMetricsAnnotation implements RequestHandler<Map<String, Object>, String> {
622+
@Override
623+
@Metrics(namespace = "CustomNamespace", service = "CustomService")
624+
public String handleRequest(Map<String, Object> input, Context context) {
625+
MetricsLogger metricsLogger = MetricsLoggerFactory.getMetricsLogger();
626+
metricsLogger.addMetric("test-metric", 100, MetricUnit.COUNT);
627+
return "OK";
628+
}
629+
}
630+
}
631+
```

0 commit comments

Comments
 (0)