@@ -59,15 +59,18 @@ public class App implements RequestHandler<APIGatewayProxyRequestEvent, APIGatew
5959 "Cache Init" , App ::initializeCache );
6060
6161 public APIGatewayProxyResponseEvent handleRequest (APIGatewayProxyRequestEvent input , final Context context ) {
62- final Subsegment handlerSubsegment = AWSXRay .beginSubsegment ("Lambda handleRequest" );
6362 final String responseType = input .getHeaders ().getOrDefault ("Accept" , "application/xml" );
6463 final APIGatewayProxyResponseEvent response = new APIGatewayProxyResponseEvent ()
6564 .withHeaders (new HashMap <>());
6665 response .getHeaders ().put ("Content-Type" , "text/plain" );
6766
6867 try {
6968 final String bodyDigest = AWSXRay .createSubsegment (
70- "Message Digest" , () -> computeMessageDigest (input .getBody ()));
69+ "Message Digest" , (subsegment ) -> {
70+ String rv = computeMessageDigest (input .getBody ());
71+ subsegment .putMetadata ("SHA256" , rv );
72+ return rv ;
73+ });
7174 response .getHeaders ().put ("x-zae-gate-cache" , "HIT" );
7275 final Document doc = cacheComputeIfNull (
7376 bodyDigest ,
@@ -89,22 +92,34 @@ public APIGatewayProxyResponseEvent handleRequest(APIGatewayProxyRequestEvent in
8992 return rv ;
9093 }
9194 );
95+ AWSXRay .beginSubsegment ("Gate Export" );
96+ AWSXRay .getCurrentSubsegment ().putMetadata ("Content-Type" , responseType );
9297 try {
9398 response .getHeaders ().put ("Content-Type" , responseType );
9499 return response .withBody (export (doc , responseType )).withStatusCode (200 );
95100 } finally {
96101 Factory .deleteResource (doc );
102+ AWSXRay .endSubsegment ();
97103 }
98104 } catch (GateException e ) {
99105 logger .error (e );
100- handlerSubsegment .addException (e );
106+ AWSXRay . getCurrentSubsegment () .addException (e );
101107 return response .withBody (e .getMessage ()).withStatusCode (400 );
102108 } catch (IOException e ) {
103109 logger .error (e );
104- handlerSubsegment .addException (e );
110+ AWSXRay . getCurrentSubsegment () .addException (e );
105111 return response .withBody (e .getMessage ()).withStatusCode (406 );
106- } finally {
107- AWSXRay .endSubsegment ();
112+ }
113+ }
114+
115+ private void cachePutDocument (String key , Document doc ) {
116+ try {
117+ DiskLruCache .Editor editor = cache .edit (key );
118+ editor .set (0 , doc .toXml ());
119+ editor .commit ();
120+ } catch (IOException e ) {
121+ logger .warn (e );
122+ AWSXRay .getCurrentSubsegment ().addException (e );
108123 }
109124 }
110125
@@ -113,28 +128,24 @@ private Document cacheComputeIfNull(String key, TextProcessor processor) throws
113128 final DiskLruCache .Snapshot snapshot = cache .get (key );
114129 if (snapshot == null ) {
115130 final Document doc = processor .process ();
116- AWSXRay .createSubsegment ("Cache Edit" , (subsegment )-> {
117- try {
118- DiskLruCache .Editor editor = cache .edit (key );
119- editor .set (0 , doc .toXml ());
120- editor .commit ();
121- } catch (IOException e ) {
122- subsegment .addException (e );
123- logger .warn (e );
124- }
125- });
131+ AWSXRay .createSubsegment ("Cache Edit" , () -> cachePutDocument (key , doc ));
126132 return doc ;
127133 } else {
134+ AWSXRay .beginSubsegment ("Cache Read" );
128135 try {
129136 return Utils .xmlToDocument (new InputStreamReader (snapshot .getInputStream (0 )));
130137 } catch (ResourceInstantiationException | XMLStreamException e ) {
131138 logger .warn (e );
139+ AWSXRay .getCurrentSubsegment ().addException (e );
132140 cache .remove (key );
133141 return processor .process ();
142+ } finally {
143+ AWSXRay .endSubsegment ();
134144 }
135145 }
136146 } catch (IOException e ) {
137147 logger .warn (e );
148+ AWSXRay .getCurrentSubsegment ().addException (e );
138149 return processor .process ();
139150 }
140151 }
@@ -160,15 +171,20 @@ private String export(Document doc, String responseType) throws IOException {
160171 // Take *all* annotation types.
161172 final AnnotationSet defaultAnnots = doc .getAnnotations ();
162173 final HashSet <String > annotationTypes = new HashSet <>();
163- for (Annotation annotation : defaultAnnots .inDocumentOrder ()) {
174+ for (Annotation annotation : defaultAnnots .inDocumentOrder ()) {
164175 annotationTypes .add (annotation .getType ());
165176 }
166177 exportOptions .put ("annotationTypes" , annotationTypes );
167178
168- final ByteArrayOutputStream baos = new ByteArrayOutputStream ();
169179 if (responseType .equals ("application/json" )) {
180+ final ByteArrayOutputStream baos = new ByteArrayOutputStream ();
170181 gateJsonExporter .setAnnotationTypes (doc .getAnnotationSetNames ());
171- gateJsonExporter .export (doc , baos , exportOptions );
182+ try {
183+ gateJsonExporter .export (doc , baos , exportOptions );
184+ } catch (IOException e ) {
185+ AWSXRay .getCurrentSubsegment ().addException (e );
186+ throw e ;
187+ }
172188 return baos .toString ();
173189 } else {
174190 return doc .toXml ();
0 commit comments