From 63588ff336231278ca9ee986ad3d33761d935630 Mon Sep 17 00:00:00 2001 From: jdoherty07 Date: Fri, 5 Jan 2024 19:45:01 +0000 Subject: [PATCH 1/7] upgraded embedded metrics library and associated tests --- .../sam/src/main/java/helloworld/App.java | 3 ++ pom.xml | 2 +- .../lambda/powertools/e2e/Function.java | 11 +++++- .../amazon/lambda/powertools/e2e/Input.java | 10 +++++ .../amazon/lambda/powertools/MetricsE2ET.java | 38 ++++++++++++++++--- .../powertools/metrics/MetricsLoggerTest.java | 10 +++++ ...rtoolsMetricsTooManyDimensionsHandler.java | 11 +++--- .../internal/LambdaMetricsAspectTest.java | 5 ++- 8 files changed, 76 insertions(+), 14 deletions(-) diff --git a/examples/powertools-examples-core-utilities/sam/src/main/java/helloworld/App.java b/examples/powertools-examples-core-utilities/sam/src/main/java/helloworld/App.java index 36ef72ae7..75e287687 100644 --- a/examples/powertools-examples-core-utilities/sam/src/main/java/helloworld/App.java +++ b/examples/powertools-examples-core-utilities/sam/src/main/java/helloworld/App.java @@ -32,6 +32,7 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import software.amazon.cloudwatchlogs.emf.model.DimensionSet; +import software.amazon.cloudwatchlogs.emf.model.StorageResolution; import software.amazon.cloudwatchlogs.emf.model.Unit; import software.amazon.lambda.powertools.logging.Logging; import software.amazon.lambda.powertools.logging.LoggingUtils; @@ -63,6 +64,8 @@ public APIGatewayProxyResponseEvent handleRequest(final APIGatewayProxyRequestEv metric.setDimensions(DimensionSet.of("AnotherService1", "CustomService1")); }); + metricsLogger().putMetric("CustomMetric3", 1, Unit.COUNT, StorageResolution.HIGH); + LoggingUtils.appendKey("test", "willBeLogged"); APIGatewayProxyResponseEvent response = new APIGatewayProxyResponseEvent() diff --git a/pom.xml b/pom.xml index a4df4116a..e87506759 100644 --- a/pom.xml +++ b/pom.xml @@ -107,7 +107,7 @@ 3.3.0 3.1.0 5.10.0 - 1.0.6 + 4.1.2 0.6.0 1.5.0 diff --git a/powertools-e2e-tests/handlers/metrics/src/main/java/software/amazon/lambda/powertools/e2e/Function.java b/powertools-e2e-tests/handlers/metrics/src/main/java/software/amazon/lambda/powertools/e2e/Function.java index d9cf575c3..03c1d7504 100644 --- a/powertools-e2e-tests/handlers/metrics/src/main/java/software/amazon/lambda/powertools/e2e/Function.java +++ b/powertools-e2e-tests/handlers/metrics/src/main/java/software/amazon/lambda/powertools/e2e/Function.java @@ -18,9 +18,15 @@ import com.amazonaws.services.lambda.runtime.RequestHandler; import software.amazon.cloudwatchlogs.emf.logger.MetricsLogger; import software.amazon.cloudwatchlogs.emf.model.DimensionSet; +import software.amazon.cloudwatchlogs.emf.model.StorageResolution; import software.amazon.cloudwatchlogs.emf.model.Unit; import software.amazon.lambda.powertools.metrics.Metrics; import software.amazon.lambda.powertools.metrics.MetricsUtils; +import java.time.LocalDateTime; +import java.time.ZoneOffset; +import java.time.temporal.ChronoUnit; +import java.time.Instant; + public class Function implements RequestHandler { @@ -29,11 +35,14 @@ public class Function implements RequestHandler { @Metrics(captureColdStart = true) public String handleRequest(Input input, Context context) { + Instant currentTimeTruncatedPlusThirty = LocalDateTime.now().truncatedTo(ChronoUnit.MINUTES).toInstant(ZoneOffset.UTC).plusSeconds(30); + metricsLogger.setTimestamp(currentTimeTruncatedPlusThirty); + DimensionSet dimensionSet = new DimensionSet(); input.getDimensions().forEach((key, value) -> dimensionSet.addDimension(key, value)); metricsLogger.putDimensions(dimensionSet); - input.getMetrics().forEach((key, value) -> metricsLogger.putMetric(key, value, Unit.COUNT)); + input.getMetrics().forEach((key, value) -> metricsLogger.putMetric(key, value, Unit.COUNT,input.getHighResolution().equalsIgnoreCase("true") ? StorageResolution.HIGH : StorageResolution.STANDARD )); return "OK"; } diff --git a/powertools-e2e-tests/handlers/metrics/src/main/java/software/amazon/lambda/powertools/e2e/Input.java b/powertools-e2e-tests/handlers/metrics/src/main/java/software/amazon/lambda/powertools/e2e/Input.java index 18c4eb747..1328ded77 100644 --- a/powertools-e2e-tests/handlers/metrics/src/main/java/software/amazon/lambda/powertools/e2e/Input.java +++ b/powertools-e2e-tests/handlers/metrics/src/main/java/software/amazon/lambda/powertools/e2e/Input.java @@ -21,6 +21,8 @@ public class Input { private Map dimensions; + private String highResolution; + public Input() { } @@ -32,6 +34,14 @@ public void setMetrics(Map metrics) { this.metrics = metrics; } + public String getHighResolution() { + return highResolution; + } + + public void setHighResolution(String highResolution) { + this.highResolution = highResolution; + } + public Map getDimensions() { return dimensions; } diff --git a/powertools-e2e-tests/src/test/java/software/amazon/lambda/powertools/MetricsE2ET.java b/powertools-e2e-tests/src/test/java/software/amazon/lambda/powertools/MetricsE2ET.java index 80673b995..61ef8e583 100644 --- a/powertools-e2e-tests/src/test/java/software/amazon/lambda/powertools/MetricsE2ET.java +++ b/powertools-e2e-tests/src/test/java/software/amazon/lambda/powertools/MetricsE2ET.java @@ -18,6 +18,10 @@ import static software.amazon.lambda.powertools.testutils.Infrastructure.FUNCTION_NAME_OUTPUT; import static software.amazon.lambda.powertools.testutils.lambda.LambdaInvoker.invokeFunction; +import java.time.Instant; +import java.time.LocalDateTime; +import java.time.ZoneOffset; +import java.time.temporal.ChronoUnit; import java.util.Collections; import java.util.List; import java.util.Map; @@ -66,12 +70,19 @@ public static void tearDown() { @Test public void test_recordMetrics() { // GIVEN + + Instant currentTimeTruncatedToMinutes = LocalDateTime.now().truncatedTo(ChronoUnit.MINUTES).toInstant(ZoneOffset.UTC); + String event1 = - "{ \"metrics\": {\"orders\": 1, \"products\": 4}, \"dimensions\": { \"Environment\": \"test\"} }"; + "{ \"metrics\": {\"orders\": 1, \"products\": 4}, \"dimensions\": { \"Environment\": \"test\"}, \"highResolution\": \"false\"}"; + String event2 = + "{ \"metrics\": {\"orders\": 1, \"products\": 8}, \"dimensions\": { \"Environment\": \"test\"}, \"highResolution\": \"true\"}"; // WHEN InvocationResult invocationResult = invokeFunction(functionName, event1); + invokeFunction(functionName, event2); + // THEN MetricsFetcher metricsFetcher = new MetricsFetcher(); List coldStart = @@ -84,18 +95,35 @@ public void test_recordMetrics() { List orderMetrics = metricsFetcher.fetchMetrics(invocationResult.getStart(), invocationResult.getEnd(), 60, namespace, "orders", Collections.singletonMap("Environment", "test")); - assertThat(orderMetrics.get(0)).isEqualTo(1); + assertThat(orderMetrics.get(0)).isEqualTo(2); List productMetrics = metricsFetcher.fetchMetrics(invocationResult.getStart(), invocationResult.getEnd(), 60, namespace, "products", Collections.singletonMap("Environment", "test")); - assertThat(productMetrics.get(0)).isEqualTo(4); + + // When searching across a 1 minute time period with a period of 60 we find both metrics and the sum is 12 + + assertThat(productMetrics.get(0)).isEqualTo(12); + orderMetrics = metricsFetcher.fetchMetrics(invocationResult.getStart(), invocationResult.getEnd(), 60, namespace, "orders", Collections.singletonMap("Service", service)); - assertThat(orderMetrics.get(0)).isEqualTo(1); + assertThat(orderMetrics.get(0)).isEqualTo(2); productMetrics = metricsFetcher.fetchMetrics(invocationResult.getStart(), invocationResult.getEnd(), 60, namespace, "products", Collections.singletonMap("Service", service)); - assertThat(productMetrics.get(0)).isEqualTo(4); + assertThat(productMetrics.get(0)).isEqualTo(12); + + Instant searchStartTime = currentTimeTruncatedToMinutes.plusSeconds(15); + Instant searchEndTime = currentTimeTruncatedToMinutes.plusSeconds(45); + + List productMetricDataResult = + metricsFetcher.fetchMetrics(searchStartTime, searchEndTime, 1, namespace, + "products", Collections.singletonMap("Environment", "test")); + +// We are searching across the time period the metric was created but with a period of 1 second. Only the high resolution metric will be available at this point + + assertThat(productMetricDataResult.get(0)).isEqualTo(8); + + } } diff --git a/powertools-metrics/src/test/java/software/amazon/lambda/powertools/metrics/MetricsLoggerTest.java b/powertools-metrics/src/test/java/software/amazon/lambda/powertools/metrics/MetricsLoggerTest.java index 0402e3cc5..94cfc05d4 100644 --- a/powertools-metrics/src/test/java/software/amazon/lambda/powertools/metrics/MetricsLoggerTest.java +++ b/powertools-metrics/src/test/java/software/amazon/lambda/powertools/metrics/MetricsLoggerTest.java @@ -25,6 +25,8 @@ import com.fasterxml.jackson.databind.ObjectMapper; import java.io.ByteArrayOutputStream; import java.io.PrintStream; +import java.util.ArrayList; +import java.util.LinkedHashMap; import java.util.Map; import java.util.function.Consumer; import org.junit.jupiter.api.AfterEach; @@ -35,6 +37,7 @@ import software.amazon.cloudwatchlogs.emf.config.SystemWrapper; import software.amazon.cloudwatchlogs.emf.logger.MetricsLogger; import software.amazon.cloudwatchlogs.emf.model.DimensionSet; +import software.amazon.cloudwatchlogs.emf.model.StorageResolution; import software.amazon.cloudwatchlogs.emf.model.Unit; class MetricsLoggerTest { @@ -245,6 +248,7 @@ private void testLogger(Consumer> methodToTest) { { metricsLogger.setDimensions(DimensionSet.of("Dimension1", "Value1")); metricsLogger.putMetric("Metric1", 1, Unit.COUNT); + metricsLogger.putMetric("Metric2", 1, Unit.COUNT, StorageResolution.HIGH); }); assertThat(out.toString()) @@ -263,6 +267,12 @@ private void testLogger(Consumer> methodToTest) { assertThat(aws.get("CloudWatchMetrics")) .asString() .contains("Namespace=GlobalName"); + + ArrayList cloudWatchMetrics = (ArrayList) aws.get("CloudWatchMetrics"); + LinkedHashMap values = (java.util.LinkedHashMap) cloudWatchMetrics.get(0); + ArrayList metricArray = (ArrayList) values.get("Metrics"); + LinkedHashMap metricValues = (LinkedHashMap) metricArray.get(1); + assertThat(metricValues.get("StorageResolution")).isEqualTo(1); }); } } diff --git a/powertools-metrics/src/test/java/software/amazon/lambda/powertools/metrics/handlers/PowertoolsMetricsTooManyDimensionsHandler.java b/powertools-metrics/src/test/java/software/amazon/lambda/powertools/metrics/handlers/PowertoolsMetricsTooManyDimensionsHandler.java index bc8a6e949..fd406b9cd 100644 --- a/powertools-metrics/src/test/java/software/amazon/lambda/powertools/metrics/handlers/PowertoolsMetricsTooManyDimensionsHandler.java +++ b/powertools-metrics/src/test/java/software/amazon/lambda/powertools/metrics/handlers/PowertoolsMetricsTooManyDimensionsHandler.java @@ -26,13 +26,14 @@ public class PowertoolsMetricsTooManyDimensionsHandler implements RequestHandler { @Override - @Metrics + @Metrics(namespace = "ExampleApplication",service = "booking") public Object handleRequest(Object input, Context context) { MetricsLogger metricsLogger = metricsLogger(); - - metricsLogger.setDimensions(IntStream.range(1, 15) - .mapToObj(value -> DimensionSet.of("Dimension" + value, "DimensionValue" + value)) - .toArray(DimensionSet[]::new)); + DimensionSet dimensionSet = new DimensionSet(); + for (int i = 0; i < 35; i++) { + dimensionSet.addDimension("Dimension" + i, "value" + i); + } + metricsLogger.setDimensions(dimensionSet); return null; } diff --git a/powertools-metrics/src/test/java/software/amazon/lambda/powertools/metrics/internal/LambdaMetricsAspectTest.java b/powertools-metrics/src/test/java/software/amazon/lambda/powertools/metrics/internal/LambdaMetricsAspectTest.java index 81e10ed22..7a6b6c1a5 100644 --- a/powertools-metrics/src/test/java/software/amazon/lambda/powertools/metrics/internal/LambdaMetricsAspectTest.java +++ b/powertools-metrics/src/test/java/software/amazon/lambda/powertools/metrics/internal/LambdaMetricsAspectTest.java @@ -40,6 +40,7 @@ import org.mockito.Mock; import org.mockito.MockedStatic; import software.amazon.cloudwatchlogs.emf.config.SystemWrapper; +import software.amazon.cloudwatchlogs.emf.exception.DimensionSetExceededException; import software.amazon.lambda.powertools.common.internal.LambdaHandlerProcessor; import software.amazon.lambda.powertools.metrics.MetricsUtils; import software.amazon.lambda.powertools.metrics.ValidationException; @@ -389,9 +390,9 @@ public void exceptionWhenTooManyDimensionsSet() { requestHandler = new PowertoolsMetricsTooManyDimensionsHandler(); - assertThatExceptionOfType(ValidationException.class) + assertThatExceptionOfType(DimensionSetExceededException.class) .isThrownBy(() -> requestHandler.handleRequest("input", context)) - .withMessage("Number of Dimensions must be in range of 0-9. Actual size: 14."); + .withMessage("Maximum number of dimensions allowed are 30. Account for default dimensions if not using setDimensions."); } } From 65e29e27c3c2efcf4de5bf9f64d65814d7a34f81 Mon Sep 17 00:00:00 2001 From: ritigupt <102658810+ritigupt@users.noreply.github.com> Date: Tue, 2 Jan 2024 18:19:24 +0530 Subject: [PATCH 2/7] Removing LambdaJsonLayout from logging in examples (#1545) --- .../src/main/resources/log4j2.xml | 2 +- .../src/main/resources/log4j2.xml | 2 +- powertools-tracing/src/test/resources/log4j2.xml | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/examples/powertools-examples-parameters/src/main/resources/log4j2.xml b/examples/powertools-examples-parameters/src/main/resources/log4j2.xml index 033da8a11..fe943d707 100644 --- a/examples/powertools-examples-parameters/src/main/resources/log4j2.xml +++ b/examples/powertools-examples-parameters/src/main/resources/log4j2.xml @@ -2,7 +2,7 @@ - + diff --git a/examples/powertools-examples-serialization/src/main/resources/log4j2.xml b/examples/powertools-examples-serialization/src/main/resources/log4j2.xml index 033da8a11..fe943d707 100644 --- a/examples/powertools-examples-serialization/src/main/resources/log4j2.xml +++ b/examples/powertools-examples-serialization/src/main/resources/log4j2.xml @@ -2,7 +2,7 @@ - + diff --git a/powertools-tracing/src/test/resources/log4j2.xml b/powertools-tracing/src/test/resources/log4j2.xml index 108e32b75..030d11725 100644 --- a/powertools-tracing/src/test/resources/log4j2.xml +++ b/powertools-tracing/src/test/resources/log4j2.xml @@ -2,7 +2,7 @@ - + From bf9676414d41013e1eb3eb62ca1a5b1289ed33c0 Mon Sep 17 00:00:00 2001 From: jdoherty07 Date: Fri, 5 Jan 2024 20:40:59 +0000 Subject: [PATCH 3/7] updated formatting and changed assertion for sonar --- .../software/amazon/lambda/powertools/e2e/Function.java | 7 +++++-- .../software/amazon/lambda/powertools/MetricsE2ET.java | 3 ++- .../lambda/powertools/metrics/MetricsLoggerTest.java | 7 ++++--- .../metrics/internal/LambdaMetricsAspectTest.java | 3 ++- 4 files changed, 13 insertions(+), 7 deletions(-) diff --git a/powertools-e2e-tests/handlers/metrics/src/main/java/software/amazon/lambda/powertools/e2e/Function.java b/powertools-e2e-tests/handlers/metrics/src/main/java/software/amazon/lambda/powertools/e2e/Function.java index 03c1d7504..a86e515f7 100644 --- a/powertools-e2e-tests/handlers/metrics/src/main/java/software/amazon/lambda/powertools/e2e/Function.java +++ b/powertools-e2e-tests/handlers/metrics/src/main/java/software/amazon/lambda/powertools/e2e/Function.java @@ -35,14 +35,17 @@ public class Function implements RequestHandler { @Metrics(captureColdStart = true) public String handleRequest(Input input, Context context) { - Instant currentTimeTruncatedPlusThirty = LocalDateTime.now().truncatedTo(ChronoUnit.MINUTES).toInstant(ZoneOffset.UTC).plusSeconds(30); + Instant currentTimeTruncatedPlusThirty = + LocalDateTime.now().truncatedTo(ChronoUnit.MINUTES).toInstant(ZoneOffset.UTC).plusSeconds(30); metricsLogger.setTimestamp(currentTimeTruncatedPlusThirty); DimensionSet dimensionSet = new DimensionSet(); input.getDimensions().forEach((key, value) -> dimensionSet.addDimension(key, value)); metricsLogger.putDimensions(dimensionSet); - input.getMetrics().forEach((key, value) -> metricsLogger.putMetric(key, value, Unit.COUNT,input.getHighResolution().equalsIgnoreCase("true") ? StorageResolution.HIGH : StorageResolution.STANDARD )); + input.getMetrics().forEach((key, value) -> metricsLogger.putMetric(key, value, Unit.COUNT, + input.getHighResolution().equalsIgnoreCase("true") ? StorageResolution.HIGH : + StorageResolution.STANDARD)); return "OK"; } diff --git a/powertools-e2e-tests/src/test/java/software/amazon/lambda/powertools/MetricsE2ET.java b/powertools-e2e-tests/src/test/java/software/amazon/lambda/powertools/MetricsE2ET.java index 61ef8e583..235255dff 100644 --- a/powertools-e2e-tests/src/test/java/software/amazon/lambda/powertools/MetricsE2ET.java +++ b/powertools-e2e-tests/src/test/java/software/amazon/lambda/powertools/MetricsE2ET.java @@ -71,7 +71,8 @@ public static void tearDown() { public void test_recordMetrics() { // GIVEN - Instant currentTimeTruncatedToMinutes = LocalDateTime.now().truncatedTo(ChronoUnit.MINUTES).toInstant(ZoneOffset.UTC); + Instant currentTimeTruncatedToMinutes = + LocalDateTime.now().truncatedTo(ChronoUnit.MINUTES).toInstant(ZoneOffset.UTC); String event1 = "{ \"metrics\": {\"orders\": 1, \"products\": 4}, \"dimensions\": { \"Environment\": \"test\"}, \"highResolution\": \"false\"}"; diff --git a/powertools-metrics/src/test/java/software/amazon/lambda/powertools/metrics/MetricsLoggerTest.java b/powertools-metrics/src/test/java/software/amazon/lambda/powertools/metrics/MetricsLoggerTest.java index 94cfc05d4..26ae41a00 100644 --- a/powertools-metrics/src/test/java/software/amazon/lambda/powertools/metrics/MetricsLoggerTest.java +++ b/powertools-metrics/src/test/java/software/amazon/lambda/powertools/metrics/MetricsLoggerTest.java @@ -269,10 +269,11 @@ private void testLogger(Consumer> methodToTest) { .contains("Namespace=GlobalName"); ArrayList cloudWatchMetrics = (ArrayList) aws.get("CloudWatchMetrics"); - LinkedHashMap values = (java.util.LinkedHashMap) cloudWatchMetrics.get(0); + LinkedHashMap values = + (java.util.LinkedHashMap) cloudWatchMetrics.get(0); ArrayList metricArray = (ArrayList) values.get("Metrics"); - LinkedHashMap metricValues = (LinkedHashMap) metricArray.get(1); - assertThat(metricValues.get("StorageResolution")).isEqualTo(1); + LinkedHashMap metricValues = (LinkedHashMap) metricArray.get(1); + assertThat(metricValues).containsEntry("StorageResolution", 1); }); } } diff --git a/powertools-metrics/src/test/java/software/amazon/lambda/powertools/metrics/internal/LambdaMetricsAspectTest.java b/powertools-metrics/src/test/java/software/amazon/lambda/powertools/metrics/internal/LambdaMetricsAspectTest.java index 7a6b6c1a5..d27af1fdf 100644 --- a/powertools-metrics/src/test/java/software/amazon/lambda/powertools/metrics/internal/LambdaMetricsAspectTest.java +++ b/powertools-metrics/src/test/java/software/amazon/lambda/powertools/metrics/internal/LambdaMetricsAspectTest.java @@ -392,7 +392,8 @@ public void exceptionWhenTooManyDimensionsSet() { assertThatExceptionOfType(DimensionSetExceededException.class) .isThrownBy(() -> requestHandler.handleRequest("input", context)) - .withMessage("Maximum number of dimensions allowed are 30. Account for default dimensions if not using setDimensions."); + .withMessage( + "Maximum number of dimensions allowed are 30. Account for default dimensions if not using setDimensions."); } } From cda20c7c12ea46c306f48542a36df23bf5a245b6 Mon Sep 17 00:00:00 2001 From: Jerome Van Der Linden Date: Fri, 21 Jun 2024 17:35:32 +0200 Subject: [PATCH 4/7] fix example --- .../sam/src/main/java/helloworld/App.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/powertools-examples-core-utilities/sam/src/main/java/helloworld/App.java b/examples/powertools-examples-core-utilities/sam/src/main/java/helloworld/App.java index 0513be410..e7c410042 100644 --- a/examples/powertools-examples-core-utilities/sam/src/main/java/helloworld/App.java +++ b/examples/powertools-examples-core-utilities/sam/src/main/java/helloworld/App.java @@ -67,7 +67,7 @@ public APIGatewayProxyResponseEvent handleRequest(final APIGatewayProxyRequestEv metricsLogger().putMetric("CustomMetric3", 1, Unit.COUNT, StorageResolution.HIGH); - LoggingUtils.appendKey("test", "willBeLogged"); + MDC.put("test", "willBeLogged"); APIGatewayProxyResponseEvent response = new APIGatewayProxyResponseEvent() .withHeaders(headers); From ca90740c91e27e5a4fa5b6b4b7863caabbd9f8b7 Mon Sep 17 00:00:00 2001 From: Jerome Van Der Linden Date: Fri, 21 Jun 2024 17:42:28 +0200 Subject: [PATCH 5/7] do not build on java 8 for v2 (oopsy) --- .github/workflows/run-e2e-tests.yml | 1 - 1 file changed, 1 deletion(-) diff --git a/.github/workflows/run-e2e-tests.yml b/.github/workflows/run-e2e-tests.yml index b358fe355..c56671761 100644 --- a/.github/workflows/run-e2e-tests.yml +++ b/.github/workflows/run-e2e-tests.yml @@ -24,7 +24,6 @@ on: pull_request: branches: - main - - v2 paths: - 'powertools-e2e-tests/**' From fd010690ee2974fb773a39ef891c8374ce9fb0b2 Mon Sep 17 00:00:00 2001 From: Jerome Van Der Linden Date: Mon, 24 Jun 2024 10:43:57 +0200 Subject: [PATCH 6/7] spotbugs fix --- spotbugs-exclude.xml | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/spotbugs-exclude.xml b/spotbugs-exclude.xml index ee44f7b4d..e959204ad 100644 --- a/spotbugs-exclude.xml +++ b/spotbugs-exclude.xml @@ -233,6 +233,10 @@ + + + + From 514a9b8cf7887d7ac4b14b3b3d27f46deb685edc Mon Sep 17 00:00:00 2001 From: Jerome Van Der Linden Date: Mon, 24 Jun 2024 11:09:09 +0200 Subject: [PATCH 7/7] update documentation --- docs/core/metrics.md | 112 ++++++++++++++----------------------------- 1 file changed, 37 insertions(+), 75 deletions(-) diff --git a/docs/core/metrics.md b/docs/core/metrics.md index e06ab6d10..165c26e1c 100644 --- a/docs/core/metrics.md +++ b/docs/core/metrics.md @@ -28,9 +28,7 @@ If you're new to Amazon CloudWatch, there are two terminologies you must be awar ## Install - Depending on your version of Java (either Java 1.8 or 11+), the configuration slightly changes. - -=== "Maven Java 11+" +=== "Maven" ```xml hl_lines="3-7 16 18 24-27" @@ -75,52 +73,7 @@ If you're new to Amazon CloudWatch, there are two terminologies you must be awar ``` -=== "Maven Java 1.8" - - ```xml hl_lines="3-7 16 18 24-27" - - ... - - software.amazon.lambda - powertools-metrics - {{ powertools.version }} - - ... - - ... - - - - ... - - org.codehaus.mojo - aspectj-maven-plugin - 1.14.0 - - 1.8 - 1.8 - 1.8 - - - software.amazon.lambda - powertools-metrics - - - - - - - compile - - - - - ... - - - ``` - -=== "Gradle Java 11+" +=== "Gradle" ```groovy hl_lines="3 11" plugins { @@ -140,34 +93,14 @@ If you're new to Amazon CloudWatch, there are two terminologies you must be awar targetCompatibility = 11 ``` -=== "Gradle Java 1.8" - - ```groovy hl_lines="3 11" - plugins { - id 'java' - id 'io.freefair.aspectj.post-compile-weaving' version '6.6.3' - } - - repositories { - mavenCentral() - } - - dependencies { - aspect 'software.amazon.lambda:powertools-metrics:{{ powertools.version }}' - } - - sourceCompatibility = 1.8 - targetCompatibility = 1.8 - ``` - ## Getting started Metric has two global settings that will be used across all metrics emitted: -Setting | Description | Environment variable | Constructor parameter -------------------------------------------------- | ------------------------------------------------- | ------------------------------------------------- | ------------------------------------------------- -**Metric namespace** | Logical container where all metrics will be placed e.g. `ServerlessAirline` | `POWERTOOLS_METRICS_NAMESPACE` | `namespace` -**Service** | Optionally, sets **service** metric dimension across all metrics e.g. `payment` | `POWERTOOLS_SERVICE_NAME` | `service` +| Setting | Description | Environment variable | Constructor parameter | +|----------------------|---------------------------------------------------------------------------------|--------------------------------|-----------------------| +| **Metric namespace** | Logical container where all metrics will be placed e.g. `ServerlessAirline` | `POWERTOOLS_METRICS_NAMESPACE` | `namespace` | +| **Service** | Optionally, sets **service** metric dimension across all metrics e.g. `payment` | `POWERTOOLS_SERVICE_NAME` | `service` | !!! tip "Use your application or main service as the metric namespace to easily group all metrics" @@ -198,7 +131,7 @@ Setting | Description | Environment variable | Constructor parameter @Override @Metrics(namespace = "ExampleApplication", service = "booking") public Object handleRequest(Object input, Context context) { - ... + // ... } } ``` @@ -224,7 +157,7 @@ You can create metrics using `putMetric`, and manually create dimensions for all public Object handleRequest(Object input, Context context) { metricsLogger.putDimensions(DimensionSet.of("environment", "prod")); metricsLogger.putMetric("SuccessfulBooking", 1, Unit.COUNT); - ... + // ... } } ``` @@ -234,6 +167,35 @@ You can create metrics using `putMetric`, and manually create dimensions for all !!! note "Metrics overflow" CloudWatch EMF supports a max of 100 metrics. Metrics utility will flush all metrics when adding the 100th metric while subsequent metrics will be aggregated into a new EMF object, for your convenience. + +### Adding high-resolution metrics + +You can create [high-resolution metrics](https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/publishingMetrics.html#high-resolution-metrics) +passing a `storageResolution` to the `putMetric` method: + +=== "HigResMetricsHandler.java" + + ```java hl_lines="3 13" + import software.amazon.lambda.powertools.metrics.Metrics; + import software.amazon.cloudwatchlogs.emf.logger.MetricsLogger; + import software.amazon.cloudwatchlogs.emf.model.StorageResolution; + + public class MetricsEnabledHandler implements RequestHandler { + + MetricsLogger metricsLogger = MetricsUtils.metricsLogger(); + + @Override + @Metrics(namespace = "ExampleApplication", service = "booking") + public Object handleRequest(Object input, Context context) { + // ... + metricsLogger.putMetric("SuccessfulBooking", 1, Unit.COUNT, StorageResolution.HIGH); + } + } + ``` + +!!! info "When is it useful?" + High-resolution metrics are data with a granularity of one second and are very useful in several situations such as telemetry, time series, real-time incident management, and others. + ### Flushing metrics The `@Metrics` annotation **validates**, **serializes**, and **flushes** all your metrics. During metrics validation,