Skip to main content

Monitor Class

The Monitor 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 the Adaline.initMonitor() method:
With custom configuration:

Properties

buffer

In-memory array of BufferedEntry items storing traces and spans waiting to be flushed.
Items are added when you call logTrace() or logSpan(), and removed after successful flush.
Example:

projectId

The project ID that all traces and spans are associated with.

sentCount

Number of entries successfully sent to the API.

droppedCount

Number of entries dropped due to buffer overflow or send failure.

defaultContent

Default LogSpanContent used when no explicit content is provided. Defaults to { type: 'Other', input: '{}', output: '{}' }.

logger

Logger instance used for SDK diagnostics.

Methods

logTrace()

Create a new trace and add it to the buffer.

Parameters

options
object
required

Returns

trace
Trace
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

  1. Skips if a flush is already in progress (prevents concurrent flushes)
  2. Filters for entries marked as ready (via trace.end() or span.end())
  3. Sends each entry to the API with automatic retry
  4. Updates traceId on traces after successful creation
  5. Removes successfully flushed entries from buffer
  6. 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.
After calling stop(), the monitor will no longer automatically flush. You must manually call flush() to send buffered entries.

Example

enforceBufferLimit()

Enforces the maxBufferSize 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.