perf(logging): skip eager debug formatting in diagnostic

This commit is contained in:
Peter Steinberger
2026-02-15 03:25:07 +00:00
parent f832f3dcc3
commit 8189949549
2 changed files with 41 additions and 34 deletions

View File

@@ -27,7 +27,7 @@ describe("diagnostic session state pruning", () => {
}); });
it("caps tracked session states to a bounded max", () => { it("caps tracked session states to a bounded max", () => {
for (let i = 0; i < 2105; i += 1) { for (let i = 0; i < 2001; i += 1) {
logSessionStateChange({ sessionId: `session-${i}`, state: "idle" }); logSessionStateChange({ sessionId: `session-${i}`, state: "idle" });
} }

View File

@@ -104,11 +104,13 @@ export function logWebhookReceived(params: {
}) { }) {
webhookStats.received += 1; webhookStats.received += 1;
webhookStats.lastReceived = Date.now(); webhookStats.lastReceived = Date.now();
if (diag.isEnabled("debug")) {
diag.debug( diag.debug(
`webhook received: channel=${params.channel} type=${params.updateType ?? "unknown"} chatId=${ `webhook received: channel=${params.channel} type=${params.updateType ?? "unknown"} chatId=${
params.chatId ?? "unknown" params.chatId ?? "unknown"
} total=${webhookStats.received}`, } total=${webhookStats.received}`,
); );
}
emitDiagnosticEvent({ emitDiagnosticEvent({
type: "webhook.received", type: "webhook.received",
channel: params.channel, channel: params.channel,
@@ -125,6 +127,7 @@ export function logWebhookProcessed(params: {
durationMs?: number; durationMs?: number;
}) { }) {
webhookStats.processed += 1; webhookStats.processed += 1;
if (diag.isEnabled("debug")) {
diag.debug( diag.debug(
`webhook processed: channel=${params.channel} type=${ `webhook processed: channel=${params.channel} type=${
params.updateType ?? "unknown" params.updateType ?? "unknown"
@@ -132,6 +135,7 @@ export function logWebhookProcessed(params: {
webhookStats.processed webhookStats.processed
}`, }`,
); );
}
emitDiagnosticEvent({ emitDiagnosticEvent({
type: "webhook.processed", type: "webhook.processed",
channel: params.channel, channel: params.channel,
@@ -173,11 +177,13 @@ export function logMessageQueued(params: {
const state = getSessionState(params); const state = getSessionState(params);
state.queueDepth += 1; state.queueDepth += 1;
state.lastActivity = Date.now(); state.lastActivity = Date.now();
if (diag.isEnabled("debug")) {
diag.debug( diag.debug(
`message queued: sessionId=${state.sessionId ?? "unknown"} sessionKey=${ `message queued: sessionId=${state.sessionId ?? "unknown"} sessionKey=${
state.sessionKey ?? "unknown" state.sessionKey ?? "unknown"
} source=${params.source} queueDepth=${state.queueDepth} sessionState=${state.state}`, } source=${params.source} queueDepth=${state.queueDepth} sessionState=${state.state}`,
); );
}
emitDiagnosticEvent({ emitDiagnosticEvent({
type: "message.queued", type: "message.queued",
sessionId: state.sessionId, sessionId: state.sessionId,
@@ -200,6 +206,8 @@ export function logMessageProcessed(params: {
reason?: string; reason?: string;
error?: string; error?: string;
}) { }) {
const wantsLog = params.outcome === "error" ? diag.isEnabled("error") : diag.isEnabled("debug");
if (wantsLog) {
const payload = `message processed: channel=${params.channel} chatId=${ const payload = `message processed: channel=${params.channel} chatId=${
params.chatId ?? "unknown" params.chatId ?? "unknown"
} messageId=${params.messageId ?? "unknown"} sessionId=${ } messageId=${params.messageId ?? "unknown"} sessionId=${
@@ -211,11 +219,10 @@ export function logMessageProcessed(params: {
}`; }`;
if (params.outcome === "error") { if (params.outcome === "error") {
diag.error(payload); diag.error(payload);
} else if (params.outcome === "skipped") {
diag.debug(payload);
} else { } else {
diag.debug(payload); diag.debug(payload);
} }
}
emitDiagnosticEvent({ emitDiagnosticEvent({
type: "message.processed", type: "message.processed",
channel: params.channel, channel: params.channel,
@@ -245,7 +252,7 @@ export function logSessionStateChange(
if (params.state === "idle") { if (params.state === "idle") {
state.queueDepth = Math.max(0, state.queueDepth - 1); state.queueDepth = Math.max(0, state.queueDepth - 1);
} }
if (!isProbeSession) { if (!isProbeSession && diag.isEnabled("debug")) {
diag.debug( diag.debug(
`session state: sessionId=${state.sessionId ?? "unknown"} sessionKey=${ `session state: sessionId=${state.sessionId ?? "unknown"} sessionKey=${
state.sessionKey ?? "unknown" state.sessionKey ?? "unknown"