Skip to content

Commit 4b788c5

Browse files
committed
fix: update logic and tests for pieceing url parts together
1 parent 542cdcb commit 4b788c5

File tree

2 files changed

+37
-10
lines changed

2 files changed

+37
-10
lines changed

plugins/node/opentelemetry-instrumentation-aws-lambda/src/instrumentation.ts

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -443,17 +443,22 @@ export class AwsLambdaInstrumentation extends InstrumentationBase<AwsLambdaInstr
443443

444444
private static _extractFullUrl(event: any): string | undefined {
445445
// API gateway encodes a lot of url information in various places to recompute this
446-
if (!event.headers || !event.path) {
446+
if (
447+
!(
448+
event.headers &&
449+
(event.path || event.rawPath) &&
450+
event.headers['host'] &&
451+
event.headers['x-forwarded-proto']
452+
)
453+
) {
447454
return undefined;
448455
}
449-
let answer = '';
450-
if (event.headers['x-forwarded-proto']) {
451-
answer += event.headers['x-forwarded-proto'] + '://';
452-
}
453-
if (event.headers['host']) {
454-
answer += event.headers['host'];
456+
let answer = event.headers['x-forwarded-proto'] + '://';
457+
answer += event.headers['host'];
458+
if (event.headers['x-forwarded-port']) {
459+
answer += ':' + event.headers['x-forwarded-port'];
455460
}
456-
answer += event.path;
461+
answer += event.path ? event.path : event.rawPath;
457462
if (event.queryStringParameters) {
458463
let first = true;
459464
for (const key in event.queryStringParameters) {
@@ -464,7 +469,7 @@ export class AwsLambdaInstrumentation extends InstrumentationBase<AwsLambdaInstr
464469
first = false;
465470
}
466471
}
467-
return answer.length === 0 ? undefined : answer;
472+
return answer;
468473
}
469474

470475
private static _determineParent(

plugins/node/opentelemetry-instrumentation-aws-lambda/test/integrations/lambda-handler.test.ts

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1082,7 +1082,7 @@ describe('lambda handler', () => {
10821082
});
10831083

10841084
describe('url parsing', () => {
1085-
it('pulls url from api gateway events', async () => {
1085+
it('pulls url from api gateway rest events', async () => {
10861086
initializeHandler('lambda-test/sync.handler');
10871087
const event = {
10881088
path: '/lambda/test/path',
@@ -1103,5 +1103,27 @@ describe('lambda handler', () => {
11031103
);
11041104
console.log(span);
11051105
});
1106+
it('pulls url from api gateway http events', async () => {
1107+
initializeHandler('lambda-test/sync.handler');
1108+
const event = {
1109+
rawPath: '/lambda/test/path',
1110+
headers: {
1111+
host: 'www.example.com',
1112+
'x-forwarded-proto': 'http',
1113+
'x-forwarded-port': 1234,
1114+
},
1115+
queryStringParameters: {
1116+
key: 'value',
1117+
},
1118+
};
1119+
1120+
await lambdaRequire('lambda-test/sync').handler(event, ctx, () => {});
1121+
const [span] = memoryExporter.getFinishedSpans();
1122+
assert.strictEqual(
1123+
span.attributes[ATTR_URL_FULL],
1124+
'http://www.example.com:1234/lambda/test/path?key=value'
1125+
);
1126+
console.log(span);
1127+
});
11061128
});
11071129
});

0 commit comments

Comments
 (0)