Skip to content

Commit 76bad50

Browse files
committed
Updated tests and implementation to support the response event.
1 parent 6eae382 commit 76bad50

File tree

2 files changed

+95
-0
lines changed

2 files changed

+95
-0
lines changed

src/main/java/com/newrelic/opentracing/aws/ResponseParser.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
package com.newrelic.opentracing.aws;
77

88
import com.amazonaws.services.lambda.runtime.events.APIGatewayProxyResponseEvent;
9+
import com.amazonaws.services.lambda.runtime.events.APIGatewayV2ProxyResponseEvent;
910
import io.opentracing.Span;
1011
import java.util.Map;
1112

@@ -34,6 +35,12 @@ public static <Output> void parseResponse(Output response, Span span) {
3435
if (response != null) {
3536
statusCode = apiGatewayProxyResponseEvent.getStatusCode() + "";
3637
}
38+
} else if (response instanceof APIGatewayV2ProxyResponseEvent) {
39+
final APIGatewayV2ProxyResponseEvent apiGatewayV2ProxyResponseEvent =
40+
(APIGatewayV2ProxyResponseEvent) response;
41+
if (response != null) {
42+
statusCode = apiGatewayV2ProxyResponseEvent.getStatusCode() + "";
43+
}
3744
}
3845

3946
if (statusCode != null && !statusCode.isEmpty()) {

src/test/java/com/newrelic/opentracing/aws/TracingRequestHandlerTest.java

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@
1010
import com.amazonaws.services.lambda.runtime.Context;
1111
import com.amazonaws.services.lambda.runtime.LambdaLogger;
1212
import com.amazonaws.services.lambda.runtime.events.APIGatewayProxyRequestEvent;
13+
import com.amazonaws.services.lambda.runtime.events.APIGatewayProxyResponseEvent;
1314
import com.amazonaws.services.lambda.runtime.events.APIGatewayV2ProxyRequestEvent;
15+
import com.amazonaws.services.lambda.runtime.events.APIGatewayV2ProxyResponseEvent;
1416
import com.amazonaws.services.lambda.runtime.events.CloudFrontEvent;
1517
import com.amazonaws.services.lambda.runtime.events.CloudWatchLogsEvent;
1618
import com.amazonaws.services.lambda.runtime.events.CodeCommitEvent;
@@ -254,6 +256,29 @@ public void testAPIGatewayProxyRequestEvent() {
254256
"APIGatewayProxyRequestEventUserARN", span.tags().get("aws.lambda.eventSource.arn"));
255257
}
256258

