org.apache.kafka.common.metrics
Class Metrics

java.lang.Object
  extended by org.apache.kafka.common.metrics.Metrics

public class Metrics
extends java.lang.Object

A registry of sensors and metrics.

A metric is a named, numerical measurement. A sensor is a handle to record numerical measurements as they occur. Each Sensor has zero or more associated metrics. For example a Sensor might represent message sizes and we might associate with this sensor a metric for the average, maximum, or other statistics computed off the sequence of message sizes that are recorded by the sensor.

Usage looks something like this:

 // set up metrics:
 Metrics metrics = new Metrics(); // this is the global repository of metrics and sensors
 Sensor sensor = metrics.sensor("message-sizes");
 sensor.add("kafka.producer.message-sizes.avg", new Avg());
 sensor.add("kafka.producer.message-sizes.max", new Max());
 
 // as messages are sent we record the sizes
 sensor.record(messageSize);
 


Constructor Summary
Metrics()
          Create a metrics repository with no metric reporters and default configuration.
Metrics(MetricConfig defaultConfig)
          Create a metrics repository with no reporters and the given default config.
Metrics(MetricConfig defaultConfig, java.util.List<MetricsReporter> reporters, Time time)
          Create a metrics repository with a default config and the given metric reporters
Metrics(Time time)
          Create a metrics repository with no metric reporters and default configuration.
 
Method Summary
 void addMetric(java.lang.String name, Measurable measurable)
          Add a metric to monitor an object that implements measurable.
 void addMetric(java.lang.String name, MetricConfig config, Measurable measurable)
          Add a metric to monitor an object that implements measurable.
 void addMetric(java.lang.String name, java.lang.String description, Measurable measurable)
          Add a metric to monitor an object that implements measurable.
 void addMetric(java.lang.String name, java.lang.String description, MetricConfig config, Measurable measurable)
          Add a metric to monitor an object that implements measurable.
 void addReporter(MetricsReporter reporter)
          Add a MetricReporter
 void close()
          Close this metrics repository.
 Sensor getSensor(java.lang.String name)
          Get the sensor with the given name if it exists
 java.util.Map<java.lang.String,KafkaMetric> metrics()
          Get all the metrics currently maintained indexed by metric name
 Sensor sensor(java.lang.String name)
          Get or create a sensor with the given unique name and no parent sensors.
 Sensor sensor(java.lang.String name, MetricConfig config, Sensor... parents)
          Get or create a sensor with the given unique name and zero or more parent sensors.
 Sensor sensor(java.lang.String name, Sensor... parents)
          Get or create a sensor with the given unique name and zero or more parent sensors.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Metrics

public Metrics()
Create a metrics repository with no metric reporters and default configuration.


Metrics

public Metrics(Time time)
Create a metrics repository with no metric reporters and default configuration.


Metrics

public Metrics(MetricConfig defaultConfig)
Create a metrics repository with no reporters and the given default config. This config will be used for any metric that doesn't override its own config.

Parameters:
defaultConfig - The default config to use for all metrics that don't override their config

Metrics

public Metrics(MetricConfig defaultConfig,
               java.util.List<MetricsReporter> reporters,
               Time time)
Create a metrics repository with a default config and the given metric reporters

Parameters:
defaultConfig - The default config
reporters - The metrics reporters
time - The time instance to use with the metrics
Method Detail

getSensor

public Sensor getSensor(java.lang.String name)
Get the sensor with the given name if it exists

Parameters:
name - The name of the sensor
Returns:
Return the sensor or null if no such sensor exists

sensor

public Sensor sensor(java.lang.String name)
Get or create a sensor with the given unique name and no parent sensors.

Parameters:
name - The sensor name
Returns:
The sensor

sensor

public Sensor sensor(java.lang.String name,
                     Sensor... parents)
Get or create a sensor with the given unique name and zero or more parent sensors. All parent sensors will receive every value recorded with this sensor.

Parameters:
name - The name of the sensor
parents - The parent sensors
Returns:
The sensor that is created

sensor

public Sensor sensor(java.lang.String name,
                     MetricConfig config,
                     Sensor... parents)
Get or create a sensor with the given unique name and zero or more parent sensors. All parent sensors will receive every value recorded with this sensor.

Parameters:
name - The name of the sensor
config - A default configuration to use for this sensor for metrics that don't have their own config
parents - The parent sensors
Returns:
The sensor that is created

addMetric

public void addMetric(java.lang.String name,
                      Measurable measurable)
Add a metric to monitor an object that implements measurable. This metric won't be associated with any sensor. This is a way to expose existing values as metrics.

Parameters:
name - The name of the metric
measurable - The measurable that will be measured by this metric

addMetric

public void addMetric(java.lang.String name,
                      java.lang.String description,
                      Measurable measurable)
Add a metric to monitor an object that implements measurable. This metric won't be associated with any sensor. This is a way to expose existing values as metrics.

Parameters:
name - The name of the metric
description - A human-readable description to include in the metric
measurable - The measurable that will be measured by this metric

addMetric

public void addMetric(java.lang.String name,
                      MetricConfig config,
                      Measurable measurable)
Add a metric to monitor an object that implements measurable. This metric won't be associated with any sensor. This is a way to expose existing values as metrics.

Parameters:
name - The name of the metric
config - The configuration to use when measuring this measurable
measurable - The measurable that will be measured by this metric

addMetric

public void addMetric(java.lang.String name,
                      java.lang.String description,
                      MetricConfig config,
                      Measurable measurable)
Add a metric to monitor an object that implements measurable. This metric won't be associated with any sensor. This is a way to expose existing values as metrics.

Parameters:
name - The name of the metric
description - A human-readable description to include in the metric
config - The configuration to use when measuring this measurable
measurable - The measurable that will be measured by this metric

addReporter

public void addReporter(MetricsReporter reporter)
Add a MetricReporter


metrics

public java.util.Map<java.lang.String,KafkaMetric> metrics()
Get all the metrics currently maintained indexed by metric name


close

public void close()
Close this metrics repository.