openvibers/openvibe.trade Commits
Trade: telemetry flushes each minute and keeps 14 days (#65)
Changes
@@@ -125,8 +125,8 @@ async function createApp(opts = {}) {app.locals.metrics = metrics.registry;// Request telemetry (openvibe-sdk/service, plan T1): one aggregated sample per route template, method and status// class each flush, stored in this service's telemetry_samples table; /shared, health, readiness and /metrics make-- // none. server/index.js flushes it every 15 s and on stop, and keeps the table to 30 days.-- ctx.telemetry = createHttpTelemetry({ service: 'trade', sink: opts.telemetrySink || createPgTelemetrySink({ db: store.db, log }), skipPrefixes: ['/shared'] });++ // none. server/index.js flushes it every minute and on stop, and keeps the table to 14 days.++ ctx.telemetry = createHttpTelemetry({ service: 'trade', sink: opts.telemetrySink || createPgTelemetrySink({ db: store.db, log }), skipPrefixes: ['/shared'], intervalMs: 60_000 });app.use(ctx.telemetry.middleware());app.locals.ctx = ctx;// Per-actor limits (http/actor-limits.js) on /api/v1 and the forms, counted once each router resolved@@@ -23,11 +23,11 @@ const server = app.listen(config.port, config.host, () => {server.keepAliveTimeout = 65_000;ctx.outbox.start();ctx.worker.start();--// telemetry_samples rows older than 30 days go: a minute after start, then daily (openvibe-sdk/telemetry).--ctx.retention = startTelemetryRetention({ db: ctx.store.db, log: console });++// telemetry_samples rows older than 14 days go: a minute after start, then daily (openvibe-sdk/telemetry).++ctx.retention = startTelemetryRetention({ db: ctx.store.db, log: console, olderThanDays: 14 });// 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);++// this tick writes each minute's samples; the stop clears it and flushes once more.++ctx.telemetryTick = setInterval(() => { ctx.telemetry.flush().catch(() => {}); }, 60_000);ctx.telemetryTick.unref();// The two account subscriptions at OpenVibe.Events (ADR-033), created when missing; off without EVENTS_URL,// TRADE_EVENTS_WEBHOOK_SECRET or the client secret. The sources.* one stays scripts/subscribe.js's.@@@ -2,7 +2,7 @@/*** Request telemetry (openvibe-sdk/service, plan T1 step 10): app.js builds one collector for service 'trade' with a* PostgreSQL sink, a page hit five times flushes as ONE aggregated http.request sample (route template, 2xx, count 5),-- * /metrics and /shared files make none, and the 30-day retention deletes only older rows.++ * /metrics and /shared files make none, and the 14-day retention deletes only older rows.*/const assert = require('assert');const { pruneTelemetry } = require('openvibe-sdk/telemetry');@@@ -11,7 +11,7 @@ const fs = require('fs');const path = require('path');(async () => {-- await check('index.js flushes the collector every 15 s and clears that tick before the last flush on stop', () => {++ await check('index.js flushes the collector every minute and clears that tick before the last flush on stop', () => {const index = fs.readFileSync(path.join(__dirname, '..', 'server', 'index.js'), 'utf8');assert.match(index, /ctx\.telemetryTick = setInterval\(\(\) => \{ ctx\.telemetry\.flush\(\)/);assert.match(index, /clearInterval\(ctx\.telemetryTick\); if \(ctx\.retention\) await ctx\.retention\.stop\(\); if \(ctx\.telemetry\) await ctx\.telemetry\.stop\(\);/);@@@ -38,13 +38,13 @@ const path = require('path');assert.deepStrictEqual(requests(), []);});-- await check('the table exists and the retention deletes rows older than 30 days, keeping newer ones', async () => {++ await check('the table exists and the retention deletes rows older than 14 days, keeping newer ones', async () => {const db = t.ctx.store.db;await db.query("DELETE FROM telemetry_samples");const insert = (days) => db.query("INSERT INTO telemetry_samples (at, service, operation) VALUES (now() - make_interval(days => $1), 'trade', 'http.request')", [days]);-- await insert(45);++ await insert(20);await insert(1);-- assert.strictEqual(await pruneTelemetry({ db }), 1);++ assert.strictEqual(await pruneTelemetry({ db, olderThanDays: 14 }), 1);assert.strictEqual((await db.query('SELECT count(*)::int AS n FROM telemetry_samples')).rows[0].n, 1);});} finally { await t.close(); }
@@ -125,8 +125,8 @@ async function createApp(opts = {}) {
@@ -125,8 +125,8 @@ async function createApp(opts = {}) {
app.locals.metrics = metrics.registry;
app.locals.metrics = metrics.registry;
// Request telemetry (openvibe-sdk/service, plan T1): one aggregated sample per route template, method and status
// Request telemetry (openvibe-sdk/service, plan T1): one aggregated sample per route template, method and status
// class each flush, stored in this service's telemetry_samples table; /shared, health, readiness and /metrics make
// class each flush, stored in this service's telemetry_samples table; /shared, health, readiness and /metrics make
- // none. server/index.js flushes it every 15 s and on stop, and keeps the table to 30 days.
- ctx.telemetry = createHttpTelemetry({ service: 'trade', sink: opts.telemetrySink || createPgTelemetrySink({ db: store.db, log }), skipPrefixes: ['/shared'] });
+ // none. server/index.js flushes it every minute and on stop, and keeps the table to 14 days.
+ ctx.telemetry = createHttpTelemetry({ service: 'trade', sink: opts.telemetrySink || createPgTelemetrySink({ db: store.db, log }), skipPrefixes: ['/shared'], intervalMs: 60_000 });
app.use(ctx.telemetry.middleware());
app.use(ctx.telemetry.middleware());
app.locals.ctx = ctx;
app.locals.ctx = ctx;
// Per-actor limits (http/actor-limits.js) on /api/v1 and the forms, counted once each router resolved
// Per-actor limits (http/actor-limits.js) on /api/v1 and the forms, counted once each router resolved
@@ -23,11 +23,11 @@ const server = app.listen(config.port, config.host, () => {
@@ -23,11 +23,11 @@ const server = app.listen(config.port, config.host, () => {
server.keepAliveTimeout = 65_000;
server.keepAliveTimeout = 65_000;
ctx.outbox.start();
ctx.outbox.start();
ctx.worker.start();
ctx.worker.start();
-// telemetry_samples rows older than 30 days go: a minute after start, then daily (openvibe-sdk/telemetry).
-ctx.retention = startTelemetryRetention({ db: ctx.store.db, log: console });
+// telemetry_samples rows older than 14 days go: a minute after start, then daily (openvibe-sdk/telemetry).
+ctx.retention = startTelemetryRetention({ db: ctx.store.db, log: console, olderThanDays: 14 });
// The request collector aggregates in memory and has no timer of its own (openvibe-sdk 0.46 createHttpTelemetry):
// 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);
+// this tick writes each minute's samples; the stop clears it and flushes once more.
+ctx.telemetryTick = setInterval(() => { ctx.telemetry.flush().catch(() => {}); }, 60_000);
ctx.telemetryTick.unref();
ctx.telemetryTick.unref();
// The two account subscriptions at OpenVibe.Events (ADR-033), created when missing; off without EVENTS_URL,
// The two account subscriptions at OpenVibe.Events (ADR-033), created when missing; off without EVENTS_URL,
// TRADE_EVENTS_WEBHOOK_SECRET or the client secret. The sources.* one stays scripts/subscribe.js's.
// TRADE_EVENTS_WEBHOOK_SECRET or the client secret. The sources.* one stays scripts/subscribe.js's.
@@ -2,7 +2,7 @@
@@ -2,7 +2,7 @@
/**
/**
* Request telemetry (openvibe-sdk/service, plan T1 step 10): app.js builds one collector for service 'trade' with a
* Request telemetry (openvibe-sdk/service, plan T1 step 10): app.js builds one collector for service 'trade' with a
* PostgreSQL sink, a page hit five times flushes as ONE aggregated http.request sample (route template, 2xx, count 5),
* PostgreSQL sink, a page hit five times flushes as ONE aggregated http.request sample (route template, 2xx, count 5),
- * /metrics and /shared files make none, and the 30-day retention deletes only older rows.
+ * /metrics and /shared files make none, and the 14-day retention deletes only older rows.
*/
*/
const assert = require('assert');
const assert = require('assert');
const { pruneTelemetry } = require('openvibe-sdk/telemetry');
const { pruneTelemetry } = require('openvibe-sdk/telemetry');
@@ -11,7 +11,7 @@ const fs = require('fs');
@@ -11,7 +11,7 @@ const fs = require('fs');
const path = require('path');
const path = require('path');
(async () => {
(async () => {
- await check('index.js flushes the collector every 15 s and clears that tick before the last flush on stop', () => {
+ await check('index.js flushes the collector every minute and clears that tick before the last flush on stop', () => {
const index = fs.readFileSync(path.join(__dirname, '..', 'server', 'index.js'), 'utf8');
const index = fs.readFileSync(path.join(__dirname, '..', 'server', 'index.js'), 'utf8');
assert.match(index, /ctx\.telemetryTick = setInterval\(\(\) => \{ ctx\.telemetry\.flush\(\)/);
assert.match(index, /ctx\.telemetryTick = setInterval\(\(\) => \{ ctx\.telemetry\.flush\(\)/);
assert.match(index, /clearInterval\(ctx\.telemetryTick\); if \(ctx\.retention\) await ctx\.retention\.stop\(\); if \(ctx\.telemetry\) await ctx\.telemetry\.stop\(\);/);
assert.match(index, /clearInterval\(ctx\.telemetryTick\); if \(ctx\.retention\) await ctx\.retention\.stop\(\); if \(ctx\.telemetry\) await ctx\.telemetry\.stop\(\);/);
@@ -38,13 +38,13 @@ const path = require('path');
@@ -38,13 +38,13 @@ const path = require('path');
assert.deepStrictEqual(requests(), []);
assert.deepStrictEqual(requests(), []);
});
});
- await check('the table exists and the retention deletes rows older than 30 days, keeping newer ones', async () => {
+ await check('the table exists and the retention deletes rows older than 14 days, keeping newer ones', async () => {
const db = t.ctx.store.db;
const db = t.ctx.store.db;
await db.query("DELETE FROM telemetry_samples");
await db.query("DELETE FROM telemetry_samples");
const insert = (days) => db.query("INSERT INTO telemetry_samples (at, service, operation) VALUES (now() - make_interval(days => $1), 'trade', 'http.request')", [days]);
const insert = (days) => db.query("INSERT INTO telemetry_samples (at, service, operation) VALUES (now() - make_interval(days => $1), 'trade', 'http.request')", [days]);
- await insert(45);
+ await insert(20);
await insert(1);
await insert(1);
- assert.strictEqual(await pruneTelemetry({ db }), 1);
+ assert.strictEqual(await pruneTelemetry({ db, olderThanDays: 14 }), 1);
assert.strictEqual((await db.query('SELECT count(*)::int AS n FROM telemetry_samples')).rows[0].n, 1);
assert.strictEqual((await db.query('SELECT count(*)::int AS n FROM telemetry_samples')).rows[0].n, 1);
});
});
} finally { await t.close(); }
} finally { await t.close(); }