mirror of
https://github.com/mohamadihsan/topuponline.git
synced 2026-12-05 11:06:28 +00:00
46 lines
1.2 KiB
JavaScript
46 lines
1.2 KiB
JavaScript
const { Counter } = require('prom-client');
|
|
|
|
const skipped = new Counter({
|
|
name: 'topuponline_stdout_copies_skipped_total',
|
|
help: 'Log copies skipped while stdout was unavailable or under backpressure',
|
|
labelNames: ['reason'],
|
|
});
|
|
|
|
function createStdoutWriter(stream, onSkip) {
|
|
let failed = false;
|
|
stream.on('error', () => { failed = true; });
|
|
|
|
return (line) => {
|
|
// Arsip atau log file sudah menjadi salinan lokal. Jangan menambah antrean stdout.
|
|
if (failed || !stream.writable) {
|
|
onSkip('unavailable');
|
|
return;
|
|
}
|
|
if (stream.writableNeedDrain) {
|
|
onSkip('backpressure');
|
|
return;
|
|
}
|
|
try {
|
|
stream.write(`${line}\n`, (error) => {
|
|
if (error) onSkip('unavailable');
|
|
});
|
|
} catch {
|
|
failed = true;
|
|
onSkip('unavailable');
|
|
}
|
|
};
|
|
}
|
|
|
|
const writeStdoutLine = createStdoutWriter(process.stdout, (reason) => skipped.inc({ reason }));
|
|
|
|
function formatLog(event, time = Date.now()) {
|
|
return JSON.stringify({
|
|
timestamp: new Date(time).toISOString(),
|
|
app: 'topuponline',
|
|
pid: process.pid,
|
|
...event,
|
|
});
|
|
}
|
|
|
|
module.exports = { createStdoutWriter, writeStdoutLine, formatLog, stdoutMetrics: [skipped] };
|