Skip to content
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

feat: report url for lambda invoked via api gateway #2404

Merged
merged 16 commits into from
Dec 10, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
Expand Up @@ -443,17 +443,22 @@ export class AwsLambdaInstrumentation extends InstrumentationBase<AwsLambdaInstr

private static _extractFullUrl(event: any): string | undefined {
// API gateway encodes a lot of url information in various places to recompute this
if (!event.headers || !event.path) {
if (
!(
event.headers &&
(event.path || event.rawPath) &&
event.headers['host'] &&
event.headers['x-forwarded-proto']
)
) {
return undefined;
}
let answer = '';
if (event.headers['x-forwarded-proto']) {
answer += event.headers['x-forwarded-proto'] + '://';
}
if (event.headers['host']) {
answer += event.headers['host'];
let answer = event.headers['x-forwarded-proto'] + '://';
answer += event.headers['host'];
if (event.headers['x-forwarded-port']) {
answer += ':' + event.headers['x-forwarded-port'];
}
johnbley marked this conversation as resolved.
Show resolved Hide resolved
answer += event.path;
answer += event.path ? event.path : event.rawPath;
if (event.queryStringParameters) {
let first = true;
for (const key in event.queryStringParameters) {
Expand All @@ -464,7 +469,7 @@ export class AwsLambdaInstrumentation extends InstrumentationBase<AwsLambdaInstr
first = false;
}
}
return answer.length === 0 ? undefined : answer;
return answer;
}

private static _determineParent(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1082,7 +1082,7 @@ describe('lambda handler', () => {
});

describe('url parsing', () => {
johnbley marked this conversation as resolved.
Show resolved Hide resolved
it('pulls url from api gateway events', async () => {
it('pulls url from api gateway rest events', async () => {
initializeHandler('lambda-test/sync.handler');
const event = {
johnbley marked this conversation as resolved.
Show resolved Hide resolved
path: '/lambda/test/path',
Expand All @@ -1103,5 +1103,27 @@ describe('lambda handler', () => {
);
console.log(span);
johnbley marked this conversation as resolved.
Show resolved Hide resolved
});
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);
johnbley marked this conversation as resolved.
Show resolved Hide resolved
});
});
});