34
34
import java .util .stream .Collectors ;
35
35
import org .apache .logging .log4j .LogManager ;
36
36
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 ;
37
40
import software .amazon .cloudwatchlogs .emf .model .DimensionSet ;
38
41
import software .amazon .cloudwatchlogs .emf .model .Unit ;
39
42
import software .amazon .lambda .powertools .logging .Logging ;
47
50
* Handler for requests to Lambda function.
48
51
*/
49
52
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 );
51
54
52
55
@ Logging (logEvent = true , samplingRate = 0.7 )
53
56
@ Tracing (captureMode = CaptureMode .RESPONSE_AND_ERROR )
@@ -58,12 +61,20 @@ public APIGatewayProxyResponseEvent handleRequest(final APIGatewayProxyRequestEv
58
61
headers .put ("Content-Type" , "application/json" );
59
62
headers .put ("X-Custom-Header" , "application/json" );
60
63
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
+ }
62
69
63
- withSingleMetric ("CustomMetrics2" , 1 , Unit .COUNT , "Another" , ( metric ) ->
70
+ withSingleMetric ("CustomMetrics2" , 1 , Unit .COUNT , "Another" , metric ->
64
71
{
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
+ }
67
78
});
68
79
69
80
LoggingUtils .appendKey ("test" , "willBeLogged" );
0 commit comments