public class Metrics extends Object implements Closeable
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");
MetricName metricName = new MetricName("message-size-avg", "producer-metrics");
sensor.add(metricName, new Avg());
metricName = new MetricName("message-size-max", "producer-metrics");
sensor.add(metricName, new Max());
// as messages are sent we record the sizes
sensor.record(messageSize);
| Constructor and Description |
|---|
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,
List<MetricsReporter> reporters,
org.apache.kafka.common.utils.Time time)
Create a metrics repository with a default config and the given metric reporters.
|
Metrics(MetricConfig defaultConfig,
List<MetricsReporter> reporters,
org.apache.kafka.common.utils.Time time,
boolean enableExpiration)
Create a metrics repository with a default config, given metric reporters and the ability to expire eligible sensors
|
Metrics(MetricConfig defaultConfig,
List<MetricsReporter> reporters,
org.apache.kafka.common.utils.Time time,
boolean enableExpiration,
MetricsContext metricsContext)
Create a metrics repository with a default config, given metric reporters, the ability to expire eligible sensors
and MetricContext
|
Metrics(MetricConfig defaultConfig,
List<MetricsReporter> reporters,
org.apache.kafka.common.utils.Time time,
MetricsContext metricsContext)
Create a metrics repository with a default config, metric reporters and metric context
Expiration of Sensors is disabled.
|
Metrics(MetricConfig defaultConfig,
org.apache.kafka.common.utils.Time time)
Create a metrics repository with no metric reporters and the given default configuration.
|
Metrics(org.apache.kafka.common.utils.Time time)
Create a metrics repository with no metric reporters and default configuration.
|
| Modifier and Type | Method and Description |
|---|---|
void |
addMetric(MetricName metricName,
Measurable measurable)
Add a metric to monitor an object that implements measurable.
|
void |
addMetric(MetricName metricName,
MetricConfig config,
Measurable measurable)
Add a metric to monitor an object that implements Measurable.
|
void |
addMetric(MetricName metricName,
MetricConfig config,
MetricValueProvider<?> metricValueProvider)
Add a metric to monitor an object that implements MetricValueProvider.
|
void |
addMetric(MetricName metricName,
MetricValueProvider<?> metricValueProvider)
Add a metric to monitor an object that implements MetricValueProvider.
|
void |
addReporter(MetricsReporter reporter)
Add a MetricReporter
|
void |
close()
Close this metrics repository.
|
MetricConfig |
config() |
Sensor |
getSensor(String name)
Get the sensor with the given name if it exists
|
KafkaMetric |
metric(MetricName metricName) |
MetricName |
metricInstance(MetricNameTemplate template,
Map<String,String> tags) |
MetricName |
metricInstance(MetricNameTemplate template,
String... keyValue) |
MetricName |
metricName(String name,
String group)
Create a MetricName with the given name, group and default tags specified in the metric configuration.
|
MetricName |
metricName(String name,
String group,
Map<String,String> tags)
Create a MetricName with the given name, group and tags, plus default tags specified in the metric
configuration.
|
MetricName |
metricName(String name,
String group,
String description)
Create a MetricName with the given name, group, description, and default tags
specified in the metric configuration.
|
MetricName |
metricName(String name,
String group,
String description,
Map<String,String> tags)
Create a MetricName with the given name, group, description and tags, plus default tags specified in the metric
configuration.
|
MetricName |
metricName(String name,
String group,
String description,
String... keyValue)
Create a MetricName with the given name, group, description, and keyValue as tags, plus default tags specified in the metric
configuration.
|
Map<MetricName,KafkaMetric> |
metrics()
Get all the metrics currently maintained indexed by metricName
|
KafkaMetric |
removeMetric(MetricName metricName)
Remove a metric if it exists and return it.
|
void |
removeReporter(MetricsReporter reporter)
Remove a MetricReporter
|
void |
removeSensor(String name)
Remove a sensor (if it exists), associated metrics and its children.
|
List<MetricsReporter> |
reporters() |
Sensor |
sensor(String name)
Get or create a sensor with the given unique name and no parent sensors.
|
Sensor |
sensor(String name,
MetricConfig config,
long inactiveSensorExpirationTimeSeconds,
Sensor... parents)
Get or create a sensor with the given unique name and zero or more parent sensors.
|
Sensor |
sensor(String name,
MetricConfig config,
long inactiveSensorExpirationTimeSeconds,
Sensor.RecordingLevel recordingLevel,
Sensor... parents)
Get or create a sensor with the given unique name and zero or more parent sensors.
|
Sensor |
sensor(String name,
MetricConfig config,
Sensor... parents)
Get or create a sensor with the given unique name and zero or more parent sensors.
|
Sensor |
sensor(String name,
MetricConfig config,
Sensor.RecordingLevel recordingLevel,
Sensor... parents)
Get or create a sensor with the given unique name and zero or more parent sensors.
|
Sensor |
sensor(String name,
Sensor... parents)
Get or create a sensor with the given unique name and zero or more parent sensors.
|
Sensor |
sensor(String name,
Sensor.RecordingLevel recordingLevel)
Get or create a sensor with the given unique name and no parent sensors and with a given
recording level.
|
Sensor |
sensor(String name,
Sensor.RecordingLevel recordingLevel,
Sensor... parents)
Get or create a sensor with the given unique name and zero or more parent sensors.
|
static String |
toHtmlTable(String domain,
Iterable<MetricNameTemplate> allMetrics)
Use the specified domain and metric name templates to generate an HTML table documenting the metrics.
|
public Metrics()
public Metrics(org.apache.kafka.common.utils.Time time)
public Metrics(MetricConfig defaultConfig, org.apache.kafka.common.utils.Time time)
public Metrics(MetricConfig defaultConfig)
defaultConfig - The default config to use for all metrics that don't override their configpublic Metrics(MetricConfig defaultConfig, List<MetricsReporter> reporters, org.apache.kafka.common.utils.Time time)
defaultConfig - The default configreporters - The metrics reporterstime - The time instance to use with the metricspublic Metrics(MetricConfig defaultConfig, List<MetricsReporter> reporters, org.apache.kafka.common.utils.Time time, MetricsContext metricsContext)
defaultConfig - The default configreporters - The metrics reporterstime - The time instance to use with the metricsmetricsContext - The metricsContext to initialize metrics reporter withpublic Metrics(MetricConfig defaultConfig, List<MetricsReporter> reporters, org.apache.kafka.common.utils.Time time, boolean enableExpiration)
defaultConfig - The default configreporters - The metrics reporterstime - The time instance to use with the metricsenableExpiration - true if the metrics instance can garbage collect inactive sensors, false otherwisepublic Metrics(MetricConfig defaultConfig, List<MetricsReporter> reporters, org.apache.kafka.common.utils.Time time, boolean enableExpiration, MetricsContext metricsContext)
defaultConfig - The default configreporters - The metrics reporterstime - The time instance to use with the metricsenableExpiration - true if the metrics instance can garbage collect inactive sensors, false otherwisemetricsContext - The metricsContext to initialize metrics reporter withpublic MetricName metricName(String name, String group, String description, Map<String,String> tags)
name - The name of the metricgroup - logical group name of the metrics to which this metric belongsdescription - A human-readable description to include in the metrictags - additional key/value attributes of the metricpublic MetricName metricName(String name, String group, String description)
name - The name of the metricgroup - logical group name of the metrics to which this metric belongsdescription - A human-readable description to include in the metricpublic MetricName metricName(String name, String group)
name - The name of the metricgroup - logical group name of the metrics to which this metric belongspublic MetricName metricName(String name, String group, String description, String... keyValue)
name - The name of the metricgroup - logical group name of the metrics to which this metric belongsdescription - A human-readable description to include in the metrickeyValue - additional key/value attributes of the metric (must come in pairs)public MetricName metricName(String name, String group, Map<String,String> tags)
name - The name of the metricgroup - logical group name of the metrics to which this metric belongstags - key/value attributes of the metricpublic static String toHtmlTable(String domain, Iterable<MetricNameTemplate> allMetrics)
MetricNameTemplate.domain - the domain or prefix for the JMX MBean names; may not be nullallMetrics - the collection of all MetricNameTemplate instances each describing one metric; may not be nullpublic MetricConfig config()
public Sensor getSensor(String name)
name - The name of the sensorpublic Sensor sensor(String name)
name - The sensor namepublic Sensor sensor(String name, Sensor.RecordingLevel recordingLevel)
name - The sensor name.recordingLevel - The recording level.public Sensor sensor(String name, Sensor... parents)
name - The name of the sensorparents - The parent sensorspublic Sensor sensor(String name, Sensor.RecordingLevel recordingLevel, Sensor... parents)
name - The name of the sensor.parents - The parent sensors.recordingLevel - The recording level.public Sensor sensor(String name, MetricConfig config, Sensor... parents)
name - The name of the sensorconfig - A default configuration to use for this sensor for metrics that don't have their own configparents - The parent sensorspublic Sensor sensor(String name, MetricConfig config, Sensor.RecordingLevel recordingLevel, Sensor... parents)
name - The name of the sensorconfig - A default configuration to use for this sensor for metrics that don't have their own configrecordingLevel - The recording level.parents - The parent sensorspublic Sensor sensor(String name, MetricConfig config, long inactiveSensorExpirationTimeSeconds, Sensor.RecordingLevel recordingLevel, Sensor... parents)
name - The name of the sensorconfig - A default configuration to use for this sensor for metrics that don't have their own configinactiveSensorExpirationTimeSeconds - If no value if recorded on the Sensor for this duration of time,
it is eligible for removalparents - The parent sensorsrecordingLevel - The recording level.public Sensor sensor(String name, MetricConfig config, long inactiveSensorExpirationTimeSeconds, Sensor... parents)
name - The name of the sensorconfig - A default configuration to use for this sensor for metrics that don't have their own configinactiveSensorExpirationTimeSeconds - If no value if recorded on the Sensor for this duration of time,
it is eligible for removalparents - The parent sensorspublic void removeSensor(String name)
name - The name of the sensor to be removedpublic void addMetric(MetricName metricName, Measurable measurable)
addMetric(MetricName, MetricValueProvider).metricName - The name of the metricmeasurable - The measurable that will be measured by this metricpublic void addMetric(MetricName metricName, MetricConfig config, Measurable measurable)
addMetric(MetricName, MetricConfig, MetricValueProvider).metricName - The name of the metricconfig - The configuration to use when measuring this measurablemeasurable - The measurable that will be measured by this metricpublic void addMetric(MetricName metricName, MetricConfig config, MetricValueProvider<?> metricValueProvider)
metricName - The name of the metricmetricValueProvider - The metric value provider associated with this metricpublic void addMetric(MetricName metricName, MetricValueProvider<?> metricValueProvider)
metricName - The name of the metricmetricValueProvider - The metric value provider associated with this metricpublic KafkaMetric removeMetric(MetricName metricName)
metricName - The name of the metricpublic void addReporter(MetricsReporter reporter)
public void removeReporter(MetricsReporter reporter)
public Map<MetricName,KafkaMetric> metrics()
public List<MetricsReporter> reporters()
public KafkaMetric metric(MetricName metricName)
public MetricName metricInstance(MetricNameTemplate template, String... keyValue)
public MetricName metricInstance(MetricNameTemplate template, Map<String,String> tags)
public void close()
close in interface Closeableclose in interface AutoCloseable