Skip to content

Commit 2e49253

Browse files
committed
update example and e2e test
1 parent 046b7f6 commit 2e49253

File tree

2 files changed

+33
-8
lines changed
  • examples/powertools-examples-core/src/main/java/helloworld
  • powertools-e2e-tests/handlers/metrics/src/main/java/software/amazon/lambda/powertools/e2e

2 files changed

+33
-8
lines changed

examples/powertools-examples-core/src/main/java/helloworld/App.java

+16-5
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@
3434
import java.util.stream.Collectors;
3535
import org.apache.logging.log4j.LogManager;
3636
import org.apache.logging.log4j.Logger;
37+
import software.amazon.cloudwatchlogs.emf.exception.DimensionSetExceededException;
38+
import software.amazon.cloudwatchlogs.emf.exception.InvalidDimensionException;
39+
import software.amazon.cloudwatchlogs.emf.exception.InvalidMetricException;
3740
import software.amazon.cloudwatchlogs.emf.model.DimensionSet;
3841
import software.amazon.cloudwatchlogs.emf.model.Unit;
3942
import software.amazon.lambda.powertools.logging.Logging;
@@ -47,7 +50,7 @@
4750
* Handler for requests to Lambda function.
4851
*/
4952
public class App implements RequestHandler<APIGatewayProxyRequestEvent, APIGatewayProxyResponseEvent> {
50-
private final static Logger log = LogManager.getLogger(App.class);
53+
private static final Logger log = LogManager.getLogger(App.class);
5154

5255
@Logging(logEvent = true, samplingRate = 0.7)
5356
@Tracing(captureMode = CaptureMode.RESPONSE_AND_ERROR)
@@ -58,12 +61,20 @@ public APIGatewayProxyResponseEvent handleRequest(final APIGatewayProxyRequestEv
5861
headers.put("Content-Type", "application/json");
5962
headers.put("X-Custom-Header", "application/json");
6063

61-
metricsLogger().putMetric("CustomMetric1", 1, Unit.COUNT);
64+
try {
65+
metricsLogger().putMetric("CustomMetric1", 1, Unit.COUNT);
66+
} catch (InvalidMetricException e) {
67+
log.error(e);
68+
}
6269

63-
withSingleMetric("CustomMetrics2", 1, Unit.COUNT, "Another", (metric) ->
70+
withSingleMetric("CustomMetrics2", 1, Unit.COUNT, "Another", metric ->
6471
{
65-
metric.setDimensions(DimensionSet.of("AnotherService", "CustomService"));
66-
metric.setDimensions(DimensionSet.of("AnotherService1", "CustomService1"));
72+
try {
73+
metric.setDimensions(DimensionSet.of("AnotherService", "CustomService"));
74+
metric.setDimensions(DimensionSet.of("AnotherService1", "CustomService1"));
75+
} catch (InvalidDimensionException | DimensionSetExceededException e) {
76+
log.error(e);
77+
}
6778
});
6879

6980
LoggingUtils.appendKey("test", "willBeLogged");

powertools-e2e-tests/handlers/metrics/src/main/java/software/amazon/lambda/powertools/e2e/Function.java

+17-3
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@
1616

1717
import com.amazonaws.services.lambda.runtime.Context;
1818
import com.amazonaws.services.lambda.runtime.RequestHandler;
19+
import software.amazon.cloudwatchlogs.emf.exception.DimensionSetExceededException;
20+
import software.amazon.cloudwatchlogs.emf.exception.InvalidDimensionException;
21+
import software.amazon.cloudwatchlogs.emf.exception.InvalidMetricException;
1922
import software.amazon.cloudwatchlogs.emf.logger.MetricsLogger;
2023
import software.amazon.cloudwatchlogs.emf.model.DimensionSet;
2124
import software.amazon.cloudwatchlogs.emf.model.Unit;
@@ -30,10 +33,21 @@ public class Function implements RequestHandler<Input, String> {
3033
public String handleRequest(Input input, Context context) {
3134

3235
DimensionSet dimensionSet = new DimensionSet();
33-
input.getDimensions().forEach((key, value) -> dimensionSet.addDimension(key, value));
36+
input.getDimensions().forEach((key, value) -> {
37+
try {
38+
dimensionSet.addDimension(key, value);
39+
} catch (InvalidDimensionException | DimensionSetExceededException e) {
40+
// log or something
41+
}
42+
});
3443
metricsLogger.putDimensions(dimensionSet);
35-
36-
input.getMetrics().forEach((key, value) -> metricsLogger.putMetric(key, value, Unit.COUNT));
44+
input.getMetrics().forEach((key, value) -> {
45+
try {
46+
metricsLogger.putMetric(key, value, Unit.COUNT);
47+
} catch (InvalidMetricException e) {
48+
// log or something
49+
}
50+
});
3751

3852
return "OK";
3953
}

0 commit comments

Comments
 (0)