@plinthjs/monitor
A headless application performance recorder for Plinth. Monitor records small { type, key, value?, tags? } measurements into a swappable MonitorStore, and rolls them up with count, min, max, sum, avg and p95 aggregates, optionally bucketed into fixed time windows. Recorder helpers turn slow queries, slow requests, exceptions, cache hits, user activity and queue jobs into entries.
The clock is injected (epoch milliseconds), so timestamps and buckets are deterministic in tests.
Install
npm install @plinthjs/monitorUsage
import { Monitor } from '@plinthjs/monitor'
const monitor = new Monitor() // ArrayMonitorStore + Date.now by default
monitor.record('slow_query', 'select * from users', 1450)
monitor.record('slow_query', 'select * from users', 1200)
monitor.record('slow_query', 'select * from orders', 2100)
monitor.aggregate('slow_query', { aggregate: 'max', limit: 5 })
// [{ key: 'select * from orders', value: 2100, count: 1 }, { key: 'select * from users', value: 1450, count: 2 }]
monitor.aggregate('slow_query', { aggregate: 'sum', period: 60 }) // one row per key per 60s bucket
monitor.values('slow_query') // [1450, 1200, 2100]
monitor.entries({ type: 'slow_query', since: Date.now() - 3_600_000 }) // most recent firstaggregate() defaults to count, ordered by value descending (orderBy: 'asc' flips it), and accepts since / before bounds. period is in seconds, and bucketed rows carry a bucket start instant.
Recorders
import {
cacheInteraction,
exceptionEntry,
MonitorType,
queueEntry,
slowQuery,
slowRequest,
userRequest,
} from '@plinthjs/monitor'
// Threshold recorders return undefined below the threshold (default 1000 ms).
const draft = slowQuery(sql, durationMs, { threshold: 500 })
if (draft) monitor.ingestDraft(draft)
monitor.ingestDraft(slowRequest('GET /reports', 1800))
monitor.ingestDraft(exceptionEntry(new TypeError('boom'))) // keyed by error name
monitor.ingestDraft(cacheInteraction('user:1', true)) // cache_hit or cache_miss
monitor.ingestDraft(userRequest(42))
monitor.ingestDraft(queueEntry('emails', 320))
monitor.aggregate(MonitorType.CacheMiss) // most-missed keysFiltering, buffering and toggling
const monitor = new Monitor({ lazy: true }) // buffer entries until flush()
monitor.filter((entry) => !entry.key.startsWith('/health')) // drop matching entries
monitor.record('user_request', '7')
monitor.pending() // 1
monitor.flush() // writes the buffer to the store, returns the count
monitor.disable() // record() becomes a no-op
monitor.recording() // false
monitor.enable()Reads (aggregate, values, entries) flush the buffer first, so they always see every recorded entry.
Storage and retention
import { ArrayMonitorStore, Monitor } from '@plinthjs/monitor'
let now = 0
const monitor = new Monitor({ store: new ArrayMonitorStore(), now: () => now })
monitor.prune(now - 7 * 24 * 3_600_000) // remove entries older than a week, returns the count
monitor.clear() // drop the buffer and every stored entryImplement the MonitorStore interface (record, entries, aggregate, prune, clear) to persist entries elsewhere.