pub struct Metrics { /* private fields */ }Expand description
A registry of sensors and metrics.
A metrics registry is a global repository of metrics and sensors. Sensors
apply a sequence of values to associated metrics. This is the consumer-
relevant subset of Java’s Metrics; JMX, the sensor-expiry scheduler thread,
and quota machinery are omitted (the expiry mechanism is exposed via
Metrics::expire_sensors so the consumer/tests can drive it explicitly).
Implementations§
Source§impl Metrics
impl Metrics
Sourcepub fn new() -> Self
pub fn new() -> Self
Create a metrics repository with a default config, no reporters, and the
system clock. Mirrors Java’s Metrics() (Metrics.java:85).
Sourcepub fn with_default_config(default_config: Arc<MetricConfig>) -> Self
pub fn with_default_config(default_config: Arc<MetricConfig>) -> Self
Create a metrics repository with the supplied default config.
Mirrors Java’s Metrics(MetricConfig defaultConfig) (:111).
Create a MetricName with the given name, group, description and tags,
plus default tags specified in the metric configuration. Tags in tags
take precedence over default tags with the same key.
Mirrors Java’s
metricName(String name, String group, String description, Map<String, String> tags)
(:194).
Sourcepub fn metric_name_description(
&self,
name: impl Into<String>,
group: impl Into<String>,
description: impl Into<String>,
) -> MetricName
pub fn metric_name_description( &self, name: impl Into<String>, group: impl Into<String>, description: impl Into<String>, ) -> MetricName
Create a MetricName with the given name, group and description, plus the
default tags specified in the metric configuration.
Mirrors Java’s metricName(String name, String group, String description)
(:208).
Create a MetricName with the given name, group and tags, plus the default
tags specified in the metric configuration. Tags in tags take precedence
over default tags with the same key.
Mirrors Java’s
metricName(String name, String group, Map<String, String> tags) (:243).
Sourcepub fn metric_name(
&self,
name: impl Into<String>,
group: impl Into<String>,
) -> MetricName
pub fn metric_name( &self, name: impl Into<String>, group: impl Into<String>, ) -> MetricName
Create a MetricName with name and group (empty description, default tags).
Mirrors Java’s metricName(String name, String group) (:218), whose
parameters are the group’s intersection — hence the plain name.
Sourcepub fn metric_name_description_key_value(
&self,
name: impl Into<String>,
group: impl Into<String>,
description: impl Into<String>,
key_value: &[&str],
) -> Result<MetricName, Error>
pub fn metric_name_description_key_value( &self, name: impl Into<String>, group: impl Into<String>, description: impl Into<String>, key_value: &[&str], ) -> Result<MetricName, Error>
Create a MetricName from key, value tag pairs, plus default tags.
Returns an error if the pairs are not in twos (Java
IllegalArgumentException).
Mirrors Java’s
metricName(String name, String group, String description, String... keyValue)
(:231).
Sourcepub fn config(&self) -> &Arc<MetricConfig>
pub fn config(&self) -> &Arc<MetricConfig>
The default config of this registry.
Sourcepub fn get_sensor(&self, name: &str) -> Option<Arc<Sensor>>
pub fn get_sensor(&self, name: &str) -> Option<Arc<Sensor>>
Get the sensor with the given name if it exists.
Sourcepub fn sensor(&self, name: &str) -> Result<Arc<Sensor>, Error>
pub fn sensor(&self, name: &str) -> Result<Arc<Sensor>, Error>
Get or create a sensor with the given unique name and no parents at INFO
recording level. Mirrors Java’s sensor(String name) (:325).
Sourcepub fn sensor_recording_level(
&self,
name: &str,
recording_level: RecordingLevel,
) -> Result<Arc<Sensor>, Error>
pub fn sensor_recording_level( &self, name: &str, recording_level: RecordingLevel, ) -> Result<Arc<Sensor>, Error>
Get or create a sensor with the given name, recording level, and no parents.
Mirrors Java’s sensor(String name, Sensor.RecordingLevel recordingLevel) (:336).
Sourcepub fn sensor_parents(
&self,
name: &str,
parents: &[Arc<Sensor>],
) -> Result<Arc<Sensor>, Error>
pub fn sensor_parents( &self, name: &str, parents: &[Arc<Sensor>], ) -> Result<Arc<Sensor>, Error>
Get or create a sensor with parents at INFO recording level.
Mirrors Java’s sensor(String name, Sensor... parents) (:348).
Sourcepub fn sensor_recording_level_parents(
&self,
name: &str,
recording_level: RecordingLevel,
parents: &[Arc<Sensor>],
) -> Result<Arc<Sensor>, Error>
pub fn sensor_recording_level_parents( &self, name: &str, recording_level: RecordingLevel, parents: &[Arc<Sensor>], ) -> Result<Arc<Sensor>, Error>
Get or create a sensor with the given name, recording level and parents.
Mirrors Java’s
sensor(String name, Sensor.RecordingLevel recordingLevel, Sensor... parents)
(:360).
Sourcepub fn sensor_config_parents(
&self,
name: &str,
config: Option<Arc<MetricConfig>>,
parents: &[Arc<Sensor>],
) -> Result<Arc<Sensor>, Error>
pub fn sensor_config_parents( &self, name: &str, config: Option<Arc<MetricConfig>>, parents: &[Arc<Sensor>], ) -> Result<Arc<Sensor>, Error>
Get or create a sensor with a config and parents, at INFO recording level.
Mirrors Java’s sensor(String name, MetricConfig config, Sensor... parents)
(:372).
Sourcepub fn sensor_config_recording_level_parents(
&self,
name: &str,
config: Option<Arc<MetricConfig>>,
recording_level: RecordingLevel,
parents: &[Arc<Sensor>],
) -> Result<Arc<Sensor>, Error>
pub fn sensor_config_recording_level_parents( &self, name: &str, config: Option<Arc<MetricConfig>>, recording_level: RecordingLevel, parents: &[Arc<Sensor>], ) -> Result<Arc<Sensor>, Error>
Get or create a sensor with a config, recording level and parents.
Mirrors Java’s
sensor(String name, MetricConfig config, Sensor.RecordingLevel recordingLevel, Sensor... parents)
(:386).
Sourcepub fn sensor_config_inactive_sensor_expiration_time_seconds_parents(
&self,
name: &str,
config: Option<Arc<MetricConfig>>,
inactive_sensor_expiration_time_seconds: i64,
parents: &[Arc<Sensor>],
) -> Result<Arc<Sensor>, Error>
pub fn sensor_config_inactive_sensor_expiration_time_seconds_parents( &self, name: &str, config: Option<Arc<MetricConfig>>, inactive_sensor_expiration_time_seconds: i64, parents: &[Arc<Sensor>], ) -> Result<Arc<Sensor>, Error>
Get or create a sensor with a config, expiration and parents, at INFO
recording level. Mirrors Java’s
sensor(String name, MetricConfig config, long inactiveSensorExpirationTimeSeconds, Sensor... parents)
(:427).
Sourcepub fn sensor_options(
&self,
options: SensorOptions<'_>,
) -> Result<Arc<Sensor>, Error>
pub fn sensor_options( &self, options: SensorOptions<'_>, ) -> Result<Arc<Sensor>, Error>
Get or create a sensor with a config, expiration, recording level and parents.
Mirrors Java’s
sensor(String name, MetricConfig config, long inactiveSensorExpirationTimeSeconds, Sensor.RecordingLevel recordingLevel, Sensor... parents)
(:401). Every parameter, name included, is carried by
SensorOptions — see the note above this overload group.
Sourcepub fn remove_sensor(&self, name: &str)
pub fn remove_sensor(&self, name: &str)
Remove a sensor (if it exists), its associated metrics, and its children.
Sourcepub fn add_metric_measurable(
&self,
metric_name: MetricName,
measurable: Box<dyn Measurable>,
) -> Result<(), Error>
pub fn add_metric_measurable( &self, metric_name: MetricName, measurable: Box<dyn Measurable>, ) -> Result<(), Error>
Add a metric to monitor a measurable. This metric won’t be associated
with any sensor. Mirrors Java’s
addMetric(MetricName metricName, Measurable measurable) (:470).
Sourcepub fn add_gauge(
&self,
metric_name: MetricName,
gauge: Box<dyn Gauge>,
) -> Result<(), Error>
pub fn add_gauge( &self, metric_name: MetricName, gauge: Box<dyn Gauge>, ) -> Result<(), Error>
Add a metric backed by a gauge. This metric won’t be associated with any sensor.
Rust-only convenience — Java has no addGauge; callers there pass a
Gauge through addMetric(MetricName, MetricValueProvider).
Sourcepub fn add_metric_config_provider(
&self,
metric_name: MetricName,
config: Option<Arc<MetricConfig>>,
provider: MetricValueProvider,
) -> Result<(), Error>
pub fn add_metric_config_provider( &self, metric_name: MetricName, config: Option<Arc<MetricConfig>>, provider: MetricValueProvider, ) -> Result<(), Error>
Add a metric backed by a value provider with an optional config.
Mirrors Java’s
addMetric(MetricName metricName, MetricConfig config, MetricValueProvider<?> metricValueProvider)
(:498); config: None covers the (MetricName, MetricValueProvider)
form (:518), which Java implements by passing null.
Sourcepub fn add_metric_if_absent(
&self,
metric_name: MetricName,
config: Option<Arc<MetricConfig>>,
provider: MetricValueProvider,
) -> Arc<KafkaMetric>
pub fn add_metric_if_absent( &self, metric_name: MetricName, config: Option<Arc<MetricConfig>>, provider: MetricValueProvider, ) -> Arc<KafkaMetric>
Register a metric backed by a value provider only if it is not already
present. Returns the existing metric if one is already registered under
the same name, otherwise the newly registered metric. Translates Java’s
Metrics.addMetricIfAbsent(MetricName, MetricConfig, MetricValueProvider)
— idempotent registration (the consumer’s preferred-read-replica gauge
re-registers across assignment updates without error).
Sourcepub fn remove_metric(
&self,
metric_name: &MetricName,
) -> Option<Arc<KafkaMetric>>
pub fn remove_metric( &self, metric_name: &MetricName, ) -> Option<Arc<KafkaMetric>>
Remove a metric if it exists and return it. metric_removal is invoked
on each reporter when a metric is removed.
Sourcepub fn add_reporter(&self, reporter: Arc<dyn MetricsReporter>)
pub fn add_reporter(&self, reporter: Arc<dyn MetricsReporter>)
Add a metrics reporter, initializing it with all existing metrics.
Sourcepub fn metrics(&self) -> HashMap<MetricName, Arc<KafkaMetric>>
pub fn metrics(&self) -> HashMap<MetricName, Arc<KafkaMetric>>
Get all the metrics currently maintained, indexed by metric name.
Sourcepub fn metric(&self, metric_name: &MetricName) -> Option<Arc<KafkaMetric>>
pub fn metric(&self, metric_name: &MetricName) -> Option<Arc<KafkaMetric>>
Get a single metric by name.
Sourcepub fn metric_instance_key_value(
&self,
template: &MetricNameTemplate,
key_value: &[&str],
) -> Result<MetricName, Error>
pub fn metric_instance_key_value( &self, template: &MetricNameTemplate, key_value: &[&str], ) -> Result<MetricName, Error>
Create a MetricName from a template and tag pairs. Mirrors Java’s
metricInstance(MetricNameTemplate template, String... keyValue) (:651).
Create a MetricName from a template and a tags map. Mirrors Java’s
metricInstance(MetricNameTemplate template, Map<String, String> tags) (:655).
Sourcepub fn expire_sensors(&self)
pub fn expire_sensors(&self)
Iterate over every sensor and remove the ones that have expired.
This is the explicit equivalent of Java’s ExpireSensorTask /
metricsScheduler; the consumer (or tests) drives it instead of a
background thread.