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();
}
}