-
Notifications
You must be signed in to change notification settings - Fork 36
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
+1,194
−164
Merged
Changes from 22 commits
Commits
Show all changes
38 commits
Select commit
Hold shift + click to select a range
59051ce
wip: auth trace context
astuyve 801fb15
Merge branch 'main' into aj/authorizer-trace-context
astuyve 735e3f6
wip
astuyve ffd6ad6
Merge remote-tracking branch 'origin/main' into aj/authorizer-trace-c…
astuyve 17de1df
wip stash
astuyve b16e5ff
Merge branch 'main' into aj/authorizer-trace-context
astuyve 1e4317d
feat: working surrogate authorizer span and propagation.
astuyve 08b035c
feat: Use the tracer implementation of inject
astuyve f90b065
refactor: Clean up trace header extraction into common method
astuyve 3323833
refactor: clean up unneeded vars
astuyve 0a265f0
feat: Config for encodeAuthorizerContext.
astuyve d4f3655
feat: Update default config tests
astuyve cab5361
feat: working authorization propagation for token-based authorizers
astuyve 7bf2c13
feat: Add spec, remove unused attribute
astuyve a454d42
feat: Specs for context and span inferrer
astuyve 688e784
feat: Parse JSON with try
astuyve 1b087d9
feat: fix bad autoimport
astuyve 391537d
Merge branch 'main' into aj/authorizer-trace-context
astuyve f438a86
feat: remove unused variable
astuyve 02e2fdf
feat: remove unused id class
astuyve 433b529
fix: and remove unused id export
astuyve 0b20546
feat: Remove more POC code
astuyve 6f92593
Merge branch 'main' into aj/authorizer-trace-context
astuyve 9213906
feat: Coalesce isTracedAuthorizerInvocation into a bool, add comments…
astuyve b3a2ebd
Merge branch 'main' of github.com:DataDog/datadog-lambda-js into aj/a…
joeyzhao2018 b7237cb
httpapi + restapi handled
joeyzhao2018 033e9e4
move the injection from onEndingInvocation to onCompleteInvocation
joeyzhao2018 c33843d
lint fixes
joeyzhao2018 0419e03
all base64encode and all use authorizing requestId
joeyzhao2018 6687f3f
Merge branch 'main' of github.com:DataDog/datadog-lambda-js into aj/a…
joeyzhao2018 6bb2e54
Merge branch 'main' of github.com:DataDog/datadog-lambda-js into aj/a…
joeyzhao2018 49bf887
add cached test cases and websocket test cases
joeyzhao2018 2fbc808
fix token-type authorizer
joeyzhao2018 065e748
fix the endTime() of SpanWrapper
joeyzhao2018 860028f
fix no-string-throw error
joeyzhao2018 eabcd0f
padding ms to ns to be compatible with lambdas in other languages
joeyzhao2018 73a9132
Merge branch 'main' into aj/authorizer-trace-context
joeyzhao2018 79668df
add config.decodeAuthorizerContext and httpapi no zero-length span
joeyzhao2018 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
"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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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) { | ||
|
@@ -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; | ||
} | ||
|
@@ -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; | ||
} | ||
|
@@ -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) { | ||
|
@@ -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) { | ||
|
@@ -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; | ||
} | ||
|
@@ -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); | ||
} | ||
|
@@ -640,3 +546,22 @@ export function convertToAPMParentID(xrayParentID: string): string | undefined { | |
} | ||
return hex.toString(10); | ||
} | ||
|
||
function exportTraceData(traceData: any): TraceContext | undefined { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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, | ||
}; | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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