|
6 | 6 |
|
7 | 7 | import com.fasterxml.jackson.core.JsonProcessingException; |
8 | 8 | import com.fasterxml.jackson.databind.ObjectMapper; |
9 | | -import org.assertj.core.api.Assertions; |
10 | 9 | import org.junit.jupiter.api.AfterEach; |
11 | 10 | import org.junit.jupiter.api.BeforeAll; |
12 | 11 | import org.junit.jupiter.api.BeforeEach; |
|
16 | 15 | import software.amazon.cloudwatchlogs.emf.model.DimensionSet; |
17 | 16 | import software.amazon.cloudwatchlogs.emf.model.Unit; |
18 | 17 |
|
19 | | -import static java.util.Collections.*; |
20 | | -import static org.assertj.core.api.Assertions.*; |
| 18 | +import static java.util.Collections.emptyMap; |
21 | 19 | import static org.assertj.core.api.Assertions.assertThat; |
| 20 | +import static org.assertj.core.api.Assertions.assertThatNullPointerException; |
22 | 21 | import static org.mockito.Mockito.mockStatic; |
23 | 22 | import static software.amazon.lambda.powertools.core.internal.SystemWrapper.getenv; |
24 | 23 |
|
@@ -123,6 +122,38 @@ void singleMetricsCaptureUtilityWithDefaultNameSpace() { |
123 | 122 | } |
124 | 123 | } |
125 | 124 |
|
| 125 | + @Test |
| 126 | + void metricsLoggerCaptureUtilityWithDefaultNameSpace() { |
| 127 | + try (MockedStatic<SystemWrapper> mocked = mockStatic(SystemWrapper.class); |
| 128 | + MockedStatic<software.amazon.lambda.powertools.core.internal.SystemWrapper> internalWrapper = mockStatic(software.amazon.lambda.powertools.core.internal.SystemWrapper.class)) { |
| 129 | + mocked.when(() -> SystemWrapper.getenv("AWS_EMF_ENVIRONMENT")).thenReturn("Lambda"); |
| 130 | + mocked.when(() -> SystemWrapper.getenv("POWERTOOLS_METRICS_NAMESPACE")).thenReturn("GlobalName"); |
| 131 | + internalWrapper.when(() -> getenv("_X_AMZN_TRACE_ID")).thenReturn("Root=1-5759e988-bd862e3fe1be46a994272793;Parent=53995c3f42cd8ad8;Sampled=1\""); |
| 132 | + |
| 133 | + MetricsUtils.withMetricLogger(metricsLogger -> { |
| 134 | + metricsLogger.setDimensions(DimensionSet.of("Dimension1", "Value1")); |
| 135 | + metricsLogger.putMetric("Metric1", 1, Unit.COUNT); |
| 136 | + }); |
| 137 | + |
| 138 | + assertThat(out.toString()) |
| 139 | + .satisfies(s -> { |
| 140 | + Map<String, Object> logAsJson = readAsJson(s); |
| 141 | + |
| 142 | + assertThat(logAsJson) |
| 143 | + .containsEntry("Metric1", 1.0) |
| 144 | + .containsEntry("Dimension1", "Value1") |
| 145 | + .containsKey("_aws") |
| 146 | + .containsEntry("xray_trace_id", "1-5759e988-bd862e3fe1be46a994272793"); |
| 147 | + |
| 148 | + Map<String, Object> aws = (Map<String, Object>) logAsJson.get("_aws"); |
| 149 | + |
| 150 | + assertThat(aws.get("CloudWatchMetrics")) |
| 151 | + .asString() |
| 152 | + .contains("Namespace=GlobalName"); |
| 153 | + }); |
| 154 | + } |
| 155 | + } |
| 156 | + |
126 | 157 | @Test |
127 | 158 | void shouldThrowExceptionWhenDefaultDimensionIsNull() { |
128 | 159 | assertThatNullPointerException() |
|
0 commit comments