Class MetricsBuffer

java.lang.Object
com.nmontytskyi.monitoring.starter.buffer.MetricsBuffer

public class MetricsBuffer extends Object
Thread-safe metric buffer that accumulates MetricPushRequest records and sends them as a single HTTP batch to the monitoring server.

Flushing policy:

  • Periodic: a scheduler calls flush() every bufferFlushIntervalMs ms.
  • Immediate: if the queue reaches bufferMaxSize, flush() is triggered synchronously inside the calling thread.
  • Shutdown: shutdown() (annotated with @PreDestroy) flushes any remaining metrics before the JVM stops.

Fail-safe behaviour: if the HTTP batch call fails, the drained metrics are returned to the queue so they are retried on the next flush. This prevents metric loss but may cause memory growth when the monitoring server is permanently unavailable. A WARN log is emitted on every failed attempt.

  • Constructor Details

  • Method Details

    • add

      public void add(MetricPushRequest request)
      Adds a metric to the buffer. If the queue size reaches bufferMaxSize, an immediate synchronous flush is triggered.
    • flush

      public void flush()
      Drains all buffered metrics and sends them as a single batch. No-op when the buffer is empty. Thread-safe via synchronized.
    • shutdown

      @PreDestroy public void shutdown()
      Flushes remaining metrics on JVM shutdown so no data is lost at graceful stop.