From 54c0841624421f67be176a77edea697125a4c2a2 Mon Sep 17 00:00:00 2001 From: John Bley Date: Mon, 26 Aug 2024 13:56:35 -0400 Subject: [PATCH 01/10] feat: report url for lambda invoked via api gateway --- .../src/instrumentation.ts | 38 +++++++++++++++++++ .../test/integrations/lambda-handler.test.ts | 25 ++++++++++++ 2 files changed, 63 insertions(+) diff --git a/plugins/node/opentelemetry-instrumentation-aws-lambda/src/instrumentation.ts b/plugins/node/opentelemetry-instrumentation-aws-lambda/src/instrumentation.ts index 4aef1509ce..c222e89d0d 100644 --- a/plugins/node/opentelemetry-instrumentation-aws-lambda/src/instrumentation.ts +++ b/plugins/node/opentelemetry-instrumentation-aws-lambda/src/instrumentation.ts @@ -38,6 +38,7 @@ import { TraceFlags, TracerProvider, ROOT_CONTEXT, + Attributes, } from '@opentelemetry/api'; import { AWSXRAY_TRACE_ID_HEADER, @@ -45,6 +46,7 @@ import { } from '@opentelemetry/propagator-aws-xray'; import { SEMATTRS_FAAS_EXECUTION, + SEMATTRS_HTTP_URL, SEMRESATTRS_CLOUD_ACCOUNT_ID, SEMRESATTRS_FAAS_ID, } from '@opentelemetry/semantic-conventions'; @@ -203,6 +205,7 @@ export class AwsLambdaInstrumentation extends InstrumentationBase { assert.strictEqual(span.parentSpanId, undefined); }); }); + + describe('url parsing', () => { + it('pulls url from api gateway events', async () => { + initializeHandler('lambda-test/sync.handler'); + const event = { + path: '/lambda/test/path', + headers: { + host: 'www.example.com', + 'x-forwarded-proto': 'http', + }, + queryStringParameters: { + key: 'value', + }, + }; + + await lambdaRequire('lambda-test/sync').handler(event, ctx, () => {}); + const [span] = memoryExporter.getFinishedSpans(); + assert.strictEqual( + span.attributes[SEMATTRS_HTTP_URL], + 'http://www.example.com/lambda/test/path?key=value' + ); + console.log(span); + }); + }); }); From 542cdcb6818910538cfce2790c5141fd33d71f38 Mon Sep 17 00:00:00 2001 From: John Bley Date: Mon, 9 Sep 2024 10:55:23 -0400 Subject: [PATCH 02/10] chore: update http.url -> url.full --- .../src/instrumentation.ts | 4 ++-- .../test/integrations/lambda-handler.test.ts | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/plugins/node/opentelemetry-instrumentation-aws-lambda/src/instrumentation.ts b/plugins/node/opentelemetry-instrumentation-aws-lambda/src/instrumentation.ts index a4d3765976..2c72b62b03 100644 --- a/plugins/node/opentelemetry-instrumentation-aws-lambda/src/instrumentation.ts +++ b/plugins/node/opentelemetry-instrumentation-aws-lambda/src/instrumentation.ts @@ -45,8 +45,8 @@ import { AWSXRayPropagator, } from '@opentelemetry/propagator-aws-xray'; import { + ATTR_URL_FULL, SEMATTRS_FAAS_EXECUTION, - SEMATTRS_HTTP_URL, SEMRESATTRS_CLOUD_ACCOUNT_ID, SEMRESATTRS_FAAS_ID, } from '@opentelemetry/semantic-conventions'; @@ -436,7 +436,7 @@ export class AwsLambdaInstrumentation extends InstrumentationBase { await lambdaRequire('lambda-test/sync').handler(event, ctx, () => {}); const [span] = memoryExporter.getFinishedSpans(); assert.strictEqual( - span.attributes[SEMATTRS_HTTP_URL], + span.attributes[ATTR_URL_FULL], 'http://www.example.com/lambda/test/path?key=value' ); console.log(span); From 4b788c5cd2bd04f8a58fa47a7ff083321f2fada7 Mon Sep 17 00:00:00 2001 From: John Bley Date: Mon, 9 Sep 2024 14:17:08 -0400 Subject: [PATCH 03/10] fix: update logic and tests for pieceing url parts together --- .../src/instrumentation.ts | 23 +++++++++++------- .../test/integrations/lambda-handler.test.ts | 24 ++++++++++++++++++- 2 files changed, 37 insertions(+), 10 deletions(-) diff --git a/plugins/node/opentelemetry-instrumentation-aws-lambda/src/instrumentation.ts b/plugins/node/opentelemetry-instrumentation-aws-lambda/src/instrumentation.ts index 2c72b62b03..0b21eb2208 100644 --- a/plugins/node/opentelemetry-instrumentation-aws-lambda/src/instrumentation.ts +++ b/plugins/node/opentelemetry-instrumentation-aws-lambda/src/instrumentation.ts @@ -443,17 +443,22 @@ export class AwsLambdaInstrumentation extends InstrumentationBase { }); describe('url parsing', () => { - it('pulls url from api gateway events', async () => { + it('pulls url from api gateway rest events', async () => { initializeHandler('lambda-test/sync.handler'); const event = { path: '/lambda/test/path', @@ -1103,5 +1103,27 @@ describe('lambda handler', () => { ); console.log(span); }); + it('pulls url from api gateway http events', async () => { + initializeHandler('lambda-test/sync.handler'); + const event = { + rawPath: '/lambda/test/path', + headers: { + host: 'www.example.com', + 'x-forwarded-proto': 'http', + 'x-forwarded-port': 1234, + }, + queryStringParameters: { + key: 'value', + }, + }; + + await lambdaRequire('lambda-test/sync').handler(event, ctx, () => {}); + const [span] = memoryExporter.getFinishedSpans(); + assert.strictEqual( + span.attributes[ATTR_URL_FULL], + 'http://www.example.com:1234/lambda/test/path?key=value' + ); + console.log(span); + }); }); }); From 5d0c474cd21e7cbd41c9fab66e2cf105b04cf5fa Mon Sep 17 00:00:00 2001 From: John Bley Date: Tue, 10 Sep 2024 14:12:14 -0400 Subject: [PATCH 04/10] chore: remove debug printouts from test --- .../test/integrations/lambda-handler.test.ts | 2 -- 1 file changed, 2 deletions(-) diff --git a/plugins/node/opentelemetry-instrumentation-aws-lambda/test/integrations/lambda-handler.test.ts b/plugins/node/opentelemetry-instrumentation-aws-lambda/test/integrations/lambda-handler.test.ts index e57b0c9007..f4daf8d2df 100644 --- a/plugins/node/opentelemetry-instrumentation-aws-lambda/test/integrations/lambda-handler.test.ts +++ b/plugins/node/opentelemetry-instrumentation-aws-lambda/test/integrations/lambda-handler.test.ts @@ -1101,7 +1101,6 @@ describe('lambda handler', () => { span.attributes[ATTR_URL_FULL], 'http://www.example.com/lambda/test/path?key=value' ); - console.log(span); }); it('pulls url from api gateway http events', async () => { initializeHandler('lambda-test/sync.handler'); @@ -1123,7 +1122,6 @@ describe('lambda handler', () => { span.attributes[ATTR_URL_FULL], 'http://www.example.com:1234/lambda/test/path?key=value' ); - console.log(span); }); }); }); From 7b0d0f41c5cd25d57e671b3ddf6cf6bbf13f211c Mon Sep 17 00:00:00 2001 From: John Bley Date: Thu, 14 Nov 2024 14:58:02 -0500 Subject: [PATCH 05/10] Read the git conflict the wrong way :-( --- .../src/instrumentation.ts | 4 ---- 1 file changed, 4 deletions(-) diff --git a/plugins/node/opentelemetry-instrumentation-aws-lambda/src/instrumentation.ts b/plugins/node/opentelemetry-instrumentation-aws-lambda/src/instrumentation.ts index 857ea7ede9..96cae96680 100644 --- a/plugins/node/opentelemetry-instrumentation-aws-lambda/src/instrumentation.ts +++ b/plugins/node/opentelemetry-instrumentation-aws-lambda/src/instrumentation.ts @@ -39,10 +39,6 @@ import { ROOT_CONTEXT, Attributes, } from '@opentelemetry/api'; -import { - AWSXRAY_TRACE_ID_HEADER, - AWSXRayPropagator, -} from '@opentelemetry/propagator-aws-xray'; import { ATTR_URL_FULL, SEMATTRS_FAAS_EXECUTION, From a21a40070686c599d940da0895cdc6c35cb48c39 Mon Sep 17 00:00:00 2001 From: John Bley Date: Thu, 14 Nov 2024 15:38:50 -0500 Subject: [PATCH 06/10] Test logic for multiple query params --- .../test/integrations/lambda-handler.test.ts | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/plugins/node/opentelemetry-instrumentation-aws-lambda/test/integrations/lambda-handler.test.ts b/plugins/node/opentelemetry-instrumentation-aws-lambda/test/integrations/lambda-handler.test.ts index 40a2fc8702..640005160e 100644 --- a/plugins/node/opentelemetry-instrumentation-aws-lambda/test/integrations/lambda-handler.test.ts +++ b/plugins/node/opentelemetry-instrumentation-aws-lambda/test/integrations/lambda-handler.test.ts @@ -781,14 +781,15 @@ describe('lambda handler', () => { }, queryStringParameters: { key: 'value', + key2: 'value2' }, }; await lambdaRequire('lambda-test/sync').handler(event, ctx, () => {}); const [span] = memoryExporter.getFinishedSpans(); - assert.strictEqual( - span.attributes[ATTR_URL_FULL], - 'http://www.example.com/lambda/test/path?key=value' + assert.ok( + (span.attributes[ATTR_URL_FULL] === 'http://www.example.com/lambda/test/path?key=value&key2=value2') || + (span.attributes[ATTR_URL_FULL] === 'http://www.example.com/lambda/test/path?key2=value2&key=value') ); }); it('pulls url from api gateway http events', async () => { From 74411a0bae30e3854044a086cf9b8e508be6701d Mon Sep 17 00:00:00 2001 From: John Bley Date: Thu, 14 Nov 2024 15:48:36 -0500 Subject: [PATCH 07/10] lint:fix --- .../test/integrations/lambda-handler.test.ts | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/plugins/node/opentelemetry-instrumentation-aws-lambda/test/integrations/lambda-handler.test.ts b/plugins/node/opentelemetry-instrumentation-aws-lambda/test/integrations/lambda-handler.test.ts index 640005160e..e821c12883 100644 --- a/plugins/node/opentelemetry-instrumentation-aws-lambda/test/integrations/lambda-handler.test.ts +++ b/plugins/node/opentelemetry-instrumentation-aws-lambda/test/integrations/lambda-handler.test.ts @@ -781,15 +781,17 @@ describe('lambda handler', () => { }, queryStringParameters: { key: 'value', - key2: 'value2' + key2: 'value2', }, }; await lambdaRequire('lambda-test/sync').handler(event, ctx, () => {}); const [span] = memoryExporter.getFinishedSpans(); assert.ok( - (span.attributes[ATTR_URL_FULL] === 'http://www.example.com/lambda/test/path?key=value&key2=value2') || - (span.attributes[ATTR_URL_FULL] === 'http://www.example.com/lambda/test/path?key2=value2&key=value') + span.attributes[ATTR_URL_FULL] === + 'http://www.example.com/lambda/test/path?key=value&key2=value2' || + span.attributes[ATTR_URL_FULL] === + 'http://www.example.com/lambda/test/path?key2=value2&key=value' ); }); it('pulls url from api gateway http events', async () => { From 20255678a9d51166e2f28714de02645180599bf3 Mon Sep 17 00:00:00 2001 From: John Bley Date: Fri, 15 Nov 2024 09:13:54 -0500 Subject: [PATCH 08/10] Allow case variations in headers when computing invoked url --- .../src/instrumentation.ts | 30 +++++++++++-------- 1 file changed, 17 insertions(+), 13 deletions(-) diff --git a/plugins/node/opentelemetry-instrumentation-aws-lambda/src/instrumentation.ts b/plugins/node/opentelemetry-instrumentation-aws-lambda/src/instrumentation.ts index 96cae96680..6c95a79b4c 100644 --- a/plugins/node/opentelemetry-instrumentation-aws-lambda/src/instrumentation.ts +++ b/plugins/node/opentelemetry-instrumentation-aws-lambda/src/instrumentation.ts @@ -437,22 +437,26 @@ export class AwsLambdaInstrumentation extends InstrumentationBase Date: Fri, 15 Nov 2024 09:26:08 -0500 Subject: [PATCH 09/10] Improve checking logic for undefineds --- .../src/instrumentation.ts | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/plugins/node/opentelemetry-instrumentation-aws-lambda/src/instrumentation.ts b/plugins/node/opentelemetry-instrumentation-aws-lambda/src/instrumentation.ts index 6c95a79b4c..519fdb5891 100644 --- a/plugins/node/opentelemetry-instrumentation-aws-lambda/src/instrumentation.ts +++ b/plugins/node/opentelemetry-instrumentation-aws-lambda/src/instrumentation.ts @@ -437,7 +437,9 @@ export class AwsLambdaInstrumentation extends InstrumentationBase Date: Fri, 15 Nov 2024 09:50:32 -0500 Subject: [PATCH 10/10] Test for case variations on headers --- .../test/integrations/lambda-handler.test.ts | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/plugins/node/opentelemetry-instrumentation-aws-lambda/test/integrations/lambda-handler.test.ts b/plugins/node/opentelemetry-instrumentation-aws-lambda/test/integrations/lambda-handler.test.ts index e821c12883..65c4a76df8 100644 --- a/plugins/node/opentelemetry-instrumentation-aws-lambda/test/integrations/lambda-handler.test.ts +++ b/plugins/node/opentelemetry-instrumentation-aws-lambda/test/integrations/lambda-handler.test.ts @@ -776,8 +776,9 @@ describe('lambda handler', () => { const event = { path: '/lambda/test/path', headers: { - host: 'www.example.com', - 'x-forwarded-proto': 'http', + Host: 'www.example.com', + 'X-Forwarded-Proto': 'http', + 'X-Forwarded-Port': 1234, }, queryStringParameters: { key: 'value', @@ -789,9 +790,9 @@ describe('lambda handler', () => { const [span] = memoryExporter.getFinishedSpans(); assert.ok( span.attributes[ATTR_URL_FULL] === - 'http://www.example.com/lambda/test/path?key=value&key2=value2' || + 'http://www.example.com:1234/lambda/test/path?key=value&key2=value2' || span.attributes[ATTR_URL_FULL] === - 'http://www.example.com/lambda/test/path?key2=value2&key=value' + 'http://www.example.com:1234/lambda/test/path?key2=value2&key=value' ); }); it('pulls url from api gateway http events', async () => {