Skip to content


Metrics is a registry of sensors and performance metrics (of Kafka brokers and clients).

Creating Instance

Metrics takes the following to be created:

  • MetricConfig
  • MetricsReporters
  • Time
  • enableExpiration flag
  • MetricsContext

Metrics is created when:

  • Server utility is used to buildMetrics
  • KafkaAdminClient utility is used to createInternal
  • KafkaConsumer utility is used to buildMetrics
  • KafkaProducer is created
  • KafkaStreams (Kafka Streams) utility is used to getMetrics
  • Kafka Connect clients


void addReporter(
  MetricsReporter reporter)


addReporter is used when:

  • DynamicMetricsReporters is requested to createReporters


Metrics defines sensors collection of metric Sensors by name (ConcurrentMap<String, Sensor>).

sensors is empty when Metrics is created.

A new Sensor is added in sensor.


Sensor sensor(
  String name,
  MetricConfig config,
  long inactiveSensorExpirationTimeSeconds,
  Sensor.RecordingLevel recordingLevel,
  Sensor... parents)
Sensor sensor(...) // (1)
  1. There are others

sensor looks up the sensor (by name) and returns it immediately if available.

Otherwise, sensor creates a new Sensor and adds it to the sensors registry.

In the end, sensor prints out the following TRACE message to the logs:

Added sensor with name [name]


Sensor getSensor(
  String name)

getSensor looks up the given name in the sensors registry.


Enable ALL logging level for org.apache.kafka.common.metrics.Metrics logger to see what happens inside.

Add the following line to conf/

Refer to Logging.

Back to top