Skip to content

Commit 6eae382

Browse files
committed
Addresses GitHub Issue #24 - Implements support for APIGatewayV2 Events.
Signed-off-by: Andrew Garbutt <agarbutt@users.noreply.github.com>
1 parent 118cf5c commit 6eae382

File tree

3 files changed

+55
-2
lines changed

3 files changed

+55
-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/test/java/com/newrelic/opentracing/aws/TracingRequestHandlerTest.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
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.APIGatewayV2ProxyRequestEvent;
1314
import com.amazonaws.services.lambda.runtime.events.CloudFrontEvent;
1415
import com.amazonaws.services.lambda.runtime.events.CloudWatchLogsEvent;
1516
import com.amazonaws.services.lambda.runtime.events.CodeCommitEvent;
@@ -253,6 +254,27 @@ public void testAPIGatewayProxyRequestEvent() {
253254
"APIGatewayProxyRequestEventUserARN", span.tags().get("aws.lambda.eventSource.arn"));
254255
}
255256

257+
@Test
258+
public void testAPIGatewayV2ProxyRequestEvent() {
259+
final MyApiGatewayV2ProxyRequestHandler myApiGatewayV2ProxyRequestHandler =
260+
new MyApiGatewayV2ProxyRequestHandler();
261+
262+
final APIGatewayV2ProxyRequestEvent apiGatewayV2ProxyRequestEvent =
263+
new APIGatewayV2ProxyRequestEvent();
264+
final APIGatewayV2ProxyRequestEvent.RequestIdentity requestIdentity =
265+
new APIGatewayV2ProxyRequestEvent.RequestIdentity();
266+
requestIdentity.setUserArn("APIGatewayV2ProxyRequestEventUserARN");
267+
final APIGatewayV2ProxyRequestEvent.RequestContext proxyRequestContext =
268+
new APIGatewayV2ProxyRequestEvent.RequestContext();
269+
proxyRequestContext.setIdentity(requestIdentity);
270+
apiGatewayV2ProxyRequestEvent.setRequestContext(proxyRequestContext);
271+
272+
myApiGatewayV2ProxyRequestHandler.handleRequest(apiGatewayV2ProxyRequestEvent, createContext());
273+
final MockSpan span = mockTracer.finishedSpans().get(0);
274+
Assert.assertEquals(
275+
"APIGatewayV2ProxyRequestEventUserARN", span.tags().get("aws.lambda.eventSource.arn"));
276+
}
277+
256278
@Test
257279
@Ignore("We would like this but there doesn't seem to be an available arn currently")
258280
public void testCloudWatchLogsEvent() {
@@ -373,6 +395,16 @@ public Object doHandleRequest(
373395
}
374396
}
375397

398+
static class MyApiGatewayV2ProxyRequestHandler
399+
implements TracingRequestHandler<APIGatewayV2ProxyRequestEvent, Object> {
400+
401+
@Override
402+
public Object doHandleRequest(
403+
APIGatewayV2ProxyRequestEvent apiGatewayV2ProxyRequestEvent, Context context) {
404+
return "null";
405+
}
406+
}
407+
376408
// Cloud Front
377409
static class MyCloudFrontRequestHandler
378410
implements TracingRequestHandler<CloudFrontEvent, Object> {

0 commit comments

Comments
 (0)