Skip to content

Commit 772cfb9

Browse files
committed
bump metrics version to 4.0.3
1 parent 23261c7 commit 772cfb9

15 files changed

+185
-82
lines changed

docs/core/metrics.md

+23-7
Original file line numberDiff line numberDiff line change
@@ -222,8 +222,12 @@ You can create metrics using `putMetric`, and manually create dimensions for all
222222
@Override
223223
@Metrics(namespace = "ExampleApplication", service = "booking")
224224
public Object handleRequest(Object input, Context context) {
225-
metricsLogger.putDimensions(DimensionSet.of("environment", "prod"));
226-
metricsLogger.putMetric("SuccessfulBooking", 1, Unit.COUNT);
225+
try {
226+
metricsLogger.putDimensions(DimensionSet.of("environment", "prod"));
227+
metricsLogger.putMetric("SuccessfulBooking", 1, Unit.COUNT);
228+
} catch (InvalidDimensionException | InvalidMetricException | DimensionSetExceededException e) {
229+
LOG.error(e);
230+
}
227231
...
228232
}
229233
}
@@ -306,8 +310,12 @@ You can use `putMetadata` for advanced use cases, where you want to metadata as
306310
@Override
307311
@Metrics(namespace = "ServerlessAirline", service = "payment")
308312
public Object handleRequest(Object input, Context context) {
309-
metricsLogger().putMetric("CustomMetric1", 1, Unit.COUNT);
310-
metricsLogger().putMetadata("booking_id", "1234567890");
313+
try {
314+
metricsLogger().putMetadata("booking_id", "1234567890");
315+
metricsLogger().putMetric("CustomMetric1", 1, Unit.COUNT);
316+
} catch (InvalidMetricException e) {
317+
throw new RuntimeException(e);
318+
}
311319
...
312320
}
313321
}
@@ -332,7 +340,11 @@ Dimension, it can be done via `#!java MetricsUtils.defaultDimensions()`.
332340
MetricsLogger metricsLogger = MetricsUtils.metricsLogger();
333341
334342
static {
335-
MetricsUtils.defaultDimensions(DimensionSet.of("CustomDimension", "booking"));
343+
try {
344+
MetricsUtils.defaultDimensions(DimensionSet.of("CustomDimension", "booking"));
345+
} catch (InvalidDimensionException | DimensionSetExceededException e) {
346+
throw new RuntimeException(e);
347+
}
336348
}
337349

