Skip to content

Commit 36fcdcf

Browse files
committed
bump metrics version to 4.0.3
1 parent a07e5d9 commit 36fcdcf

15 files changed

+155
-52
lines changed

docs/core/metrics.md

+23-7
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,12 @@ You can create metrics using `putMetric`, and manually create dimensions for all
8989
@Override
9090
@Metrics(namespace = "ExampleApplication", service = "booking")
9191
public Object handleRequest(Object input, Context context) {
92-
metricsLogger.putDimensions(DimensionSet.of("environment", "prod"));
93-
metricsLogger.putMetric("SuccessfulBooking", 1, Unit.COUNT);
92+
try {
93+
metricsLogger.putDimensions(DimensionSet.of("environment", "prod"));
94+
metricsLogger.putMetric("SuccessfulBooking", 1, Unit.COUNT);
95+
} catch (InvalidDimensionException | InvalidMetricException | DimensionSetExceededException e) {
96+
LOG.error(e);
97+
}
9498
...
9599
}
96100
}
@@ -173,8 +177,12 @@ You can use `putMetadata` for advanced use cases, where you want to metadata as
173177
@Override
174178
@Metrics(namespace = "ServerlessAirline", service = "payment")
175179
public Object handleRequest(Object input, Context context) {
176-
metricsLogger().putMetric("CustomMetric1", 1, Unit.COUNT);
177-
metricsLogger().putMetadata("booking_id", "1234567890");
180+
try {
181+
metricsLogger().putMetadata("booking_id", "1234567890");
182+
metricsLogger().putMetric("CustomMetric1", 1, Unit.COUNT);
183+
} catch (InvalidMetricException e) {
184+
throw new RuntimeException(e);
185+
}
178186
...
179187
}
180188
}
@@ -199,7 +207,11 @@ Dimension, it can be done via `#!java MetricsUtils.defaultDimensions()`.
199207
MetricsLogger metricsLogger = MetricsUtils.metricsLogger();
200208
201209
static {
202-
MetricsUtils.defaultDimensions(DimensionSet.of("CustomDimension", "booking"));
210+
try {
211+
MetricsUtils.defaultDimensions(DimensionSet.of("CustomDimension", "booking"));
212+
} catch (InvalidDimensionException | DimensionSetExceededException e) {
213+
throw new RuntimeException(e);
214+
}
203215
}
204216

