Add a Diagnostic Log, ported and generalized from Sloth Manager
Sloth Manager tracked every API call made to DNS providers for connectivity troubleshooting. Port that here, generalized to cover every outbound integration this app makes, not just DNS -- Tailscale, Proxmox, Synology, Semaphore, Gitea, and Dockhand calls now show up too, since a broken API token or unreachable host on any of them is just as worth diagnosing. New services/diagLog.ts: a generic withDiagLogging(source, adapter) wraps every async method of any adapter object with timing + success/failure recording, without touching a single adapter's request/error-handling internals -- every DNS and integration adapter interface here is already just a flat set of async methods, so this one wrapper works for all twelve of them. Applied it at each adapter factory's own return statement (one line each) rather than at the route layer, so background jobs that construct adapters directly (the Tailscale key-expiry scheduler, IPAM sync, agent-driven Proxmox lookups) get logged too, not just requests through routes/integrations.ts. New diag_log table (ring-buffered to the last 500 rows, mirroring Sloth Manager's approach -- this is for live troubleshooting, not a durable record) and admin-only GET/DELETE /api/diag-log routes, source/ result filters, pagination. New admin-only Diagnostic Log page: filterable, paginated table with a Clear button. Also introduces the shared useSortable hook + SortableTh component used here for the first time -- a follow-up commit applies the same sorting (and CSV export) to the rest of the app's tables, per the same request. Verified end-to-end against a temp SQLite DB with real migrations: a fake wrapped adapter's successful and failing calls both land correctly in the log with the right source/operation/latency/error, and the source/ok filters and clear-log operation all behave correctly. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
1 parent
42f073df81
commit
e35da87886
26 files changed
+1592
-12
No files matched your search
@@ -0,0 +1,87 @@
|
||||
import { and, desc, eq, lt, sql } from "drizzle-orm";
|
||||
import { db } from "../db/client.js";
|
||||
import { diagLog } from "../db/schema.js";
|
||||
|
||||
const MAX_ENTRIES = 500;
|
||||
|
||||
/** Records one outbound-call result. Never throws — a logging failure must not break the call it's logging. */
|
||||
async function recordDiagEntry(entry: { source: string; operation: string; ok: boolean; latencyMs: number; error: string | null }) {
|
||||
try {
|
||||
await db.insert(diagLog).values(entry);
|
||||
// Trim to the most recent MAX_ENTRIES rows (a simple ring buffer, mirroring
|
||||
// Sloth Manager's diagnostic log — this table is for live troubleshooting,
|
||||
// not a durable audit trail, so unbounded growth isn't worth guarding here).
|
||||
const [cutoff] = await db.select({ id: diagLog.id }).from(diagLog).orderBy(desc(diagLog.id)).limit(1).offset(MAX_ENTRIES);
|
||||
if (cutoff) {
|
||||
await db.delete(diagLog).where(lt(diagLog.id, cutoff.id));
|
||||
}
|
||||
} catch (err) {
|
||||
console.error("[diagLog] failed to record entry:", err);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Wraps every method of an adapter (DNS provider or integration) so each call
|
||||
* is timed and recorded to the diagnostic log, success or failure, without
|
||||
* touching the adapter's own request/error-handling logic. Safe for any
|
||||
* adapter whose interface is entirely async methods (true for every DNS and
|
||||
* integration adapter in this codebase).
|
||||
*/
|
||||
export function withDiagLogging<T extends object>(source: string, adapter: T): T {
|
||||
const wrapped = {} as T;
|
||||
for (const key of Object.keys(adapter) as (keyof T)[]) {
|
||||
const value = adapter[key];
|
||||
if (typeof value !== "function") {
|
||||
wrapped[key] = value;
|
||||
continue;
|
||||
}
|
||||
const original = value as (...args: unknown[]) => Promise<unknown>;
|
||||
wrapped[key] = (async (...args: unknown[]) => {
|
||||
const start = Date.now();
|
||||
try {
|
||||
const result = await original.apply(adapter, args);
|
||||
recordDiagEntry({ source, operation: String(key), ok: true, latencyMs: Date.now() - start, error: null });
|
||||
return result;
|
||||
} catch (err) {
|
||||
recordDiagEntry({
|
||||
source,
|
||||
operation: String(key),
|
||||
ok: false,
|
||||
latencyMs: Date.now() - start,
|
||||
error: err instanceof Error ? err.message : String(err),
|
||||
});
|
||||
throw err;
|
||||
}
|
||||
}) as T[keyof T];
|
||||
}
|
||||
return wrapped;
|
||||
}
|
||||
|
||||
export interface DiagLogQuery {
|
||||
source?: string;
|
||||
ok?: boolean;
|
||||
limit?: number;
|
||||
offset?: number;
|
||||
}
|
||||
|
||||
export async function getDiagEntries({ source, ok, limit = 100, offset = 0 }: DiagLogQuery) {
|
||||
const conditions = [];
|
||||
if (source) conditions.push(eq(diagLog.source, source));
|
||||
if (ok !== undefined) conditions.push(eq(diagLog.ok, ok));
|
||||
const where = conditions.length > 0 ? and(...conditions) : undefined;
|
||||
|
||||
const [{ total }] = await db.select({ total: sql<number>`count(*)` }).from(diagLog).where(where);
|
||||
const entries = await db
|
||||
.select()
|
||||
.from(diagLog)
|
||||
.where(where)
|
||||
.orderBy(desc(diagLog.id))
|
||||
.limit(Math.min(limit, 200))
|
||||
.offset(offset);
|
||||
|
||||
return { total, entries };
|
||||
}
|
||||
|
||||
export async function clearDiagLog(): Promise<void> {
|
||||
await db.delete(diagLog);
|
||||
}
|
||||
Reference in new issue
Block a user