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

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 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
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,15 @@ import {
TraceFlags,
TracerProvider,
ROOT_CONTEXT,
Attributes,
} from '@opentelemetry/api';
import {
AWSXRAY_TRACE_ID_HEADER,
AWSXRayPropagator,
} from '@opentelemetry/propagator-aws-xray';
import {
SEMATTRS_FAAS_EXECUTION,
SEMATTRS_HTTP_URL,
johnbley marked this conversation as resolved.
Show resolved Hide resolved
SEMRESATTRS_CLOUD_ACCOUNT_ID,
SEMRESATTRS_FAAS_ID,
} from '@opentelemetry/semantic-conventions';
Expand Down Expand Up @@ -247,6 +249,7 @@ export class AwsLambdaInstrumentation extends InstrumentationBase<AwsLambdaInstr
context.invokedFunctionArn
),
[ATTR_FAAS_COLDSTART]: requestIsColdStart,
...AwsLambdaInstrumentation._extractOtherEventFields(event),
},
},
parent
Expand Down Expand Up @@ -429,6 +432,41 @@ export class AwsLambdaInstrumentation extends InstrumentationBase<AwsLambdaInstr
return propagation.extract(otelContext.active(), httpHeaders, headerGetter);
}

private static _extractOtherEventFields(event: any): Attributes {
const answer: Attributes = {};
const fullUrl = this._extractFullUrl(event);
if (fullUrl) {
answer[SEMATTRS_HTTP_URL] = fullUrl;
}
return answer;
}

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) {
johnbley marked this conversation as resolved.
Show resolved Hide resolved
return undefined;
}
let answer = '';
if (event.headers['x-forwarded-proto']) {
answer += event.headers['x-forwarded-proto'] + '://';
}
if (event.headers['host']) {
answer += event.headers['host'];
johnbley marked this conversation as resolved.
Show resolved Hide resolved
}
johnbley marked this conversation as resolved.
Show resolved Hide resolved
answer += event.path;
if (event.queryStringParameters) {
let first = true;
for (const key in event.queryStringParameters) {
answer += first ? '?' : '&';
answer += encodeURIComponent(key);
answer += '=';
answer += encodeURIComponent(event.queryStringParameters[key]);
first = false;
}
}
return answer.length === 0 ? undefined : answer;
}

private static _determineParent(
event: any,
context: Context,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import {
SEMATTRS_EXCEPTION_MESSAGE,
SEMATTRS_FAAS_COLDSTART,
SEMATTRS_FAAS_EXECUTION,
SEMATTRS_HTTP_URL,
johnbley marked this conversation as resolved.
Show resolved Hide resolved
SEMRESATTRS_FAAS_NAME,
} from '@opentelemetry/semantic-conventions';
import {
Expand Down Expand Up @@ -1079,4 +1080,28 @@ describe('lambda handler', () => {
assert.strictEqual(span.parentSpanId, undefined);
});
});

describe('url parsing', () => {
johnbley marked this conversation as resolved.
Show resolved Hide resolved
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);
johnbley marked this conversation as resolved.
Show resolved Hide resolved
});
});
});