7

I'm trying to configure sentry on AWS Lambda (nodejs 8.10) but the exceptions are not sent to Sentry. I'm the feeling that is a issue of timing: the lambda is terminated before the data are sent to sentry.

Which is the right way to integrate sentry on AWS Lambda?

Thank you!

NearHuscarl
  • 66,950
  • 18
  • 261
  • 230
aGO
  • 1,263
  • 14
  • 30

4 Answers4

12

Update: Sentry automatically reports exceptions on Node/Lambda. see docs

You have to use the function flush which makes sure all events that are currently queued up will be sent:

Sentry.captureException(new Error('test'));
await Sentry.flush();

http://getsentry.github.io/sentry-javascript/modules/node.html#flush

ajs
  • 3
  • 3
HazA
  • 1,274
  • 12
  • 17
2

Install the Sentry module

npm install @sentry/node

See the bellow example to configure AWS Lambda function with Sentry.

      // Import the Sentry module.
      const Sentry = require("@sentry/node");
      // Configure the Sentry SDK.
      Sentry.init({
        dsn: "your dsn",
      });

      exports.handler = function (event, context) {
        try {
          textFunctionCall(); // Call undefined function.
        } catch (e) {
          Sentry.captureException(e); // using this function exception captured in Sentry dashboard.
          Sentry.flush(2000);
        }

        const response = {
          statusCode: 200,
          body: "Hello, exception captured",
        };

       return response;
      };

https://docs.sentry.io/platforms/node/serverless/#aws-lambda

Still not Capturing error in Sentry, then install the bellow node module these are the dependency.

npm install lru-cache
npm  install lru_map
npm install tslib
1

Install sentry/node

npm i @sentry/node

Add this code in the top of your lambda

const Sentry = require("@sentry/node");
Sentry.init({
  dsn: "https://0dc9558ffb934657b6c3b97182b4338d@sentry.io/182963" // your_dsn_key_here
});

Use sentry object

 try {
   //Logic here
  } catch (error) {
    Sentry.captureException("Error on getLogsById");
  }
Abdelhadi Abdo
  • 392
  • 2
  • 9
0

I try to work on this and got a solution using already existing function

Create file name sentry-lib.js and add below code

 const Sentry = require("@sentry/node");
    
    // Importing @sentry/tracing patches the global hub for tracing to work.
    const SentryTracing = require("@sentry/tracing");
    let transaction = null;
    let SentryInstance = null;
   export class SentryLib {
    
     constructor(){
           Sentry.init({
             dsn: process.env.SENTRY_DSN,
             tracesSampleRate: 1.0,
            });
      }

    startLogging(){
       transaction = Sentry.startTransaction();
     }

    logError(error){
        Sentry.captureException(error);
    }

     async endLogging(){
       await Sentry.flush(); // Flush all awaiting event in queue
       transaction.finish();
     }
 }

Now create file name second.js

export const sum = (){
   const sentryLibIns = new SentryLib();
   try{
     // Something will break here
    } catch(error){
     sentryLibIns.error(error); 
   }
}

Now create the handler file name handler.js

const handler = async (event, context, callback)=>{
   const sentryLibIns = new SentryLib();
   sentryLibIns.startLogging();  
   try{
        fn1();
    } catch(error){
      sentryLibIns.error(error); 
    } finally{
      await sentryLibIns.endLogging();
   }
}
Himanshu Joshi
  • 100
  • 1
  • 5