Skip to content

Job application tracker and AI-powered job search assistant. Helps job seekers manage their search journey with AI resume review, job matching, task logging, and application analytics, all while keeping your data private.

feat(telemetry): trace automation runs end to end and move server logs to log.*

Khuram Niaz1f8ada2b6965
diff --git a/src/app/api/ai/deepseek/models/route.ts b/src/app/api/ai/deepseek/models/route.ts
index a7f8cac..bd90764 100644
--- a/src/app/api/ai/deepseek/models/route.ts
+++ b/src/app/api/ai/deepseek/models/route.ts
@@ -1,6 +1,7 @@
import { auth } from "@/auth";
import { NextResponse } from "next/server";
import { resolveApiKey } from "@/lib/api-key-resolver";
+import { log } from "@/lib/telemetry";
export async function GET() {
try {
@@ -32,7 +33,10 @@ export async function GET() {
const data = await response.json();
return NextResponse.json(data);
} catch (error) {
- console.error("Error fetching DeepSeek models:", error);
+ log.error("Error fetching models", {
+ provider: "deepseek",
+ error: String(error),
+ });
return NextResponse.json(
{ error: "Failed to fetch DeepSeek models" },
{ status: 500 }
diff --git a/src/app/api/ai/gemini/models/route.ts b/src/app/api/ai/gemini/models/route.ts
index d3a4697..2a38460 100644
--- a/src/app/api/ai/gemini/models/route.ts
+++ b/src/app/api/ai/gemini/models/route.ts
@@ -1,6 +1,7 @@
import { auth } from "@/auth";
import { NextResponse } from "next/server";
import { resolveApiKey } from "@/lib/api-key-resolver";
+import { log } from "@/lib/telemetry";
export async function GET() {
try {
@@ -30,7 +31,10 @@ export async function GET() {
const data = await response.json();
return NextResponse.json(data);
} catch (error) {
- console.error("Error fetching Gemini models:", error);
+ log.error("Error fetching models", {
+ provider: "gemini",
+ error: String(error),
+ });
return NextResponse.json(
{ error: "Failed to fetch Gemini models" },
{ status: 500 }
diff --git a/src/app/api/ai/ollama/generate/route.ts b/src/app/api/ai/ollama/generate/route.ts
index 82642c4..9a3fb33 100644
--- a/src/app/api/ai/ollama/generate/route.ts
+++ b/src/app/api/ai/ollama/generate/route.ts
@@ -2,6 +2,7 @@ import { auth } from "@/auth";
import { getOllamaBaseUrl } from "@/actions/apiKey.actions";
import { NextResponse } from "next/server";
import { APP_CONSTANTS } from "@/lib/constants";
+import { log } from "@/lib/telemetry";
export async function POST(request: Request) {
try {
@@ -29,7 +30,10 @@ export async function POST(request: Request) {
const data = await response.json();
return NextResponse.json(data);
} catch (error) {
- console.error("Error proxying Ollama generate:", error);
+ log.error("Error proxying Ollama generate", {
+ provider: "ollama",
+ error: String(error),
+ });
return NextResponse.json(
{ error: "Cannot connect to Ollama service" },
{ status: 502 },
diff --git a/src/app/api/ai/ollama/ps/route.ts b/src/app/api/ai/ollama/ps/route.ts
index e0bc8af..c288087 100644
--- a/src/app/api/ai/ollama/ps/route.ts
+++ b/src/app/api/ai/ollama/ps/route.ts
@@ -2,6 +2,7 @@ import { auth } from "@/auth";
import { getOllamaBaseUrl } from "@/actions/apiKey.actions";
import { NextResponse } from "next/server";
import { APP_CONSTANTS } from "@/lib/constants";
+import { log } from "@/lib/telemetry";
export async function GET() {
try {
@@ -25,7 +26,10 @@ export async function GET() {
const data = await response.json();
return NextResponse.json(data);
} catch (error) {
- console.error("Error proxying Ollama ps:", error);
+ log.error("Error proxying Ollama ps", {
+ provider: "ollama",
+ error: String(error),
+ });
return NextResponse.json(
{ error: "Cannot connect to Ollama service" },
{ status: 502 },
diff --git a/src/app/api/ai/ollama/tags/route.ts b/src/app/api/ai/ollama/tags/route.ts
index ea67965..58f1fb0 100644
--- a/src/app/api/ai/ollama/tags/route.ts
+++ b/src/app/api/ai/ollama/tags/route.ts
@@ -2,6 +2,7 @@ import { auth } from "@/auth";
import { getOllamaBaseUrl } from "@/actions/apiKey.actions";
import { NextResponse } from "next/server";
import { APP_CONSTANTS } from "@/lib/constants";
+import { log } from "@/lib/telemetry";
export async function GET() {
try {
@@ -25,7 +26,10 @@ export async function GET() {
const data = await response.json();
return NextResponse.json(data);
} catch (error) {
- console.warn("Ollama tags unreachable:", error);
+ log.warn("Ollama tags unreachable", {
+ provider: "ollama",
+ error: String(error),
+ });
return NextResponse.json(
{ error: "Cannot connect to Ollama service" },
{ status: 502 },
diff --git a/src/app/api/ai/openai/models/route.ts b/src/app/api/ai/openai/models/route.ts
index 5d79f1c..f341dd8 100644
--- a/src/app/api/ai/openai/models/route.ts
+++ b/src/app/api/ai/openai/models/route.ts
@@ -1,6 +1,7 @@
import { auth } from "@/auth";
import { NextResponse } from "next/server";
import { resolveApiKey } from "@/lib/api-key-resolver";
+import { log } from "@/lib/telemetry";
export async function GET() {
try {
@@ -32,7 +33,10 @@ export async function GET() {
const data = await response.json();
return NextResponse.json(data);
} catch (error) {
- console.error("Error fetching OpenAI models:", error);
+ log.error("Error fetching models", {
+ provider: "openai",
+ error: String(error),
+ });
return NextResponse.json(
{ error: "Failed to fetch OpenAI models" },
{ status: 500 }
diff --git a/src/app/api/ai/openrouter/models/route.ts b/src/app/api/ai/openrouter/models/route.ts
index 3440c17..8554697 100644
--- a/src/app/api/ai/openrouter/models/route.ts
+++ b/src/app/api/ai/openrouter/models/route.ts
@@ -1,6 +1,7 @@
import { auth } from "@/auth";
import { NextResponse } from "next/server";
import { resolveApiKey } from "@/lib/api-key-resolver";
+import { log } from "@/lib/telemetry";
export async function GET() {
try {
@@ -32,7 +33,10 @@ export async function GET() {
const data = await response.json();
return NextResponse.json(data);
} catch (error) {
- console.error("Error fetching OpenRouter models:", error);
+ log.error("Error fetching models", {
+ provider: "openrouter",
+ error: String(error),
+ });
return NextResponse.json(
{ error: "Failed to fetch OpenRouter models" },
{ status: 500 }
diff --git a/src/app/api/ai/resume/import/route.ts b/src/app/api/ai/resume/import/route.ts
index 5251251..f13afa0 100644
--- a/src/app/api/ai/resume/import/route.ts
+++ b/src/app/api/ai/resume/import/route.ts
@@ -22,6 +22,7 @@ import {
genAiRequestAttrs,
genAiResponseAttrs,
inputSizeAttrs,
+ log,
runInSpan,
startSpan,
SURFACES,
@@ -182,7 +183,7 @@ export const POST = async (req: NextRequest) => {
},
onError: ({ error }) => {
clearTimeout(timer);
- console.error("Resume import stream error:", error);
+ log.error("Resume import stream error", { error: String(error) });
streamErrorMessage = error instanceof Error ? error.message : undefined;
importSpan.setError(error);
importSpan.end();
@@ -205,7 +206,9 @@ export const POST = async (req: NextRequest) => {
} catch (err) {
// Abort/network errors: close cleanly so the client salvages the
// last complete snapshot. onError already logged it.
- console.error("Resume import stream interrupted:", err);
+ log.error("Resume import stream interrupted", {
+ error: String(err),
+ });
} finally {
// The 200 and headers are committed before the provider is called,
// so a failure can only reach the client in-band as a final line.
diff --git a/src/app/api/automations/[id]/run/route.ts b/src/app/api/automations/[id]/run/route.ts
index b63b4ec..3dbe2d4 100644
--- a/src/app/api/automations/[id]/run/route.ts
+++ b/src/app/api/automations/[id]/run/route.ts
@@ -13,6 +13,7 @@ import { getOllamaBaseUrl } from "@/actions/apiKey.actions";
import { AiProvider } from "@/models/ai.model";
import type { JobBoard } from "@/models/automation.model";
import { APP_CONSTANTS } from "@/lib/constants";
+import { log } from "@/lib/telemetry";
const recentRuns = new Map<string, number[]>();
@@ -132,15 +133,22 @@ export async function POST(
updatedAt: automation.updatedAt,
}).catch((err) => {
if (err instanceof AutomationAlreadyRunningError) {
- console.log(`Skipping manual run for ${automation.id} - run already in progress`);
+ log.info("Skipping manual run - run already in progress", {
+ "automation.id": automation.id,
+ "automation.name": automation.name,
+ });
return;
}
- console.error("Background automation run failed:", err);
+ log.error("Background automation run failed", {
+ "automation.id": automation.id,
+ "automation.name": automation.name,
+ error: String(err),
+ });
});
return NextResponse.json({ success: true, started: true });
} catch (error) {
- console.error("Manual run error:", error);
+ log.error("Manual run error", { error: String(error) });
const message = error instanceof Error ? error.message : "Run failed";
return NextResponse.json({ success: false, message }, { status: 500 });
}
diff --git a/src/app/api/backup/export/route.ts b/src/app/api/backup/export/route.ts
index dc77f28..c242a46 100644
--- a/src/app/api/backup/export/route.ts
+++ b/src/app/api/backup/export/route.ts
@@ -1,6 +1,7 @@
import { auth } from "@/auth";
import { NextResponse } from "next/server";
import { buildBackupZip } from "@/lib/backup/export";
+import { log } from "@/lib/telemetry";
// A route handler rather than a server action: server actions cannot return a
// binary stream. Covered by the /api/* middleware matcher; auth() here is
@@ -24,7 +25,7 @@ export async function GET() {
},
});
} catch (error) {
- console.error("[Backup] Export failed:", error);
+ log.error("[Backup] Export failed", { error: String(error) });
return NextResponse.json({ error: "Export failed" }, { status: 500 });
}
}
diff --git a/src/app/api/backup/import/preflight/route.ts b/src/app/api/backup/import/preflight/route.ts
index d9538ce..a0a2d8a 100644
--- a/src/app/api/backup/import/preflight/route.ts
+++ b/src/app/api/backup/import/preflight/route.ts
@@ -3,6 +3,7 @@ import { NextRequest, NextResponse } from "next/server";
import { APP_CONSTANTS } from "@/lib/constants";
import { BackupError, preflightBackup } from "@/lib/backup";
import { isOverUploadCap } from "@/lib/backup/upload";
+import { log } from "@/lib/telemetry";
export async function POST(req: NextRequest) {
const session = await auth();
@@ -41,7 +42,7 @@ export async function POST(req: NextRequest) {
if (error instanceof BackupError) {
return NextResponse.json({ error: error.userMessage }, { status: 400 });
}
- console.error("[Backup] Preflight failed:", error);
+ log.error("[Backup] Preflight failed", { error: String(error) });
return NextResponse.json({ error: "Could not read that backup." }, { status: 500 });
}
}
diff --git a/src/app/api/backup/import/route.ts b/src/app/api/backup/import/route.ts
index b7c6418..88b2b14 100644
--- a/src/app/api/backup/import/route.ts
+++ b/src/app/api/backup/import/route.ts
@@ -3,6 +3,7 @@ import { NextRequest, NextResponse } from "next/server";
import { APP_CONSTANTS } from "@/lib/constants";
import { BackupError, importBackup } from "@/lib/backup";
import { isOverUploadCap } from "@/lib/backup/upload";
+import { log } from "@/lib/telemetry";
// The app's first destructive route handler. Route handlers get none of the
// Origin/Host checking Next.js applies to Server Actions; what stands in for it
@@ -49,7 +50,7 @@ export async function POST(req: NextRequest) {
if (error instanceof BackupError) {
return NextResponse.json({ error: error.userMessage }, { status: 400 });
}
- console.error("[Backup] Import failed:", error);
+ log.error("[Backup] Import failed", { error: String(error) });
return NextResponse.json({ error: "Import failed." }, { status: 500 });
}
}
diff --git a/src/app/api/backup/rollback/route.ts b/src/app/api/backup/rollback/route.ts
index ae91472..e281f2c 100644
--- a/src/app/api/backup/rollback/route.ts
+++ b/src/app/api/backup/rollback/route.ts
@@ -1,6 +1,7 @@
import { auth } from "@/auth";
import { NextRequest, NextResponse } from "next/server";
import { BackupError, importBackup, readSnapshot } from "@/lib/backup";
+import { log } from "@/lib/telemetry";
// A rollback is an import whose bytes come off local disk instead of an
// upload, so it inherits every guard importBackup already has — including
@@ -31,7 +32,7 @@ export async function POST(req: NextRequest) {
if (error instanceof BackupError) {
return NextResponse.json({ error: error.userMessage }, { status: 400 });
}
- console.error("[Backup] Rollback failed:", error);
+ log.error("[Backup] Rollback failed", { error: String(error) });
return NextResponse.json({ error: "Rollback failed." }, { status: 500 });
}
}
diff --git a/src/app/api/backup/snapshots/route.ts b/src/app/api/backup/snapshots/route.ts
index ade8c35..42bf2aa 100644
--- a/src/app/api/backup/snapshots/route.ts
+++ b/src/app/api/backup/snapshots/route.ts
@@ -1,6 +1,7 @@
import { auth } from "@/auth";
import { NextResponse } from "next/server";
import { listSnapshots } from "@/lib/backup";
+import { log } from "@/lib/telemetry";
// The user id comes from the session and is the only thing that selects the
// directory — there is no client-supplied path anywhere in this handler.
@@ -13,7 +14,7 @@ export async function GET() {
try {
return NextResponse.json(await listSnapshots(session.user.id), { status: 200 });
} catch (error) {
- console.error("[Backup] Listing snapshots failed:", error);
+ log.error("[Backup] Listing snapshots failed", { error: String(error) });
return NextResponse.json({ error: "Could not list snapshots." }, { status: 500 });
}
}
diff --git a/src/app/api/jobs/export/route.ts b/src/app/api/jobs/export/route.ts
index a691b60..c1a6f62 100644
--- a/src/app/api/jobs/export/route.ts
+++ b/src/app/api/jobs/export/route.ts
@@ -5,6 +5,7 @@ import { getJobsIterator } from "@/actions/job.actions";
import { format } from "date-fns";
import { auth } from "@/auth";
import { getWorkplaceTypeLabel } from "@/models/job.model";
+import { log } from "@/lib/telemetry";
const FIELDS: string[] = [
"index",
@@ -90,7 +91,7 @@ export const POST = async () => {
}
} catch (error) {
hasError = true;
- console.error("Error streaming CSV:", error);
+ log.error("Error streaming jobs CSV", { error: String(error) });
if (error instanceof Error) {
return NextResponse.json(
{
diff --git a/src/app/api/profile/resume/route.ts b/src/app/api/profile/resume/route.ts
index 55cb077..3694e83 100644
--- a/src/app/api/profile/resume/route.ts
+++ b/src/app/api/profile/resume/route.ts
@@ -11,6 +11,7 @@ import fs from "fs";
import { getTimestampedFileName } from "@/lib/utils";
import { APP_CONSTANTS } from "@/lib/constants";
import { PDF_MAGIC, ZIP_MAGIC } from "@/lib/ai/import/extract-text";
+import { log } from "@/lib/telemetry";
const ALLOWED_MIME = new Set<string>(APP_CONSTANTS.RESUME_ALLOWED_MIME_TYPES);
@@ -87,7 +88,7 @@ export const POST = async (req: NextRequest) => {
);
return NextResponse.json(response, { status: 201 });
} catch (error) {
- console.error(error);
+ log.error("[Resume] Upload failed", { error: String(error) });
if (error instanceof Error) {
return NextResponse.json(
{
@@ -162,7 +163,7 @@ export const GET = async (req: NextRequest) => {
return response;
} catch (error) {
- console.error(error);
+ log.error("[Resume] File download failed", { error: String(error) });
if (error instanceof Error) {
return NextResponse.json(
{
diff --git a/src/app/api/version/route.ts b/src/app/api/version/route.ts
index 141d20c..bba92fc 100644
--- a/src/app/api/version/route.ts
+++ b/src/app/api/version/route.ts
@@ -4,6 +4,7 @@ import { APP_CONSTANTS } from "@/lib/constants";
import { isNewerVersion } from "@/lib/version";
import { AppVersionInfo } from "@/models/version.model";
import packageJson from "../../../../package.json";
+import { log } from "@/lib/telemetry";
const RELEASE_URL = `https://api.github.com/repos/${APP_CONSTANTS.GITHUB_REPO}/releases/latest`;
@@ -34,7 +35,7 @@ export async function GET() {
try {
release = await fetchLatestRelease();
} catch (error) {
- console.error("[Version] Update check failed:", error);
+ log.error("[Version] Update check failed", { error: String(error) });
}
const updateAvailable = release
diff --git a/src/lib/backup/import.ts b/src/lib/backup/import.ts
index f2e99dc..7bea5db 100644
--- a/src/lib/backup/import.ts
+++ b/src/lib/backup/import.ts
@@ -21,6 +21,7 @@ import {
type BackupModel,
} from "./ordering";
import { writeSnapshot } from "./snapshot";
+import { log } from "@/lib/telemetry";
import { BackupDataSchema, type BackupData, type BackupManifest } from "./schema";
export interface PreflightResult {
@@ -282,7 +283,10 @@ export async function importBackup(
}
if (rejectedFiles.length > 0) {
- console.warn("[Backup] Dropped file entries that are not resumes:", rejectedFiles);
+ log.warn("[Backup] Dropped file entries that are not resumes", {
+ "backup.rejected_file_count": rejectedFiles.length,
+ "backup.rejected_file_names": rejectedFiles.join(", "),
+ });
}
// Captured before the wipe so the old bytes can be unlinked after it commits.
@@ -371,7 +375,7 @@ export async function importBackup(
if (/database is locked|SQLITE_BUSY/i.test(message)) {
throw new BackupError("The database is busy. Please try the import again.");
}
- console.error("[Backup] Import transaction failed:", error);
+ log.error("[Backup] Import transaction failed", { error: String(error) });
throw new BackupError("Import failed and nothing was changed.");
}
@@ -380,7 +384,10 @@ export async function importBackup(
for (const filePath of oldFilePaths) {
if (writtenPaths.includes(filePath)) continue;
await fs.unlink(filePath).catch((error) => {
- console.warn("[Backup] Could not remove replaced file", filePath, error);
+ log.warn("[Backup] Could not remove replaced file", {
+ "file.path": filePath,
+ error: String(error),
+ });
});
}
diff --git a/src/lib/backup/snapshot.ts b/src/lib/backup/snapshot.ts
index d815fdf..32092b1 100644
--- a/src/lib/backup/snapshot.ts
+++ b/src/lib/backup/snapshot.ts
@@ -3,6 +3,7 @@ import path from "path";
import { APP_CONSTANTS } from "@/lib/constants";
import { buildBackupZip } from "./export";
import { BackupError, openBackupZip, readManifest } from "./manifest";
+import { log } from "@/lib/telemetry";
export interface SnapshotInfo {
id: string;
@@ -60,7 +61,10 @@ export async function listSnapshots(userId: string): Promise<SnapshotInfo[]> {
} catch (error) {
// An unreadable snapshot is skipped, not fatal — the list is a recovery
// surface and must not be taken down by one bad file.
- console.warn("[Backup] Skipping unreadable snapshot", name, error);
+ log.warn("[Backup] Skipping unreadable snapshot", {
+ "snapshot.name": name,
+ error: String(error),
+ });
}
}
@@ -122,6 +126,11 @@ export async function pruneSnapshots(
for (const name of stale) {
await fs
.unlink(path.join(dir, name))
- .catch((error) => console.warn("[Backup] Could not prune", name, error));
+ .catch((error) =>
+ log.warn("[Backup] Could not prune snapshot", {
+ "snapshot.name": name,
+ error: String(error),
+ }),
+ );
}
}
diff --git a/src/lib/scheduler/index.ts b/src/lib/scheduler/index.ts
index 5edc5fc..c4782f1 100644
--- a/src/lib/scheduler/index.ts
+++ b/src/lib/scheduler/index.ts
@@ -3,12 +3,15 @@ import { SCHEDULER_CONSTANTS } from "@/lib/constants";
import db from "@/lib/db";
import { runAutomation, AutomationAlreadyRunningError } from "@/lib/scraper";
import type { JobBoard } from "@/models/automation.model";
+import { log } from "@/lib/telemetry";
let scheduledTask: ScheduledTask | null = null;
async function runDueAutomations() {
const now = new Date();
- console.log(`[Scheduler] Checking for due automations at ${now.toISOString()}`);
+ log.info("[Scheduler] Checking for due automations", {
+ "scheduler.checked_at": now.toISOString(),
+ });
try {
const dueAutomations = await db.automation.findMany({
@@ -22,15 +25,20 @@ async function runDueAutomations() {
});
if (dueAutomations.length === 0) {
- console.log("[Scheduler] No automations due to run");
+ log.info("[Scheduler] No automations due to run");
return;
}
- console.log(`[Scheduler] Found ${dueAutomations.length} automation(s) to run`);
+ log.info("[Scheduler] Found automations to run", {
+ "scheduler.due_count": dueAutomations.length,
+ });
for (const automation of dueAutomations) {
if (!automation.resume) {
- console.log(`[Scheduler] Skipping automation ${automation.id} - no resume`);
+ log.info("[Scheduler] Skipping automation - no resume", {
+ "automation.id": automation.id,
+ "automation.name": automation.name,
+ });
await db.automationRun.create({
data: {
automationId: automation.id,
@@ -53,12 +61,18 @@ async function runDueAutomations() {
select: { id: true },
});
if (activeRun) {
- console.log(`[Scheduler] Skipping automation ${automation.id} - run already in progress`);
+ log.info("[Scheduler] Skipping automation - run already in progress", {
+ "automation.id": automation.id,
+ "automation.name": automation.name,
+ });
continue;
}
try {
- console.log(`[Scheduler] Running automation: ${automation.name}`);
+ log.info("[Scheduler] Running automation", {
+ "automation.id": automation.id,
+ "automation.name": automation.name,
+ });
const result = await runAutomation({
id: automation.id,
userId: automation.userId,
@@ -76,53 +90,69 @@ async function runDueAutomations() {
createdAt: automation.createdAt,
updatedAt: automation.updatedAt,
});
- console.log(`[Scheduler] Automation ${automation.name} completed: ${result.status}, saved ${result.jobsSaved} jobs`);
+ log.info("[Scheduler] Automation completed", {
+ "automation.id": automation.id,
+ "automation.name": automation.name,
+ "run.status": result.status,
+ "run.jobs_saved": result.jobsSaved,
+ });
} catch (error) {
if (error instanceof AutomationAlreadyRunningError) {
- console.log(`[Scheduler] Skipping automation ${automation.id} - run already in progress`);
+ log.info("[Scheduler] Skipping automation - run already in progress", {
+ "automation.id": automation.id,
+ "automation.name": automation.name,
+ });
continue;
}
const message = error instanceof Error ? error.message : "Unknown error";
- console.error(`[Scheduler] Automation ${automation.name} failed:`, message);
+ log.error("[Scheduler] Automation failed", {
+ "automation.id": automation.id,
+ "automation.name": automation.name,
+ error: message,
+ });
}
}
} catch (error) {
- console.error("[Scheduler] Error running due automations:", error);
+ log.error("[Scheduler] Error running due automations", {
+ error: String(error),
+ });
}
}
export function startScheduler() {
if (!SCHEDULER_CONSTANTS.ENABLED) {
- console.log("[Scheduler] Disabled via SCHEDULER_CONSTANTS.ENABLED");
+ log.info("[Scheduler] Disabled via SCHEDULER_CONSTANTS.ENABLED");
return;
}
if (scheduledTask) {
- console.log("[Scheduler] Already running");
+ log.info("[Scheduler] Already running");
return;
}
const cronExpression = SCHEDULER_CONSTANTS.CRON_EXPRESSION;
if (!cron.validate(cronExpression)) {
- console.error(`[Scheduler] Invalid cron expression: ${cronExpression}`);
+ log.error("[Scheduler] Invalid cron expression", {
+ "scheduler.cron": cronExpression,
+ });
return;
}
- console.log(`[Scheduler] Starting with schedule: ${cronExpression}`);
+ log.info("[Scheduler] Starting", { "scheduler.cron": cronExpression });
scheduledTask = cron.schedule(cronExpression, runDueAutomations, {
timezone: process.env.TZ || "UTC",
});
- console.log("[Scheduler] Started successfully");
+ log.info("[Scheduler] Started successfully");
}
export function stopScheduler() {
if (scheduledTask) {
scheduledTask.stop();
scheduledTask = null;
- console.log("[Scheduler] Stopped");
+ log.info("[Scheduler] Stopped");
}
}
@@ -144,11 +174,15 @@ export async function reapStaleRuns(): Promise<number> {
},
});
if (result.count > 0) {
- console.log(`[Scheduler] Reaped ${result.count} stale running run(s)`);
+ log.info("[Scheduler] Reaped stale running runs", {
+ "scheduler.reaped_count": result.count,
+ });
}
return result.count;
} catch (error) {
- console.error("[Scheduler] Failed to reap stale runs:", error);
+ log.error("[Scheduler] Failed to reap stale runs", {
+ error: String(error),
+ });
return 0;
}
}
diff --git a/src/lib/scraper/runner.ts b/src/lib/scraper/runner.ts
index edd4ac7..a82aad3 100644
--- a/src/lib/scraper/runner.ts
+++ b/src/lib/scraper/runner.ts
@@ -46,6 +46,7 @@ import {
genAiRequestAttrs,
genAiResponseAttrs,
inputSizeAttrs,
+ log,
SURFACES,
withSpan,
} from "@/lib/telemetry";
@@ -174,9 +175,40 @@ export class AutomationAlreadyRunningError extends Error {
}
}
+// Wraps the work, not the trigger: the 03:00 scheduler and a manual run from
+// the UI are two entry points into identical work, and instrumenting the
+// trigger would export every manual run's scraper.match span parentless.
export async function runAutomation(
automation: Automation,
signal?: AbortSignal,
+): Promise<RunnerResult> {
+ return withSpan(
+ "automation.run",
+ {
+ "jobsync.automation_id": automation.id,
+ "jobsync.automation_name": automation.name,
+ "jobsync.job_board": automation.jobBoard,
+ "jobsync.user_id": automation.userId,
+ },
+ (span) =>
+ runAutomationTraced(automation, signal).then((result) => {
+ span.setAttrs({
+ "jobsync.run.id": result.runId,
+ "jobsync.run.status": result.status,
+ "jobsync.run.jobs_searched": result.jobsSearched,
+ "jobsync.run.jobs_deduplicated": result.jobsDeduplicated,
+ "jobsync.run.jobs_processed": result.jobsProcessed,
+ "jobsync.run.jobs_matched": result.jobsMatched,
+ "jobsync.run.jobs_saved": result.jobsSaved,
+ });
+ return result;
+ }),
+ );
+}
+
+async function runAutomationTraced(
+ automation: Automation,
+ signal?: AbortSignal,
): Promise<RunnerResult> {
// Atomic claim: a partial unique index (AutomationRun_automationId_active_key,
// see migrations/20260710000001_automation_run_single_active) allows at most
@@ -200,9 +232,15 @@ export async function runAutomation(
throw error;
}
- console.log(`[Automation ${automation.id}] Starting automation run`);
+ log.info("[Automation] Starting automation run", {
+ "automation.id": automation.id,
+ "automation.name": automation.name,
+ });
automationLogger.startRun(automation.id);
- console.log(`[Automation ${automation.id}] Created run with ID: ${run.id}`);
+ log.info("[Automation] Created run", {
+ "automation.id": automation.id,
+ "run.id": run.id,
+ });
automationLogger.log(
automation.id,
"info",
@@ -568,7 +606,10 @@ export async function runAutomation(
"error",
`Failed to save job: ${errorMsg}`,
);
- console.error("Failed to save job:", err);
+ log.error("[Automation] Failed to save job", {
+ "automation.id": automation.id,
+ error: errorMsg,
+ });
}
};
@@ -639,7 +680,11 @@ export async function runAutomation(
);
automationLogger.endRun(automation.id);
- console.error("Automation run failed:", error);
+ log.error("[Automation] Run failed", {
+ "automation.id": automation.id,
+ "run.id": run.id,
+ error: message,
+ });
return await finalizeRun(run.id, {
status: "failed",
errorMessage: message,
@@ -945,7 +990,11 @@ async function runAtsRun(
);
if (saved) jobsSaved++;
} catch (err) {
- console.error(`${label} Failed to save listing:`, err);
+ log.error("[ATS] Failed to save listing", {
+ "automation.id": automation.id,
+ provider: provider.label,
+ error: String(err),
+ });
}
}
}
@@ -977,7 +1026,11 @@ async function runAtsRun(
);
if (saved) jobsSaved++;
} catch (err) {
- console.error(`${label} Failed to save listing:`, err);
+ log.error("[ATS] Failed to save listing", {
+ "automation.id": automation.id,
+ provider: provider.label,
+ error: String(err),
+ });
}
};
@@ -1055,7 +1108,11 @@ async function runAtsRun(
);
if (saved) jobsSaved++;
} catch (err) {
- console.error(`${label} Failed to save analyzed job:`, err);
+ log.error("[ATS] Failed to save analyzed job", {
+ "automation.id": automation.id,
+ provider: provider.label,
+ error: String(err),
+ });
}
};
@@ -1111,7 +1168,12 @@ async function runAtsRun(
`${label} Run failed: ${message}`,
);
automationLogger.endRun(automation.id);
- console.error(`${label} Run failed:`, error);
+ log.error("[ATS] Run failed", {
+ "automation.id": automation.id,
+ "run.id": runId,
+ provider: provider.label,
+ error: message,
+ });
return await finalizeRun(runId, {
status: "failed",
errorMessage: message,
@@ -1216,7 +1278,7 @@ ${removeHtmlTags(job.description)}
const message =
error instanceof Error ? error.message : "AI matching failed";
- console.error("AI matching error:", message);
+ log.error("[Automation] AI matching error", { error: message });
if (
message.includes("ECONNREFUSED") ||