Class MetricsBuffer
java.lang.Object
com.nmontytskyi.monitoring.starter.buffer.MetricsBuffer
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()everybufferFlushIntervalMsms. - 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 Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidadd(MetricPushRequest request) Adds a metric to the buffer.voidflush()Drains all buffered metrics and sends them as a single batch.voidshutdown()Flushes remaining metrics on JVM shutdown so no data is lost at graceful stop.
-
Constructor Details
-
MetricsBuffer
-
-
Method Details
-
add
Adds a metric to the buffer. If the queue size reachesbufferMaxSize, 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 viasynchronized. -
shutdown
@PreDestroy public void shutdown()Flushes remaining metrics on JVM shutdown so no data is lost at graceful stop.
-