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.
TypeScript ai-agentai-resume-analyzerapplication-trackerapplication-tracking-systematscareerjob-applicationjob-automationjob-huntingjob-searchjob-seeker-toolsjob-trackerjob-trackingjobsearchjobseekersmcpollamaresumeresume-reviewself-hosted
feat(telemetry): add log records and fan automation-logger entries out to OTLP
Khuram Niazf944da055588
diff --git a/__tests__/telemetry/automationLoggerAdapter.spec.ts b/__tests__/telemetry/automationLoggerAdapter.spec.tsnew file mode 100644index 0000000..3245f51--- /dev/null+++ b/__tests__/telemetry/automationLoggerAdapter.spec.ts@@ -0,0 +1,43 @@+import { vi } from "vitest";++vi.mock("@/lib/telemetry", () => ({+ log: { info: vi.fn(), warn: vi.fn(), error: vi.fn() },+}));++import { automationLogger } from "@/lib/automation-logger";+import { log } from "@/lib/telemetry";++describe("automation logger telemetry fan-out", () => {+ beforeEach(() => {+ vi.spyOn(console, "log").mockImplementation(() => {});+ vi.spyOn(console, "warn").mockImplementation(() => {});+ });+ afterEach(() => vi.restoreAllMocks());++ it("fans an entry out to telemetry with the automation id as an attribute", () => {+ automationLogger.startRun("a1");+ automationLogger.log("a1", "info", "Found 12 jobs", { jobBoard: "lever" });++ expect(log.info).toHaveBeenCalledWith("Found 12 jobs", {+ "automation.id": "a1",+ jobBoard: "lever",+ });+ });++ it("maps success to info and warning to warn", () => {+ automationLogger.startRun("a2");+ automationLogger.log("a2", "success", "Saved 3 jobs");+ automationLogger.log("a2", "warning", "Ollama slow");+ automationLogger.log("a2", "error", "Scrape failed");++ expect(log.info).toHaveBeenCalledWith("Saved 3 jobs", { "automation.id": "a2" });+ expect(log.warn).toHaveBeenCalledWith("Ollama slow", { "automation.id": "a2" });+ expect(log.error).toHaveBeenCalledWith("Scrape failed", { "automation.id": "a2" });+ });++ it("still keeps the entry in the in-memory store", () => {+ automationLogger.startRun("a3");+ automationLogger.log("a3", "info", "kept");+ expect(automationLogger.getLogs("a3").map((l) => l.message)).toContain("kept");+ });+});diff --git a/__tests__/telemetry/logger.spec.ts b/__tests__/telemetry/logger.spec.tsnew file mode 100644index 0000000..39579d5--- /dev/null+++ b/__tests__/telemetry/logger.spec.ts@@ -0,0 +1,80 @@+import { loadTelemetry, mockFetchOk } from "./helpers";++const ENDPOINT = "http://homelab:5080/api/default";++describe("telemetry logger", () => {+ afterEach(() => {+ vi.unstubAllEnvs();+ vi.unstubAllGlobals();+ vi.restoreAllMocks();+ });++ it("always writes to console, telemetry off", async () => {+ const info = vi.spyOn(console, "info").mockImplementation(() => {});+ const { logger, otlp } = await loadTelemetry();++ logger.log.info("[Scheduler] tick", { automationId: "a1" });++ expect(info).toHaveBeenCalledWith("[Scheduler] tick", { automationId: "a1" });+ expect(otlp.getTelemetryStats().queuedLogs).toBe(0);+ });++ it("writes to console AND enqueues, telemetry on", async () => {+ mockFetchOk();+ const error = vi.spyOn(console, "error").mockImplementation(() => {});+ const { logger, otlp } = await loadTelemetry({ endpoint: ENDPOINT });++ logger.log.error("scrape failed", { automationId: "a1" });++ expect(error).toHaveBeenCalledWith("scrape failed", { automationId: "a1" });+ expect(otlp.getTelemetryStats().queuedLogs).toBe(1);+ });++ it("inherits trace_id and span_id from the active span", async () => {+ const fetchMock = mockFetchOk();+ vi.useFakeTimers();+ vi.spyOn(console, "info").mockImplementation(() => {});+ const { logger, span, otlp } = await loadTelemetry({ endpoint: ENDPOINT });++ const parent = span.startSpan("automation.run");+ span.runInSpan(parent, () => logger.log.info("inside the run"));+ parent.end();++ await vi.advanceTimersByTimeAsync(5_000);+ const logsCall = fetchMock.mock.calls.find(([url]) =>+ String(url).endsWith("/v1/logs"),+ )!;+ const body = JSON.parse(logsCall[1]!.body as string);+ const record = body.resourceLogs[0].scopeLogs[0].logRecords[0];++ expect(record.traceId).toBe(parent.traceId);+ expect(record.spanId).toBe(parent.spanId);+ expect(otlp.getTelemetryStats().dropped).toBe(0);+ vi.useRealTimers();+ });++ it("maps levels to OTLP severity numbers", async () => {+ mockFetchOk();+ vi.spyOn(console, "info").mockImplementation(() => {});+ vi.spyOn(console, "warn").mockImplementation(() => {});+ vi.spyOn(console, "error").mockImplementation(() => {});+ const { logger, otlp } = await loadTelemetry({ endpoint: ENDPOINT });++ logger.log.info("i");+ logger.log.warn("w");+ logger.log.error("e");++ expect(otlp.getTelemetryStats().queuedLogs).toBe(3);+ expect(logger.SEVERITY).toEqual({ info: 9, warn: 13, error: 17 });+ });++ it("does not throw when the console call itself fails", async () => {+ mockFetchOk();+ vi.spyOn(console, "error").mockImplementation(() => {+ throw new Error("stdout closed");+ });+ const { logger } = await loadTelemetry({ endpoint: ENDPOINT });++ expect(() => logger.log.error("boom")).not.toThrow();+ });+});diff --git a/src/lib/automation-logger.ts b/src/lib/automation-logger.tsindex d255abd..dbe8b33 100644--- a/src/lib/automation-logger.ts+++ b/src/lib/automation-logger.ts@@ -1,5 +1,16 @@+import { log as telemetryLog } from "@/lib/telemetry";+export type LogLevel = "info" | "success" | "warning" | "error";+// The store's four levels collapse onto OTLP's three; success is an+// automation-UI distinction, not a severity.+const TELEMETRY_LEVEL: Record<LogLevel, "info" | "warn" | "error"> = {+ info: "info",+ success: "info",+ warning: "warn",+ error: "error",+};+export interface AutomationLog {timestamp: Date;level: LogLevel;@@ -87,6 +98,13 @@ class AutomationLoggerService {`[Logger] Logged [${level}] for ${automationId}: ${message} (total logs: ${store.logs.length})`,);+ // Fans out to OTLP so an automation run's logs outlive the one-hour+ // in-memory retention and become searchable beside its spans.+ telemetryLog[TELEMETRY_LEVEL[level]](message, {+ "automation.id": automationId,+ ...metadata,+ });+// Keep only the most recent logsif (store.logs.length > this.MAX_LOGS_PER_RUN) {store.logs.shift();diff --git a/src/lib/telemetry/index.ts b/src/lib/telemetry/index.tsindex d514f42..bcc43c9 100644--- a/src/lib/telemetry/index.ts+++ b/src/lib/telemetry/index.ts@@ -6,3 +6,4 @@ export {export { currentSpan, type ActiveSpan } from "./context";export { runInSpan, startSpan, withSpan, type Span } from "./span";export { type Attrs } from "./otlp";+export { log, SEVERITY } from "./logger";diff --git a/src/lib/telemetry/logger.ts b/src/lib/telemetry/logger.tsindex 8bd13c2..939a17e 100644--- a/src/lib/telemetry/logger.ts+++ b/src/lib/telemetry/logger.ts@@ -1,2 +1,53 @@-// Filled in by Phase 2 — log.info / warn / error over console.*.-export {};+import { TELEMETRY_ENABLED } from "./config";+import { currentSpan } from "./context";+import { enqueueLog, type Attrs } from "./otlp";++type Level = "info" | "warn" | "error";++// OTLP severity numbers. INFO=9, WARN=13, ERROR=17.+export const SEVERITY: Record<Level, number> = {+ info: 9,+ warn: 13,+ error: 17,+};++const CONSOLE: Record<Level, (...args: unknown[]) => void> = {+ info: (...args) => console.info(...args),+ warn: (...args) => console.warn(...args),+ error: (...args) => console.error(...args),+};++// console first, always. Debugging a container must never require an+// observability stack to be running — docker compose logs -f is identical+// whether telemetry is on or off.+function emit(level: Level, message: string, attrs?: Attrs): void {+ try {+ if (attrs) CONSOLE[level](message, attrs);+ else CONSOLE[level](message);+ } catch {+ // stdout is not a reason to fail a request either.+ }++ if (!TELEMETRY_ENABLED) return;++ try {+ const active = currentSpan();+ enqueueLog({+ timestamp: Date.now(),+ severityNumber: SEVERITY[level],+ severityText: level.toUpperCase(),+ body: message,+ attrs: attrs ?? {},+ traceId: active?.traceId,+ spanId: active?.spanId,+ });+ } catch {+ // Enqueueing must never throw into a request.+ }+}++export const log = {+ info: (message: string, attrs?: Attrs) => emit("info", message, attrs),+ warn: (message: string, attrs?: Attrs) => emit("warn", message, attrs),+ error: (message: string, attrs?: Attrs) => emit("error", message, attrs),+};