feat(hub): add resolve-fixed-patterns endpoint for CRM routing parity (BIT-25072)
POST /api/error-monitoring/resolve-fixed-patterns — resolves hub incidents matching known-fixed/transient patterns from config/resolveFixedPatterns.ts. Fixes 404 from run-error-loop-with-resolve.sh when CRM_RESOLVE_API_URL points to the hub (CRM module removed in BIT-24199; hub now owns all error monitoring). Patterns file is extensible; initial set covers Galera/WSREP transients, Google API backendError, session_write_close cascades, and storage COMMIT noise. Paperclip-Issue: BIT-25072 Paperclip-Agent: tiago-neves
This commit is contained in:
1 parent
1181093739
commit
7fe045d314
2 files changed
+148
No files matched your search
@@ -0,0 +1,41 @@
|
||||
/**
|
||||
* Known-resolved error patterns for the hub resolve-fixed-patterns endpoint (BIT-25072).
|
||||
* Each entry matches against er_incident_occurrences.message (via LIKE or exact).
|
||||
*
|
||||
* Schema: { messagePatterns: string[], note: string }
|
||||
* messagePatterns: SQL LIKE patterns (use % wildcards). All patterns in an entry are AND-ed.
|
||||
* note: human-readable reason / fix reference (written to er_incident_state_history.change_note).
|
||||
*
|
||||
* Transient infrastructure errors: noise from known-bad infra states that self-resolve.
|
||||
* Fixed code errors: added after the fix is deployed to production.
|
||||
*/
|
||||
export interface FixedPattern {
|
||||
/** Each element becomes: o.message LIKE ? (all AND-ed together for this entry). */
|
||||
messagePatterns: string[];
|
||||
note: string;
|
||||
}
|
||||
|
||||
const patterns: FixedPattern[] = [
|
||||
// Galera/WSREP cluster transient — node not ready
|
||||
{
|
||||
messagePatterns: ['%WSREP has not yet prepared node%'],
|
||||
note: 'Transient: Galera cluster node not ready; infrastructure',
|
||||
},
|
||||
// Google API backendError — transient external
|
||||
{
|
||||
messagePatterns: ['%Internal error encountered%', '%backendError%'],
|
||||
note: 'Transient: Google API 500 backendError; external; no code fix',
|
||||
},
|
||||
// Session write fails when DB is unavailable (cascading)
|
||||
{
|
||||
messagePatterns: ['%session_write_close(): Failed to write session data%'],
|
||||
note: 'Transient: cascades from DB unavailability; no code fix',
|
||||
},
|
||||
// Storage / COMMIT transient
|
||||
{
|
||||
messagePatterns: ['%No such device or address%'],
|
||||
note: 'Transient: storage/infrastructure during COMMIT; retry on next request',
|
||||
},
|
||||
];
|
||||
|
||||
export default patterns;
|
||||
@@ -3,11 +3,13 @@
|
||||
* POST /api/error-monitoring/resolve
|
||||
* POST /api/error-monitoring/bulk-resolve
|
||||
* POST /api/error-monitoring/resolve-by-pattern
|
||||
* POST /api/error-monitoring/resolve-fixed-patterns
|
||||
* POST /api/error-monitoring/purge-resolved
|
||||
*/
|
||||
|
||||
import type { FastifyInstance, FastifyRequest, FastifyReply } from 'fastify';
|
||||
import type { ResultSetHeader, RowDataPacket } from 'mysql2';
|
||||
import fixedPatterns from '../../config/resolveFixedPatterns.js';
|
||||
import {
|
||||
authorizeMutation,
|
||||
changedByRef,
|
||||
@@ -611,10 +613,115 @@ async function handlePurgeResolved(request: FastifyRequest, reply: FastifyReply)
|
||||
});
|
||||
}
|
||||
|
||||
async function handleResolveFixedPatterns(request: FastifyRequest, reply: FastifyReply): Promise<void> {
|
||||
const db = request.server.db;
|
||||
const body = parseJsonBody(request.body);
|
||||
const auth = await authorizeMutation(request, reply, body, 'errors:triage');
|
||||
if (!auth) {
|
||||
return;
|
||||
}
|
||||
|
||||
const dryRun = Boolean(body.dry_run);
|
||||
const { type, ref } = changedByRef(auth);
|
||||
|
||||
const details: Array<{ note: string; resolved?: number; would_resolve?: number }> = [];
|
||||
let total = 0;
|
||||
|
||||
for (const pattern of fixedPatterns) {
|
||||
if (pattern.messagePatterns.length === 0) {
|
||||
continue;
|
||||
}
|
||||
const likeClauses = pattern.messagePatterns.map(() => 'o.message LIKE ?').join(' AND ');
|
||||
const params: string[] = pattern.messagePatterns;
|
||||
|
||||
const [countRows] = await db.execute<RowDataPacket[]>(
|
||||
`SELECT COUNT(DISTINCT i.id) AS c
|
||||
FROM er_incidents i
|
||||
INNER JOIN er_incident_occurrences o ON o.incident_id = i.id
|
||||
WHERE i.current_state IN ('open','investigating')
|
||||
AND (${likeClauses})`,
|
||||
params,
|
||||
);
|
||||
const n = Number((countRows[0] as { c?: number }).c ?? 0);
|
||||
if (n === 0) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (dryRun) {
|
||||
total += n;
|
||||
details.push({ note: pattern.note, would_resolve: n });
|
||||
continue;
|
||||
}
|
||||
|
||||
const [matchRows] = await db.execute<RowDataPacket[]>(
|
||||
`SELECT DISTINCT CAST(i.id AS CHAR) AS incident_id,
|
||||
CAST(i.project_id AS CHAR) AS project_id,
|
||||
i.current_state AS current_state
|
||||
FROM er_incidents i
|
||||
INNER JOIN er_incident_occurrences o ON o.incident_id = i.id
|
||||
WHERE i.current_state IN ('open','investigating')
|
||||
AND (${likeClauses})`,
|
||||
params,
|
||||
);
|
||||
const targets = (matchRows as Record<string, unknown>[]).map((r) => ({
|
||||
incidentId: String(r.incident_id),
|
||||
projectId: String(r.project_id),
|
||||
currentState: String(r.current_state),
|
||||
}));
|
||||
|
||||
let resolved = 0;
|
||||
const conn = await db.getConnection();
|
||||
try {
|
||||
await conn.beginTransaction();
|
||||
for (const row of targets) {
|
||||
const [upd] = await conn.execute<ResultSetHeader>(
|
||||
`UPDATE er_incidents
|
||||
SET current_state = 'resolved', updated_at = CURRENT_TIMESTAMP(6)
|
||||
WHERE id = ? AND current_state IN ('open','investigating')`,
|
||||
[row.incidentId],
|
||||
);
|
||||
if (upd.affectedRows === 0) {
|
||||
continue;
|
||||
}
|
||||
await conn.execute(
|
||||
`INSERT INTO er_incident_state_history
|
||||
(incident_id, project_id, from_state, to_state, changed_by_type, changed_by_ref, change_note)
|
||||
VALUES (?, ?, ?, 'resolved', ?, ?, ?)`,
|
||||
[row.incidentId, row.projectId, row.currentState, type, ref, pattern.note],
|
||||
);
|
||||
resolved += upd.affectedRows;
|
||||
}
|
||||
await conn.commit();
|
||||
} catch (e) {
|
||||
await conn.rollback();
|
||||
throw e;
|
||||
} finally {
|
||||
conn.release();
|
||||
}
|
||||
|
||||
total += resolved;
|
||||
details.push({ note: pattern.note, resolved });
|
||||
}
|
||||
|
||||
if (!dryRun) {
|
||||
await auditMutation(request.server, request, auth, 'error_monitoring.resolve_fixed_patterns', 'batch', {
|
||||
resolved: total,
|
||||
pattern_count: fixedPatterns.length,
|
||||
});
|
||||
}
|
||||
|
||||
return void reply.send({
|
||||
success: true,
|
||||
message: dryRun ? `Would resolve ${total} incidents` : `Resolved ${total} incidents`,
|
||||
data: { total, dry_run: dryRun, details },
|
||||
});
|
||||
}
|
||||
|
||||
export default async function errorMonitoringMutationsRoutes(app: FastifyInstance): Promise<void> {
|
||||
const rl = { config: { rateLimit: app.rateLimits.mutation } };
|
||||
app.post('/api/error-monitoring/resolve', rl, handleResolve);
|
||||
app.post('/api/error-monitoring/bulk-resolve', rl, handleBulkResolve);
|
||||
app.post('/api/error-monitoring/resolve-by-pattern', rl, handleResolveByPattern);
|
||||
app.post('/api/error-monitoring/resolve-fixed-patterns', rl, handleResolveFixedPatterns);
|
||||
app.post('/api/error-monitoring/purge-resolved', rl, handlePurgeResolved);
|
||||
}
|
||||
Reference in new issue
Block a user