Skip to content

🪢 Open source agent evals & observability: Trace, evaluate, and improve LLM applications with one open platform.

feat: improve sentry on worker (#2426)

Max Deichmann94387d1f6bb7
diff --git a/worker/src/app.ts b/worker/src/app.ts
index c6b9788fc3..ab8dd752a5 100644
--- a/worker/src/app.ts
+++ b/worker/src/app.ts
@@ -1,12 +1,10 @@
+import "./instrumentation"; // this is required to make instrumentation work
import express from "express";
import cors from "cors";
import * as Sentry from "@sentry/node";
-import { nodeProfilingIntegration } from "@sentry/profiling-node";
-
import * as middlewares from "./middlewares";
import api from "./api";
import MessageResponse from "./interfaces/MessageResponse";
-import { env } from "./env";
require("dotenv").config();
@@ -19,28 +17,6 @@ import helmet from "helmet";
const app = express();
-const isSentryEnabled = String(env.SENTRY_DSN) !== undefined;
-
-if (isSentryEnabled) {
- Sentry.init({
- dsn: String(env.SENTRY_DSN),
- integrations: [
- Sentry.httpIntegration(),
- Sentry.expressIntegration(),
- nodeProfilingIntegration(),
- Sentry.redisIntegration(),
- Sentry.prismaIntegration(),
- ],
-
- tracesSampleRate: 0.01, // Capture 100% of the transactions
-
- profilesSampleRate: 0.01,
- sampleRate: 0.1,
- });
-
- logger.info("Sentry enabled");
-}
-
app.use(helmet());
app.use(cors());
app.use(express.json());
@@ -52,10 +28,9 @@ app.get<{}, MessageResponse>("/", (req, res) => {
app.use("/api", api);
-if (isSentryEnabled) {
- // The error handler must be before any other error middleware and after all controllers
- app.use(Sentry.expressErrorHandler());
-}
+// The error handler must be before any other error middleware and after all controllers
+app.use(Sentry.expressErrorHandler());
+
app.use(middlewares.notFound);
app.use(middlewares.errorHandler);
diff --git a/worker/src/instrumentation.ts b/worker/src/instrumentation.ts
index 20962222d9..a413dda8da 100644
--- a/worker/src/instrumentation.ts
+++ b/worker/src/instrumentation.ts
@@ -1,6 +1,27 @@
+import { nodeProfilingIntegration } from "@sentry/profiling-node";
import { env } from "./env";
import * as Sentry from "@sentry/node";
+Sentry.init({
+ dsn: String(env.SENTRY_DSN),
+ debug: true,
+ integrations: [
+ Sentry.httpIntegration(),
+ Sentry.expressIntegration(),
+ nodeProfilingIntegration(),
+ Sentry.redisIntegration(),
+ Sentry.prismaIntegration(),
+ ],
+
+ // Add Tracing by setting tracesSampleRate
+ // We recommend adjusting this value in production
+ tracesSampleRate: 0.01,
+
+ // Set sampling rate for profiling
+ // This is relative to tracesSampleRate
+ profilesSampleRate: 0.01,
+});
+
type CallbackAsyncFn<T> = (span?: Sentry.Span) => Promise<T>;
export async function instrumentAsync<T>(