From 8087c872b1806b27c2d6cface2f1f6b4b702815c Mon Sep 17 00:00:00 2001 From: Nicholas Hulston Date: Tue, 29 Oct 2024 14:37:37 -0400 Subject: [PATCH] Add `http.route` tags for API Gateway (#584) --- .../snapshots/logs/process-input-traced_node16.log | 1 + .../snapshots/logs/process-input-traced_node18.log | 1 + .../snapshots/logs/process-input-traced_node20.log | 1 + .../snapshots/logs/status-code-500s_node16.log | 1 + .../snapshots/logs/status-code-500s_node18.log | 1 + .../snapshots/logs/status-code-500s_node20.log | 1 + src/trace/trigger.spec.ts | 2 ++ src/trace/trigger.ts | 8 ++++++++ 8 files changed, 16 insertions(+) diff --git a/integration_tests/snapshots/logs/process-input-traced_node16.log b/integration_tests/snapshots/logs/process-input-traced_node16.log index 5734dc9b..6c2c2151 100644 --- a/integration_tests/snapshots/logs/process-input-traced_node16.log +++ b/integration_tests/snapshots/logs/process-input-traced_node16.log @@ -48,6 +48,7 @@ START "_inferred_span.tag_source": "self", "_inferred_span.synchronicity": "sync", "http.method": "GET", + "http.route":"/{proxy+}", "stage": "test", "http.status_code": "200", "_dd.base_service": "integration-tests-js-XXXX-process-input-traced_node16", diff --git a/integration_tests/snapshots/logs/process-input-traced_node18.log b/integration_tests/snapshots/logs/process-input-traced_node18.log index bdee824c..f38d929c 100644 --- a/integration_tests/snapshots/logs/process-input-traced_node18.log +++ b/integration_tests/snapshots/logs/process-input-traced_node18.log @@ -48,6 +48,7 @@ START "_inferred_span.tag_source": "self", "_inferred_span.synchronicity": "sync", "http.method": "GET", + "http.route":"/{proxy+}", "stage": "test", "http.status_code": "200", "_dd.base_service": "integration-tests-js-XXXX-process-input-traced_node18", diff --git a/integration_tests/snapshots/logs/process-input-traced_node20.log b/integration_tests/snapshots/logs/process-input-traced_node20.log index e04b7a03..dfc45ae7 100644 --- a/integration_tests/snapshots/logs/process-input-traced_node20.log +++ b/integration_tests/snapshots/logs/process-input-traced_node20.log @@ -48,6 +48,7 @@ START "_inferred_span.tag_source": "self", "_inferred_span.synchronicity": "sync", "http.method": "GET", + "http.route":"/{proxy+}", "stage": "test", "http.status_code": "200", "_dd.base_service": "integration-tests-js-XXXX-process-input-traced_node20", diff --git a/integration_tests/snapshots/logs/status-code-500s_node16.log b/integration_tests/snapshots/logs/status-code-500s_node16.log index 9662d57e..79f4a0c8 100644 --- a/integration_tests/snapshots/logs/status-code-500s_node16.log +++ b/integration_tests/snapshots/logs/status-code-500s_node16.log @@ -54,6 +54,7 @@ START "_inferred_span.tag_source": "self", "_inferred_span.synchronicity": "sync", "http.method": "GET", + "http.route":"/{proxy+}", "stage": "test", "http.status_code": "500", "_dd.base_service": "integration-tests-js-XXXX-status-code-500s_node16", diff --git a/integration_tests/snapshots/logs/status-code-500s_node18.log b/integration_tests/snapshots/logs/status-code-500s_node18.log index f8a31bee..7248a341 100644 --- a/integration_tests/snapshots/logs/status-code-500s_node18.log +++ b/integration_tests/snapshots/logs/status-code-500s_node18.log @@ -54,6 +54,7 @@ START "_inferred_span.tag_source": "self", "_inferred_span.synchronicity": "sync", "http.method": "GET", + "http.route":"/{proxy+}", "stage": "test", "http.status_code": "500", "_dd.base_service": "integration-tests-js-XXXX-status-code-500s_node18", diff --git a/integration_tests/snapshots/logs/status-code-500s_node20.log b/integration_tests/snapshots/logs/status-code-500s_node20.log index 8a0c50b3..938aec86 100644 --- a/integration_tests/snapshots/logs/status-code-500s_node20.log +++ b/integration_tests/snapshots/logs/status-code-500s_node20.log @@ -54,6 +54,7 @@ START "_inferred_span.tag_source": "self", "_inferred_span.synchronicity": "sync", "http.method": "GET", + "http.route":"/{proxy+}", "stage": "test", "http.status_code": "500", "_dd.base_service": "integration-tests-js-XXXX-status-code-500s_node20", diff --git a/src/trace/trigger.spec.ts b/src/trace/trigger.spec.ts index 471e363f..29b64892 100644 --- a/src/trace/trigger.spec.ts +++ b/src/trace/trigger.spec.ts @@ -17,6 +17,7 @@ describe("parseEventSource", () => { "http.url": "id.execute-api.us-east-1.amazonaws.com", "http.url_details.path": "/my/path", "http.method": "GET", + "http.route": "/my/path", }, file: "api-gateway-v1.json", }, @@ -27,6 +28,7 @@ describe("parseEventSource", () => { "http.url": "r3pmxmplak.execute-api.us-east-2.amazonaws.com", "http.url_details.path": "/default/nodejs-apig-function-1G3XMPLZXVXYI", "http.method": "GET", + "http.route": "/nodejs-apig-function-1G3XMPLZXVXYI", }, file: "api-gateway-v2.json", }, diff --git a/src/trace/trigger.ts b/src/trace/trigger.ts index ab670059..e23e0fb6 100644 --- a/src/trace/trigger.ts +++ b/src/trace/trigger.ts @@ -275,6 +275,9 @@ function extractHTTPTags(event: APIGatewayEvent | APIGatewayProxyEventV2 | ALBEv if (event.headers?.Referer) { httpTags["http.referer"] = event.headers.Referer; } + if (event.resource) { + httpTags["http.route"] = event.resource; + } return httpTags; } @@ -286,6 +289,11 @@ function extractHTTPTags(event: APIGatewayEvent | APIGatewayProxyEventV2 | ALBEv if (event.headers?.Referer) { httpTags["http.referer"] = event.headers.Referer; } + if (event.routeKey) { + // "GET /my/endpoint" => "/my/endpoint" + const array = event.routeKey.split(" "); + httpTags["http.route"] = array[array.length - 1]; + } return httpTags; }