259+
@Test
260+
public void testAPIGatewayProxyRequestResponseEvent() {
261+
int expectedStatusCode = 200;
262+
final MyApiGatewayProxyRequestResponseHandler myApiGatewayProxyRequestHandler =
263+
new MyApiGatewayProxyRequestResponseHandler(expectedStatusCode);
264+
265+
final APIGatewayProxyRequestEvent apiGatewayProxyRequestEvent =
266+
new APIGatewayProxyRequestEvent();
267+
final APIGatewayProxyRequestEvent.RequestIdentity requestIdentity =
268+
new APIGatewayProxyRequestEvent.RequestIdentity();
269+
requestIdentity.setUserArn("APIGatewayProxyRequestEventUserARN");
270+
final APIGatewayProxyRequestEvent.ProxyRequestContext proxyRequestContext =
271+
new APIGatewayProxyRequestEvent.ProxyRequestContext();
272+
proxyRequestContext.setIdentity(requestIdentity);
273+
apiGatewayProxyRequestEvent.setRequestContext(proxyRequestContext);
274+
275+
myApiGatewayProxyRequestHandler.handleRequest(apiGatewayProxyRequestEvent, createContext());
276+
final MockSpan span = mockTracer.finishedSpans().get(0);
277+
Assert.assertEquals(
278+
"APIGatewayProxyRequestEventUserARN", span.tags().get("aws.lambda.eventSource.arn"));
279+
Assert.assertEquals(Integer.toString(expectedStatusCode), span.tags().get("http.status_code"));
280+
}
281+
257282
@Test
258283
public void testAPIGatewayV2ProxyRequestEvent() {
259284
final MyApiGatewayV2ProxyRequestHandler myApiGatewayV2ProxyRequestHandler =
@@ -273,6 +298,30 @@ public void testAPIGatewayV2ProxyRequestEvent() {
273298
final MockSpan span = mockTracer.finishedSpans().get(0);
274299
Assert.assertEquals(
275300
"APIGatewayV2ProxyRequestEventUserARN", span.tags().get("aws.lambda.eventSource.arn"));
301+
Assert.assertFalse(span.tags().containsKey("http.status_code"));
302+
}
303+
304+
@Test
305+
public void testAPIGatewayV2ProxyRequestResponseEvent() {
306+
int expectedStatusCode = 200;
307+
final MyApiGatewayV2ProxyRequestResponseHandler myApiGatewayV2ProxyRequestHandler =
308+
new MyApiGatewayV2ProxyRequestResponseHandler(expectedStatusCode);
309+
310+
final APIGatewayV2ProxyRequestEvent apiGatewayV2ProxyRequestEvent =
311+
new APIGatewayV2ProxyRequestEvent();
312+
final APIGatewayV2ProxyRequestEvent.RequestIdentity requestIdentity =
313+
new APIGatewayV2ProxyRequestEvent.RequestIdentity();
314+
requestIdentity.setUserArn("APIGatewayV2ProxyRequestEventUserARN");
315+
final APIGatewayV2ProxyRequestEvent.RequestContext proxyRequestContext =
316+
new APIGatewayV2ProxyRequestEvent.RequestContext();
317+
proxyRequestContext.setIdentity(requestIdentity);
318+
apiGatewayV2ProxyRequestEvent.setRequestContext(proxyRequestContext);
319+
320+
myApiGatewayV2ProxyRequestHandler.handleRequest(apiGatewayV2ProxyRequestEvent, createContext());
321+
final MockSpan span = mockTracer.finishedSpans().get(0);
322+
Assert.assertEquals(
323+
"APIGatewayV2ProxyRequestEventUserARN", span.tags().get("aws.lambda.eventSource.arn"));
324+
Assert.assertEquals(Integer.toString(expectedStatusCode), span.tags().get("http.status_code"));
276325
}
277326

278327
@Test
@@ -395,6 +444,25 @@ public Object doHandleRequest(
395444
}
396445
}
397446

447+
static class MyApiGatewayProxyRequestResponseHandler
448+
implements TracingRequestHandler<APIGatewayProxyRequestEvent, APIGatewayProxyResponseEvent> {
449+
450+
private int statusCode;
451+
452+
public MyApiGatewayProxyRequestResponseHandler(int statusCode) {
453+
this.statusCode = statusCode;
454+
}
455+
456+
@Override
457+
public APIGatewayProxyResponseEvent doHandleRequest(
458+
APIGatewayProxyRequestEvent apiGatewayV2ProxyRequestEvent, Context context) {
459+
APIGatewayProxyResponseEvent responseEvent = new APIGatewayProxyResponseEvent();
460+
responseEvent.setStatusCode(statusCode);
461+
responseEvent.setBody("null");
462+
return responseEvent;
463+
}
464+
}
465+
398466
static class MyApiGatewayV2ProxyRequestHandler
399467
implements TracingRequestHandler<APIGatewayV2ProxyRequestEvent, Object> {
400468

@@ -405,6 +473,26 @@ public Object doHandleRequest(
405473
}
406474
}
407475

476+
static class MyApiGatewayV2ProxyRequestResponseHandler
477+
implements TracingRequestHandler<
478+
APIGatewayV2ProxyRequestEvent, APIGatewayV2ProxyResponseEvent> {
479+
480+
private int statusCode;
481+
482+
public MyApiGatewayV2ProxyRequestResponseHandler(int statusCode) {
483+
this.statusCode = statusCode;
484+
}
485+
486+
@Override
487+
public APIGatewayV2ProxyResponseEvent doHandleRequest(
488+
APIGatewayV2ProxyRequestEvent apiGatewayV2ProxyRequestEvent, Context context) {
489+
APIGatewayV2ProxyResponseEvent responseEvent = new APIGatewayV2ProxyResponseEvent();
490+
responseEvent.setStatusCode(statusCode);
491+
responseEvent.setBody("null");
492+
return responseEvent;
493+
}
494+
}
495+
408496
// Cloud Front
409497
static class MyCloudFrontRequestHandler
410498
implements TracingRequestHandler<CloudFrontEvent, Object> {

0 commit comments

Comments
 (0)