Class BatchProcessor

java.lang.Object
codes.grow.packages.betterstack.logging.BatchProcessor
All Implemented Interfaces:
AutoCloseable

public final class BatchProcessor extends Object implements AutoCloseable
Queues log records and exports them in batches from one background thread (a single-threaded ScheduledExecutorService).

A batch is exported when maxBatchSize records or maxBatchBytes bytes are queued, every flushInterval, on flush() and on shutdown. A batch whose export fails with a retryable SDK exception and nothing accepted is re-attempted up to two times (100 ms, 200 ms backoff) within the export timeout; partial acceptance is final. Every callback exception is caught and ignored.

  • Constructor Details

    • BatchProcessor

      public BatchProcessor(LogExporter exporter)
      Creates a processor with default options.
      Parameters:
      exporter - the exporter
    • BatchProcessor

      public BatchProcessor(LogExporter exporter, BatchOptions options)
      Creates a processor and starts its worker thread.
      Parameters:
      exporter - the exporter
      options - batch options
  • Method Details

    • options

      public BatchOptions options()
      The options.
      Returns:
      options
    • add

      public boolean add(LogRecord record)
      Queues a record. Never blocks unless the drop policy is DropPolicy.BLOCK.
      Parameters:
      record - the record
      Returns:
      false when the record was dropped
    • flush

      public void flush()
      Exports everything queued now, waiting at most the export timeout.
      Throws:
      TimeoutException - when the records are not exported in time
    • flush

      public void flush(Duration timeout)
      Exports everything queued now.
      Parameters:
      timeout - maximum wait
      Throws:
      TimeoutException - when the records are not exported in time
    • flushAsync

      public CompletableFuture<Void> flushAsync(Duration timeout)
      Exports everything queued now, asynchronously.
      Parameters:
      timeout - maximum wait; the future fails with TimeoutException after it
      Returns:
      completes when the records queued at call time were exported (or failed)
    • shutdown

      public void shutdown()
      Drains the queue, shuts the exporter down and stops the worker, waiting at most the export timeout. Idempotent.
      Throws:
      TimeoutException - when draining did not finish in time (remaining records are dropped)
    • shutdown

      public void shutdown(Duration timeout)
      Drains the queue, shuts the exporter down and stops the worker. Idempotent; later add(codes.grow.packages.betterstack.logging.LogRecord) calls drop the record with reason DropReason.SHUTDOWN.
      Parameters:
      timeout - maximum wait
      Throws:
      TimeoutException - when draining did not finish in time (remaining records are dropped)
    • shutdownAsync

      public CompletableFuture<Void> shutdownAsync(Duration timeout)
      Asynchronous shutdown(Duration). Repeated calls return the first call's future.
      Parameters:
      timeout - maximum wait
      Returns:
      completes when the processor stopped
    • close

      public void close()
      Same as shutdown().
      Specified by:
      close in interface AutoCloseable
    • stats

      public BatchStats stats()
      Current counters.
      Returns:
      stats snapshot