Skip to content

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

fix(telemetry): initial run & safeguard against exceptions (#534)

Marc Klingen6088025a8153
diff --git a/src/features/telemetry/index.ts b/src/features/telemetry/index.ts
index 3ac8b89ec1..f4d0283a7f 100644
--- a/src/features/telemetry/index.ts
+++ b/src/features/telemetry/index.ts
@@ -13,33 +13,38 @@ const JOB_INTERVAL_MINUTES = Prisma.raw("60");
const JOB_TIMEOUT_MINUTES = Prisma.raw("10");
export async function telemetry() {
- // Only run in prod
- if (process.env.NODE_ENV !== "production") return;
- // Do not run in Lanfuse cloud, separate telemetry is used
- if (process.env.NEXT_PUBLIC_LANGFUSE_CLOUD_REGION !== undefined) return;
- // Check if telemetry is not disabled
- if (process.env.TELEMETRY_ENABLED === "false") return;
- // Do not run in CI
- if (process.env.CI) return;
-
- // Check via db cron_jobs table if it is time to run job
- const job = await jobScheduler();
-
- if (job.shouldRunJob) {
- const { jobStartedAt, lastRun, clientId } = job;
-
- // Run telemetry job
- await posthogTelemetry({
- startTimeframe: lastRun,
- endTimeframe: jobStartedAt,
- clientId,
- });
+ try {
+ // Only run in prod
+ if (process.env.NODE_ENV !== "production") return;
+ // Do not run in Lanfuse cloud, separate telemetry is used
+ if (process.env.NEXT_PUBLIC_LANGFUSE_CLOUD_REGION !== undefined) return;
+ // Check if telemetry is not disabled
+ if (process.env.TELEMETRY_ENABLED === "false") return;
+ // Do not run in CI
+ if (process.env.CI) return;
- // Update cron_jobs table
- await prisma.cronJobs.update({
- where: { name: "telemetry" },
- data: { lastRun: jobStartedAt, state: clientId, jobStartedAt: null },
- });
+ // Check via db cron_jobs table if it is time to run job
+ const job = await jobScheduler();
+
+ if (job.shouldRunJob) {
+ const { jobStartedAt, lastRun, clientId } = job;
+
+ // Run telemetry job
+ await posthogTelemetry({
+ startTimeframe: lastRun,
+ endTimeframe: jobStartedAt,
+ clientId,
+ });
+
+ // Update cron_jobs table
+ await prisma.cronJobs.update({
+ where: { name: "telemetry" },
+ data: { lastRun: jobStartedAt, state: clientId, jobStartedAt: null },
+ });
+ }
+ } catch (error) {
+ // Catch all errors to be sure telemetry does not break the application
+ console.error("Telemetry, unexpected error:", error);
}
}
@@ -61,13 +66,28 @@ async function jobScheduler(): Promise<
}
> {
// Check if job should run, without a lock to not impact performance
- const checkNoLock = await prisma.$queryRaw<Array<{ name: string }>>`
- SELECT name FROM cron_jobs
- WHERE name = 'telemetry'
- AND (last_run IS NULL OR last_run <= (NOW() - INTERVAL '${JOB_INTERVAL_MINUTES} minutes'))
- AND (job_started_at IS NULL OR job_started_at <= (NOW() - INTERVAL '${JOB_TIMEOUT_MINUTES} minutes'))`;
+ // "not exists" triggers when this is run for the very first time in a container
+ const checkNoLock = await prisma.$queryRaw<Array<{ status: boolean }>>`
+ SELECT (
+ EXISTS (
+ SELECT 1
+ FROM cron_jobs
+ WHERE name = 'telemetry'
+ AND (last_run IS NULL OR last_run <= (NOW() - INTERVAL '${JOB_INTERVAL_MINUTES} minute'))
+ AND (job_started_at IS NULL OR job_started_at <= (NOW() - INTERVAL '${JOB_TIMEOUT_MINUTES} minute'))
+ )
+ OR NOT EXISTS (
+ SELECT 1
+ FROM cron_jobs
+ WHERE name = 'telemetry'
+ )
+ ) AS status;`;
// Return if job should not run
- if (checkNoLock.length === 0) return { shouldRunJob: false };
+ if (checkNoLock.length !== 1) {
+ console.error("Telemetry failed to check if job should run");
+ return { shouldRunJob: false };
+ }
+ if (checkNoLock[0]!.status === false) return { shouldRunJob: false };
// Lock table and update job_started_at if no other job was created in the meantime
const res = await prisma.$transaction([