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

aj/authorizer trace context #300

Merged
merged 38 commits into from
Nov 15, 2022
Merged
Show file tree
Hide file tree
Changes from 24 commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
59051ce
wip: auth trace context
astuyve Jun 8, 2022
801fb15
Merge branch 'main' into aj/authorizer-trace-context
astuyve Jun 17, 2022
735e3f6
wip
astuyve Jun 21, 2022
ffd6ad6
Merge remote-tracking branch 'origin/main' into aj/authorizer-trace-c…
astuyve Jul 1, 2022
17de1df
wip stash
astuyve Jul 11, 2022
b16e5ff
Merge branch 'main' into aj/authorizer-trace-context
astuyve Aug 4, 2022
1e4317d
feat: working surrogate authorizer span and propagation.
astuyve Aug 17, 2022
08b035c
feat: Use the tracer implementation of inject
astuyve Aug 18, 2022
f90b065
refactor: Clean up trace header extraction into common method
astuyve Aug 19, 2022
3323833
refactor: clean up unneeded vars
astuyve Aug 19, 2022
0a265f0
feat: Config for encodeAuthorizerContext.
astuyve Aug 19, 2022
d4f3655
feat: Update default config tests
astuyve Aug 19, 2022
cab5361
feat: working authorization propagation for token-based authorizers
astuyve Aug 22, 2022
7bf2c13
feat: Add spec, remove unused attribute
astuyve Aug 22, 2022
a454d42
feat: Specs for context and span inferrer
astuyve Aug 22, 2022
688e784
feat: Parse JSON with try
astuyve Aug 22, 2022
1b087d9
feat: fix bad autoimport
astuyve Aug 22, 2022
391537d
Merge branch 'main' into aj/authorizer-trace-context
astuyve Aug 22, 2022
f438a86
feat: remove unused variable
astuyve Aug 22, 2022
02e2fdf
feat: remove unused id class
astuyve Aug 22, 2022
433b529
fix: and remove unused id export
astuyve Aug 22, 2022
0b20546
feat: Remove more POC code
astuyve Aug 22, 2022
6f92593
Merge branch 'main' into aj/authorizer-trace-context
astuyve Aug 24, 2022
9213906
feat: Coalesce isTracedAuthorizerInvocation into a bool, add comments…
astuyve Aug 29, 2022
b3a2ebd
Merge branch 'main' of github.com:DataDog/datadog-lambda-js into aj/a…
joeyzhao2018 Oct 5, 2022
b7237cb
httpapi + restapi handled
joeyzhao2018 Oct 12, 2022
033e9e4
move the injection from onEndingInvocation to onCompleteInvocation
joeyzhao2018 Oct 13, 2022
c33843d
lint fixes
joeyzhao2018 Oct 13, 2022
0419e03
all base64encode and all use authorizing requestId
joeyzhao2018 Oct 14, 2022
6687f3f
Merge branch 'main' of github.com:DataDog/datadog-lambda-js into aj/a…
joeyzhao2018 Oct 24, 2022
6bb2e54
Merge branch 'main' of github.com:DataDog/datadog-lambda-js into aj/a…
joeyzhao2018 Oct 24, 2022
49bf887
add cached test cases and websocket test cases
joeyzhao2018 Oct 24, 2022
2fbc808
fix token-type authorizer
joeyzhao2018 Oct 26, 2022
065e748
fix the endTime() of SpanWrapper
joeyzhao2018 Oct 26, 2022
860028f
fix no-string-throw error
joeyzhao2018 Oct 26, 2022
eabcd0f
padding ms to ns to be compatible with lambdas in other languages
joeyzhao2018 Nov 4, 2022
73a9132
Merge branch 'main' into aj/authorizer-trace-context
joeyzhao2018 Nov 8, 2022
79668df
add config.decodeAuthorizerContext and httpapi no zero-length span
joeyzhao2018 Nov 9, 2022
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
89 changes: 89 additions & 0 deletions event_samples/api-gateway-traced-authorizer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
{
"resource": "/hello",
"path": "/hello",
"httpMethod": "POST",
"headers": {
"Accept": "*/*",
"Accept-Encoding": "gzip, deflate, br",
"Authorization": "password",
"CloudFront-Forwarded-Proto": "https",
"CloudFront-Is-Desktop-Viewer": "true",
"CloudFront-Is-Mobile-Viewer": "false",
"CloudFront-Is-SmartTV-Viewer": "false",
"CloudFront-Is-Tablet-Viewer": "false",
"CloudFront-Viewer-ASN": "174",
"CloudFront-Viewer-Country": "US",
"Host": "3gsxz7lha4.execute-api.sa-east-1.amazonaws.com",
"Postman-Token": "62ccb3d9-a44f-427c-9952-418c0a2eb1c3",
"User-Agent": "PostmanRuntime/7.29.0",
"Via": "1.1 685fd458a839bc725280d7e9380648c6.cloudfront.net (CloudFront)",
"X-Amz-Cf-Id": "90JXZEr6stVabQV78Zwn5EADW0evkpWINdmt3jzkuHQh9KtqowKejw==",
"X-Amzn-Trace-Id": "Root=1-62ffee4f-373bdfda15f09a065a39ac73",
"X-Forwarded-For": "38.142.177.195, 64.252.135.71",
"X-Forwarded-Port": "443",
"X-Forwarded-Proto": "https"
},
"multiValueHeaders": {
"Accept": ["*/*"],
"Accept-Encoding": ["gzip, deflate, br"],
"Authorization": ["password"],
"CloudFront-Forwarded-Proto": ["https"],
"CloudFront-Is-Desktop-Viewer": ["true"],
"CloudFront-Is-Mobile-Viewer": ["false"],
"CloudFront-Is-SmartTV-Viewer": ["false"],
"CloudFront-Is-Tablet-Viewer": ["false"],
"CloudFront-Viewer-ASN": ["174"],
"CloudFront-Viewer-Country": ["US"],
"Host": ["3gsxz7lha4.execute-api.sa-east-1.amazonaws.com"],
"Postman-Token": ["62ccb3d9-a44f-427c-9952-418c0a2eb1c3"],
"User-Agent": ["PostmanRuntime/7.29.0"],
"Via": ["1.1 685fd458a839bc725280d7e9380648c6.cloudfront.net (CloudFront)"],
"X-Amz-Cf-Id": ["90JXZEr6stVabQV78Zwn5EADW0evkpWINdmt3jzkuHQh9KtqowKejw=="],
"X-Amzn-Trace-Id": ["Root=1-62ffee4f-373bdfda15f09a065a39ac73"],
"X-Forwarded-For": ["38.142.177.195, 64.252.135.71"],
"X-Forwarded-Port": ["443"],
"X-Forwarded-Proto": ["https"]
},
"queryStringParameters": null,
"multiValueQueryStringParameters": null,
"pathParameters": null,
"stageVariables": null,
"requestContext": {
"resourceId": "oozq9u",
"authorizer": {
"_datadog": "{\"x-datadog-trace-id\":\"2095319761084710747\",\"x-datadog-parent-id\":\"2095319761084710747\",\"x-datadog-sampling-priority\":\"1\",\"x-datadog-parent-span-finish-time\":1660939857052}",
"principalId": "foo",
"integrationLatency": 1419,
"preserve": "this key set by a customer"
},
"resourcePath": "/hello",
"httpMethod": "POST",
"extendedRequestId": "XIIseElXGjQFvXg=",
"requestTime": "19/Aug/2022:20:10:55 +0000",
"path": "/dev/hello",
"accountId": "601427279990",
"protocol": "HTTP/1.1",
"stage": "dev",
"domainPrefix": "3gsxz7lha4",
"requestTimeEpoch": 1660939855656,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is an example about the other comment

"requestId": "f1f8d46d-ef6f-46af-a3ed-7a20a26e2651",
"identity": {
"cognitoIdentityPoolId": null,
"accountId": null,
"cognitoIdentityId": null,
"caller": null,
"sourceIp": "38.142.177.195",
"principalOrgId": null,
"accessKey": null,
"cognitoAuthenticationType": null,
"cognitoAuthenticationProvider": null,
"userArn": null,
"userAgent": "PostmanRuntime/7.29.0",
"user": null
},
"domainName": "3gsxz7lha4.execute-api.sa-east-1.amazonaws.com",
"apiId": "3gsxz7lha4"
},
"body": null,
"isBase64Encoded": false
}
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ export const defaultConfig: Config = {
captureLambdaPayload: false,
createInferredSpan: true,
debugLogging: false,
encodeAuthorizerContext: true,
enhancedMetrics: true,
forceWrap: false,
injectLogContext: true,
Expand Down
1 change: 1 addition & 0 deletions src/trace/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,4 @@ export const xrayTraceEnvVar = "_X_AMZN_TRACE_ID";
export const awsXrayDaemonAddressEnvVar = "AWS_XRAY_DAEMON_ADDRESS";
export const ddtraceVersion = "X.X.X";
export const apiGatewayEventV2 = "2.0";
export const parentSpanFinishTimeHeader = "x-datadog-parent-span-finish-time";
22 changes: 22 additions & 0 deletions src/trace/context.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {
readTraceFromSQSEvent,
readTraceFromHTTPEvent,
readTraceFromLambdaContext,
readTraceFromAuthorizerEvent,
} from "./context";

let sentSegment: any;
Expand Down Expand Up @@ -221,6 +222,27 @@ describe("readTraceFromEvent", () => {
});
});

