Node SDK · BufferedRecorder

The default recorder buffers events in memory and flushes in batches:

const rec = es.newRecorder({ bufferSize: 2000, flushInterval: 2_000 });
// or
const rec = recorder.create(projectId, apiKey, { bufferSize: 2000 });

Options

Option Default Purpose
bufferSize 1000 Capacity of the in-memory event buffer.
flushSize 100 Event count that triggers an immediate flush.
flushInterval 5000 Max ms between flushes when the size threshold isn't reached.
flushTimeout 30000 Per-flush timeout (ms) for HTTP calls.
overflowPolicy "drop-newest" Behavior when buffer is full: "drop-newest", "block", or "error".
drainTimeout 30000 Max ms close() waits for in-flight events.
requestTimeout 10000 Per-request HTTP timeout (ms).
logger console Logger for diagnostics (overflow warnings, flush errors).
baseUrl production Override the ingestion endpoint (tests, staging).
fetch global Custom fetch implementation.
autoIdempotencyKey off Copy event.id into event.idempotencyKey when the latter is empty.

Overflow policies

"drop-newest" // drop the incoming event, log a warning (default)
"block"       // wait for space, abort signal, or close()
"error"       // throw BufferFullError

Close & flush

  • close() flushes pending events and waits up to drainTimeout for the final HTTP call. It is async, so await it. Rejects with DrainTimeoutError if the deadline is hit. Call it exactly once, on the path that actually runs at shutdown.
  • flush() drains buffered events at call time. Useful in tests and for graceful shutdown that needs to observe drain success.

Where close goes

A server never falls out of the bottom of its entry module, so a close() written after app.listen() runs immediately and drains an empty buffer. Hang it off the signal your process manager sends instead:

const server = app.listen(port);

async function shutdown() {
  await new Promise<void>((resolve) => server.close(() => resolve()));
  try {
    await rec.close(); // runs, and drains
  } catch (err) {
    console.error("everscribe: drain incomplete", err);
  }
  process.exit(0);
}

process.once("SIGTERM", shutdown);
process.once("SIGINT", shutdown);

Stop the server first, then close the recorder: events recorded by in-flight requests have to reach the buffer before it drains. And because close() is async, process.on("exit") is not a place to call it. That handler only runs synchronous work, so the drain never gets awaited and the buffer is discarded.