Skip to content

Commit d812fb6

Browse files
fix: use RouteKey in resource path for API Gateway inferred spans (#442)
* use route key * Align with js implementation. * Update existing tests. * Add new parametrized tests. * Revert v1 to original. --------- Co-authored-by: Rey Abolofia <[email protected]>
1 parent 4669117 commit d812fb6

File tree

4 files changed

+209
-4
lines changed

4 files changed

+209
-4
lines changed

datadog_lambda/tracing.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -893,7 +893,8 @@ def create_inferred_span_from_api_gateway_event(
893893
)
894894
method = event.get("httpMethod")
895895
path = event.get("path")
896-
resource = "{0} {1}".format(method, path)
896+
resource_path = _get_resource_path(event, request_context)
897+
resource = "{0} {1}".format(method, resource_path)
897898
tags = {
898899
"operation_name": "aws.apigateway.rest",
899900
"http.url": domain + path,
@@ -936,6 +937,16 @@ def create_inferred_span_from_api_gateway_event(
936937
return span
937938

938939

940+
def _get_resource_path(event, request_context):
941+
route_key = request_context.get("routeKey") or ""
942+
if "{" in route_key:
943+
try:
944+
return route_key.split(" ")[1]
945+
except Exception as e:
946+
logger.debug("Error parsing routeKey: %s", e)
947+
return event.get("rawPath") or request_context.get("resourcePath") or route_key
948+
949+
939950
def create_inferred_span_from_http_api_event(
940951
event, context, decode_authorizer_context: bool = True
941952
):
@@ -947,7 +958,8 @@ def create_inferred_span_from_http_api_event(
947958
)
948959
method = request_context.get("http", {}).get("method")
949960
path = event.get("rawPath")
950-
resource = "{0} {1}".format(method, path)
961+
resource_path = _get_resource_path(event, request_context)
962+
resource = "{0} {1}".format(method, resource_path)
951963
tags = {
952964
"operation_name": "aws.httpapi",
953965
"endpoint": path,
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
{
2+
"resource": "/user/{id}",
3+
"path": "/user/42",
4+
"httpMethod": "GET",
5+
"headers": {
6+
"Accept": "*/*",
7+
"CloudFront-Forwarded-Proto": "https",
8+
"CloudFront-Is-Desktop-Viewer": "true",
9+
"CloudFront-Is-Mobile-Viewer": "false",
10+
"CloudFront-Is-SmartTV-Viewer": "false",
11+
"CloudFront-Is-Tablet-Viewer": "false",
12+
"CloudFront-Viewer-ASN": "7922",
13+
"CloudFront-Viewer-Country": "US",
14+
"Host": "mcwkra0ya4.execute-api.sa-east-1.amazonaws.com",
15+
"User-Agent": "curl/8.1.2",
16+
"Via": "2.0 xxx.cloudfront.net (CloudFront)",
17+
"X-Amz-Cf-Id": "Tz3yUVcJkwOhQGqZgKTzrEHqAoOd8ZprYAHpg2S6BNxdd-Ym79pb6g==",
18+
"X-Amzn-Trace-Id": "Root=1-65f49d20-7ba106216238dd0078a5db31",
19+
"X-Forwarded-For": "76.115.124.192, 15.158.54.119",
20+
"X-Forwarded-Port": "443",
21+
"X-Forwarded-Proto": "https"
22+
},
23+
"multiValueHeaders": {
24+
"Accept": [
25+
"*/*"
26+
],
27+
"CloudFront-Forwarded-Proto": [
28+
"https"
29+
],
30+
"CloudFront-Is-Desktop-Viewer": [
31+
"true"
32+
],
33+
"CloudFront-Is-Mobile-Viewer": [
34+
"false"
35+
],
36+
"CloudFront-Is-SmartTV-Viewer": [
37+
"false"
38+
],
39+
"CloudFront-Is-Tablet-Viewer": [
40+
"false"
41+
],
42+
"CloudFront-Viewer-ASN": [
43+
"7922"
44+
],
45+
"CloudFront-Viewer-Country": [
46+
"US"
47+
],
48+
"Host": [
49+
"mcwkra0ya4.execute-api.sa-east-1.amazonaws.com"
50+
],
51+
"User-Agent": [
52+
"curl/8.1.2"
53+
],
54+
"Via": [
55+
"2.0 xxx.cloudfront.net (CloudFront)"
56+
],
57+
"X-Amz-Cf-Id": [
58+
"Tz3yUVcJkwOhQGqZgKTzrEHqAoOd8ZprYAHpg2S6BNxdd-Ym79pb6g=="
59+
],
60+
"X-Amzn-Trace-Id": [
61+
"Root=1-65f49d20-7ba106216238dd0078a5db31"
62+
],
63+
"X-Forwarded-For": [
64+
"76.115.124.192, 15.158.54.119"
65+
],
66+
"X-Forwarded-Port": [
67+
"443"
68+
],
69+
"X-Forwarded-Proto": [
70+
"https"
71+
]
72+
},
73+
"queryStringParameters": null,
74+
"multiValueQueryStringParameters": null,
75+
"pathParameters": {
76+
"id": "42"
77+
},
78+
"stageVariables": null,
79+
"requestContext": {
80+
"resourceId": "ojg3nk",
81+
"resourcePath": "/user/{id}",
82+
"httpMethod": "GET",
83+
"extendedRequestId": "Ur19IHYDmjQEU5A=",
84+
"requestTime": "15/Mar/2024:19:10:24 +0000",
85+
"path": "/dev/user/42",
86+
"accountId": "425362996713",
87+
"protocol": "HTTP/1.1",
88+
"stage": "dev",
89+
"domainPrefix": "mcwkra0ya4",
90+
"requestTimeEpoch": 1710529824520,
91+
"requestId": "e16399f7-e984-463a-9931-745ba021a27f",
92+
"identity": {
93+
"cognitoIdentityPoolId": null,
94+
"accountId": null,
95+
"cognitoIdentityId": null,
96+
"caller": null,
97+
"sourceIp": "76.115.124.192",
98+
"principalOrgId": null,
99+
"accessKey": null,
100+
"cognitoAuthenticationType": null,
101+
"cognitoAuthenticationProvider": null,
102+
"userArn": null,
103+
"userAgent": "curl/8.1.2",
104+
"user": null
105+
},
106+
"domainName": "mcwkra0ya4.execute-api.sa-east-1.amazonaws.com",
107+
"apiId": "mcwkra0ya4"
108+
},
109+
"body": null,
110+
"isBase64Encoded": false
111+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
{
2+
"version": "2.0",
3+
"routeKey": "GET /user/{id}",
4+
"rawPath": "/user/42",
5+
"rawQueryString": "",
6+
"headers": {
7+
"accept": "*/*",
8+
"content-length": "0",
9+
"host": "9vj54we5ih.execute-api.sa-east-1.amazonaws.com",
10+
"user-agent": "curl/8.1.2",
11+
"x-amzn-trace-id": "Root=1-65f49d71-505edb3b69b8abd513cfa08b",
12+
"x-forwarded-for": "76.115.124.192",
13+
"x-forwarded-port": "443",
14+
"x-forwarded-proto": "https"
15+
},
16+
"requestContext": {
17+
"accountId": "425362996713",
18+
"apiId": "9vj54we5ih",
19+
"domainName": "9vj54we5ih.execute-api.sa-east-1.amazonaws.com",
20+
"domainPrefix": "9vj54we5ih",
21+
"http": {
22+
"method": "GET",
23+
"path": "/user/42",
24+
"protocol": "HTTP/1.1",
25+
"sourceIp": "76.115.124.192",
26+
"userAgent": "curl/8.1.2"
27+
},
28+
"requestId": "Ur2JtjEfGjQEPOg=",
29+
"routeKey": "GET /user/{id}",
30+
"stage": "$default",
31+
"time": "15/Mar/2024:19:11:45 +0000",
32+
"timeEpoch": 1710529905066
33+
},
34+
"pathParameters": {
35+
"id": "42"
36+
},
37+
"isBase64Encoded": false
38+
}

tests/test_tracing.py

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1378,7 +1378,7 @@ def __init__(self, service, start, span_type, parent_name=None, tags=None):
13781378
"http.url": "70ixmpl4fl.execute-api.us-east-2.amazonaws.com/path/to/resource",
13791379
"operation_name": "aws.apigateway.rest",
13801380
"request_id": "123",
1381-
"resource_names": "POST /path/to/resource",
1381+
"resource_names": "POST /{proxy+}",
13821382
"stage": "prod",
13831383
},
13841384
),
@@ -1452,6 +1452,50 @@ def __init__(self, service, start, span_type, parent_name=None, tags=None):
14521452
},
14531453
),
14541454
),
1455+
(
1456+
"api-gateway-v1-parametrized",
1457+
_Span(
1458+
service="mcwkra0ya4.execute-api.sa-east-1.amazonaws.com",
1459+
start=1710529824.52,
1460+
span_type="http",
1461+
tags={
1462+
"_dd.origin": "lambda",
1463+
"_inferred_span.synchronicity": "sync",
1464+
"_inferred_span.tag_source": "self",
1465+
"apiid": "mcwkra0ya4",
1466+
"apiname": "mcwkra0ya4",
1467+
"endpoint": "/user/42",
1468+
"http.method": "GET",
1469+
"http.url": "mcwkra0ya4.execute-api.sa-east-1.amazonaws.com/user/42",
1470+
"operation_name": "aws.apigateway.rest",
1471+
"request_id": "123",
1472+
"resource_names": "GET /user/{id}",
1473+
"stage": "dev",
1474+
},
1475+
),
1476+
),
1477+
(
1478+
"api-gateway-v2-parametrized",
1479+
_Span(
1480+
service="9vj54we5ih.execute-api.sa-east-1.amazonaws.com",
1481+
start=1710529905.066,
1482+
span_type="http",
1483+
tags={
1484+
"_dd.origin": "lambda",
1485+
"_inferred_span.synchronicity": "sync",
1486+
"_inferred_span.tag_source": "self",
1487+
"apiid": "9vj54we5ih",
1488+
"apiname": "9vj54we5ih",
1489+
"endpoint": "/user/42",
1490+
"http.method": "GET",
1491+
"http.url": "9vj54we5ih.execute-api.sa-east-1.amazonaws.com/user/42",
1492+
"operation_name": "aws.httpapi",
1493+
"request_id": "123",
1494+
"resource_names": "GET /user/{id}",
1495+
"stage": "$default",
1496+
},
1497+
),
1498+
),
14551499
(
14561500
"api-gateway-websocket-default",
14571501
_Span(
@@ -1716,7 +1760,7 @@ def __init__(self, service, start, span_type, parent_name=None, tags=None):
17161760
"http.url": "70ixmpl4fl.execute-api.us-east-2.amazonaws.com/path/to/resource",
17171761
"operation_name": "aws.apigateway.rest",
17181762
"request_id": "123",
1719-
"resource_names": "POST /path/to/resource",
1763+
"resource_names": "POST /{proxy+}",
17201764
"stage": "prod",
17211765
},
17221766
),

0 commit comments

Comments
 (0)