it("can parse a traced authorizer source", () => {
const result = readTraceFromEvent({
requestContext: {
resourceId: "oozq9u",
authorizer: {
_datadog:
'{"x-datadog-trace-id":"2389589954026090296","x-datadog-parent-id":"2389589954026090296","x-datadog-sampling-priority":"1","x-datadog-parent-span-finish-time":1660939899233}',
principalId: "foo",
integrationLatency: 71,
preserve: "this key set by a customer",
},
},
});
expect(result).toEqual({
parentID: "2389589954026090296",
sampleMode: 1,
source: "event",
traceID: "2389589954026090296",
});
});

it("can parse an SNS message source", () => {
const result = readTraceFromEvent({
Records: [
Expand Down
159 changes: 42 additions & 117 deletions src/trace/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -207,23 +207,8 @@ export function readTraceFromSQSEvent(event: SQSEvent): TraceContext | undefined
const traceHeaders = event.Records[0].messageAttributes._datadog.stringValue;

try {
const traceData = JSON.parse(traceHeaders);
const traceID = traceData[traceIDHeader];
const parentID = traceData[parentIDHeader];
const sampledHeader = traceData[samplingPriorityHeader];
const trace = exportTraceData(JSON.parse(traceHeaders));

if (typeof traceID !== "string" || typeof parentID !== "string" || typeof sampledHeader !== "string") {
return;
}

const sampleMode = parseInt(sampledHeader, 10);

const trace = {
parentID,
sampleMode,
source: Source.Event,
traceID,
};
logDebug(`extracted trace context from sqs event`, { trace, event });
return trace;
} catch (err) {
Expand Down Expand Up @@ -253,21 +238,8 @@ export function readTraceFromSNSSQSEvent(event: SQSEvent): TraceContext | undefi
const b64Decoded = Buffer.from(parsedBody.MessageAttributes._datadog.Value, "base64").toString("ascii");
traceData = JSON.parse(b64Decoded);
}
const traceID = traceData[traceIDHeader];
const parentID = traceData[parentIDHeader];
const sampledHeader = traceData[samplingPriorityHeader];
const trace = exportTraceData(traceData);

if (typeof traceID !== "string" || typeof parentID !== "string" || typeof sampledHeader !== "string") {
return;
}
const sampleMode = parseInt(sampledHeader, 10);

const trace = {
parentID,
sampleMode,
source: Source.Event,
traceID,
};
logDebug(`extracted trace context from SNS SQS event`, { trace, event });
return trace;
}
Expand All @@ -285,22 +257,7 @@ export function readTraceFromKinesisEvent(event: KinesisStreamEvent): TraceConte
try {
const parsedBody = JSON.parse(Buffer.from(event.Records[0].kinesis.data, "base64").toString("ascii")) as any;
if (parsedBody && parsedBody._datadog) {
const traceData = parsedBody._datadog;
const traceID = traceData[traceIDHeader];
const parentID = traceData[parentIDHeader];
const sampledHeader = traceData[samplingPriorityHeader];

if (typeof traceID !== "string" || typeof parentID !== "string" || typeof sampledHeader !== "string") {
return;
}
const sampleMode = parseInt(sampledHeader, 10);

const trace = {
parentID,
sampleMode,
source: Source.Event,
traceID,
};
const trace = exportTraceData(parsedBody._datadog);
logDebug(`extracted trace context from Kinesis event`, { trace });
return trace;
}
Expand All @@ -316,22 +273,7 @@ export function readTraceFromKinesisEvent(event: KinesisStreamEvent): TraceConte
export function readTraceFromEventbridgeEvent(event: EventBridgeEvent<any, any>): TraceContext | undefined {
if (event?.detail?._datadog) {
try {
const traceData = event.detail._datadog;
const traceID = traceData[traceIDHeader];
const parentID = traceData[parentIDHeader];
const sampledHeader = traceData[samplingPriorityHeader];

if (typeof traceID !== "string" || typeof parentID !== "string" || typeof sampledHeader !== "string") {
return;
}
const sampleMode = parseInt(sampledHeader, 10);

const trace = {
parentID,
sampleMode,
source: Source.Event,
traceID,
};
const trace = exportTraceData(event.detail._datadog);
logDebug(`extracted trace context from Eventbridge event`, { trace, event });
return trace;
} catch (err) {
Expand All @@ -355,21 +297,7 @@ export function readTraceFromSNSEvent(event: SNSEvent): TraceContext | undefined
);
traceData = JSON.parse(b64Decoded);
}
const traceID = traceData[traceIDHeader];
const parentID = traceData[parentIDHeader];
const sampledHeader = traceData[samplingPriorityHeader];

if (typeof traceID !== "string" || typeof parentID !== "string" || typeof sampledHeader !== "string") {
return;
}
const sampleMode = parseInt(sampledHeader, 10);

const trace = {
parentID,
sampleMode,
source: Source.Event,
traceID,
};
const trace = exportTraceData(traceData);
logDebug(`extracted trace context from SNS event`, { trace, event });
return trace;
} catch (err) {
Expand Down Expand Up @@ -411,26 +339,7 @@ export function readTraceFromLambdaContext(context: any): TraceContext | undefin
return;
}

const traceID = traceData[traceIDHeader];
if (typeof traceID !== "string") {
return;
}
const parentID = traceData[parentIDHeader];
if (typeof parentID !== "string") {
return;
}
const sampledHeader = traceData[samplingPriorityHeader];
if (typeof sampledHeader !== "string") {
return;
}
const sampleMode = parseInt(sampledHeader, 10);

const trace = {
parentID,
sampleMode,
source: Source.Event,
traceID,
};
const trace = exportTraceData(traceData);
logDebug(`extracted trace context from lambda context`, { trace, context });
return trace;
}
Expand All @@ -443,43 +352,40 @@ export function readTraceFromHTTPEvent(event: any): TraceContext | undefined {
lowerCaseHeaders[key.toLowerCase()] = headers[key];
}

const traceID = lowerCaseHeaders[traceIDHeader];
if (typeof traceID !== "string") {
return;
}
const parentID = lowerCaseHeaders[parentIDHeader];
if (typeof parentID !== "string") {
return;
}
const sampledHeader = lowerCaseHeaders[samplingPriorityHeader];
if (typeof sampledHeader !== "string") {
return;
}
const sampleMode = parseInt(sampledHeader, 10);

const trace = {
parentID,
sampleMode,
source: Source.Event,
traceID,
};
const trace = exportTraceData(lowerCaseHeaders);

logDebug(`extracted trace context from http event`, { trace, event });
return trace;
}

export function readTraceFromAuthorizerEvent(event: any): TraceContext | undefined {
let traceData;
try {
traceData = JSON.parse(event.requestContext.authorizer._datadog);
} catch (error) {
logDebug(`unable to extract trace context from authorizer event`, { error });
return;
}
return exportTraceData(traceData);
}

export function readTraceFromEvent(event: any): TraceContext | undefined {
if (!event || typeof event !== "object") {
return;
}

if (event?.requestContext?.authorizer?._datadog && event.requestContext.authorizer.integrationLatency > 0) {
return readTraceFromAuthorizerEvent(event);
}

if (event.headers !== null && typeof event.headers === "object") {
return readTraceFromHTTPEvent(event);
}

if (isSNSEvent(event)) {
return readTraceFromSNSEvent(event);
}

if (isSNSSQSEvent(event)) {
return readTraceFromSNSSQSEvent(event);
}
Expand Down Expand Up @@ -640,3 +546,22 @@ export function convertToAPMParentID(xrayParentID: string): string | undefined {
}
return hex.toString(10);
}

function exportTraceData(traceData: any): TraceContext | undefined {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice refactor

const traceID = traceData[traceIDHeader];
const parentID = traceData[parentIDHeader];
const sampledHeader = traceData[samplingPriorityHeader];

if (typeof traceID !== "string" || typeof parentID !== "string" || typeof sampledHeader !== "string") {
return;
}

const sampleMode = parseInt(sampledHeader, 10);

return {
parentID,
sampleMode,
source: Source.Event,
traceID,
};
}
Loading