205217
@Override
@@ -228,8 +240,12 @@ CloudWatch EMF uses the same dimensions across all your metrics. Use `withSingle
228240

229241
@Override
230242
public Object handleRequest(Object input, Context context) {
231-
withSingleMetric("CustomMetrics2", 1, Unit.COUNT, "Another", (metric) -> {
232-
metric.setDimensions(DimensionSet.of("AnotherService", "CustomService"));
243+
withSingleMetric("CustomMetrics2", 1, Unit.COUNT, "Another", (metric) -> {
244+
try {
245+
metric.setDimensions(DimensionSet.of("AnotherService", "CustomService"));
246+
} catch (InvalidDimensionException | DimensionSetExceededException e) {
247+
throw new RuntimeException(e);
248+
}
233249
});
234250
}
235251
}

pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@
7575
<maven-source-plugin.version>3.2.1</maven-source-plugin.version>
7676
<maven-gpg-plugin.version>3.0.1</maven-gpg-plugin.version>
7777
<junit-jupiter.version>5.9.1</junit-jupiter.version>
78-
<aws-embedded-metrics.version>1.0.6</aws-embedded-metrics.version>
78+
<aws-embedded-metrics.version>4.0.3</aws-embedded-metrics.version>
7979
<jmespath.version>0.5.1</jmespath.version>
8080
</properties>
8181

powertools-metrics/src/main/java/software/amazon/cloudwatchlogs/emf/model/MetricsLoggerHelper.java

+8-1
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
package software.amazon.cloudwatchlogs.emf.model;
22

3+
import software.amazon.cloudwatchlogs.emf.exception.DimensionSetExceededException;
4+
35
import java.lang.reflect.Field;
46

57
import static software.amazon.lambda.powertools.metrics.MetricsUtils.metricsLogger;
68

79
public final class MetricsLoggerHelper {
10+
811
private MetricsLoggerHelper() {
912
}
1013

@@ -13,7 +16,11 @@ public static boolean hasNoMetrics() {
1316
}
1417

1518
public static long dimensionsCount() {
16-
return metricsContext().getDimensions().size();
19+
try {
20+
return metricsContext().getDimensions().size();
21+
} catch (DimensionSetExceededException e) {
22+
throw new RuntimeException("Too many dimensions defined", e);
23+
}
1724
}
1825

1926
public static MetricsContext metricsContext() {

powertools-metrics/src/main/java/software/amazon/lambda/powertools/metrics/MetricsUtils.java

+10-18
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,14 @@
66

77
import software.amazon.cloudwatchlogs.emf.config.SystemWrapper;
88
import software.amazon.cloudwatchlogs.emf.environment.EnvironmentProvider;
9+
import software.amazon.cloudwatchlogs.emf.exception.InvalidMetricException;
10+
import software.amazon.cloudwatchlogs.emf.exception.InvalidNamespaceException;
911
import software.amazon.cloudwatchlogs.emf.logger.MetricsLogger;
1012
import software.amazon.cloudwatchlogs.emf.model.DimensionSet;
1113
import software.amazon.cloudwatchlogs.emf.model.MetricsContext;
1214
import software.amazon.cloudwatchlogs.emf.model.MetricsLoggerHelper;
1315
import software.amazon.cloudwatchlogs.emf.model.Unit;
1416

15-
import static java.util.Objects.requireNonNull;
1617
import static java.util.Optional.ofNullable;
1718
import static software.amazon.lambda.powertools.core.internal.LambdaHandlerProcessor.getXrayTraceId;
1819
import static software.amazon.lambda.powertools.metrics.internal.LambdaMetricsAspect.REQUEST_ID_PROPERTY;
@@ -49,23 +50,6 @@ public static void defaultDimensions(final DimensionSet... dimensionSets) {
4950
MetricsUtils.defaultDimensions = dimensionSets;
5051
}
5152

52-
/**
53-
* Configure default dimension to be used by logger.
54-
* By default, @{@link Metrics} annotation captures configured service as a dimension <i>Service</i>
55-
* @param dimensionSet Default value of dimension set for logger
56-
* @deprecated use {@link #defaultDimensions(DimensionSet...)} instead
57-
*
58-
*/
59-
@Deprecated
60-
public static void defaultDimensionSet(final DimensionSet dimensionSet) {
61-
requireNonNull(dimensionSet, "Null dimension set not allowed");
62-
63-
if(dimensionSet.getDimensionKeys().size() > 0) {
64-
defaultDimensions(dimensionSet);
65-
}
66-
}
67-
68-
6953
/**
7054
* Add and immediately flush a single metric. It will use the default namespace
7155
* specified either on {@link Metrics} annotation or via POWERTOOLS_METRICS_NAMESPACE env var.
@@ -88,6 +72,10 @@ public static void withSingleMetric(final String name,
8872
metricsLogger.putMetric(name, value, unit);
8973
captureRequestAndTraceId(metricsLogger);
9074
logger.accept(metricsLogger);
75+
} catch (InvalidNamespaceException e) {
76+
throw new RuntimeException("A valid namespace is required, either pass it to the @Metrics annotation or set the environment variable POWERTOOLS_METRICS_NAMESPACE", e);
77+
} catch (InvalidMetricException e) {
78+
throw new RuntimeException(e);
9179
} finally {
9280
metricsLogger.flush();
9381
}
@@ -116,6 +104,10 @@ public static void withSingleMetric(final String name,
116104
metricsLogger.putMetric(name, value, unit);
117105
captureRequestAndTraceId(metricsLogger);
118106
logger.accept(metricsLogger);
107+
} catch (InvalidNamespaceException e) {
108+
throw new RuntimeException("A valid namespace is required, either pass it to the @Metrics annotation or set the environment variable POWERTOOLS_METRICS_NAMESPACE", e);
109+
} catch (InvalidMetricException e) {
110+
throw new RuntimeException(e);
119111
} finally {
120112
metricsLogger.flush();
121113
}

powertools-metrics/src/main/java/software/amazon/lambda/powertools/metrics/internal/LambdaMetricsAspect.java

+21-3
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@
77
import org.aspectj.lang.annotation.Around;
88
import org.aspectj.lang.annotation.Aspect;
99
import org.aspectj.lang.annotation.Pointcut;
10+
import software.amazon.cloudwatchlogs.emf.exception.DimensionSetExceededException;
11+
import software.amazon.cloudwatchlogs.emf.exception.InvalidDimensionException;
12+
import software.amazon.cloudwatchlogs.emf.exception.InvalidMetricException;
13+
import software.amazon.cloudwatchlogs.emf.exception.InvalidNamespaceException;
1014
import software.amazon.cloudwatchlogs.emf.logger.MetricsLogger;
1115
import software.amazon.cloudwatchlogs.emf.model.DimensionSet;
1216
import software.amazon.cloudwatchlogs.emf.model.MetricsContext;
@@ -83,12 +87,24 @@ private void coldStartSingleMetricIfApplicable(final String awsRequestId,
8387
final Metrics metrics) {
8488
if (metrics.captureColdStart()
8589
&& isColdStart()) {
86-
MetricsLogger metricsLogger = new MetricsLogger();
90+
MetricsLogger metricsLogger = new MetricsLogger();
91+
try {
8792
metricsLogger.setNamespace(namespace(metrics));
93+
} catch (InvalidNamespaceException e) {
94+
throw new RuntimeException("A valid namespace is required, either pass it to the @Metrics annotation or set the environment variable POWERTOOLS_METRICS_NAMESPACE", e);
95+
}
96+
try {
8897
metricsLogger.putMetric("ColdStart", 1, Unit.COUNT);
98+
} catch (InvalidMetricException e) {
99+
// should not occur
100+
}
101+
try {
89102
metricsLogger.setDimensions(DimensionSet.of("Service", service(metrics), "FunctionName", functionName));
90-
metricsLogger.putProperty(REQUEST_ID_PROPERTY, awsRequestId);
91-
metricsLogger.flush();
103+
} catch (InvalidDimensionException | DimensionSetExceededException e) {
104+
throw new RuntimeException("A valid service is required, either pass it to the @Metrics annotation or set the environment variable POWERTOOLS_SERVICE_NAME", e);
105+
}
106+
metricsLogger.putProperty(REQUEST_ID_PROPERTY, awsRequestId);
107+
metricsLogger.flush();
92108
}
93109

94110
}
@@ -136,6 +152,8 @@ public static void refreshMetricsContext(Metrics metrics) {
136152
f.set(metricsLogger(), context);
137153
} catch (NoSuchFieldException | IllegalAccessException e) {
138154
throw new RuntimeException(e);
155+
} catch (InvalidDimensionException | DimensionSetExceededException e) {
156+
throw new RuntimeException("A valid service is required, either pass it to the @Metrics annotation or set the environment variable POWERTOOLS_SERVICE_NAME", e);
139157
}
140158
}
141159
}

powertools-metrics/src/test/java/software/amazon/lambda/powertools/metrics/MetricsLoggerTest.java

+17-10
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
import org.junit.jupiter.api.Test;
1414
import org.mockito.MockedStatic;
1515
import software.amazon.cloudwatchlogs.emf.config.SystemWrapper;
16+
import software.amazon.cloudwatchlogs.emf.exception.DimensionSetExceededException;
17+
import software.amazon.cloudwatchlogs.emf.exception.InvalidDimensionException;
1618
import software.amazon.cloudwatchlogs.emf.model.DimensionSet;
1719
import software.amazon.cloudwatchlogs.emf.model.Unit;
1820

@@ -46,7 +48,7 @@ static void beforeAll() {
4648
}
4749

4850
@Test
49-
void singleMetricsCaptureUtilityWithDefaultDimension() {
51+
void singleMetricsCaptureUtilityWithDefaultDimension() throws InvalidDimensionException, DimensionSetExceededException {
5052
try (MockedStatic<SystemWrapper> mocked = mockStatic(SystemWrapper.class);
5153
MockedStatic<software.amazon.lambda.powertools.core.internal.SystemWrapper> internalWrapper = mockStatic(software.amazon.lambda.powertools.core.internal.SystemWrapper.class)) {
5254
mocked.when(() -> SystemWrapper.getenv("AWS_EMF_ENVIRONMENT")).thenReturn("Lambda");
@@ -78,7 +80,13 @@ void singleMetricsCaptureUtility() {
7880
internalWrapper.when(() -> getenv("_X_AMZN_TRACE_ID")).thenReturn("Root=1-5759e988-bd862e3fe1be46a994272793;Parent=53995c3f42cd8ad8;Sampled=1\"");
7981

8082
MetricsUtils.withSingleMetric("Metric1", 1, Unit.COUNT, "test",
81-
metricsLogger -> metricsLogger.setDimensions(DimensionSet.of("Dimension1", "Value1")));
83+
metricsLogger -> {
84+
try {
85+
metricsLogger.setDimensions(DimensionSet.of("Dimension1", "Value1"));
86+
} catch (InvalidDimensionException | DimensionSetExceededException e) {
87+
throw new RuntimeException(e);
88+
}
89+
});
8290

8391
assertThat(out.toString())
8492
.satisfies(s -> {
@@ -102,7 +110,13 @@ void singleMetricsCaptureUtilityWithDefaultNameSpace() {
102110
internalWrapper.when(() -> getenv("_X_AMZN_TRACE_ID")).thenReturn("Root=1-5759e988-bd862e3fe1be46a994272793;Parent=53995c3f42cd8ad8;Sampled=1\"");
103111

104112
MetricsUtils.withSingleMetric("Metric1", 1, Unit.COUNT,
105-
metricsLogger -> metricsLogger.setDimensions(DimensionSet.of("Dimension1", "Value1")));
113+
metricsLogger -> {
114+
try {
115+
metricsLogger.setDimensions(DimensionSet.of("Dimension1", "Value1"));
116+
} catch (InvalidDimensionException | DimensionSetExceededException e) {
117+
throw new RuntimeException(e);
118+
}
119+
});
106120

107121
assertThat(out.toString())
108122
.satisfies(s -> {
@@ -123,13 +137,6 @@ void singleMetricsCaptureUtilityWithDefaultNameSpace() {
123137
}
124138
}
125139

126-
@Test
127-
void shouldThrowExceptionWhenDefaultDimensionIsNull() {
128-
assertThatNullPointerException()
129-
.isThrownBy(() -> MetricsUtils.defaultDimensionSet(null))
130-
.withMessage("Null dimension set not allowed");
131-
}
132-
133140
private Map<String, Object> readAsJson(String s) {
134141
try {
135142
return mapper.readValue(s, Map.class);

powertools-metrics/src/test/java/software/amazon/lambda/powertools/metrics/handlers/PowertoolsMetricsColdStartEnabledHandler.java

+6-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import com.amazonaws.services.lambda.runtime.Context;
44
import com.amazonaws.services.lambda.runtime.RequestHandler;
5+
import software.amazon.cloudwatchlogs.emf.exception.InvalidMetricException;
56
import software.amazon.cloudwatchlogs.emf.logger.MetricsLogger;
67
import software.amazon.cloudwatchlogs.emf.model.Unit;
78
import software.amazon.lambda.powertools.metrics.Metrics;
@@ -14,7 +15,11 @@ public class PowertoolsMetricsColdStartEnabledHandler implements RequestHandler<
1415
@Metrics(namespace = "ExampleApplication", service = "booking", captureColdStart = true)
1516
public Object handleRequest(Object input, Context context) {
1617
MetricsLogger metricsLogger = metricsLogger();
17-
metricsLogger.putMetric("Metric1", 1, Unit.BYTES);
18+
try {
19+
metricsLogger.putMetric("Metric1", 1, Unit.BYTES);
20+
} catch (InvalidMetricException e) {
21+
throw new RuntimeException(e);
22+
}
1823

1924
return null;
2025
}

powertools-metrics/src/test/java/software/amazon/lambda/powertools/metrics/handlers/PowertoolsMetricsEnabledDefaultDimensionHandler.java

+14-2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
import com.amazonaws.services.lambda.runtime.Context;
44
import com.amazonaws.services.lambda.runtime.RequestHandler;
5+
import software.amazon.cloudwatchlogs.emf.exception.DimensionSetExceededException;
6+
import software.amazon.cloudwatchlogs.emf.exception.InvalidDimensionException;
7+
import software.amazon.cloudwatchlogs.emf.exception.InvalidMetricException;
58
import software.amazon.cloudwatchlogs.emf.logger.MetricsLogger;
69
import software.amazon.cloudwatchlogs.emf.model.DimensionSet;
710
import software.amazon.cloudwatchlogs.emf.model.Unit;
@@ -14,14 +17,23 @@
1417
public class PowertoolsMetricsEnabledDefaultDimensionHandler implements RequestHandler<Object, Object> {
1518

1619
static {
17-
defaultDimensions(DimensionSet.of("CustomDimension", "booking"));
20+
try {
21+
defaultDimensions(DimensionSet.of("CustomDimension", "booking"));
22+
} catch (InvalidDimensionException | DimensionSetExceededException e) {
23+
throw new RuntimeException(e);
24+
}
1825
}
1926

2027
@Override
2128
@Metrics(namespace = "ExampleApplication", service = "booking")
2229
public Object handleRequest(Object input, Context context) {
2330
MetricsLogger metricsLogger = metricsLogger();
24-
metricsLogger.putMetric("Metric1", 1, Unit.BYTES);
31+
32+
try {
33+
metricsLogger.putMetric("Metric1", 1, Unit.BYTES);
34+
} catch (InvalidMetricException e) {
35+
throw new RuntimeException(e);
36+
}
2537

2638
withSingleMetric("Metric2", 1, Unit.COUNT, log -> {});
2739

powertools-metrics/src/test/java/software/amazon/lambda/powertools/metrics/handlers/PowertoolsMetricsEnabledDefaultNoDimensionHandler.java

+6-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import com.amazonaws.services.lambda.runtime.Context;
44
import com.amazonaws.services.lambda.runtime.RequestHandler;
5+
import software.amazon.cloudwatchlogs.emf.exception.InvalidMetricException;
56
import software.amazon.cloudwatchlogs.emf.logger.MetricsLogger;
67
import software.amazon.cloudwatchlogs.emf.model.Unit;
78
import software.amazon.lambda.powertools.metrics.Metrics;
@@ -20,7 +21,11 @@ public class PowertoolsMetricsEnabledDefaultNoDimensionHandler implements Reques
2021
@Metrics(namespace = "ExampleApplication", service = "booking")
2122
public Object handleRequest(Object input, Context context) {
2223
MetricsLogger metricsLogger = metricsLogger();
23-
metricsLogger.putMetric("Metric1", 1, Unit.BYTES);
24+
try {
25+
metricsLogger.putMetric("Metric1", 1, Unit.BYTES);
26+
} catch (InvalidMetricException e) {
27+
throw new RuntimeException(e);
28+
}
2429

2530
withSingleMetric("Metric2", 1, Unit.COUNT, log -> {});
2631

powertools-metrics/src/test/java/software/amazon/lambda/powertools/metrics/handlers/PowertoolsMetricsEnabledHandler.java

+15-3
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
import com.amazonaws.services.lambda.runtime.Context;
44
import com.amazonaws.services.lambda.runtime.RequestHandler;
5+
import software.amazon.cloudwatchlogs.emf.exception.DimensionSetExceededException;
6+
import software.amazon.cloudwatchlogs.emf.exception.InvalidDimensionException;
7+
import software.amazon.cloudwatchlogs.emf.exception.InvalidMetricException;
58
import software.amazon.cloudwatchlogs.emf.logger.MetricsLogger;
69
import software.amazon.cloudwatchlogs.emf.model.DimensionSet;
710
import software.amazon.cloudwatchlogs.emf.model.Unit;
@@ -16,11 +19,20 @@ public class PowertoolsMetricsEnabledHandler implements RequestHandler<Object, O
1619
@Metrics(namespace = "ExampleApplication", service = "booking")
1720
public Object handleRequest(Object input, Context context) {
1821
MetricsLogger metricsLogger = metricsLogger();
19-
metricsLogger.putMetric("Metric1", 1, Unit.BYTES);
20-
22+
try {
23+
metricsLogger.putMetric("Metric1", 1, Unit.BYTES);
24+
} catch (InvalidMetricException e) {
25+
throw new RuntimeException(e);
26+
}
2127

2228
withSingleMetric("Metric2", 1, Unit.COUNT,
23-
log -> log.setDimensions(DimensionSet.of("Dimension1", "Value1")));
29+
log -> {
30+
try {
31+
log.setDimensions(DimensionSet.of("Dimension1", "Value1"));
32+
} catch (InvalidDimensionException | DimensionSetExceededException e) {
33+
throw new RuntimeException(e);
34+
}
35+
});
2436

2537
return null;
2638
}

powertools-metrics/src/test/java/software/amazon/lambda/powertools/metrics/handlers/PowertoolsMetricsEnabledStreamHandler.java

+6-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
import com.amazonaws.services.lambda.runtime.Context;
77
import com.amazonaws.services.lambda.runtime.RequestStreamHandler;
8+
import software.amazon.cloudwatchlogs.emf.exception.InvalidMetricException;
89
import software.amazon.cloudwatchlogs.emf.logger.MetricsLogger;
910
import software.amazon.cloudwatchlogs.emf.model.Unit;
1011
import software.amazon.lambda.powertools.metrics.Metrics;
@@ -17,6 +18,10 @@ public class PowertoolsMetricsEnabledStreamHandler implements RequestStreamHandl
1718
@Metrics(namespace = "ExampleApplication", service = "booking")
1819
public void handleRequest(InputStream input, OutputStream output, Context context) {
1920
MetricsLogger metricsLogger = metricsLogger();
20-
metricsLogger.putMetric("Metric1", 1, Unit.BYTES);
21+
try {
22+
metricsLogger.putMetric("Metric1", 1, Unit.BYTES);
23+
} catch (InvalidMetricException e) {
24+
throw new RuntimeException(e);
25+
}
2126
}
2227
}

0 commit comments

Comments
 (0)