|
| 1 | +/* |
| 2 | + * Copyright 2023 Amazon.com, Inc. or its affiliates. |
| 3 | + * Licensed under the Apache License, Version 2.0 (the |
| 4 | + * "License"); you may not use this file except in compliance |
| 5 | + * with the License. You may obtain a copy of the License at |
| 6 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | + * Unless required by applicable law or agreed to in writing, software |
| 8 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 9 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 10 | + * See the License for the specific language governing permissions and |
| 11 | + * limitations under the License. |
| 12 | + * |
| 13 | + */ |
| 14 | + |
| 15 | +package software.amazon.lambda.powertools.metrics; |
| 16 | + |
| 17 | +import static org.assertj.core.api.Assertions.assertThat; |
| 18 | + |
| 19 | +import java.io.ByteArrayOutputStream; |
| 20 | +import java.io.PrintStream; |
| 21 | +import java.lang.reflect.Method; |
| 22 | +import java.util.HashMap; |
| 23 | +import java.util.Map; |
| 24 | + |
| 25 | +import org.junit.jupiter.api.AfterEach; |
| 26 | +import org.junit.jupiter.api.BeforeEach; |
| 27 | +import org.junit.jupiter.api.Test; |
| 28 | +import org.junitpioneer.jupiter.SetEnvironmentVariable; |
| 29 | + |
| 30 | +import com.amazonaws.services.lambda.runtime.Context; |
| 31 | +import com.amazonaws.services.lambda.runtime.RequestHandler; |
| 32 | +import com.fasterxml.jackson.databind.JsonNode; |
| 33 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 34 | + |
| 35 | +import software.amazon.lambda.powertools.common.internal.LambdaHandlerProcessor; |
| 36 | +import software.amazon.lambda.powertools.metrics.model.MetricUnit; |
| 37 | +import software.amazon.lambda.powertools.metrics.testutils.TestContext; |
| 38 | + |
| 39 | +/** |
| 40 | + * Tests to verify the hierarchy of precedence for configuration: |
| 41 | + * 1. Metrics annotation |
| 42 | + * 2. MetricsLoggerBuilder |
| 43 | + * 3. Environment variables |
| 44 | + */ |
| 45 | +class ConfigurationPrecedenceTest { |
| 46 | + |
| 47 | + private final PrintStream standardOut = System.out; |
| 48 | + private final ByteArrayOutputStream outputStreamCaptor = new ByteArrayOutputStream(); |
| 49 | + private final ObjectMapper objectMapper = new ObjectMapper(); |
| 50 | + |
| 51 | + @BeforeEach |
| 52 | + void setUp() throws Exception { |
| 53 | + System.setOut(new PrintStream(outputStreamCaptor)); |
| 54 | + |
| 55 | + // Reset LambdaHandlerProcessor's SERVICE_NAME |
| 56 | + Method resetServiceName = LambdaHandlerProcessor.class.getDeclaredMethod("resetServiceName"); |
| 57 | + resetServiceName.setAccessible(true); |
| 58 | + resetServiceName.invoke(null); |
| 59 | + |
| 60 | + // Reset IS_COLD_START |
| 61 | + java.lang.reflect.Field coldStartField = LambdaHandlerProcessor.class.getDeclaredField("IS_COLD_START"); |
| 62 | + coldStartField.setAccessible(true); |
| 63 | + coldStartField.set(null, null); |
| 64 | + } |
| 65 | + |
| 66 | + @AfterEach |
| 67 | + void tearDown() throws Exception { |
| 68 | + System.setOut(standardOut); |
| 69 | + |
| 70 | + // Reset the singleton state between tests |
| 71 | + java.lang.reflect.Field field = MetricsLoggerFactory.class.getDeclaredField("metricsLogger"); |
| 72 | + field.setAccessible(true); |
| 73 | + field.set(null, null); |
| 74 | + |
| 75 | + field = MetricsLoggerFactory.class.getDeclaredField("provider"); |
| 76 | + field.setAccessible(true); |
| 77 | + field.set(null, new software.amazon.lambda.powertools.metrics.provider.EmfMetricsProvider()); |
| 78 | + } |
| 79 | + |
| 80 | + @Test |
| 81 | + @SetEnvironmentVariable(key = "POWERTOOLS_METRICS_NAMESPACE", value = "EnvNamespace") |
| 82 | + @SetEnvironmentVariable(key = "POWERTOOLS_SERVICE_NAME", value = "EnvService") |
| 83 | + void annotationShouldOverrideBuilderAndEnvironment() throws Exception { |
| 84 | + // Given |
| 85 | + // Configure with builder first |
| 86 | + MetricsLoggerBuilder.builder() |
| 87 | + .withNamespace("BuilderNamespace") |
| 88 | + .withService("BuilderService") |
| 89 | + .build(); |
| 90 | + |
| 91 | + RequestHandler<Map<String, Object>, String> handler = new HandlerWithMetricsAnnotation(); |
| 92 | + Context context = new TestContext(); |
| 93 | + Map<String, Object> input = new HashMap<>(); |
| 94 | + |
| 95 | + // When |
| 96 | + handler.handleRequest(input, context); |
| 97 | + |
| 98 | + // Then |
| 99 | + String emfOutput = outputStreamCaptor.toString().trim(); |
| 100 | + JsonNode rootNode = objectMapper.readTree(emfOutput); |
| 101 | + |
| 102 | + // Annotation values should take precedence |
| 103 | + assertThat(rootNode.get("_aws").get("CloudWatchMetrics").get(0).get("Namespace").asText()) |
| 104 | + .isEqualTo("AnnotationNamespace"); |
| 105 | + assertThat(rootNode.has("Service")).isTrue(); |
| 106 | + assertThat(rootNode.get("Service").asText()).isEqualTo("AnnotationService"); |
| 107 | + } |
| 108 | + |
| 109 | + @Test |
| 110 | + @SetEnvironmentVariable(key = "POWERTOOLS_METRICS_NAMESPACE", value = "EnvNamespace") |
| 111 | + @SetEnvironmentVariable(key = "POWERTOOLS_SERVICE_NAME", value = "EnvService") |
| 112 | + void builderShouldOverrideEnvironment() throws Exception { |
| 113 | + // Given |
| 114 | + // Configure with builder |
| 115 | + MetricsLoggerBuilder.builder() |
| 116 | + .withNamespace("BuilderNamespace") |
| 117 | + .withService("BuilderService") |
| 118 | + .build(); |
| 119 | + |
| 120 | + RequestHandler<Map<String, Object>, String> handler = new HandlerWithDefaultMetricsAnnotation(); |
| 121 | + Context context = new TestContext(); |
| 122 | + Map<String, Object> input = new HashMap<>(); |
| 123 | + |
| 124 | + // When |
| 125 | + handler.handleRequest(input, context); |
| 126 | + |
| 127 | + // Then |
| 128 | + String emfOutput = outputStreamCaptor.toString().trim(); |
| 129 | + JsonNode rootNode = objectMapper.readTree(emfOutput); |
| 130 | + |
| 131 | + // Builder values should take precedence over environment variables |
| 132 | + assertThat(rootNode.get("_aws").get("CloudWatchMetrics").get(0).get("Namespace").asText()) |
| 133 | + .isEqualTo("BuilderNamespace"); |
| 134 | + assertThat(rootNode.has("Service")).isTrue(); |
| 135 | + assertThat(rootNode.get("Service").asText()).isEqualTo("BuilderService"); |
| 136 | + } |
| 137 | + |
| 138 | + @Test |
| 139 | + @SetEnvironmentVariable(key = "POWERTOOLS_METRICS_NAMESPACE", value = "EnvNamespace") |
| 140 | + @SetEnvironmentVariable(key = "POWERTOOLS_SERVICE_NAME", value = "EnvService") |
| 141 | + void environmentVariablesShouldBeUsedWhenNoOverrides() throws Exception { |
| 142 | + // Given |
| 143 | + RequestHandler<Map<String, Object>, String> handler = new HandlerWithDefaultMetricsAnnotation(); |
| 144 | + Context context = new TestContext(); |
| 145 | + Map<String, Object> input = new HashMap<>(); |
| 146 | + |
| 147 | + // When |
| 148 | + handler.handleRequest(input, context); |
| 149 | + |
| 150 | + // Then |
| 151 | + String emfOutput = outputStreamCaptor.toString().trim(); |
| 152 | + JsonNode rootNode = objectMapper.readTree(emfOutput); |
| 153 | + |
| 154 | + // Environment variable values should be used |
| 155 | + assertThat(rootNode.get("_aws").get("CloudWatchMetrics").get(0).get("Namespace").asText()) |
| 156 | + .isEqualTo("EnvNamespace"); |
| 157 | + assertThat(rootNode.has("Service")).isTrue(); |
| 158 | + assertThat(rootNode.get("Service").asText()).isEqualTo("EnvService"); |
| 159 | + } |
| 160 | + |
| 161 | + @Test |
| 162 | + void shouldUseDefaultsWhenNoConfiguration() throws Exception { |
| 163 | + // Given |
| 164 | + RequestHandler<Map<String, Object>, String> handler = new HandlerWithDefaultMetricsAnnotation(); |
| 165 | + Context context = new TestContext(); |
| 166 | + Map<String, Object> input = new HashMap<>(); |
| 167 | + |
| 168 | + // When |
| 169 | + handler.handleRequest(input, context); |
| 170 | + |
| 171 | + // Then |
| 172 | + String emfOutput = outputStreamCaptor.toString().trim(); |
| 173 | + JsonNode rootNode = objectMapper.readTree(emfOutput); |
| 174 | + |
| 175 | + // Default values should be used |
| 176 | + assertThat(rootNode.get("_aws").get("CloudWatchMetrics").get(0).get("Namespace").asText()) |
| 177 | + .isEqualTo("aws-embedded-metrics"); |
| 178 | + assertThat(rootNode.has("Service")).isTrue(); |
| 179 | + assertThat(rootNode.get("Service").asText()).isEqualTo("service_undefined"); |
| 180 | + } |
| 181 | + |
| 182 | + private static class HandlerWithMetricsAnnotation implements RequestHandler<Map<String, Object>, String> { |
| 183 | + @Override |
| 184 | + @Metrics(namespace = "AnnotationNamespace", service = "AnnotationService") |
| 185 | + public String handleRequest(Map<String, Object> input, Context context) { |
| 186 | + MetricsLogger metricsLogger = MetricsLoggerFactory.getMetricsLogger(); |
| 187 | + metricsLogger.addMetric("test-metric", 100, MetricUnit.COUNT); |
| 188 | + return "OK"; |
| 189 | + } |
| 190 | + } |
| 191 | + |
| 192 | + private static class HandlerWithDefaultMetricsAnnotation implements RequestHandler<Map<String, Object>, String> { |
| 193 | + @Override |
| 194 | + @Metrics |
| 195 | + public String handleRequest(Map<String, Object> input, Context context) { |
| 196 | + MetricsLogger metricsLogger = MetricsLoggerFactory.getMetricsLogger(); |
| 197 | + metricsLogger.addMetric("test-metric", 100, MetricUnit.COUNT); |
| 198 | + return "OK"; |
| 199 | + } |
| 200 | + } |
| 201 | + |
| 202 | +} |
0 commit comments