feat(error-hub): wire errors.bitreport.pt reporter into process diagnostics
ai-news-writer-ci / test (pull_request) Failing after 20s
ai-news-writer-ci / test (pull_request) Failing after 20s
Adds a minimal fire-and-forget client (POST /v1/ingest/events, Bearer token) and wires it into installProcessDiagnostics() so uncaught exceptions and unhandled rejections also reach the hub, not just the local winston logs. No-op when ERROR_REPORTING_TOKEN is unset. Paperclip-Issue: BIT-31162
This commit is contained in:
1 parent
0587a32529
commit
7093619a0a
4 files changed
+129
No files matched your search
@@ -83,6 +83,12 @@ QUEUE_MAX_DEPTH=5
|
|||||||
# PIPELINE_ALERT_WEBHOOK_URL=
|
# PIPELINE_ALERT_WEBHOOK_URL=
|
||||||
# PIPELINE_ALERT_WEBHOOK_TIMEOUT_MS=4000
|
# PIPELINE_ALERT_WEBHOOK_TIMEOUT_MS=4000
|
||||||
|
|
||||||
|
# Error hub ingest (errors.bitreport.pt) — vault only; leave empty in Git examples
|
||||||
|
# ERROR_REPORTING_ENDPOINT=https://errors.bitreport.pt
|
||||||
|
# ERROR_REPORTING_TOKEN=
|
||||||
|
ERROR_REPORTING_PROJECT_SLUG=ai-news-writer
|
||||||
|
ERROR_REPORTING_GITEA_REPO=BitReport.pt/ai-news-writer
|
||||||
|
|
||||||
# Writing style — optional overrides (defaults come from config/writing-style.example.json)
|
# Writing style — optional overrides (defaults come from config/writing-style.example.json)
|
||||||
# WRITING_STYLE_CONFIG_PATH=
|
# WRITING_STYLE_CONFIG_PATH=
|
||||||
# WRITING_TONE=
|
# WRITING_TONE=
|
||||||
|
|||||||
@@ -0,0 +1,73 @@
|
|||||||
|
'use strict';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Fire-and-forget ingest to errors.bitreport.pt (/v1/ingest/events).
|
||||||
|
* Enabled when ERROR_REPORTING_TOKEN is set.
|
||||||
|
*/
|
||||||
|
|
||||||
|
const PROJECT_NAME = 'ai-news-writer';
|
||||||
|
const DEFAULT_PROJECT_SLUG = 'ai-news-writer';
|
||||||
|
const DEFAULT_GITEA_REPO = 'BitReport.pt/ai-news-writer';
|
||||||
|
const INGEST_PATH = '/v1/ingest/events';
|
||||||
|
const MAX_STACK_LEN = 12000;
|
||||||
|
|
||||||
|
function ingestBase() {
|
||||||
|
return (process.env.ERROR_REPORTING_ENDPOINT || 'https://errors.bitreport.pt').replace(/\/$/, '');
|
||||||
|
}
|
||||||
|
|
||||||
|
function ingestToken() {
|
||||||
|
return (process.env.ERROR_REPORTING_TOKEN || '').trim();
|
||||||
|
}
|
||||||
|
|
||||||
|
function routingMeta() {
|
||||||
|
const slug = (process.env.ERROR_REPORTING_PROJECT_SLUG || DEFAULT_PROJECT_SLUG).trim() || DEFAULT_PROJECT_SLUG;
|
||||||
|
const repo = (process.env.ERROR_REPORTING_GITEA_REPO || DEFAULT_GITEA_REPO).trim() || DEFAULT_GITEA_REPO;
|
||||||
|
return { project_slug: slug, gitea_repo: repo };
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Report an Error (or error-like object) to the hub ingest API. Never throws.
|
||||||
|
* @param {Error} error
|
||||||
|
* @param {{component?: string}} context
|
||||||
|
*/
|
||||||
|
function reportError(error, context = {}) {
|
||||||
|
const token = ingestToken();
|
||||||
|
if (!token) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const stack = (error && error.stack) || '';
|
||||||
|
const stacktrace = stack.length > MAX_STACK_LEN ? `${stack.slice(0, MAX_STACK_LEN)}\n... (truncated)` : stack;
|
||||||
|
const meta = routingMeta();
|
||||||
|
|
||||||
|
const event = {
|
||||||
|
schema_version: 1,
|
||||||
|
occurred_at: new Date().toISOString(),
|
||||||
|
level: 'error',
|
||||||
|
message: (error && error.message) || String(error),
|
||||||
|
stacktrace,
|
||||||
|
context: {
|
||||||
|
project_name: PROJECT_NAME,
|
||||||
|
project_slug: meta.project_slug,
|
||||||
|
gitea_repo: meta.gitea_repo,
|
||||||
|
component: context.component || 'worker',
|
||||||
|
environment: process.env.NODE_ENV || 'production',
|
||||||
|
error_type: (error && error.name) || 'Error',
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
const url = `${ingestBase()}${INGEST_PATH}`;
|
||||||
|
fetch(url, {
|
||||||
|
method: 'POST',
|
||||||
|
headers: {
|
||||||
|
'Content-Type': 'application/json',
|
||||||
|
Authorization: `Bearer ${token}`,
|
||||||
|
'X-Api-Key': token,
|
||||||
|
},
|
||||||
|
body: JSON.stringify({ events: [event] }),
|
||||||
|
}).catch(() => {
|
||||||
|
// Never let reporting failures affect the app.
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = { reportError };
|
||||||
@@ -0,0 +1,46 @@
|
|||||||
|
'use strict';
|
||||||
|
|
||||||
|
const test = require('node:test');
|
||||||
|
const assert = require('node:assert');
|
||||||
|
const { reportError } = require('./errorReporting');
|
||||||
|
|
||||||
|
test('reportError is a no-op without ERROR_REPORTING_TOKEN', async () => {
|
||||||
|
delete process.env.ERROR_REPORTING_TOKEN;
|
||||||
|
const originalFetch = global.fetch;
|
||||||
|
let called = false;
|
||||||
|
global.fetch = async () => {
|
||||||
|
called = true;
|
||||||
|
};
|
||||||
|
try {
|
||||||
|
reportError(new Error('boom'));
|
||||||
|
assert.strictEqual(called, false);
|
||||||
|
} finally {
|
||||||
|
global.fetch = originalFetch;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
test('reportError posts a well-formed event to /v1/ingest/events when token is set', async () => {
|
||||||
|
process.env.ERROR_REPORTING_TOKEN = 'er_test_token';
|
||||||
|
const originalFetch = global.fetch;
|
||||||
|
let capturedUrl;
|
||||||
|
let capturedBody;
|
||||||
|
global.fetch = async (url, opts) => {
|
||||||
|
capturedUrl = url;
|
||||||
|
capturedBody = JSON.parse(opts.body);
|
||||||
|
return { ok: true };
|
||||||
|
};
|
||||||
|
try {
|
||||||
|
reportError(new Error('boom'), { component: 'worker' });
|
||||||
|
// reportError fires a promise; let the microtask run.
|
||||||
|
await new Promise((resolve) => setImmediate(resolve));
|
||||||
|
assert.ok(capturedUrl.endsWith('/v1/ingest/events'));
|
||||||
|
const event = capturedBody.events[0];
|
||||||
|
assert.strictEqual(event.message, 'boom');
|
||||||
|
assert.strictEqual(event.context.project_slug, 'ai-news-writer');
|
||||||
|
assert.strictEqual(event.context.gitea_repo, 'BitReport.pt/ai-news-writer');
|
||||||
|
assert.strictEqual(event.context.component, 'worker');
|
||||||
|
} finally {
|
||||||
|
global.fetch = originalFetch;
|
||||||
|
delete process.env.ERROR_REPORTING_TOKEN;
|
||||||
|
}
|
||||||
|
});
|
||||||
@@ -1,6 +1,7 @@
|
|||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
const logger = require('../logger');
|
const logger = require('../logger');
|
||||||
|
const { reportError } = require('../errorReporting');
|
||||||
|
|
||||||
let _installed = false;
|
let _installed = false;
|
||||||
|
|
||||||
@@ -17,13 +18,16 @@ function installProcessDiagnostics() {
|
|||||||
process.on('unhandledRejection', (reason) => {
|
process.on('unhandledRejection', (reason) => {
|
||||||
if (reason instanceof Error) {
|
if (reason instanceof Error) {
|
||||||
logger.error(`unhandledRejection: ${reason.message}`, { stack: reason.stack });
|
logger.error(`unhandledRejection: ${reason.message}`, { stack: reason.stack });
|
||||||
|
reportError(reason, { component: 'worker' });
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
logger.error(`unhandledRejection: ${String(reason)}`);
|
logger.error(`unhandledRejection: ${String(reason)}`);
|
||||||
|
reportError(new Error(String(reason)), { component: 'worker' });
|
||||||
});
|
});
|
||||||
|
|
||||||
process.on('uncaughtException', (err) => {
|
process.on('uncaughtException', (err) => {
|
||||||
logger.error(`uncaughtException: ${err.message}`, { stack: err.stack });
|
logger.error(`uncaughtException: ${err.message}`, { stack: err.stack });
|
||||||
|
reportError(err, { component: 'worker' });
|
||||||
process.exit(1);
|
process.exit(1);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in new issue
Block a user