Skip to content

fix: use RouteKey in resource path for API Gateway inferred spans #442

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 5 commits into from
Mar 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
16 changes: 14 additions & 2 deletions datadog_lambda/tracing.py
Original file line number Diff line number Diff line change
Expand Up @@ -893,7 +893,8 @@ def create_inferred_span_from_api_gateway_event(
)
method = event.get("httpMethod")
path = event.get("path")
resource = "{0} {1}".format(method, path)
resource_path = _get_resource_path(event, request_context)
resource = "{0} {1}".format(method, resource_path)
Copy link
Contributor

Choose a reason for hiding this comment

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

Let's use f string, instead of format. Due to performance.

Copy link
Contributor

Choose a reason for hiding this comment

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

Oh good call!

tags = {
"operation_name": "aws.apigateway.rest",
"http.url": domain + path,
Expand Down Expand Up @@ -936,6 +937,16 @@ def create_inferred_span_from_api_gateway_event(
return span


def _get_resource_path(event, request_context):
route_key = request_context.get("routeKey") or ""
if "{" in route_key:
try:
return route_key.split(" ")[1]
except Exception as e:
logger.debug("Error parsing routeKey: %s", e)
return event.get("rawPath") or request_context.get("resourcePath") or route_key


def create_inferred_span_from_http_api_event(
event, context, decode_authorizer_context: bool = True
):
Expand All @@ -947,7 +958,8 @@ def create_inferred_span_from_http_api_event(
)
method = request_context.get("http", {}).get("method")
path = event.get("rawPath")
resource = "{0} {1}".format(method, path)
resource_path = _get_resource_path(event, request_context)
resource = "{0} {1}".format(method, resource_path)
Copy link
Contributor

Choose a reason for hiding this comment

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

same here

tags = {
"operation_name": "aws.httpapi",
"endpoint": path,
Expand Down
111 changes: 111 additions & 0 deletions tests/event_samples/api-gateway-v1-parametrized.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
{
"resource": "/user/{id}",
"path": "/user/42",
"httpMethod": "GET",
"headers": {
"Accept": "*/*",
"CloudFront-Forwarded-Proto": "https",
"CloudFront-Is-Desktop-Viewer": "true",
"CloudFront-Is-Mobile-Viewer": "false",
"CloudFront-Is-SmartTV-Viewer": "false",
"CloudFront-Is-Tablet-Viewer": "false",
"CloudFront-Viewer-ASN": "7922",
"CloudFront-Viewer-Country": "US",
"Host": "mcwkra0ya4.execute-api.sa-east-1.amazonaws.com",
"User-Agent": "curl/8.1.2",
"Via": "2.0 xxx.cloudfront.net (CloudFront)",
"X-Amz-Cf-Id": "Tz3yUVcJkwOhQGqZgKTzrEHqAoOd8ZprYAHpg2S6BNxdd-Ym79pb6g==",
"X-Amzn-Trace-Id": "Root=1-65f49d20-7ba106216238dd0078a5db31",
"X-Forwarded-For": "76.115.124.192, 15.158.54.119",
"X-Forwarded-Port": "443",
"X-Forwarded-Proto": "https"
},
"multiValueHeaders": {
"Accept": [
"*/*"
],
"CloudFront-Forwarded-Proto": [
"https"
],
"CloudFront-Is-Desktop-Viewer": [
"true"
],
"CloudFront-Is-Mobile-Viewer": [
"false"
],
"CloudFront-Is-SmartTV-Viewer": [
"false"
],
"CloudFront-Is-Tablet-Viewer": [
"false"
],
"CloudFront-Viewer-ASN": [
"7922"
],
"CloudFront-Viewer-Country": [
"US"
],
"Host": [
"mcwkra0ya4.execute-api.sa-east-1.amazonaws.com"
],
"User-Agent": [
"curl/8.1.2"
],
"Via": [
"2.0 xxx.cloudfront.net (CloudFront)"
],
"X-Amz-Cf-Id": [
"Tz3yUVcJkwOhQGqZgKTzrEHqAoOd8ZprYAHpg2S6BNxdd-Ym79pb6g=="
],
"X-Amzn-Trace-Id": [
"Root=1-65f49d20-7ba106216238dd0078a5db31"
],
"X-Forwarded-For": [
"76.115.124.192, 15.158.54.119"
],
"X-Forwarded-Port": [
"443"
],
"X-Forwarded-Proto": [
"https"
]
},
"queryStringParameters": null,
"multiValueQueryStringParameters": null,
"pathParameters": {
"id": "42"
},
"stageVariables": null,
"requestContext": {
"resourceId": "ojg3nk",
"resourcePath": "/user/{id}",
"httpMethod": "GET",
"extendedRequestId": "Ur19IHYDmjQEU5A=",
"requestTime": "15/Mar/2024:19:10:24 +0000",
"path": "/dev/user/42",
"accountId": "425362996713",
"protocol": "HTTP/1.1",
"stage": "dev",
"domainPrefix": "mcwkra0ya4",
"requestTimeEpoch": 1710529824520,
"requestId": "e16399f7-e984-463a-9931-745ba021a27f",
"identity": {
"cognitoIdentityPoolId": null,
"accountId": null,
"cognitoIdentityId": null,
"caller": null,
"sourceIp": "76.115.124.192",
"principalOrgId": null,
"accessKey": null,
"cognitoAuthenticationType": null,
"cognitoAuthenticationProvider": null,
"userArn": null,
"userAgent": "curl/8.1.2",
"user": null
},
"domainName": "mcwkra0ya4.execute-api.sa-east-1.amazonaws.com",
"apiId": "mcwkra0ya4"
},
"body": null,
"isBase64Encoded": false
}
38 changes: 38 additions & 0 deletions tests/event_samples/api-gateway-v2-parametrized.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
{
"version": "2.0",
"routeKey": "GET /user/{id}",
"rawPath": "/user/42",
"rawQueryString": "",
"headers": {
"accept": "*/*",
"content-length": "0",
"host": "9vj54we5ih.execute-api.sa-east-1.amazonaws.com",
"user-agent": "curl/8.1.2",
"x-amzn-trace-id": "Root=1-65f49d71-505edb3b69b8abd513cfa08b",
"x-forwarded-for": "76.115.124.192",
"x-forwarded-port": "443",
"x-forwarded-proto": "https"
},
"requestContext": {
"accountId": "425362996713",
"apiId": "9vj54we5ih",
"domainName": "9vj54we5ih.execute-api.sa-east-1.amazonaws.com",
"domainPrefix": "9vj54we5ih",
"http": {
"method": "GET",
"path": "/user/42",
"protocol": "HTTP/1.1",
"sourceIp": "76.115.124.192",
"userAgent": "curl/8.1.2"
},
"requestId": "Ur2JtjEfGjQEPOg=",
"routeKey": "GET /user/{id}",
"stage": "$default",
"time": "15/Mar/2024:19:11:45 +0000",
"timeEpoch": 1710529905066
},
"pathParameters": {
"id": "42"
},
"isBase64Encoded": false
}
48 changes: 46 additions & 2 deletions tests/test_tracing.py
Original file line number Diff line number Diff line change
Expand Up @@ -1378,7 +1378,7 @@ def __init__(self, service, start, span_type, parent_name=None, tags=None):
"http.url": "70ixmpl4fl.execute-api.us-east-2.amazonaws.com/path/to/resource",
"operation_name": "aws.apigateway.rest",
"request_id": "123",
"resource_names": "POST /path/to/resource",
"resource_names": "POST /{proxy+}",
"stage": "prod",
},
),
Expand Down Expand Up @@ -1452,6 +1452,50 @@ def __init__(self, service, start, span_type, parent_name=None, tags=None):
},
),
),
(
"api-gateway-v1-parametrized",
_Span(
service="mcwkra0ya4.execute-api.sa-east-1.amazonaws.com",
start=1710529824.52,
span_type="http",
tags={
"_dd.origin": "lambda",
"_inferred_span.synchronicity": "sync",
"_inferred_span.tag_source": "self",
"apiid": "mcwkra0ya4",
"apiname": "mcwkra0ya4",
"endpoint": "/user/42",
"http.method": "GET",
"http.url": "mcwkra0ya4.execute-api.sa-east-1.amazonaws.com/user/42",
"operation_name": "aws.apigateway.rest",
"request_id": "123",
"resource_names": "GET /user/{id}",
"stage": "dev",
},
),
),
(
"api-gateway-v2-parametrized",
_Span(
service="9vj54we5ih.execute-api.sa-east-1.amazonaws.com",
start=1710529905.066,
span_type="http",
tags={
"_dd.origin": "lambda",
"_inferred_span.synchronicity": "sync",
"_inferred_span.tag_source": "self",
"apiid": "9vj54we5ih",
"apiname": "9vj54we5ih",
"endpoint": "/user/42",
"http.method": "GET",
"http.url": "9vj54we5ih.execute-api.sa-east-1.amazonaws.com/user/42",
"operation_name": "aws.httpapi",
"request_id": "123",
"resource_names": "GET /user/{id}",
"stage": "$default",
},
),
),
(
"api-gateway-websocket-default",
_Span(
Expand Down Expand Up @@ -1716,7 +1760,7 @@ def __init__(self, service, start, span_type, parent_name=None, tags=None):
"http.url": "70ixmpl4fl.execute-api.us-east-2.amazonaws.com/path/to/resource",
"operation_name": "aws.apigateway.rest",
"request_id": "123",
"resource_names": "POST /path/to/resource",
"resource_names": "POST /{proxy+}",
"stage": "prod",
},
),
Expand Down