Monitor Class
TheMonitor class manages buffering, batching, and flushing of traces and spans to the Adaline API. It handles automatic retries, background flushing, and buffer management.
Overview
The Monitor acts as a central coordinator for all observability operations:- Buffers traces and spans in memory
- Batches multiple entries for efficient API calls
- Flushes automatically based on time intervals or buffer size
- Retries failed requests with exponential backoff
- Tracks sent and dropped entries for monitoring
Creation
Create a Monitor using theAdaline.initMonitor() method:
Properties
buffer
Items are added when you call
logTrace() or logSpan(), and removed after successful flush.projectId
sentCount
droppedCount
defaultContent
{ type: 'Other', input: '{}', output: '{}' }.
logger
Methods
logTrace()
Create a new trace and add it to the buffer.Parameters
Returns
A new Trace instance. See Trace Class for details.
Examples
flush()
Manually flush all ready entries in the buffer to the API.This method is automatically called on a timer (based on
flushInterval) and when the buffer reaches maxBufferSize. Manual calls are typically only needed during shutdown (especially in serverless environments) or testing.Behavior
- Skips if a flush is already in progress (prevents concurrent flushes)
- Filters for entries marked as
ready(viatrace.end()orspan.end()) - Sends each entry to the API with automatic retry
- Updates
traceIdon traces after successful creation - Removes successfully flushed entries from buffer
- Drops entries that fail after retries and increments
droppedCount
Retry Logic
- 5xx errors: Retry with exponential backoff (up to 10 retries, 20s total)
- 4xx errors: Fail immediately (no retry)
- Network errors: Retry with exponential backoff
Examples
stop()
Stop the background flush timer.Example
enforceBufferLimit()
Enforces themaxBufferSize limit by dropping the oldest entries when the buffer is full.
This method is called automatically when entries are added to the buffer. You typically don’t need to call it manually.
Complete Examples
Basic Usage
Production Setup with Health Monitoring
High-Volume Application
Testing with Monitor
Type Definitions
The
attributes field accepts Record<string, LogAttributesValue> where LogAttributesValue = string | number | boolean.