Skip to content

Commit 236ea52

Browse files
authored
Merge pull request #25 from agarbutt/APIGatewayV2Proxy-Events
Addresses GitHub Issue #24 - Implements support for APIGatewayV2 Events.
2 parents 7944e08 + 76bad50 commit 236ea52

File tree

4 files changed

+150
-2
lines changed

4 files changed

+150
-2
lines changed

build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ targetCompatibility = JavaVersion.VERSION_1_8
3737

3838
dependencies {
3939
implementation 'com.amazonaws:aws-lambda-java-core:1.1.0'
40-
// 2.2.2 is earliest version that has all needed event sources
41-
implementation 'com.amazonaws:aws-lambda-java-events:2.2.2'
40+
// 2.2.7 is earliest version that has all needed event sources
41+
implementation 'com.amazonaws:aws-lambda-java-events:2.2.7'
4242
implementation 'com.amazonaws:aws-java-sdk-s3:1.11.163'
4343
implementation 'com.amazonaws:aws-java-sdk-kinesis:1.11.163'
4444
implementation 'com.amazonaws:aws-java-sdk-dynamodb:1.11.163'

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

Lines changed: 21 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.APIGatewayProxyRequestEvent;
9+
import com.amazonaws.services.lambda.runtime.events.APIGatewayV2ProxyRequestEvent;
910
import com.amazonaws.services.lambda.runtime.events.CodeCommitEvent;
1011
import com.amazonaws.services.lambda.runtime.events.DynamodbEvent;
1112
import com.amazonaws.services.lambda.runtime.events.KinesisEvent;
@@ -49,6 +50,8 @@ static String parseEventSourceArn(Object object) {
4950
return parseCodeCommitEventSourceArn(object);
5051
} else if (object instanceof APIGatewayProxyRequestEvent) {
5152
return parseAPIGatewayProxyRequestEventUserArn(object);
53+
} else if (object instanceof APIGatewayV2ProxyRequestEvent) {
54+
return parseAPIGatewayV2ProxyRequestEventUserArn(object);
5255
}
5356
return null;
5457
}
@@ -221,4 +224,22 @@ private static String parseAPIGatewayProxyRequestEventUserArn(Object object) {
221224

222225
return identity.getUserArn();
223226
}
227+
228+
private static String parseAPIGatewayV2ProxyRequestEventUserArn(Object object) {
229+
APIGatewayV2ProxyRequestEvent apiGatewayV2ProxyRequestEvent =
230+
(APIGatewayV2ProxyRequestEvent) object;
231+
232+
APIGatewayV2ProxyRequestEvent.RequestContext requestContext =
233+
apiGatewayV2ProxyRequestEvent.getRequestContext();
234+
if (requestContext == null) {
235+
return null;
236+
}
237+
238+
APIGatewayV2ProxyRequestEvent.RequestIdentity identity = requestContext.getIdentity();
239+
if (identity == null) {
240+
return null;
241+
}
242+
243+
return identity.getUserArn();
244+
}
224245
}

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: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +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;
14+
import com.amazonaws.services.lambda.runtime.events.APIGatewayV2ProxyRequestEvent;
15+
import com.amazonaws.services.lambda.runtime.events.APIGatewayV2ProxyResponseEvent;
1316
import com.amazonaws.services.lambda.runtime.events.CloudFrontEvent;
1417
import com.amazonaws.services.lambda.runtime.events.CloudWatchLogsEvent;
1518
import com.amazonaws.services.lambda.runtime.events.CodeCommitEvent;
@@ -253,6 +256,74 @@ public void testAPIGatewayProxyRequestEvent() {
253256
"APIGatewayProxyRequestEventUserARN", span.tags().get("aws.lambda.eventSource.arn"));
254257
}
255258

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+
282+
@Test
283+
public void testAPIGatewayV2ProxyRequestEvent() {
284+
final MyApiGatewayV2ProxyRequestHandler myApiGatewayV2ProxyRequestHandler =
285+
new MyApiGatewayV2ProxyRequestHandler();
286+
287+
final APIGatewayV2ProxyRequestEvent apiGatewayV2ProxyRequestEvent =
288+
new APIGatewayV2ProxyRequestEvent();
289+
final APIGatewayV2ProxyRequestEvent.RequestIdentity requestIdentity =
290+
new APIGatewayV2ProxyRequestEvent.RequestIdentity();
291+
requestIdentity.setUserArn("APIGatewayV2ProxyRequestEventUserARN");
292+
final APIGatewayV2ProxyRequestEvent.RequestContext proxyRequestContext =
293+
new APIGatewayV2ProxyRequestEvent.RequestContext();
294+
proxyRequestContext.setIdentity(requestIdentity);
295+
apiGatewayV2ProxyRequestEvent.setRequestContext(proxyRequestContext);
296+
297+
myApiGatewayV2ProxyRequestHandler.handleRequest(apiGatewayV2ProxyRequestEvent, createContext());
298+
final MockSpan span = mockTracer.finishedSpans().get(0);
299+
Assert.assertEquals(
300+
"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"));
325+
}
326+
256327
@Test
257328
@Ignore("We would like this but there doesn't seem to be an available arn currently")
258329
public void testCloudWatchLogsEvent() {
@@ -373,6 +444,55 @@ public Object doHandleRequest(
373444
}
374445
}
375446

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+
466+
static class MyApiGatewayV2ProxyRequestHandler
467+
implements TracingRequestHandler<APIGatewayV2ProxyRequestEvent, Object> {
468+
469+
@Override
470+
public Object doHandleRequest(
471+
APIGatewayV2ProxyRequestEvent apiGatewayV2ProxyRequestEvent, Context context) {
472+
return "null";
473+
}
474+
}
475+
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+
376496
// Cloud Front
377497
static class MyCloudFrontRequestHandler
378498
implements TracingRequestHandler<CloudFrontEvent, Object> {

0 commit comments

Comments
 (0)