From b757ec671aa95ab37992dbfdf4731548db094ddc Mon Sep 17 00:00:00 2001 From: David Santos Date: Wed, 25 Nov 2026 05:10:08 +0000 Subject: [PATCH] feat(hub): populate runtime/host/release/request_id columns, honor environment, expose metadata in recent-errors (BIT-33316) Paperclip-Issue: BIT-33316 --- .../api/error-monitoring/php-error/route.ts | 21 ++++++++ api/src/routes/public/recentErrors.ts | 18 +++++++ .../extractOccurrenceMetadata.test.ts | 27 ++++++++++ ingest-api/src/services/processIngestBatch.ts | 51 ++++++++++++++++++- 4 files changed, 115 insertions(+), 2 deletions(-) create mode 100644 ingest-api/src/services/extractOccurrenceMetadata.test.ts diff --git a/admin-ui/app/api/error-monitoring/php-error/route.ts b/admin-ui/app/api/error-monitoring/php-error/route.ts index e7583fa..1c95917 100644 --- a/admin-ui/app/api/error-monitoring/php-error/route.ts +++ b/admin-ui/app/api/error-monitoring/php-error/route.ts @@ -38,6 +38,15 @@ interface CrmPhpPayload { // Paperclip project UUID — used by error loop to route issues to the correct project board column paperclip_project_id?: unknown; paperclipProjectId?: unknown; + // telemetry metadata (BIT-33316) — canonical keys + legacy aliases + component?: unknown; + release?: unknown; + release_version?: unknown; + runtime_name?: unknown; + runtime_version?: unknown; + host_name?: unknown; + hostname?: unknown; + environment?: unknown; } function safeStr(v: unknown): string { @@ -103,6 +112,18 @@ export async function POST(request: NextRequest) { if (project_name) contextData.project_name = project_name; if (paperclip_project_id) contextData.paperclip_project_id = paperclip_project_id; + const metaPairs: Array<[string, string]> = [ + ["component", safeStr(body.component)], + ["release_version", safeStr(body.release_version || body.release)], + ["runtime_name", safeStr(body.runtime_name)], + ["runtime_version", safeStr(body.runtime_version)], + ["host_name", safeStr(body.host_name || body.hostname)], + ["environment", safeStr(body.environment)], + ]; + for (const [k, v] of metaPairs) { + if (v) contextData[k] = v; + } + // Preserve other top-level diagnostic fields. const file_path = safeStr(body.file_path); const line_number = safeStr(body.line_number); diff --git a/api/src/routes/public/recentErrors.ts b/api/src/routes/public/recentErrors.ts index a59b5c3..dea01f7 100644 --- a/api/src/routes/public/recentErrors.ts +++ b/api/src/routes/public/recentErrors.ts @@ -65,6 +65,15 @@ function extractFileLine(ctx: Record | null): { file_path: stri return { file_path, line_number }; } +function ctxStr(ctx: Record | null, keys: string[]): string { + if (!ctx) return ''; + for (const k of keys) { + const v = ctx[k]; + if (typeof v === 'string' && v.trim() !== '') return v.trim(); + } + return ''; +} + /** Paperclip error loop reads top-level `gitea_repo` on each recent-error row. */ function extractGiteaRepo(ctx: Record | null): string { if (!ctx) { @@ -300,6 +309,15 @@ async function handleRecentErrors(request: FastifyRequest, reply: FastifyReply): if (gitea_repo !== '') { rowOut.gitea_repo = gitea_repo; } + const extra: Record = { + component: ctxStr(ctx, ['component']), + release_version: ctxStr(ctx, ['release_version', 'release', 'app_version']), + runtime_name: ctxStr(ctx, ['runtime_name', 'runtime']), + host_name: ctxStr(ctx, ['host_name', 'hostname', 'host']), + }; + for (const [k, v] of Object.entries(extra)) { + if (v !== '') rowOut[k] = v; + } if (paperclip_project_id !== '') { rowOut.paperclip_project_id = paperclip_project_id; } diff --git a/ingest-api/src/services/extractOccurrenceMetadata.test.ts b/ingest-api/src/services/extractOccurrenceMetadata.test.ts new file mode 100644 index 0000000..679e795 --- /dev/null +++ b/ingest-api/src/services/extractOccurrenceMetadata.test.ts @@ -0,0 +1,27 @@ +import { describe, expect, it } from "vitest"; +import { extractOccurrenceMetadata } from "./processIngestBatch.js"; + +describe("extractOccurrenceMetadata (BIT-33316)", () => { + it("maps canonical keys", () => { + const m = extractOccurrenceMetadata({}, { + runtime_name: "php", runtime_version: "8.3", host_name: "h1", + release_version: "1.2.3", request_id: "r1", environment: "Staging", + }); + expect(m).toEqual({ + environment: "staging", runtime_name: "php", runtime_version: "8.3", + host_name: "h1", release_version: "1.2.3", request_id: "r1", + }); + }); + it("falls back to legacy aliases and production", () => { + const m = extractOccurrenceMetadata({}, { hostname: "h2", app_version: "9" }); + expect(m.host_name).toBe("h2"); + expect(m.release_version).toBe("9"); + expect(m.environment).toBe("production"); + expect(m.runtime_name).toBeNull(); + }); + it("event.environment wins, invalid falls back", () => { + expect(extractOccurrenceMetadata({ environment: "dev" }, { environment: "x" }).environment).toBe("dev"); + expect(extractOccurrenceMetadata({}, { environment: "bad env!" }).environment).toBe("production"); + expect(extractOccurrenceMetadata({}, null).host_name).toBeNull(); + }); +}); diff --git a/ingest-api/src/services/processIngestBatch.ts b/ingest-api/src/services/processIngestBatch.ts index 532dfd6..9176d09 100644 --- a/ingest-api/src/services/processIngestBatch.ts +++ b/ingest-api/src/services/processIngestBatch.ts @@ -232,6 +232,44 @@ export async function processIngestBatch(input: { } } +const _META_ENV_RE = /^[A-Za-z0-9._-]{1,32}$/; + +function _metaStr(ctx: Record | null, keys: string[], max: number): string | null { + if (!ctx) return null; + for (const k of keys) { + const v = ctx[k]; + if (typeof v === "string" && v.trim() !== "") return v.trim().slice(0, max); + } + return null; +} + +/** Canonical keys first, legacy aliases (hostname, app_version, ...) as fallback. BIT-33316. */ +export function extractOccurrenceMetadata( + ev: unknown, + ctx: Record | null, +): { + environment: string; + runtime_name: string | null; + runtime_version: string | null; + host_name: string | null; + release_version: string | null; + request_id: string | null; +} { + const evObj = (ev && typeof ev === "object" ? ev : {}) as Record; + const rawEnv = + (typeof evObj.environment === "string" ? evObj.environment.trim() : "") || + _metaStr(ctx, ["environment", "env"], 32) || + ""; + return { + environment: _META_ENV_RE.test(rawEnv) ? rawEnv.toLowerCase() : "production", + runtime_name: _metaStr(ctx, ["runtime_name", "runtime"], 32), + runtime_version: _metaStr(ctx, ["runtime_version"], 32), + host_name: _metaStr(ctx, ["host_name", "hostname", "host"], 128), + release_version: _metaStr(ctx, ["release_version", "release", "app_version"], 64), + request_id: _metaStr(ctx, ["request_id", "requestId"], 128), + }; +} + async function _ingestOneEvent( client: PoolConnection, input: { ingestKeyId: string; projectId: string; event: IngestEventInput }, @@ -296,11 +334,14 @@ async function _ingestOneEvent( throw new Error("incident_upsert_failed"); } + const _meta = extractOccurrenceMetadata(_ev, _context); + const [occHeader] = await client.execute( `INSERT INTO er_incident_occurrences ( project_id, incident_id, ingest_key_id, occurred_at, level_raw, severity, - message, stack_trace, context_json, schema_version, environment - ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, 'production')`, + message, stack_trace, context_json, schema_version, environment, + runtime_name, runtime_version, host_name, release_version, request_id + ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`, [ input.projectId, _incidentRow.id, @@ -312,6 +353,12 @@ async function _ingestOneEvent( _stack, _context === null ? null : JSON.stringify(_context), _ev.schema_version, + _meta.environment, + _meta.runtime_name, + _meta.runtime_version, + _meta.host_name, + _meta.release_version, + _meta.request_id, ], ); const _occId = occHeader.insertId;