Skip to content

Metrics

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

addReporter

void addReporter(
  MetricsReporter reporter)

addReporter...FIXME

addReporter is used when:

  • DynamicMetricsReporters is requested to createReporters

sensors

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 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]

getSensor

Sensor getSensor(
  String name)

getSensor looks up the given name in the sensors registry.

Logging

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

Add the following line to config/log4j.properties:

log4j.logger.org.apache.kafka.common.metrics.Metrics=ALL

Refer to Logging.