Skip to content

Commit e3409bb

Browse files
Fixes HTTP tag extraction for API Gateway v1 events (#163)
* catch error if we fail to extract http tags
1 parent 53c40ff commit e3409bb

File tree

4 files changed

+100
-4
lines changed

4 files changed

+100
-4
lines changed

event_samples/api-gateway-v1.json

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
{
2+
"version": "1.0",
3+
"resource": "/my/path",
4+
"path": "/my/path",
5+
"httpMethod": "GET",
6+
"headers": {
7+
"Header1": "value1",
8+
"Header2": "value2"
9+
},
10+
"multiValueHeaders": {
11+
"Header1": [
12+
"value1"
13+
],
14+
"Header2": [
15+
"value1",
16+
"value2"
17+
]
18+
},
19+
"queryStringParameters": {
20+
"parameter1": "value1",
21+
"parameter2": "value"
22+
},
23+
"multiValueQueryStringParameters": {
24+
"parameter1": [
25+
"value1",
26+
"value2"
27+
],
28+
"parameter2": [
29+
"value"
30+
]
31+
},
32+
"requestContext": {
33+
"accountId": "123456789012",
34+
"apiId": "id",
35+
"authorizer": {
36+
"claims": null,
37+
"scopes": null
38+
},
39+
"domainName": "id.execute-api.us-east-1.amazonaws.com",
40+
"domainPrefix": "id",
41+
"extendedRequestId": "request-id",
42+
"httpMethod": "GET",
43+
"identity": {
44+
"accessKey": null,
45+
"accountId": null,
46+
"caller": null,
47+
"cognitoAuthenticationProvider": null,
48+
"cognitoAuthenticationType": null,
49+
"cognitoIdentityId": null,
50+
"cognitoIdentityPoolId": null,
51+
"principalOrgId": null,
52+
"sourceIp": "IP",
53+
"user": null,
54+
"userAgent": "user-agent",
55+
"userArn": null,
56+
"clientCert": {
57+
"clientCertPem": "CERT_CONTENT",
58+
"subjectDN": "www.example.com",
59+
"issuerDN": "Example issuer",
60+
"serialNumber": "a1:a1:a1:a1:a1:a1:a1:a1:a1:a1:a1:a1:a1:a1:a1:a1",
61+
"validity": {
62+
"notBefore": "May 28 12:30:02 2019 GMT",
63+
"notAfter": "Aug 5 09:36:04 2021 GMT"
64+
}
65+
}
66+
},
67+
"path": "/my/path",
68+
"protocol": "HTTP/1.1",
69+
"requestId": "id=",
70+
"requestTime": "04/Mar/2020:19:15:17 +0000",
71+
"requestTimeEpoch": 1583349317135,
72+
"resourceId": null,
73+
"resourcePath": "/my/path",
74+
"stage": "$default"
75+
},
76+
"pathParameters": null,
77+
"stageVariables": null,
78+
"body": "Hello from Lambda!",
79+
"isBase64Encoded": false
80+
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "datadog-lambda-js",
3-
"version": "3.47.0",
3+
"version": "3.48.0",
44
"description": "Lambda client library that supports hybrid tracing in node js",
55
"main": "dist/index.js",
66
"types": "dist/index.d.ts",

src/trace/trigger.spec.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,16 @@ const mockContext = ({
1010

1111
describe("parseEventSource", () => {
1212
const events = [
13+
{
14+
result: {
15+
"function_trigger.event_source": "api-gateway",
16+
"function_trigger.event_source_arn": "arn:aws:apigateway:us-east-1::/restapis/id/stages/$default",
17+
"http.url": "id.execute-api.us-east-1.amazonaws.com",
18+
"http.url_details.path": "/my/path",
19+
"http.method": "GET",
20+
},
21+
file: "api-gateway-v1.json",
22+
},
1323
{
1424
result: {
1525
"function_trigger.event_source": "api-gateway",
@@ -136,7 +146,7 @@ describe("parseEventSource", () => {
136146
for (let response of responses) {
137147
const statusCode = extractHTTPStatusCodeTag(triggerTags, response.responseBody);
138148
// We should always return a status code for API Gateway and ALB
139-
if (["api-gateway.json", "application-load-balancer.json"].includes(event.file)) {
149+
if (["api-gateway-v1.json", "api-gateway.json", "application-load-balancer.json"].includes(event.file)) {
140150
expect(statusCode).toEqual(response.expectedStatusCode);
141151
} else {
142152
expect(statusCode).toBeUndefined();

src/trace/trigger.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,7 @@ function extractHTTPTags(event: APIGatewayEvent | APIGatewayProxyEventV2 | ALBEv
205205
if (event.headers.Referer) {
206206
httpTags["http.referer"] = event.headers.Referer;
207207
}
208+
return httpTags;
208209
}
209210

210211
if (eventType.isAPIGatewayEventV2(event)) {
@@ -215,6 +216,7 @@ function extractHTTPTags(event: APIGatewayEvent | APIGatewayProxyEventV2 | ALBEv
215216
if (event.headers.Referer) {
216217
httpTags["http.referer"] = event.headers.Referer;
217218
}
219+
return httpTags;
218220
}
219221

220222
if (eventType.isALBEvent(event)) {
@@ -223,8 +225,8 @@ function extractHTTPTags(event: APIGatewayEvent | APIGatewayProxyEventV2 | ALBEv
223225
if (event.headers && event.headers.Referer) {
224226
httpTags["http.referer"] = event.headers.Referer;
225227
}
228+
return httpTags;
226229
}
227-
return httpTags;
228230
}
229231

230232
/**
@@ -248,7 +250,11 @@ export function extractTriggerTags(event: any, context: Context) {
248250
}
249251

250252
if (isHTTPTriggerEvent(eventSource)) {
251-
triggerTags = { ...triggerTags, ...extractHTTPTags(event) };
253+
try {
254+
triggerTags = { ...triggerTags, ...extractHTTPTags(event) };
255+
} catch (error) {
256+
logError(`failed to extract http tags from ${eventSource} event`);
257+
}
252258
}
253259
return triggerTags;
254260
}

0 commit comments

Comments
 (0)