Skip to content

Fix API Gateway type guards #165

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Feb 16, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions event_samples/api-gateway-v2.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
{
"version": "2.0",
"routeKey": "ANY /nodejs-apig-function-1G3XMPLZXVXYI",
"rawPath": "/default/nodejs-apig-function-1G3XMPLZXVXYI",
"rawQueryString": "",
"cookies": [
"s_fid=7AABXMPL1AFD9BBF-0643XMPL09956DE2",
"regStatus=pre-register"
],
"headers": {
"accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9",
"accept-encoding": "gzip, deflate, br",
"accept-language": "en-US,en;q=0.9",
"content-length": "0",
"host": "r3pmxmplak.execute-api.us-east-2.amazonaws.com",
"sec-fetch-dest": "document",
"sec-fetch-mode": "navigate",
"sec-fetch-site": "cross-site",
"sec-fetch-user": "?1",
"upgrade-insecure-requests": "1",
"user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.132 Safari/537.36",
"x-amzn-trace-id": "Root=1-5e6722a7-cc56xmpl46db7ae02d4da47e",
"x-forwarded-for": "205.255.255.176",
"x-forwarded-port": "443",
"x-forwarded-proto": "https"
},
"requestContext": {
"accountId": "123456789012",
"apiId": "r3pmxmplak",
"domainName": "r3pmxmplak.execute-api.us-east-2.amazonaws.com",
"domainPrefix": "r3pmxmplak",
"http": {
"method": "GET",
"path": "/default/nodejs-apig-function-1G3XMPLZXVXYI",
"protocol": "HTTP/1.1",
"sourceIp": "205.255.255.176",
"userAgent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.132 Safari/537.36"
},
"requestId": "JKJaXmPLvHcESHA=",
"routeKey": "ANY /nodejs-apig-function-1G3XMPLZXVXYI",
"stage": "default",
"time": "10/Mar/2020:05:16:23 +0000",
"timeEpoch": 1583817383220
},
"isBase64Encoded": true
}
124 changes: 0 additions & 124 deletions event_samples/api-gateway.json

This file was deleted.

12 changes: 6 additions & 6 deletions src/trace/trigger.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@ describe("parseEventSource", () => {
{
result: {
"function_trigger.event_source": "api-gateway",
"function_trigger.event_source_arn": "arn:aws:apigateway:us-east-1::/restapis/1234567890/stages/prod",
"http.url": "70ixmpl4fl.execute-api.us-east-2.amazonaws.com",
"http.url_details.path": "/prod/path/to/resource",
"http.method": "POST",
"function_trigger.event_source_arn": "arn:aws:apigateway:us-east-1::/restapis/r3pmxmplak/stages/default",
"http.url": "r3pmxmplak.execute-api.us-east-2.amazonaws.com",
"http.url_details.path": "/default/nodejs-apig-function-1G3XMPLZXVXYI",
"http.method": "GET",
},
file: "api-gateway.json",
file: "api-gateway-v2.json",
},
{
result: {
Expand Down Expand Up @@ -146,7 +146,7 @@ describe("parseEventSource", () => {
for (let response of responses) {
const statusCode = extractHTTPStatusCodeTag(triggerTags, response.responseBody);
// We should always return a status code for API Gateway and ALB
if (["api-gateway-v1.json", "api-gateway.json", "application-load-balancer.json"].includes(event.file)) {
if (["api-gateway-v1.json", "api-gateway-v2.json", "application-load-balancer.json"].includes(event.file)) {
expect(statusCode).toEqual(response.expectedStatusCode);
} else {
expect(statusCode).toBeUndefined();
Expand Down
4 changes: 2 additions & 2 deletions src/trace/trigger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ function extractHTTPTags(event: APIGatewayEvent | APIGatewayProxyEventV2 | ALBEv
}
httpTags["http.url_details.path"] = requestContext.path;
httpTags["http.method"] = requestContext.httpMethod;
if (event.headers.Referer) {
if (event.headers?.Referer) {
httpTags["http.referer"] = event.headers.Referer;
}
return httpTags;
Expand All @@ -213,7 +213,7 @@ function extractHTTPTags(event: APIGatewayEvent | APIGatewayProxyEventV2 | ALBEv
httpTags["http.url"] = requestContext.domainName;
httpTags["http.url_details.path"] = requestContext.http.path;
httpTags["http.method"] = requestContext.http.method;
if (event.headers.Referer) {
if (event.headers?.Referer) {
httpTags["http.referer"] = event.headers.Referer;
}
return httpTags;
Expand Down
4 changes: 2 additions & 2 deletions src/utils/event-type-guards.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ import {
} from "aws-lambda";

export function isAPIGatewayEvent(event: any): event is APIGatewayEvent {
return event.requestContext !== undefined && event.requestContext.stage !== undefined;
return event.requestContext?.stage !== undefined && event.httpMethod !== undefined && event.resource !== undefined;
}

export function isAPIGatewayEventV2(event: any): event is APIGatewayProxyEventV2 {
return event.requestContext !== undefined && event.version !== undefined;
return event.requestContext !== undefined && event.version === "2.0" && event.rawQueryString !== undefined;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Small nit: maybe declare "2.0" as a constant in src/trace/constants.ts

}

export function isALBEvent(event: any): event is ALBEvent {
Expand Down