Skip to content

@plinthjs/inspector ​

Plinth's debug recorder: the recorder, entry builders, watcher hook and entry store, with no UI. Watchers and application code call a Inspector recorder to capture requests, queries, exceptions, events, jobs, cache operations, mail and logs. Each observation becomes a typed Entry persisted to an injectable EntryStore.

Wiring watchers to live framework signals is the application's job; this package supplies the entry builders and a registerWatcher hook.

Install ​

sh
npm install @plinthjs/inspector

Usage ​

ts
import { Inspector } from '@plinthjs/inspector'

const inspector = new Inspector() // in-memory ArrayEntryStore, Date.now, counter ids

inspector.recordRequest({ method: 'GET', uri: '/users', status: 200, durationMs: 12 })
inspector.recordQuery('select * from users where id = ?', [1], 250) // tagged 'slow' (>= 100ms)
inspector.recordException(new TypeError('boom'))
inspector.recordEvent('user.registered', { id: 1 })
inspector.recordJob({ name: 'SendWelcomeEmail', queue: 'mail', status: 'processed' })
inspector.recordCache('hit', 'user:1')
inspector.recordMail({ to: 'ada@example.com', subject: 'Welcome' })
inspector.recordLog('error', 'Payment failed', { orderId: 7 })

// Or record any entry type directly.
inspector.record('schedule', { command: 'reports:send' }, ['nightly'])

Each record* call returns the stored Entry (id, batchId, type, content, tags, createdAt), or undefined when recording is paused or a filter rejected it.

Reading entries ​

ts
inspector.entries() // newest first
inspector.entries({ type: 'query', tag: 'slow', limit: 10 })
inspector.entries({ batchId: 'req-42' })
inspector.find('entry-1')

inspector.prune(Date.now() - 24 * 60 * 60 * 1000) // inspector:prune, returns the removed count
inspector.clear() // inspector:clear

Batches, tags and filters ​

ts
// Group everything recorded during one unit of work under a batch id.
inspector.withBatch('req-42', () => {
  inspector.recordQuery('select 1', [], 3)
  inspector.recordLog('info', 'handled')
})
inspector.currentBatch() // 'default' outside any batch
const id = inspector.newBatch() // start a generated batch and make it current

// Global taggers and filters (Inspector::tag / Inspector::filter).
inspector.tag((entry) => (entry.type === 'request' ? ['web'] : []))
inspector.filter((entry) => entry.type !== 'cache')

// Pause capture.
inspector.stopRecording()
inspector.recording() // false
inspector.startRecording()
inspector.withoutRecording(() => runFrameworkInternals())

Entry builders and watchers ​

The builders (requestEntry, queryEntry, exceptionEntry, eventEntry, jobEntry, cacheEntry, mailEntry, logEntry) are pure functions returning an EntryDraft (type, content, tags). A Watcher is any object with a name and a register(context) hook:

ts
import { registerWatcher, type Watcher } from '@plinthjs/inspector'

const queryWatcher: Watcher<Database> = {
  name: 'queries',
  register: (db) => db.listen((q) => inspector.recordQuery(q.sql, q.bindings, q.time)),
}

registerWatcher(queryWatcher, db)

Deterministic tests ​

Every seam is injectable: store (any EntryStore, ArrayEntryStore by default), now (an epoch-millisecond clock), ids and batchIds.

ts
import { ArrayEntryStore, Inspector } from '@plinthjs/inspector'

let n = 0
const inspector = new Inspector({
  store: new ArrayEntryStore(),
  now: () => 1_000,
  ids: () => `id-${++n}`,
})

Released under the MIT License.