openvibers/openvibe.blog Commits

Blog: write request telemetry every 15 s, not only at stop (#53)

OP OpenVibers committed 29 days ago · d27565529e · 2 files, +9 −2

The request telemetry added in the previous PR reached `telemetry_samples` only when the process stopped. In production the table stayed empty between deploys (Food: three requests at the origin, no rows after two flush intervals).

openvibe-sdk 0.46's `createHttpTelemetry` keeps each interval's aggregates in memory and moves them to the sink only in its own `flush()` or `stop()`. The SDK's inner timer flushes only the already-emitted buffer, and only `telemetry.init()` ticks the collector.

- `server/index.js` now calls `ctx.telemetry.flush()` every 15 s from `start()` (unref'd), so production writes each interval's samples. Tests aren't affected: they flush by hand.
- On stop, the tick is cleared before the retention stops and the last flush runs.
- `test/telemetry.test.js` checks that `index.js` does both.

Changes

ddiff --git a/server/index.js b/server/index.js
iindex 55359f2..4b3414b 100644
---- a/server/index.js
++++ b/server/index.js
@@@ -17,7 +17,7 @@ const { startSubscriptions } = require('openvibe-sdk/account-data');
function createLifecycle({ server, ctx, exit, signals, extra = [] }) {
return gracefulStop({
name: 'Blog', server, deadlineExitCode: 0, exit, signals,
-- stop: [() => ctx.telemetry.stop(), () => ctx.worker.stop(), () => ctx.changelog.stop(), ...extra],
++ stop: [() => { clearInterval(ctx.telemetryTick); return ctx.telemetry.stop(); }, () => ctx.worker.stop(), () => ctx.changelog.stop(), ...extra],
close: [() => ctx.outbox.stop(), () => ctx.store.close()],
});
}
@@@ -33,6 +33,10 @@ async function start() {
server.keepAliveTimeout = 65_000;
ctx.outbox.start();
ctx.worker.start();
++ // The request collector aggregates in memory and has no timer of its own (openvibe-sdk 0.46 createHttpTelemetry):
++ // this tick writes each 15-second interval's samples; the stop clears it and flushes once more.
++ ctx.telemetryTick = setInterval(() => { ctx.telemetry.flush().catch(() => {}); }, 15_000);
++ ctx.telemetryTick.unref();
// The network changelog and patch notes run where the worker runs (one process).
if (config.worker.enabled) ctx.changelog.start();
ddiff --git a/test/telemetry.test.js b/test/telemetry.test.js
iindex 33aafb3..16081b3 100644
---- a/test/telemetry.test.js
++++ b/test/telemetry.test.js
@@@ -17,7 +17,10 @@ const read = (p) => fs.readFileSync(path.join(__dirname, '..', p), 'utf8');
assert.match(app, /createHttpTelemetry\(\{[^}]*service: 'blog'/s);
assert.match(app, /app\.use\(telemetry\.middleware\(\)\)/);
assert.match(app, /ctx\.telemetry = telemetry/);
-- assert.match(read('server/index.js'), /stop: \[\(\) => ctx\.telemetry\.stop\(\),/);
++ const index = read('server/index.js');
++ assert.match(index, /stop: \[\(\) => \{ clearInterval\(ctx\.telemetryTick\); return ctx\.telemetry\.stop\(\); \},/);
++ // The collector has no timer of its own (openvibe-sdk 0.46): index.js flushes it every 15 s.
++ assert.match(index, /ctx\.telemetryTick = setInterval\(\(\) => \{ ctx\.telemetry\.flush\(\)/);
});
const samples = [];