Class SharedJvmMetricsCollector

java.lang.Object
io.confluent.telemetry.collector.SharedJvmMetricsCollector
All Implemented Interfaces:
MetricsCollector

public final class SharedJvmMetricsCollector extends Object implements MetricsCollector
A wrapper around JvmMetricsCollector that ensures only one collector instance exists per JVM process, even when multiple TelemetryReporter instances are running.

Each TelemetryReporter creates a SharedJvmMetricsCollector via its Provider, but only the "active" instance actually delegates collect(Emitter) to the underlying shared collector. When the active instance is stopped (because its reporter is closing), ownership automatically transfers to another registered instance so that JVM metrics collection continues uninterrupted.

  • Constructor Details

    • SharedJvmMetricsCollector

      public SharedJvmMetricsCollector(Context ctx)
  • Method Details

    • collect

      public void collect(Emitter emitter)
      Collect JVM metrics if this is the active instance.

      Uses a local-copy idiom instead of synchronizing: we snapshot sharedCollector into a local variable before checking activeInstance. This avoids taking a lock on every collection cycle (called every 60s per reporter), while a lock in stop() is only taken during reporter shutdown—an infrequent event.

      Without the local copy, a concurrent stop() could null out sharedCollector between the activeInstance check and the collect() call, causing a NPE. The worst case with the local copy is one extra collection on a collector whose ownership was just released—harmless since it only reads JMX MXBeans and emits metrics.

      Specified by:
      collect in interface MetricsCollector
    • stop

      public void stop()
      Specified by:
      stop in interface MetricsCollector
    • toString

      public String toString()
      Overrides:
      toString in class Object