12
12
import com .amazonaws .services .lambda .runtime .RequestHandler ;
13
13
import com .amazonaws .services .lambda .runtime .events .APIGatewayProxyRequestEvent ;
14
14
import com .amazonaws .services .lambda .runtime .events .APIGatewayProxyResponseEvent ;
15
+ import com .amazonaws .xray .AWSXRay ;
16
+ import com .amazonaws .xray .entities .Entity ;
15
17
import org .apache .logging .log4j .LogManager ;
16
18
import org .apache .logging .log4j .Logger ;
19
+ import software .amazon .cloudwatchlogs .emf .model .DimensionSet ;
20
+ import software .amazon .cloudwatchlogs .emf .model .Unit ;
21
+ import software .amazon .lambda .powertools .logging .LoggingUtils ;
17
22
import software .amazon .lambda .powertools .logging .Logging ;
18
23
import software .amazon .lambda .powertools .metrics .Metrics ;
24
+ import software .amazon .lambda .powertools .tracing .CaptureMode ;
25
+ import software .amazon .lambda .powertools .tracing .TracingUtils ;
19
26
import software .amazon .lambda .powertools .tracing .Tracing ;
20
27
21
- import static software .amazon .lambda .powertools .tracing .CaptureMode .DISABLED ;
28
+ import static software .amazon .lambda .powertools .metrics .MetricsUtils .metricsLogger ;
29
+ import static software .amazon .lambda .powertools .metrics .MetricsUtils .withSingleMetric ;
30
+ import static software .amazon .lambda .powertools .tracing .TracingUtils .putMetadata ;
31
+ import static software .amazon .lambda .powertools .tracing .TracingUtils .withEntitySubsegment ;
22
32
23
33
/**
24
34
* Handler for requests to Lambda function.
25
35
*/
26
36
public class App implements RequestHandler <APIGatewayProxyRequestEvent , APIGatewayProxyResponseEvent > {
37
+ private final static Logger log = LogManager .getLogger ();
27
38
28
- Logger log = LogManager .getLogger ();
29
-
30
- @ Logging (logEvent = true )
31
- @ Tracing (captureMode = DISABLED )
32
- @ Metrics (captureColdStart = true )
39
+ @ Logging (logEvent = true , samplingRate = 0.7 )
40
+ @ Tracing (captureMode = CaptureMode .RESPONSE_AND_ERROR )
41
+ @ Metrics (namespace = "ServerlessAirline" , service = "payment" , captureColdStart = true )
33
42
public APIGatewayProxyResponseEvent handleRequest (final APIGatewayProxyRequestEvent input , final Context context ) {
34
43
Map <String , String > headers = new HashMap <>();
44
+
35
45
headers .put ("Content-Type" , "application/json" );
36
46
headers .put ("X-Custom-Header" , "application/json" );
37
47
48
+ metricsLogger ().putMetric ("CustomMetric1" , 1 , Unit .COUNT );
49
+
50
+ withSingleMetric ("CustomMetrics2" , 1 , Unit .COUNT , "Another" , (metric ) -> {
51
+ metric .setDimensions (DimensionSet .of ("AnotherService" , "CustomService" ));
52
+ metric .setDimensions (DimensionSet .of ("AnotherService1" , "CustomService1" ));
53
+ });
54
+
55
+ LoggingUtils .appendKey ("test" , "willBeLogged" );
56
+
38
57
APIGatewayProxyResponseEvent response = new APIGatewayProxyResponseEvent ()
39
58
.withHeaders (headers );
40
59
try {
41
60
final String pageContents = this .getPageContents ("https://checkip.amazonaws.com" );
61
+ log .info (pageContents );
62
+ TracingUtils .putAnnotation ("Test" , "New" );
42
63
String output = String .format ("{ \" message\" : \" hello world\" , \" location\" : \" %s\" }" , pageContents );
43
64
65
+ TracingUtils .withSubsegment ("loggingResponse" , subsegment -> {
66
+ String sampled = "log something out" ;
67
+ log .info (sampled );
68
+ log .info (output );
69
+ });
70
+
71
+ threadOption1 ();
72
+
73
+ threadOption2 ();
74
+
75
+ log .info ("After output" );
44
76
return response
45
77
.withStatusCode (200 )
46
78
.withBody (output );
47
- } catch (IOException e ) {
79
+ } catch (IOException | InterruptedException e ) {
48
80
return response
49
81
.withBody ("{}" )
50
82
.withStatusCode (500 );
51
83
}
52
84
}
53
85
54
- @ Tracing (namespace = "getPageContents" )
86
+ private void threadOption1 () throws InterruptedException {
87
+ final Entity traceEntity = AWSXRay .getTraceEntity ();
88
+ assert traceEntity != null ;
89
+ traceEntity .run (new Thread (this ::log ));
90
+ }
91
+
92
+ private void threadOption2 () throws InterruptedException {
93
+ Entity traceEntity = AWSXRay .getTraceEntity ();
94
+ Thread anotherThread = new Thread (() -> withEntitySubsegment ("inlineLog" , traceEntity , subsegment -> {
95
+ String var = "somethingToProcess" ;
96
+ log .info ("inside threaded logging inline {}" , var );
97
+ }));
98
+ anotherThread .start ();
99
+ anotherThread .join ();
100
+ }
101
+
102
+ @ Tracing
103
+ private void log () {
104
+ log .info ("inside threaded logging for function" );
105
+ }
106
+
107
+ @ Tracing (namespace = "getPageContents" , captureMode = CaptureMode .DISABLED )
55
108
private String getPageContents (String address ) throws IOException {
56
- log .info ("Retrieving {}" , address );
57
109
URL url = new URL (address );
110
+ putMetadata ("getPageContents" , address );
58
111
try (BufferedReader br = new BufferedReader (new InputStreamReader (url .openStream ()))) {
59
112
return br .lines ().collect (Collectors .joining (System .lineSeparator ()));
60
113
}
61
114
}
62
- }
115
+ }
0 commit comments