diff --git a/worker/job-handlers.ts b/worker/job-handlers.ts index a8dffcf3..cc3a2caa 100644 --- a/worker/job-handlers.ts +++ b/worker/job-handlers.ts @@ -31,14 +31,29 @@ async function runIfNotAlreadyRunning( res.sendStatus(200); return; } + + const sentryTags = { + job: id, + chainId, + }; + + const sentryTransaction = Sentry.startTransaction({ + op: 'http', + name: `POST /${jobId}`, + tags: sentryTags, + }); + + Sentry.configureScope((scope) => scope.setSpan(sentryTransaction)); + + const sentryChildSpan = sentryTransaction.startChild({ + op: 'background-job', + description: `Running job ${jobId}`, + tags: sentryTags, + }); + try { runningJobs.add(jobId); - // TODO, this does not seem to work properly as it is a "global" scope - Sentry.configureScope((scope) => { - scope.setTransactionName(`POST /${jobId}`); - }); - console.time(jobId); console.log(`Start job ${jobId}-start`); @@ -49,12 +64,17 @@ async function runIfNotAlreadyRunning( } console.log(`Successful job ${jobId}-done`); } catch (error) { + Sentry.captureException(error); + if (process.env.AWS_ALERTS === 'true') { await cronsMetricPublisher.publish(`${jobId}-error`); } console.log(`Error job ${jobId}-error`, error); next(error); } finally { + sentryChildSpan.finish(); + sentryTransaction.finish(); + runningJobs.delete(jobId); console.timeEnd(jobId); res.sendStatus(200); diff --git a/worker/worker.ts b/worker/worker.ts index 325aefb2..5ba9d73b 100644 --- a/worker/worker.ts +++ b/worker/worker.ts @@ -14,6 +14,7 @@ export async function startWorker() { // new Tracing.Integrations.Express({ app }), new Sentry.Integrations.Http({ tracing: true }), ], + tracesSampleRate: env.DEPLOYMENT_ENV === 'main' ? 0.2 : 1.0, }); app.use(Sentry.Handlers.requestHandler());