338350
@Override
@@ -361,8 +373,12 @@ CloudWatch EMF uses the same dimensions across all your metrics. Use `withSingle
361373

362374
@Override
363375
public Object handleRequest(Object input, Context context) {
364-
withSingleMetric("CustomMetrics2", 1, Unit.COUNT, "Another", (metric) -> {
365-
metric.setDimensions(DimensionSet.of("AnotherService", "CustomService"));
376+
withSingleMetric("CustomMetrics2", 1, Unit.COUNT, "Another", (metric) -> {
377+
try {
378+
metric.setDimensions(DimensionSet.of("AnotherService", "CustomService"));
379+
} catch (InvalidDimensionException | DimensionSetExceededException e) {
380+
throw new RuntimeException(e);
381+
}
366382
});
367383
}
368384
}

pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@
9090
<maven-source-plugin.version>3.3.0</maven-source-plugin.version>
9191
<maven-gpg-plugin.version>3.1.0</maven-gpg-plugin.version>
9292
<junit.version>5.10.0</junit.version>
93-
<aws-embedded-metrics.version>1.0.6</aws-embedded-metrics.version>
93+
<aws-embedded-metrics.version>4.0.3</aws-embedded-metrics.version>
9494
<jmespath.version>0.5.1</jmespath.version>
9595
</properties>
9696

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

+8-1
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,14 @@
1414

1515
package software.amazon.cloudwatchlogs.emf.model;
1616

17+
1718
import static software.amazon.lambda.powertools.metrics.MetricsUtils.metricsLogger;
1819

1920
import java.lang.reflect.Field;
21+
import software.amazon.cloudwatchlogs.emf.exception.DimensionSetExceededException;
2022

2123
public final class MetricsLoggerHelper {
24+
2225
private MetricsLoggerHelper() {
2326
}
2427

@@ -27,7 +30,11 @@ public static boolean hasNoMetrics() {
2730
}
2831

2932
public static long dimensionsCount() {
30-
return metricsContext().getDimensions().size();
33+
try {
34+
return metricsContext().getDimensions().size();
35+
} catch (DimensionSetExceededException e) {
36+
throw new RuntimeException("Too many dimensions defined", e);
37+
}
3138
}
3239

3340
public static MetricsContext metricsContext() {

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

+27-38
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414

1515
package software.amazon.lambda.powertools.metrics;
1616

17-
import static java.util.Objects.requireNonNull;
1817
import static java.util.Optional.ofNullable;
1918
import static software.amazon.lambda.powertools.core.internal.LambdaHandlerProcessor.getXrayTraceId;
2019
import static software.amazon.lambda.powertools.metrics.internal.LambdaMetricsAspect.REQUEST_ID_PROPERTY;
@@ -25,6 +24,8 @@
2524
import java.util.function.Consumer;
2625
import software.amazon.cloudwatchlogs.emf.config.SystemWrapper;
2726
import software.amazon.cloudwatchlogs.emf.environment.EnvironmentProvider;
27+
import software.amazon.cloudwatchlogs.emf.exception.InvalidMetricException;
28+
import software.amazon.cloudwatchlogs.emf.exception.InvalidNamespaceException;
2829
import software.amazon.cloudwatchlogs.emf.logger.MetricsLogger;
2930
import software.amazon.cloudwatchlogs.emf.model.DimensionSet;
3031
import software.amazon.cloudwatchlogs.emf.model.MetricsContext;
@@ -63,23 +64,6 @@ public static void defaultDimensions(final DimensionSet... dimensionSets) {
6364
MetricsUtils.defaultDimensions = dimensionSets;
6465
}
6566

66-
/**
67-
* Configure default dimension to be used by logger.
68-
* By default, @{@link Metrics} annotation captures configured service as a dimension <i>Service</i>
69-
*
70-
* @param dimensionSet Default value of dimension set for logger
71-
* @deprecated use {@link #defaultDimensions(DimensionSet...)} instead
72-
*/
73-
@Deprecated
74-
public static void defaultDimensionSet(final DimensionSet dimensionSet) {
75-
requireNonNull(dimensionSet, "Null dimension set not allowed");
76-
77-
if (dimensionSet.getDimensionKeys().size() > 0) {
78-
defaultDimensions(dimensionSet);
79-
}
80-
}
81-
82-
8367
/**
8468
* Add and immediately flush a single metric. It will use the default namespace
8569
* specified either on {@link Metrics} annotation or via POWERTOOLS_METRICS_NAMESPACE env var.
@@ -95,11 +79,20 @@ public static void withSingleMetric(final String name,
9579
final double value,
9680
final Unit unit,
9781
final Consumer<MetricsLogger> logger) {
98-
withMetricsLogger(metricsLogger ->
99-
{
82+
MetricsLogger metricsLogger = logger();
83+
84+
try {
85+
metricsLogger.setNamespace(defaultNameSpace());
10086
metricsLogger.putMetric(name, value, unit);
87+
captureRequestAndTraceId(metricsLogger);
10188
logger.accept(metricsLogger);
102-
});
89+
} catch (InvalidNamespaceException e) {
90+
throw new RuntimeException("A valid namespace is required, either pass it to the @Metrics annotation or set the environment variable POWERTOOLS_METRICS_NAMESPACE", e);
91+
} catch (InvalidMetricException e) {
92+
throw new RuntimeException(e);
93+
} finally {
94+
metricsLogger.flush();
95+
}
10396
}
10497

10598
/**
@@ -118,12 +111,20 @@ public static void withSingleMetric(final String name,
118111
final Unit unit,
119112
final String namespace,
120113
final Consumer<MetricsLogger> logger) {
121-
withMetricsLogger(metricsLogger ->
122-
{
114+
MetricsLogger metricsLogger = logger();
115+
116+
try {
123117
metricsLogger.setNamespace(namespace);
124118
metricsLogger.putMetric(name, value, unit);
119+
captureRequestAndTraceId(metricsLogger);
125120
logger.accept(metricsLogger);
126-
});
121+
} catch (InvalidNamespaceException e) {
122+
throw new RuntimeException("A valid namespace is required, either pass it to the @Metrics annotation or set the environment variable POWERTOOLS_METRICS_NAMESPACE", e);
123+
} catch (InvalidMetricException e) {
124+
throw new RuntimeException(e);
125+
} finally {
126+
metricsLogger.flush();
127+
}
127128
}
128129

129130
/**
@@ -141,25 +142,13 @@ public static void withMetricsLogger(final Consumer<MetricsLogger> logger) {
141142
metricsLogger.setNamespace(defaultNameSpace());
142143
captureRequestAndTraceId(metricsLogger);
143144
logger.accept(metricsLogger);
145+
} catch (InvalidNamespaceException e) {
146+
throw new RuntimeException("A valid namespace is required, either pass it to the @Metrics annotation or set the environment variable POWERTOOLS_METRICS_NAMESPACE", e);
144147
} finally {
145148
metricsLogger.flush();
146149
}
147150
}
148151

149-
/**
150-
* Provide and immediately flush a {@link MetricsLogger}. It uses the default namespace
151-
* specified either on {@link Metrics} annotation or via POWERTOOLS_METRICS_NAMESPACE env var.
152-
* It by default captures function_request_id as property if used together with {@link Metrics} annotation. It will also
153-
* capture xray_trace_id as property if tracing is enabled.
154-
*
155-
* @param logger the MetricsLogger
156-
* @deprecated use {@link MetricsUtils#withMetricsLogger} instead
157-
*/
158-
@Deprecated
159-
public static void withMetricLogger(final Consumer<MetricsLogger> logger) {
160-
withMetricsLogger(logger);
161-
}
162-
163152
public static DimensionSet[] getDefaultDimensions() {
164153
return Arrays.copyOf(defaultDimensions, defaultDimensions.length);
165154
}

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

+26-7
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,17 @@
2424
import static software.amazon.lambda.powertools.metrics.MetricsUtils.hasDefaultDimension;
2525
import static software.amazon.lambda.powertools.metrics.MetricsUtils.metricsLogger;
2626

27+
2728
import com.amazonaws.services.lambda.runtime.Context;
2829
import java.lang.reflect.Field;
2930
import org.aspectj.lang.ProceedingJoinPoint;
3031
import org.aspectj.lang.annotation.Around;
3132
import org.aspectj.lang.annotation.Aspect;
3233
import org.aspectj.lang.annotation.Pointcut;
34+
import software.amazon.cloudwatchlogs.emf.exception.DimensionSetExceededException;
35+
import software.amazon.cloudwatchlogs.emf.exception.InvalidDimensionException;
36+
import software.amazon.cloudwatchlogs.emf.exception.InvalidMetricException;
37+
import software.amazon.cloudwatchlogs.emf.exception.InvalidNamespaceException;
3338
import software.amazon.cloudwatchlogs.emf.logger.MetricsLogger;
3439
import software.amazon.cloudwatchlogs.emf.model.DimensionSet;
3540
import software.amazon.cloudwatchlogs.emf.model.MetricsContext;
@@ -57,13 +62,15 @@ public static void refreshMetricsContext(Metrics metrics) {
5762
MetricsContext context = new MetricsContext();
5863

5964
DimensionSet[] defaultDimensions = hasDefaultDimension() ? MetricsUtils.getDefaultDimensions()
60-
: new DimensionSet[] {DimensionSet.of("Service", service(metrics))};
65+
: new DimensionSet[]{DimensionSet.of("Service", service(metrics))};
6166

6267
context.setDimensions(defaultDimensions);
6368

6469
f.set(metricsLogger(), context);
6570
} catch (NoSuchFieldException | IllegalAccessException e) {
6671
throw new RuntimeException(e);
72+
} catch (InvalidDimensionException | DimensionSetExceededException e) {
73+
throw new RuntimeException("A valid service is required, either pass it to the @Metrics annotation or set the environment variable POWERTOOLS_SERVICE_NAME", e);
6774
}
6875
}
6976

@@ -115,12 +122,24 @@ private void coldStartSingleMetricIfApplicable(final String awsRequestId,
115122
final Metrics metrics) {
116123
if (metrics.captureColdStart()
117124
&& isColdStart()) {
118-
MetricsLogger metricsLogger = new MetricsLogger();
119-
metricsLogger.setNamespace(namespace(metrics));
120-
metricsLogger.putMetric("ColdStart", 1, Unit.COUNT);
121-
metricsLogger.setDimensions(DimensionSet.of("Service", service(metrics), "FunctionName", functionName));
122-
metricsLogger.putProperty(REQUEST_ID_PROPERTY, awsRequestId);
123-
metricsLogger.flush();
125+
MetricsLogger metricsLogger = new MetricsLogger();
126+
try {
127+
metricsLogger.setNamespace(namespace(metrics));
128+
} catch (InvalidNamespaceException e) {
129+
throw new RuntimeException("A valid namespace is required, either pass it to the @Metrics annotation or set the environment variable POWERTOOLS_METRICS_NAMESPACE", e);
130+
}
131+
try {
132+
metricsLogger.putMetric("ColdStart", 1, Unit.COUNT);
133+
} catch (InvalidMetricException e) {
134+
// should not occur
135+
}
136+
try {
137+
metricsLogger.setDimensions(DimensionSet.of("Service", service(metrics), "FunctionName", functionName));
138+
} catch (InvalidDimensionException | DimensionSetExceededException e) {
139+
throw new RuntimeException("A valid service is required, either pass it to the @Metrics annotation or set the environment variable POWERTOOLS_SERVICE_NAME", e);
140+
}
141+
metricsLogger.putProperty(REQUEST_ID_PROPERTY, awsRequestId);
142+
metricsLogger.flush();
124143
}
125144

126145
}

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

+25-16
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@
3232
import org.junit.jupiter.api.Test;
3333
import org.mockito.MockedStatic;
3434
import software.amazon.cloudwatchlogs.emf.config.SystemWrapper;
35+
import software.amazon.cloudwatchlogs.emf.exception.DimensionSetExceededException;
36+
import software.amazon.cloudwatchlogs.emf.exception.InvalidDimensionException;
37+
import software.amazon.cloudwatchlogs.emf.exception.InvalidMetricException;
3538
import software.amazon.cloudwatchlogs.emf.logger.MetricsLogger;
3639
import software.amazon.cloudwatchlogs.emf.model.DimensionSet;
3740
import software.amazon.cloudwatchlogs.emf.model.Unit;
@@ -60,7 +63,7 @@ void tearDown() {
6063
}
6164

6265
@Test
63-
void singleMetricsCaptureUtilityWithDefaultDimension() {
66+
void singleMetricsCaptureUtilityWithDefaultDimension() throws InvalidDimensionException, DimensionSetExceededException {
6467
try (MockedStatic<SystemWrapper> mocked = mockStatic(SystemWrapper.class);
6568
MockedStatic<software.amazon.lambda.powertools.core.internal.SystemWrapper> internalWrapper = mockStatic(
6669
software.amazon.lambda.powertools.core.internal.SystemWrapper.class)) {
@@ -99,7 +102,13 @@ void singleMetricsCaptureUtility() {
99102
.thenReturn("Root=1-5759e988-bd862e3fe1be46a994272793;Parent=53995c3f42cd8ad8;Sampled=1\"");
100103

101104
MetricsUtils.withSingleMetric("Metric1", 1, Unit.COUNT, "test",
102-
metricsLogger -> metricsLogger.setDimensions(DimensionSet.of("Dimension1", "Value1")));
105+
metricsLogger -> {
106+
try {
107+
metricsLogger.setDimensions(DimensionSet.of("Dimension1", "Value1"));
108+
} catch (InvalidDimensionException | DimensionSetExceededException e) {
109+
throw new RuntimeException(e);
110+
}
111+
});
103112

104113
assertThat(out.toString())
105114
.satisfies(s ->
@@ -126,7 +135,13 @@ void singleMetricsCaptureUtilityWithDefaultNameSpace() {
126135
.thenReturn("Root=1-5759e988-bd862e3fe1be46a994272793;Parent=53995c3f42cd8ad8;Sampled=1\"");
127136

128137
MetricsUtils.withSingleMetric("Metric1", 1, Unit.COUNT,
129-
metricsLogger -> metricsLogger.setDimensions(DimensionSet.of("Dimension1", "Value1")));
138+
metricsLogger -> {
139+
try {
140+
metricsLogger.setDimensions(DimensionSet.of("Dimension1", "Value1"));
141+
} catch (InvalidDimensionException | DimensionSetExceededException e) {
142+
throw new RuntimeException(e);
143+
}
144+
});
130145

131146
assertThat(out.toString())
132147
.satisfies(s ->
@@ -148,20 +163,10 @@ void singleMetricsCaptureUtilityWithDefaultNameSpace() {
148163
}
149164
}
150165

151-
@Test
152-
void metricsLoggerCaptureUtilityWithDefaultNameSpace() {
153-
testLogger(MetricsUtils::withMetricsLogger);
154-
}
155-
156-
@Test
157-
void deprecatedMetricLoggerCaptureUtilityWithDefaultNameSpace() {
158-
testLogger(MetricsUtils::withMetricLogger);
159-
}
160-
161166
@Test
162167
void shouldThrowExceptionWhenDefaultDimensionIsNull() {
163168
assertThatNullPointerException()
164-
.isThrownBy(() -> MetricsUtils.defaultDimensionSet(null))
169+
.isThrownBy(() -> MetricsUtils.defaultDimensions(null))
165170
.withMessage("Null dimension set not allowed");
166171
}
167172

@@ -176,8 +181,12 @@ private void testLogger(Consumer<Consumer<MetricsLogger>> methodToTest) {
176181

177182
methodToTest.accept(metricsLogger ->
178183
{
179-
metricsLogger.setDimensions(DimensionSet.of("Dimension1", "Value1"));
180-
metricsLogger.putMetric("Metric1", 1, Unit.COUNT);
184+
try {
185+
metricsLogger.setDimensions(DimensionSet.of("Dimension1", "Value1"));
186+
metricsLogger.putMetric("Metric1", 1, Unit.COUNT);
187+
} catch (InvalidDimensionException | DimensionSetExceededException | InvalidMetricException e) {
188+
throw new RuntimeException(e);
189+
}
181190
});
182191

183192
assertThat(out.toString())

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

+6-1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import com.amazonaws.services.lambda.runtime.Context;
2020
import com.amazonaws.services.lambda.runtime.RequestHandler;
21+
import software.amazon.cloudwatchlogs.emf.exception.InvalidMetricException;
2122
import software.amazon.cloudwatchlogs.emf.logger.MetricsLogger;
2223
import software.amazon.cloudwatchlogs.emf.model.Unit;
2324
import software.amazon.lambda.powertools.metrics.Metrics;
@@ -28,7 +29,11 @@ public class PowertoolsMetricsColdStartEnabledHandler implements RequestHandler<
2829
@Metrics(namespace = "ExampleApplication", service = "booking", captureColdStart = true)
2930
public Object handleRequest(Object input, Context context) {
3031
MetricsLogger metricsLogger = metricsLogger();
31-
metricsLogger.putMetric("Metric1", 1, Unit.BYTES);
32+
try {
33+
metricsLogger.putMetric("Metric1", 1, Unit.BYTES);
34+
} catch (InvalidMetricException e) {
35+
throw new RuntimeException(e);
36+
}
3237

3338
return null;
3439
}

0 commit comments

Comments
 (0)