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

Extract span tags from triggering event #140

Merged
merged 26 commits into from
Feb 1, 2021
Merged

Conversation

DylanLovesCoffee
Copy link
Contributor

@DylanLovesCoffee DylanLovesCoffee commented Dec 18, 2020

What does this PR do?

Parses the event invoking the function for tags to be added to the span. Currently only adds the following services to be detected: API Gateway, ALB, SQS, SNS, DynamoDB, Kinesis, Cloudfront, CW Logs, CW Events. API Gateway and ALB trigger events will also add the HTTP url, method, path, referer and status code response if available.

For the case of X-Ray <> Datadog hybrid tracing, we create an additional dummy Datadog subsegment for the trigger tags.

Motivation

SLS-790 - Display function trigger span tags

Testing Guidelines

Unit tests

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)

@DylanLovesCoffee DylanLovesCoffee requested a review from a team as a code owner December 18, 2020 16:27
@codecov-io
Copy link

codecov-io commented Dec 23, 2020

Codecov Report

Merging #140 (693dee0) into main (e792562) will decrease coverage by 1.28%.
The diff coverage is 78.65%.

Impacted file tree graph

@@            Coverage Diff             @@
##             main     #140      +/-   ##
==========================================
- Coverage   87.33%   86.05%   -1.29%     
==========================================
  Files          29       31       +2     
  Lines        1011     1169     +158     
  Branches      179      232      +53     
==========================================
+ Hits          883     1006     +123     
- Misses         89      110      +21     
- Partials       39       53      +14     
Impacted Files Coverage Δ
src/index.ts 80.18% <50.00%> (-3.49%) ⬇️
src/trace/tracer-wrapper.ts 87.50% <60.00%> (-5.61%) ⬇️
src/trace/listener.ts 88.57% <66.66%> (-3.37%) ⬇️
src/trace/trigger.ts 78.07% <78.07%> (ø)
src/trace/constants.ts 100.00% <100.00%> (ø)
src/trace/context.ts 90.09% <100.00%> (+0.09%) ⬆️
src/utils/event-type-guards.ts 100.00% <100.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update e792562...693dee0. Read the comment docs.

@tianchu
Copy link
Contributor

tianchu commented Dec 29, 2020

@DarcyRaynerDD Can you help review as well and give @DylanLovesCoffee some feedback, as I'm not super sure about a few key changes. Hope you can share some thoughts.

@@ -16,6 +16,7 @@ export const samplingPriorityHeader = "x-datadog-sampling-priority";
export const xraySubsegmentName = "datadog-metadata";
export const xraySubsegmentKey = "trace";
export const xrayBaggageSubsegmentKey = "root_span_metadata";
export const xrayLambdaFunctionTagsKey = "lambda_function_tags";
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm still a little confused by this change. What was the reason for xrayBaggageSubsegmentKey not being sufficient?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is mainly just to differentiate the behavior in the X-Ray converter. Only the first root_span_metadata subsegment that's found is added to the root span, while all lambda_function_tags subsegments found are added to their respective parent Lambda execution span(s). On top of that, if X-Ray for API Gateway is enabled, the API Gateway spans are considered the root span.

if (result === undefined) {
statusCode = "502";
} else if (result.statusCode) {
// Use the statusCode if available
Copy link
Contributor

Choose a reason for hiding this comment

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

Using any for the result type is fine for this method, but we should be stricter with the type checking, especially since this method is called during a finally block and should never throw.

const statusCode = result?.statusCode; // Will be undefined if statusCode doesn't exist or result isn't an object

if (typeof statusCode === 'number') {
   return statusCode.toString();
} else if (typeof statusCode ==="string") {
   return statusCode;
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

TIL about optional chaining! I kept the check for result === undefined here since we would still need to return a 502 status when the Lambda doesn't receive a response.


// e.g. arn:aws:s3:::lambda-xyz123-abc890
if (source === "s3") {
eventSourceARN = extractS3EventARN(event);
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 structure may still be malformed, even after we apply our typeguard. We should add a try catch around this method

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I was curious how you knew that the S3 event structure might be malformed versus others? I wasn't sure if I should try wrapping the other similar methods with a try...catch

Copy link
Contributor

Choose a reason for hiding this comment

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

I should have clarified, they could all be malformed, I meant a generic try catch around the entire method.

@DylanLovesCoffee DylanLovesCoffee merged commit dda60f2 into main Feb 1, 2021
@DylanLovesCoffee DylanLovesCoffee deleted the dylan/trigger-tags branch February 1, 2021 20:37
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.

4 participants