DataDog / datadog-lambda-js

The Datadog AWS Lambda Library for Node
Apache License 2.0
105 stars 35 forks source link

[SVLS-4699] Detect exceptions raised outside of the handler #533

Closed TalUsvyatsky closed 2 months ago

TalUsvyatsky commented 2 months ago

What does this PR do?

This PR adds telemetry for exceptions that occur outside of the handler function.

When an exception occurs outside of the handler function:

  1. aws.lambda.enhanced.errors is emitted
  2. A trace with an error span is created

Motivation

Testing Guidelines

  1. Created a handler file like this for testing "cjs" handler:

    throw new TypeError("im a type error")
    exports.handler = function async (event) {
    const response = {
      statusCode: 200,
      body: JSON.stringify('Hello from Lambda!'),
    };
    return response;
    };

    and one like this for testing "mjs" handler:

    throw new TypeError("im a type error")
    export const handler = async (event) => {
    const response = {
      statusCode: 200,
      body: JSON.stringify('Hello from Lambda!'),
    };
    return response;
    };
  2. Deployed my function using each handler and invoked it

  3. Saw traces with an error span from the call (example trace)

  4. Saw aws.lambda.enhanced.errors emitted (graph)

  5. Traces appear in serverless detail page invocation table (link to search for function)

  6. Issues appear in error tracking and are informative

Additional Notes

Types of Changes

Check all that apply

codecov-commenter commented 2 months ago

Codecov Report

All modified and coverable lines are covered by tests :white_check_mark:

Project coverage is 82.10%. Comparing base (78c1900) to head (249712e). Report is 8 commits behind head on main.

:exclamation: Current head 249712e differs from pull request most recent head 75ae1c8. Consider uploading reports for the commit 75ae1c8 to get more accurate results

Additional details and impacted files ```diff @@ Coverage Diff @@ ## main #533 +/- ## ========================================== + Coverage 81.75% 82.10% +0.35% ========================================== Files 54 56 +2 Lines 2209 2286 +77 Branches 515 529 +14 ========================================== + Hits 1806 1877 +71 - Misses 337 344 +7 + Partials 66 65 -1 ```

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.