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

aj/authorizer trace context #300

merged 38 commits into from
Nov 15, 2022

Conversation

astuyve
Copy link
Contributor

@astuyve astuyve commented Aug 22, 2022

Fixes DataDog/serverless-plugin-datadog#252 for NodeJS

What does this PR do?

Encodes and decodes trace context from an authorizer function

Motivation

Trace propagation and inferred span creation for token-based authorizers:
image

Trace propagation and inferred span creation for request-based authorizers:
image

Testing Guidelines

Manual and automated testing

Additional Notes

Types of Changes

  • Bug fix
  • New feature
  • Breaking change
  • Misc (docs, refactoring, dependency upgrade, etc.)

Check all that apply

  • This PR's description is comprehensive
  • This PR contains breaking changes that are documented in the description
  • This PR introduces new APIs or parameters that are documented and unlikely to change in the foreseeable future
  • This PR impacts documentation, and it has been updated (or a ticket has been logged)
  • This PR's changes are covered by the automated tests
  • This PR collects user input/sensitive content into Datadog
  • This PR passes the integration tests (ask a Datadog member to run the tests)

@astuyve astuyve requested a review from a team as a code owner August 22, 2022 20:03
@codecov-commenter
Copy link

codecov-commenter commented Aug 22, 2022

Codecov Report

Merging #300 (79668df) into main (6a12c46) will increase coverage by 0.26%.
The diff coverage is 86.11%.

@@            Coverage Diff             @@
##             main     #300      +/-   ##
==========================================
+ Coverage   80.05%   80.31%   +0.26%     
==========================================
  Files          36       36              
  Lines        1715     1758      +43     
  Branches      387      409      +22     
==========================================
+ Hits         1373     1412      +39     
- Misses        290      294       +4     
  Partials       52       52              
Impacted Files Coverage Δ
src/trace/span-wrapper.ts 50.00% <0.00%> (-14.29%) ⬇️
src/trace/tracer-wrapper.ts 80.48% <25.00%> (-6.00%) ⬇️
src/trace/trace-context-service.ts 83.87% <50.00%> (-2.80%) ⬇️
src/trace/listener.ts 68.18% <63.15%> (-1.93%) ⬇️
src/trace/span-inferrer.ts 89.23% <94.73%> (+0.34%) ⬆️
src/trace/context.ts 88.17% <94.87%> (+2.77%) ⬆️
src/index.ts 86.55% <100.00%> (+0.96%) ⬆️
src/trace/constants.ts 100.00% <100.00%> (ø)
src/trace/trigger.ts 88.02% <100.00%> (+1.01%) ⬆️

📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more

Copy link
Contributor

@DarcyRaynerDD DarcyRaynerDD left a comment

Choose a reason for hiding this comment

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

Left some minor comments

@@ -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

@@ -150,6 +157,17 @@ export class TraceListener {
return false;
}

injectAuthorizerSpan(result: any): any {
const finishTime = this.inferredSpan?.isAsync() ? this.wrappedCurrentSpan?.startTime() : Date.now();
Copy link
Contributor

Choose a reason for hiding this comment

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

This logic is a bit hard to follow. Maybe explain what's going on in a comment

@@ -52,6 +55,10 @@ export class SpanInferrer {
return "sync";
}

isTracedAuthorizerInvocation(event: any): boolean {
return event.requestContext?.authorizer?._datadog;
Copy link
Contributor

Choose a reason for hiding this comment

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

Is there any danger from not explicitly coalescing this into a bool?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It could evaluate true if the key _datadog is present but empty, or an empty string or something - but the next call is wrapped in a try block anyway (to handle the said case), so I think it's okay for the usage as is - but I'll add an explicit check for !== undefined, in case someone re-uses it in the future.

this.traceWrapper.startSpan("aws.apigateway.authorizer", upstreamSpanOptions),
{ isAsync: false },
);
upstreamAuthorizerSpan.finish(event.requestContext.timeEpoch);
Copy link
Contributor

Choose a reason for hiding this comment

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

The event.requestContext.timeEpoch header exists for HTTP API (API Gateway V2).
For REST API (API Gateway V1), the corresponding key is event.requestContext.requestTimeEpoch.
I also found out that integrationLatency is only available for REST API.
Moreover, for HTTP API the key actually becomes event.requestContext.authorizer.lambda._datadog example. So this current code here fits mostly with REST API.

Also, in both cases, the epoch time here is the original user invocation timestamp. Therefore, I was using the request time epoch + integrationlatency to get an approximation in the python version. Please let me know what do you think.

In short, my suggestion for this line is
upstreamAuthorizerSpan.finish(event.requestContext.requestTimeEpoch + event.requestContext.authorizer.integrationLatency);

"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

Copy link
Contributor

@duncanista duncanista left a comment

Choose a reason for hiding this comment

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

Looks really great!

@joeyzhao2018 joeyzhao2018 merged commit 95b00f0 into main Nov 15, 2022
@joeyzhao2018 joeyzhao2018 deleted the aj/authorizer-trace-context branch November 15, 2022 21:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Authorizer traces not correlated with API gateway traces
5 participants