feat(BIT-247): consolidate error reporting repo from OTT
Merge ingest-api, collector-worker, admin-ui, database/error_reporting, deploy/lxc, docker-compose, and deployment docs into the dedicated repository. Paths updated for repo root (ER_REPO_ROOT, systemd WorkingDirectory). Root README documents layout. Co-Authored-By: Paperclip <[email protected]>
This commit is contained in:
1 parent
eeb5039a3c
commit
b0b733d5e1
88 files changed
+19321
No files matched your search
@@ -0,0 +1,189 @@
|
||||
import type { Pool, PoolClient } from "pg";
|
||||
import { stableJsonHash } from "../lib/hash.js";
|
||||
import {
|
||||
computeFingerprint,
|
||||
incidentTitleFromMessage,
|
||||
normalizeLevelToSeverity,
|
||||
parseOccurredAt,
|
||||
} from "../lib/normalize.js";
|
||||
|
||||
export type IngestEventInput = {
|
||||
schema_version: number;
|
||||
occurred_at: string;
|
||||
level: string;
|
||||
message: string;
|
||||
fingerprint?: string;
|
||||
stacktrace?: string;
|
||||
context?: Record<string, unknown>;
|
||||
};
|
||||
|
||||
export type IngestResultItem = {
|
||||
incident_id: string;
|
||||
occurrence_id: string;
|
||||
fingerprint: string;
|
||||
};
|
||||
|
||||
export async function processIngestBatch(input: {
|
||||
pool: Pool;
|
||||
ingestKeyId: string;
|
||||
projectId: string;
|
||||
events: IngestEventInput[];
|
||||
idempotencyKey: string | null;
|
||||
bodyHash: string;
|
||||
}): Promise<{ status: 200 | 409; body: unknown }> {
|
||||
const _client = await input.pool.connect();
|
||||
try {
|
||||
await _client.query("BEGIN ISOLATION LEVEL READ COMMITTED");
|
||||
if (input.idempotencyKey !== null) {
|
||||
const _existing = await _client.query<{
|
||||
body_hash: string;
|
||||
response_json: unknown;
|
||||
http_status: number;
|
||||
}>(
|
||||
`SELECT body_hash, response_json, http_status
|
||||
FROM er_ingest_idempotency
|
||||
WHERE ingest_key_id = $1::bigint
|
||||
AND idempotency_key = $2`,
|
||||
[input.ingestKeyId, input.idempotencyKey],
|
||||
);
|
||||
const _row0 = _existing.rows[0];
|
||||
if (_row0) {
|
||||
if (_row0.body_hash !== input.bodyHash) {
|
||||
await _client.query("ROLLBACK");
|
||||
return {
|
||||
status: 409,
|
||||
body: {
|
||||
error: "idempotency_conflict",
|
||||
message:
|
||||
"The same Idempotency-Key was reused with a different request body. Use a new key or retry the original payload.",
|
||||
},
|
||||
};
|
||||
}
|
||||
await _client.query("COMMIT");
|
||||
return { status: 200, body: _row0.response_json };
|
||||
}
|
||||
}
|
||||
|
||||
const _results: IngestResultItem[] = [];
|
||||
for (const _ev of input.events) {
|
||||
const _item = await _ingestOneEvent(_client, {
|
||||
ingestKeyId: input.ingestKeyId,
|
||||
projectId: input.projectId,
|
||||
event: _ev,
|
||||
});
|
||||
_results.push(_item);
|
||||
}
|
||||
|
||||
const _responseBody = { results: _results };
|
||||
|
||||
if (input.idempotencyKey !== null) {
|
||||
const _firstIncidentId = _results[0]?.incident_id;
|
||||
if (typeof _firstIncidentId === "string") {
|
||||
await _client.query(
|
||||
`INSERT INTO er_ingest_idempotency (
|
||||
ingest_key_id, idempotency_key, body_hash, incident_id, http_status, response_json
|
||||
) VALUES ($1::bigint, $2, $3, $4::bigint, 200, $5::jsonb)`,
|
||||
[
|
||||
input.ingestKeyId,
|
||||
input.idempotencyKey,
|
||||
input.bodyHash,
|
||||
_firstIncidentId,
|
||||
JSON.stringify(_responseBody),
|
||||
],
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
await _client.query(
|
||||
`UPDATE er_ingest_keys SET last_used_at = NOW() WHERE id = $1::bigint`,
|
||||
[input.ingestKeyId],
|
||||
);
|
||||
|
||||
await _client.query("COMMIT");
|
||||
return { status: 200, body: _responseBody };
|
||||
} catch (_err) {
|
||||
await _client.query("ROLLBACK");
|
||||
throw _err;
|
||||
} finally {
|
||||
_client.release();
|
||||
}
|
||||
}
|
||||
|
||||
async function _ingestOneEvent(
|
||||
client: PoolClient,
|
||||
input: { ingestKeyId: string; projectId: string; event: IngestEventInput },
|
||||
): Promise<IngestResultItem> {
|
||||
const _ev = input.event;
|
||||
if (_ev.schema_version !== 1) {
|
||||
throw new Error("unsupported_schema_version");
|
||||
}
|
||||
const _occurredMs = parseOccurredAt(_ev.occurred_at);
|
||||
const _occurred = new Date(_occurredMs);
|
||||
const _levelRaw = _ev.level.trim().slice(0, 32);
|
||||
const _severity = normalizeLevelToSeverity(_ev.level);
|
||||
const _message = _ev.message.trim();
|
||||
const _fingerprint = computeFingerprint({
|
||||
clientFingerprint: _ev.fingerprint,
|
||||
message: _message,
|
||||
stacktrace: _ev.stacktrace,
|
||||
});
|
||||
const _title = incidentTitleFromMessage(_message);
|
||||
const _stack = typeof _ev.stacktrace === "string" ? _ev.stacktrace : null;
|
||||
const _context = _ev.context && typeof _ev.context === "object" ? _ev.context : null;
|
||||
|
||||
const _incident = await client.query<{ id: string }>(
|
||||
`INSERT INTO er_incidents (
|
||||
project_id, fingerprint, title, severity, first_seen_at, last_seen_at,
|
||||
occurrences_count, last_ingest_key_id
|
||||
) VALUES (
|
||||
$1::bigint, $2, $3, $4, $5::timestamp, $5::timestamp, 1, $6::bigint
|
||||
)
|
||||
ON CONFLICT (project_id, fingerprint) DO UPDATE SET
|
||||
last_seen_at = EXCLUDED.last_seen_at,
|
||||
occurrences_count = er_incidents.occurrences_count + 1,
|
||||
last_ingest_key_id = EXCLUDED.last_ingest_key_id,
|
||||
updated_at = CURRENT_TIMESTAMP
|
||||
RETURNING id::text`,
|
||||
[input.projectId, _fingerprint, _title, _severity, _occurred, input.ingestKeyId],
|
||||
);
|
||||
const _incidentRow = _incident.rows[0];
|
||||
if (!_incidentRow) {
|
||||
throw new Error("incident_upsert_failed");
|
||||
}
|
||||
|
||||
const _occ = await client.query<{ id: string }>(
|
||||
`INSERT INTO er_occurrence_events (
|
||||
project_id, incident_id, ingest_key_id, occurred_at, level_raw, severity,
|
||||
message, stacktrace, context_json, schema_version
|
||||
) VALUES (
|
||||
$1::bigint, $2::bigint, $3::bigint, $4::timestamptz, $5, $6, $7, $8, $9::jsonb, $10
|
||||
)
|
||||
RETURNING id::text`,
|
||||
[
|
||||
input.projectId,
|
||||
_incidentRow.id,
|
||||
input.ingestKeyId,
|
||||
_occurred,
|
||||
_levelRaw,
|
||||
_severity,
|
||||
_message,
|
||||
_stack,
|
||||
_context === null ? null : JSON.stringify(_context),
|
||||
_ev.schema_version,
|
||||
],
|
||||
);
|
||||
const _occRow = _occ.rows[0];
|
||||
if (!_occRow) {
|
||||
throw new Error("occurrence_insert_failed");
|
||||
}
|
||||
|
||||
return {
|
||||
incident_id: _incidentRow.id,
|
||||
occurrence_id: _occRow.id,
|
||||
fingerprint: _fingerprint,
|
||||
};
|
||||
}
|
||||
|
||||
export function computeRequestBodyHash(body: unknown): string {
|
||||
return stableJsonHash(body);
|
||||
}
|
||||
Reference in new issue
Block a user