|
| 1 | +package software.amazon.lambda.powertools.metrics; |
| 2 | + |
| 3 | +import static org.assertj.core.api.Assertions.assertThat; |
| 4 | + |
| 5 | +import java.io.ByteArrayOutputStream; |
| 6 | +import java.io.PrintStream; |
| 7 | +import java.util.HashMap; |
| 8 | +import java.util.Map; |
| 9 | + |
| 10 | +import org.junit.jupiter.api.AfterEach; |
| 11 | +import org.junit.jupiter.api.BeforeEach; |
| 12 | +import org.junit.jupiter.api.Test; |
| 13 | +import org.junit.jupiter.api.extension.ExtendWith; |
| 14 | +import org.junitpioneer.jupiter.SetEnvironmentVariable; |
| 15 | +import org.mockito.Mock; |
| 16 | +import org.mockito.junit.jupiter.MockitoExtension; |
| 17 | + |
| 18 | +import com.amazonaws.services.lambda.runtime.Context; |
| 19 | +import com.amazonaws.services.lambda.runtime.RequestHandler; |
| 20 | +import com.fasterxml.jackson.databind.JsonNode; |
| 21 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 22 | + |
| 23 | +import software.amazon.lambda.powertools.metrics.model.MetricUnit; |
| 24 | + |
| 25 | +@ExtendWith(MockitoExtension.class) |
| 26 | +class RequestHandlerTest { |
| 27 | + |
| 28 | + // For developer convenience, no exceptions should be thrown when using a plain Lambda Context mock |
| 29 | + @Mock |
| 30 | + Context lambdaContext; |
| 31 | + |
| 32 | + private final PrintStream standardOut = System.out; |
| 33 | + private ByteArrayOutputStream outputStreamCaptor; |
| 34 | + private final ObjectMapper objectMapper = new ObjectMapper(); |
| 35 | + |
| 36 | + @BeforeEach |
| 37 | + void setUp() { |
| 38 | + outputStreamCaptor = new ByteArrayOutputStream(); |
| 39 | + System.setOut(new PrintStream(outputStreamCaptor)); |
| 40 | + } |
| 41 | + |
| 42 | + @AfterEach |
| 43 | + void tearDown() throws Exception { |
| 44 | + System.setOut(standardOut); |
| 45 | + |
| 46 | + // Reset the singleton state between tests |
| 47 | + java.lang.reflect.Field field = MetricsFactory.class.getDeclaredField("metrics"); |
| 48 | + field.setAccessible(true); |
| 49 | + field.set(null, null); |
| 50 | + |
| 51 | + field = MetricsFactory.class.getDeclaredField("provider"); |
| 52 | + field.setAccessible(true); |
| 53 | + field.set(null, new software.amazon.lambda.powertools.metrics.provider.EmfMetricsProvider()); |
| 54 | + } |
| 55 | + |
| 56 | + @Test |
| 57 | + void shouldCaptureMetricsFromAnnotatedHandler() throws Exception { |
| 58 | + // Given |
| 59 | + RequestHandler<Map<String, Object>, String> handler = new HandlerWithMetricsAnnotation(); |
| 60 | + Map<String, Object> input = new HashMap<>(); |
| 61 | + |
| 62 | + // When |
| 63 | + handler.handleRequest(input, lambdaContext); |
| 64 | + |
| 65 | + // Then |
| 66 | + String emfOutput = outputStreamCaptor.toString().trim(); |
| 67 | + JsonNode rootNode = objectMapper.readTree(emfOutput); |
| 68 | + |
| 69 | + assertThat(rootNode.has("test-metric")).isTrue(); |
| 70 | + assertThat(rootNode.get("test-metric").asDouble()).isEqualTo(100.0); |
| 71 | + assertThat(rootNode.get("_aws").get("CloudWatchMetrics").get(0).get("Namespace").asText()) |
| 72 | + .isEqualTo("CustomNamespace"); |
| 73 | + assertThat(rootNode.has("Service")).isTrue(); |
| 74 | + assertThat(rootNode.get("Service").asText()).isEqualTo("CustomService"); |
| 75 | + } |
| 76 | + |
| 77 | + @SetEnvironmentVariable(key = "POWERTOOLS_METRICS_DISABLED", value = "true") |
| 78 | + @Test |
| 79 | + void shouldNotCaptureMetricsWhenDisabled() { |
| 80 | + // Given |
| 81 | + RequestHandler<Map<String, Object>, String> handler = new HandlerWithMetricsAnnotation(); |
| 82 | + Map<String, Object> input = new HashMap<>(); |
| 83 | + |
| 84 | + // When |
| 85 | + handler.handleRequest(input, lambdaContext); |
| 86 | + |
| 87 | + // Then |
| 88 | + String emfOutput = outputStreamCaptor.toString().trim(); |
| 89 | + assertThat(emfOutput).isEmpty(); |
| 90 | + } |
| 91 | + |
| 92 | + static class HandlerWithMetricsAnnotation implements RequestHandler<Map<String, Object>, String> { |
| 93 | + @Override |
| 94 | + @FlushMetrics(namespace = "CustomNamespace", service = "CustomService") |
| 95 | + public String handleRequest(Map<String, Object> input, Context context) { |
| 96 | + Metrics metrics = MetricsFactory.getMetricsInstance(); |
| 97 | + metrics.addMetric("test-metric", 100, MetricUnit.COUNT); |
| 98 | + return "OK"; |
| 99 | + } |
| 100 | + } |
| 101 | +} |
0 commit comments