1use std::collections::{BTreeMap, HashMap};
18use std::sync::{Arc, Mutex};
19
20use crate::common::metrics::internals::MetricsUtils;
21use crate::common::metrics::metrics_view::SharedMetricMap;
22use crate::common::metrics::{
23 KafkaMetric, Measurable, MetricConfig, MetricValueProvider, MetricsReporter, MetricsView, RecordingLevel, Sensor,
24 gauge,
25};
26use crate::common::utils::{SystemTime, Time};
27use crate::common::{Error, Metric, MetricName, MetricNameTemplate, MetricValue};
28
29pub(crate) struct MetricsShared {
37 metrics: SharedMetricMap,
38 reporters: Mutex<Vec<Arc<dyn MetricsReporter>>>,
39}
40
41impl MetricsShared {
42 fn new(reporters: Vec<Arc<dyn MetricsReporter>>) -> Self {
43 Self { metrics: SharedMetricMap::default(), reporters: Mutex::new(reporters) }
44 }
45
46 pub(crate) fn register_metric(&self, metric: Arc<KafkaMetric>) -> Option<Arc<KafkaMetric>> {
49 let metric_name = metric.metric_name().clone();
50 if let Some(existing) = self.metrics.insert_if_absent(metric_name, Arc::clone(&metric)) {
51 return Some(existing);
52 }
53 let reporters = self.reporters.lock().expect("reporters mutex poisoned");
57 for reporter in reporters.iter() {
58 reporter.metric_change(&metric);
59 }
60 None
61 }
62
63 fn remove_metric(&self, metric_name: &MetricName) -> Option<Arc<KafkaMetric>> {
65 let removed = self.metrics.remove(metric_name);
66 if let Some(metric) = &removed {
67 let reporters = self.reporters.lock().expect("reporters mutex poisoned");
68 for reporter in reporters.iter() {
69 reporter.metric_removal(metric);
70 }
71 }
72 removed
73 }
74}
75
76#[doc(alias = "org.apache.kafka.common.metrics.Metrics")]
90pub struct Metrics {
91 config: Arc<MetricConfig>,
92 shared: Arc<MetricsShared>,
93 sensors: Mutex<HashMap<String, Arc<Sensor>>>,
94 children_sensors: Mutex<HashMap<String, Vec<Arc<Sensor>>>>,
98 time: Arc<dyn Time>,
99}
100
101#[derive(Clone)]
120#[non_exhaustive]
121#[derive(Debug)]
122#[doc(alias = "rust-only")]
124pub struct SensorOptions<'a> {
125 pub(crate) name: &'a str,
127 pub(crate) config: Option<Arc<MetricConfig>>,
130 pub(crate) inactive_sensor_expiration_time_seconds: i64,
133 pub(crate) recording_level: RecordingLevel,
136 pub(crate) parents: &'a [Arc<Sensor>],
139}
140
141#[derive(Debug)]
147#[doc(alias = "rust-only")]
149pub struct SensorOptionsBuilder<'a> {
150 name: Option<&'a str>,
151 config: Option<Arc<MetricConfig>>,
152 inactive_sensor_expiration_time_seconds: i64,
153 recording_level: RecordingLevel,
154 parents: &'a [Arc<Sensor>],
155}
156
157impl<'a> Default for SensorOptionsBuilder<'a> {
158 fn default() -> Self {
159 Self::new()
160 }
161}
162
163impl<'a> SensorOptionsBuilder<'a> {
164 pub fn new() -> Self {
167 Self {
168 name: None,
169 config: None,
170 inactive_sensor_expiration_time_seconds: i64::MAX,
171 recording_level: RecordingLevel::Info,
172 parents: &[],
173 }
174 }
175
176 pub fn set_name(mut self, name: &'a str) -> Self {
187 self.name = Some(name);
188 self
189 }
190 pub fn set_config(mut self, config: Option<Arc<MetricConfig>>) -> Self {
200 self.config = config;
201 self
202 }
203 pub fn set_inactive_sensor_expiration_time_seconds(mut self, inactive_sensor_expiration_time_seconds: i64) -> Self {
213 self.inactive_sensor_expiration_time_seconds = inactive_sensor_expiration_time_seconds;
214 self
215 }
216 pub fn set_recording_level(mut self, recording_level: RecordingLevel) -> Self {
226 self.recording_level = recording_level;
227 self
228 }
229 pub fn set_parents(mut self, parents: &'a [Arc<Sensor>]) -> Self {
239 self.parents = parents;
240 self
241 }
242
243 pub fn build(self) -> Result<SensorOptions<'a>, Error> {
256 Ok(SensorOptions {
257 name: self.name.ok_or_else(|| Self::missing("name"))?,
258 config: self.config,
259 inactive_sensor_expiration_time_seconds: self.inactive_sensor_expiration_time_seconds,
260 recording_level: self.recording_level,
261 parents: self.parents,
262 })
263 }
264
265 fn missing(parameter: &str) -> Error {
268 Error::local_illegal_argument(format!(
269 "SensorOptionsBuilder::build: mandatory parameter `{parameter}` was not set"
270 ))
271 }
272}
273
274impl std::fmt::Debug for Metrics {
275 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
277 let sensors = self.sensors.lock().expect("sensors mutex poisoned").len();
278 let metrics = self.shared.metrics.len();
279 f.debug_struct("Metrics")
280 .field("sensors", &sensors)
281 .field("metrics", &metrics)
282 .finish_non_exhaustive()
283 }
284}
285
286impl Metrics {
287 #[doc(alias = "org.apache.kafka.common.metrics.Metrics#Metrics")]
308 pub(crate) fn with_default_config_time(default_config: Arc<MetricConfig>, time: Arc<dyn Time>) -> Self {
309 Self::with_default_config_reporters_time(default_config, Vec::new(), time)
310 }
311
312 #[doc(alias = "org.apache.kafka.common.metrics.Metrics#Metrics")]
315 pub fn new() -> Self {
316 Self::with_default_config_reporters_time(Arc::new(MetricConfig::new()), Vec::new(), Arc::new(SystemTime))
317 }
318
319 #[doc(alias = "org.apache.kafka.common.metrics.Metrics#Metrics")]
326 pub fn with_default_config(default_config: Arc<MetricConfig>) -> Self {
327 Self::with_default_config_reporters_time(default_config, Vec::new(), Arc::new(SystemTime))
328 }
329
330 #[doc(alias = "org.apache.kafka.common.metrics.Metrics#Metrics")]
332 #[cfg_attr(not(test), expect(dead_code))] pub(crate) fn with_time(time: Arc<dyn Time>) -> Self {
334 Self::with_default_config_reporters_time(Arc::new(MetricConfig::new()), Vec::new(), time)
335 }
336
337 #[doc(alias = "org.apache.kafka.common.metrics.Metrics#Metrics")]
339 pub(crate) fn with_default_config_reporters_time(
340 default_config: Arc<MetricConfig>,
341 reporters: Vec<Arc<dyn MetricsReporter>>,
342 time: Arc<dyn Time>,
343 ) -> Self {
344 for reporter in &reporters {
345 reporter.init(&[]);
346 }
347 let metrics = Metrics {
348 config: default_config,
349 shared: Arc::new(MetricsShared::new(reporters)),
350 sensors: Mutex::new(HashMap::new()),
351 children_sensors: Mutex::new(HashMap::new()),
352 time,
353 };
354 let shared = Arc::downgrade(&metrics.shared);
361 let count_gauge = gauge::new(move |_config, _now| {
362 let n = shared.upgrade().map_or(0, |shared| shared.metrics.len());
363 MetricValue::Double(n as f64)
364 });
365 metrics
366 .add_metric_with_config_metric_value_provider(
367 metrics.metric_name_with_description_tags(
368 "count",
369 "kafka-metrics-count",
370 "total number of registered metrics",
371 BTreeMap::new(),
372 ),
373 None,
374 Box::new(count_gauge),
375 )
376 .expect("count metric registration should not fail on a fresh registry");
377 metrics
378 }
379
380 #[doc(alias = "org.apache.kafka.common.metrics.Metrics#metricName")]
395 pub fn metric_name_with_description_tags(
396 &self,
397 name: impl Into<String>,
398 group: impl Into<String>,
399 description: impl Into<String>,
400 tags: BTreeMap<String, String>,
401 ) -> MetricName {
402 let mut combined = self.config.tags().clone();
403 combined.extend(tags);
404 MetricName::new(name, group, description, combined)
405 }
406
407 #[doc(alias = "org.apache.kafka.common.metrics.Metrics#metricName")]
416 pub fn metric_name_with_description(
417 &self,
418 name: impl Into<String>,
419 group: impl Into<String>,
420 description: impl Into<String>,
421 ) -> MetricName {
422 self.metric_name_with_description_tags(name, group, description, BTreeMap::new())
423 }
424
425 #[doc(alias = "org.apache.kafka.common.metrics.Metrics#metricName")]
435 pub fn metric_name_with_tags(
436 &self,
437 name: impl Into<String>,
438 group: impl Into<String>,
439 tags: BTreeMap<String, String>,
440 ) -> MetricName {
441 self.metric_name_with_description_tags(name, group, "", tags)
442 }
443
444 #[doc(alias = "org.apache.kafka.common.metrics.Metrics#metricName")]
451 pub fn metric_name(&self, name: impl Into<String>, group: impl Into<String>) -> MetricName {
452 self.metric_name_with_description_tags(name, group, "", BTreeMap::new())
453 }
454
455 #[doc(alias = "org.apache.kafka.common.metrics.Metrics#metricName")]
468 pub fn metric_name_with_description_key_value(
469 &self,
470 name: impl Into<String>,
471 group: impl Into<String>,
472 description: impl Into<String>,
473 key_value: &[&str],
474 ) -> Result<MetricName, Error> {
475 Ok(self.metric_name_with_description_tags(name, group, description, MetricsUtils::get_tags(key_value)?))
476 }
477
478 #[doc(alias = "org.apache.kafka.common.metrics.Metrics#config")]
480 pub fn config(&self) -> &Arc<MetricConfig> {
481 &self.config
482 }
483
484 #[doc(alias = "org.apache.kafka.common.metrics.Metrics#getSensor")]
494 pub fn get_sensor(&self, name: &str) -> Option<Arc<Sensor>> {
495 self.sensors.lock().expect("sensors mutex poisoned").get(name).cloned()
496 }
497
498 #[doc(alias = "org.apache.kafka.common.metrics.Metrics#sensor")]
525 pub fn sensor(&self, name: &str) -> Result<Arc<Sensor>, Error> {
526 self.sensor_with_options(SensorOptionsBuilder::new().set_name(name).build()?)
527 }
528
529 #[doc(alias = "org.apache.kafka.common.metrics.Metrics#sensor")]
545 pub fn sensor_with_recording_level(
546 &self,
547 name: &str,
548 recording_level: RecordingLevel,
549 ) -> Result<Arc<Sensor>, Error> {
550 self.sensor_with_options(
551 SensorOptionsBuilder::new()
552 .set_name(name)
553 .set_recording_level(recording_level)
554 .build()?,
555 )
556 }
557
558 #[doc(alias = "org.apache.kafka.common.metrics.Metrics#sensor")]
574 pub fn sensor_with_parents(&self, name: &str, parents: &[Arc<Sensor>]) -> Result<Arc<Sensor>, Error> {
575 self.sensor_with_options(SensorOptionsBuilder::new().set_name(name).set_parents(parents).build()?)
576 }
577
578 #[doc(alias = "org.apache.kafka.common.metrics.Metrics#sensor")]
595 pub fn sensor_with_recording_level_parents(
596 &self,
597 name: &str,
598 recording_level: RecordingLevel,
599 parents: &[Arc<Sensor>],
600 ) -> Result<Arc<Sensor>, Error> {
601 self.sensor_with_options(
602 SensorOptionsBuilder::new()
603 .set_name(name)
604 .set_recording_level(recording_level)
605 .set_parents(parents)
606 .build()?,
607 )
608 }
609
610 #[doc(alias = "org.apache.kafka.common.metrics.Metrics#sensor")]
628 pub fn sensor_with_config_parents(
629 &self,
630 name: &str,
631 config: Option<Arc<MetricConfig>>,
632 parents: &[Arc<Sensor>],
633 ) -> Result<Arc<Sensor>, Error> {
634 self.sensor_with_config_recording_level_parents(name, config, RecordingLevel::Info, parents)
635 }
636
637 #[doc(alias = "org.apache.kafka.common.metrics.Metrics#sensor")]
656 pub fn sensor_with_config_recording_level_parents(
657 &self,
658 name: &str,
659 config: Option<Arc<MetricConfig>>,
660 recording_level: RecordingLevel,
661 parents: &[Arc<Sensor>],
662 ) -> Result<Arc<Sensor>, Error> {
663 self.sensor_with_options(
664 SensorOptionsBuilder::new()
665 .set_name(name)
666 .set_config(config)
667 .set_recording_level(recording_level)
668 .set_parents(parents)
669 .build()?,
670 )
671 }
672
673 #[doc(alias = "org.apache.kafka.common.metrics.Metrics#sensor")]
694 pub fn sensor_with_config_inactive_sensor_expiration_time_seconds_parents(
695 &self,
696 name: &str,
697 config: Option<Arc<MetricConfig>>,
698 inactive_sensor_expiration_time_seconds: i64,
699 parents: &[Arc<Sensor>],
700 ) -> Result<Arc<Sensor>, Error> {
701 self.sensor_with_options(
702 SensorOptionsBuilder::new()
703 .set_name(name)
704 .set_config(config)
705 .set_inactive_sensor_expiration_time_seconds(inactive_sensor_expiration_time_seconds)
706 .set_parents(parents)
707 .build()?,
708 )
709 }
710
711 #[doc(alias = "org.apache.kafka.common.metrics.Metrics#sensor")]
726 pub fn sensor_with_options(&self, options: SensorOptions<'_>) -> Result<Arc<Sensor>, Error> {
727 let SensorOptions { name, config, inactive_sensor_expiration_time_seconds, recording_level, parents } = options;
728 if let Some(existing) = self.get_sensor(name) {
743 return Ok(existing);
744 }
745 let mut sensors = self.sensors.lock().expect("sensors mutex poisoned");
746 if let Some(existing) = sensors.get(name) {
747 return Ok(Arc::clone(existing));
748 }
749 let sensor_config = config.unwrap_or_else(|| Arc::clone(&self.config));
750 let sensor = Arc::new(Sensor::new(
751 Some(Arc::downgrade(&self.shared)),
752 name,
753 parents.to_vec(),
754 sensor_config,
755 Arc::clone(&self.time),
756 inactive_sensor_expiration_time_seconds,
757 recording_level,
758 )?);
759 sensors.insert(name.to_string(), Arc::clone(&sensor));
760 {
761 let mut children = self.children_sensors.lock().expect("children sensors mutex poisoned");
762 for parent in parents {
763 children.entry(parent.name().to_string()).or_default().push(Arc::clone(&sensor));
764 }
765 }
766 drop(sensors);
767 Ok(sensor)
768 }
769
770 #[doc(alias = "org.apache.kafka.common.metrics.Metrics#removeSensor")]
776 pub fn remove_sensor(&self, name: &str) {
777 let sensor = self.get_sensor(name);
778 let Some(sensor) = sensor else {
779 return;
780 };
781 let mut child_sensors: Option<Vec<Arc<Sensor>>> = None;
782 {
783 let removed = {
784 let mut sensors = self.sensors.lock().expect("sensors mutex poisoned");
785 if sensors.get(name).map(Arc::as_ptr) == Some(Arc::as_ptr(&sensor)) {
787 sensors.remove(name)
788 } else {
789 None
790 }
791 };
792 if removed.is_some() {
793 for metric in sensor.metrics() {
794 self.shared.remove_metric(metric.metric_name());
795 }
796 let mut children = self.children_sensors.lock().expect("children sensors mutex poisoned");
797 child_sensors = children.remove(name);
798 for parent in sensor.parents() {
799 if let Some(siblings) = children.get_mut(parent.name()) {
800 siblings.retain(|s| !Arc::ptr_eq(s, &sensor));
801 }
802 }
803 }
804 }
805 if let Some(children) = child_sensors {
806 for child in children {
807 self.remove_sensor(child.name());
808 }
809 }
810 }
811
812 #[doc(alias = "org.apache.kafka.common.metrics.Metrics#addMetric")]
834 pub fn add_metric_with_measurable(
835 &self,
836 metric_name: MetricName,
837 measurable: Box<dyn Measurable>,
838 ) -> Result<(), Error> {
839 self.add_metric_with_config_metric_value_provider(metric_name, None, Box::new(measurable))
840 }
841
842 #[doc(alias = "org.apache.kafka.common.metrics.Metrics#addMetric")]
856 pub fn add_metric_with_metric_value_provider(
857 &self,
858 metric_name: MetricName,
859 metric_value_provider: Box<dyn MetricValueProvider>,
860 ) -> Result<(), Error> {
861 self.add_metric_with_config_metric_value_provider(metric_name, None, metric_value_provider)
862 }
863
864 #[doc(alias = "org.apache.kafka.common.metrics.Metrics#addMetric")]
880 pub fn add_metric_with_config_metric_value_provider(
881 &self,
882 metric_name: MetricName,
883 config: Option<Arc<MetricConfig>>,
884 provider: Box<dyn MetricValueProvider>,
885 ) -> Result<(), Error> {
886 let metric_config = config.unwrap_or_else(|| Arc::clone(&self.config));
887 let metric = Arc::new(KafkaMetric::new(
888 metric_name.clone(),
889 provider,
890 metric_config,
891 Arc::clone(&self.time),
892 ));
893 if self.shared.register_metric(metric).is_some() {
894 return Err(Error::local_illegal_argument(format!(
895 "A metric named '{metric_name}' already exists, can't register another one."
896 )));
897 }
898 Ok(())
899 }
900
901 #[doc(alias = "org.apache.kafka.common.metrics.Metrics#addMetricIfAbsent")]
918 pub fn add_metric_if_absent(
919 &self,
920 metric_name: MetricName,
921 config: Option<Arc<MetricConfig>>,
922 provider: Box<dyn MetricValueProvider>,
923 ) -> Arc<KafkaMetric> {
924 let metric_config = config.unwrap_or_else(|| Arc::clone(&self.config));
925 let metric = Arc::new(KafkaMetric::new(metric_name, provider, metric_config, Arc::clone(&self.time)));
926 match self.shared.register_metric(Arc::clone(&metric)) {
927 Some(existing) => existing,
928 None => metric,
929 }
930 }
931
932 #[doc(alias = "org.apache.kafka.common.metrics.Metrics#removeMetric")]
943 pub fn remove_metric(&self, metric_name: &MetricName) -> Option<Arc<KafkaMetric>> {
944 self.shared.remove_metric(metric_name)
945 }
946
947 #[doc(alias = "org.apache.kafka.common.metrics.Metrics#addReporter")]
953 pub fn add_reporter(&self, reporter: Arc<dyn MetricsReporter>) {
954 let existing: Vec<Arc<KafkaMetric>> = self.shared.metrics.current().values().cloned().collect();
955 reporter.init(&existing);
956 self.shared.reporters.lock().expect("reporters mutex poisoned").push(reporter);
957 }
958
959 #[doc(alias = "org.apache.kafka.common.metrics.Metrics#metrics")]
966 pub fn metrics(&self) -> MetricsView {
967 MetricsView::of(self.shared.metrics.clone())
968 }
969
970 #[doc(alias = "org.apache.kafka.common.metrics.Metrics#metric")]
976 pub fn metric(&self, metric_name: &MetricName) -> Option<Arc<KafkaMetric>> {
977 self.shared.metrics.get(metric_name)
978 }
979
980 #[cfg(test)]
983 #[doc(alias = "org.apache.kafka.common.metrics.Metrics#childrenSensors")]
984 pub(crate) fn children_sensors(&self, parent: &Arc<Sensor>) -> Option<Vec<Arc<Sensor>>> {
985 self.children_sensors
986 .lock()
987 .expect("children sensors mutex poisoned")
988 .get(parent.name())
989 .cloned()
990 }
991
992 #[doc(alias = "org.apache.kafka.common.metrics.Metrics#metricInstance")]
1008 pub fn metric_instance_with_key_value(
1009 &self,
1010 template: &MetricNameTemplate,
1011 key_value: &[&str],
1012 ) -> Result<MetricName, Error> {
1013 self.metric_instance_with_tags(template, MetricsUtils::get_tags(key_value)?)
1014 }
1015
1016 #[doc(alias = "org.apache.kafka.common.metrics.Metrics#metricInstance")]
1028 pub fn metric_instance_with_tags(
1029 &self,
1030 template: &MetricNameTemplate,
1031 tags: BTreeMap<String, String>,
1032 ) -> Result<MetricName, Error> {
1033 let mut runtime_tag_keys: std::collections::HashSet<String> = tags.keys().cloned().collect();
1035 runtime_tag_keys.extend(self.config.tags().keys().cloned());
1036 let template_tag_keys: std::collections::HashSet<String> = template.tags().iter().cloned().collect();
1037 if runtime_tag_keys != template_tag_keys {
1038 return Err(Error::local_illegal_argument(format!(
1039 "For '{}', runtime-defined metric tags do not match the tags in the template. \
1040 Runtime = {runtime_tag_keys:?} Template = {template_tag_keys:?}",
1041 template.name()
1042 )));
1043 }
1044 Ok(self.metric_name_with_description_tags(template.name(), template.group(), template.description(), tags))
1045 }
1046
1047 #[cfg(test)]
1053 pub(crate) fn expire_sensors(&self) {
1054 let names: Vec<String> = self.sensors.lock().expect("sensors mutex poisoned").keys().cloned().collect();
1055 for name in names {
1056 let expired = self.get_sensor(&name).map(|s| s.has_expired()).unwrap_or(false);
1057 if expired {
1058 self.remove_sensor(&name);
1059 }
1060 }
1061 }
1062
1063 #[doc(alias = "org.apache.kafka.common.metrics.Metrics#close")]
1065 pub fn close(&self) {
1066 let reporters = self.shared.reporters.lock().expect("reporters mutex poisoned");
1067 for reporter in reporters.iter() {
1068 reporter.close();
1069 }
1070 }
1071}
1072
1073impl Default for Metrics {
1074 fn default() -> Self {
1075 Self::new()
1076 }
1077}
1078
1079#[cfg(test)]
1080mod tests {
1081 use super::*;
1082 use crate::common::MetricValue;
1083 use crate::common::metrics::stats::{CumulativeCount, CumulativeSum, Value};
1084 use crate::common::utils::MockTime;
1085
1086 const EPS: f64 = 0.000001;
1088
1089 fn metrics_with_mock() -> (Metrics, Arc<MockTime>) {
1090 let time = Arc::new(MockTime::new());
1091 let metrics = Metrics::with_default_config_reporters_time(
1092 Arc::new(MetricConfig::new()),
1093 Vec::new(),
1094 Arc::clone(&time) as Arc<dyn Time>,
1095 );
1096 (metrics, time)
1097 }
1098
1099 fn double_value(metric: &Arc<KafkaMetric>) -> f64 {
1100 match metric.metric_value() {
1101 MetricValue::Double(v) => v,
1102 other => panic!("expected double metric value, got {other:?}"),
1103 }
1104 }
1105
1106 #[test]
1108 #[doc(alias = "org.apache.kafka.common.metrics.MetricsTest#testMetricName")]
1109 fn test_metric_name() {
1110 let metrics = Metrics::new();
1111 let n1 = metrics
1112 .metric_name_with_description_key_value(
1113 "name",
1114 "group",
1115 "description",
1116 &["key1", "value1", "key2", "value2"],
1117 )
1118 .unwrap();
1119 let mut tags = BTreeMap::new();
1120 tags.insert("key1".to_string(), "value1".to_string());
1121 tags.insert("key2".to_string(), "value2".to_string());
1122 let n2 = metrics.metric_name_with_description_tags("name", "group", "description", tags);
1123 assert_eq!(n1, n2, "metric names created in two different ways should be equal");
1124
1125 let err = metrics
1127 .metric_name_with_description_key_value("name", "group", "description", &["key1"])
1128 .unwrap_err();
1129 assert!(err.to_string().contains("keyValue needs to be specified in pairs"));
1130 }
1131
1132 #[test]
1144 #[doc(alias = "org.apache.kafka.common.metrics.MetricsTest#testMetricInstances")]
1145 fn test_metric_instances() {
1146 use indexmap::IndexSet;
1147
1148 let metrics = Metrics::new();
1149
1150 let mut key_tags = IndexSet::new();
1151 key_tags.insert("key1".to_string());
1152 key_tags.insert("key2".to_string());
1153 let metric1 =
1154 MetricNameTemplate::new("name", "group", "The first metric used in testMetricName()", key_tags.clone());
1155 let metric2 = MetricNameTemplate::new("name", "group", "The second metric used in testMetricName()", key_tags);
1156
1157 let n1 = metrics
1159 .metric_instance_with_key_value(&metric1, &["key1", "value1", "key2", "value2"])
1160 .expect("metric_instance from key/value pairs");
1161 let mut tags = BTreeMap::new();
1162 tags.insert("key1".to_string(), "value1".to_string());
1163 tags.insert("key2".to_string(), "value2".to_string());
1164 let n2 = metrics
1165 .metric_instance_with_tags(&metric2, tags)
1166 .expect("metric_instance from tags map");
1167 assert_eq!(n1, n2, "metric names created in two different ways should be equal");
1168
1169 let err = metrics
1171 .metric_instance_with_key_value(&metric1, &["key1"])
1172 .expect_err("odd number of keyValue should fail");
1173 assert!(
1174 err.to_string().contains("keyValue needs to be specified in pairs"),
1175 "unexpected message: {err}"
1176 );
1177
1178 let mut parent_tags = BTreeMap::new();
1181 parent_tags.insert("parent-tag".to_string(), "parent-tag-value".to_string());
1182 let mut child_tags = BTreeMap::new();
1183 child_tags.insert("child-tag".to_string(), "child-tag-value".to_string());
1184
1185 let inherited = Metrics::with_default_config(Arc::new(MetricConfig::new().set_tags(parent_tags.clone())));
1186 let mut inherited_tag_names = IndexSet::new();
1187 inherited_tag_names.insert("parent-tag".to_string());
1188 inherited_tag_names.insert("child-tag".to_string());
1189 let metric_with_inherited_tags =
1190 MetricNameTemplate::new("name", "group", "inherited-tags metric", inherited_tag_names);
1191
1192 let inherited_metric = inherited
1193 .metric_instance_with_tags(&metric_with_inherited_tags, child_tags)
1194 .expect("metric_instance with inherited parent tag");
1195 let filled_out_tags = inherited_metric.tags();
1196 assert_eq!(
1197 Some(&"parent-tag-value".to_string()),
1198 filled_out_tags.get("parent-tag"),
1199 "parent-tag should be set properly"
1200 );
1201 assert_eq!(
1202 Some(&"child-tag-value".to_string()),
1203 filled_out_tags.get("child-tag"),
1204 "child-tag should be set properly"
1205 );
1206
1207 let err = inherited
1209 .metric_instance_with_tags(&metric_with_inherited_tags, parent_tags)
1210 .expect_err("child metric tags not defined at runtime should fail");
1211 assert!(
1212 err.to_string().contains("do not match the tags in the template"),
1213 "unexpected message: {err}"
1214 );
1215
1216 let mut runtime_tags = BTreeMap::new();
1218 runtime_tags.insert("child-tag".to_string(), "child-tag-value".to_string());
1219 runtime_tags.insert("tag-not-in-template".to_string(), "unexpected-value".to_string());
1220 let err = inherited
1221 .metric_instance_with_tags(&metric_with_inherited_tags, runtime_tags)
1222 .expect_err("runtime tag not in template should fail");
1223 assert!(
1224 err.to_string().contains("do not match the tags in the template"),
1225 "unexpected message: {err}"
1226 );
1227 }
1228
1229 #[test]
1231 fn test_idempotent_add() {
1232 let metrics = Metrics::new();
1233 let sensor = metrics.sensor("sensor").unwrap();
1234
1235 assert!(
1236 sensor
1237 .add_with_metric_name(metrics.metric_name("test-metric", "test-group"), Box::new(Value::new()))
1238 .unwrap()
1239 );
1240
1241 assert!(
1243 sensor
1244 .add_with_metric_name(metrics.metric_name("test-metric", "test-group"), Box::new(Value::new()))
1245 .unwrap()
1246 );
1247
1248 let another = metrics.sensor("another-sensor").unwrap();
1250 let err = another
1251 .add_with_metric_name(metrics.metric_name("test-metric", "test-group"), Box::new(Value::new()))
1252 .unwrap_err();
1253 assert!(err.to_string().contains("already exists"));
1254
1255 assert!(
1257 sensor
1258 .add_with_metric_name(metrics.metric_name("test-metric", "test-group"), Box::new(CumulativeSum::new()))
1259 .unwrap()
1260 );
1261
1262 assert_eq!(sensor.metrics().len(), 1);
1264 }
1265
1266 #[test]
1268 #[doc(alias = "org.apache.kafka.common.metrics.MetricsTest#testHierarchicalSensors")]
1269 fn test_hierarchical_sensors() {
1270 let metrics = Metrics::new();
1271 let parent1 = metrics.sensor("test.parent1").unwrap();
1272 parent1
1273 .add_with_metric_name(
1274 metrics.metric_name("test.parent1.count", "grp1"),
1275 Box::new(CumulativeCount::new()),
1276 )
1277 .unwrap();
1278 let parent2 = metrics.sensor("test.parent2").unwrap();
1279 parent2
1280 .add_with_metric_name(
1281 metrics.metric_name("test.parent2.count", "grp1"),
1282 Box::new(CumulativeCount::new()),
1283 )
1284 .unwrap();
1285 let child1 = metrics
1286 .sensor_with_parents("test.child1", &[Arc::clone(&parent1), Arc::clone(&parent2)])
1287 .unwrap();
1288 child1
1289 .add_with_metric_name(
1290 metrics.metric_name("test.child1.count", "grp1"),
1291 Box::new(CumulativeCount::new()),
1292 )
1293 .unwrap();
1294 let child2 = metrics.sensor_with_parents("test.child2", &[Arc::clone(&parent1)]).unwrap();
1295 child2
1296 .add_with_metric_name(
1297 metrics.metric_name("test.child2.count", "grp1"),
1298 Box::new(CumulativeCount::new()),
1299 )
1300 .unwrap();
1301 let grandchild = metrics.sensor_with_parents("test.grandchild", &[Arc::clone(&child1)]).unwrap();
1302 grandchild
1303 .add_with_metric_name(
1304 metrics.metric_name("test.grandchild.count", "grp1"),
1305 Box::new(CumulativeCount::new()),
1306 )
1307 .unwrap();
1308
1309 parent1.record().unwrap();
1311 parent2.record().unwrap();
1312 child1.record().unwrap();
1313 child2.record().unwrap();
1314 grandchild.record().unwrap();
1315
1316 let p1 = double_value(&parent1.metrics()[0]);
1317 let p2 = double_value(&parent2.metrics()[0]);
1318 let c1 = double_value(&child1.metrics()[0]);
1319 let c2 = double_value(&child2.metrics()[0]);
1320 let gc = double_value(&grandchild.metrics()[0]);
1321
1322 assert_eq!(1.0, gc);
1324 assert_eq!(1.0 + gc, c1);
1325 assert_eq!(1.0, c2);
1326 assert_eq!(1.0 + c1, p2);
1327 assert_eq!(1.0 + c1 + c2, p1);
1328
1329 let p1_children = metrics.children_sensors(&parent1).unwrap();
1330 assert_eq!(p1_children.len(), 2);
1331 assert!(p1_children.iter().any(|s| Arc::ptr_eq(s, &child1)));
1332 assert!(p1_children.iter().any(|s| Arc::ptr_eq(s, &child2)));
1333 let p2_children = metrics.children_sensors(&parent2).unwrap();
1334 assert_eq!(p2_children.len(), 1);
1335 assert!(Arc::ptr_eq(&p2_children[0], &child1));
1336 assert!(metrics.children_sensors(&grandchild).is_none());
1337 }
1338
1339 #[test]
1341 #[doc(alias = "org.apache.kafka.common.metrics.MetricsTest#testBadSensorHierarchy")]
1342 fn test_bad_sensor_hierarchy() {
1343 let metrics = Metrics::new();
1344 let p = metrics.sensor("parent").unwrap();
1345 let c1 = metrics.sensor_with_parents("child1", &[Arc::clone(&p)]).unwrap();
1346 let c2 = metrics.sensor_with_parents("child2", &[Arc::clone(&p)]).unwrap();
1347 match metrics.sensor_with_parents("gc", &[c1, c2]) {
1348 Ok(_) => panic!("expected circular dependency error"),
1349 Err(err) => assert!(err.to_string().contains("Circular dependency")),
1350 }
1351 }
1352
1353 #[test]
1355 #[doc(alias = "org.apache.kafka.common.metrics.MetricsTest#testRemoveChildSensor")]
1356 fn test_remove_child_sensor() {
1357 let metrics = Metrics::new();
1358 let parent = metrics.sensor("parent").unwrap();
1359 let child = metrics.sensor_with_parents("child", &[Arc::clone(&parent)]).unwrap();
1360
1361 let children = metrics.children_sensors(&parent).unwrap();
1362 assert_eq!(children.len(), 1);
1363 assert!(Arc::ptr_eq(&children[0], &child));
1364
1365 metrics.remove_sensor("child");
1366
1367 assert!(metrics.children_sensors(&parent).unwrap().is_empty());
1368 }
1369
1370 #[test]
1372 #[doc(alias = "org.apache.kafka.common.metrics.MetricsTest#testRemoveSensor")]
1373 fn test_remove_sensor() {
1374 let metrics = Metrics::new();
1375 let size = metrics.metrics().len();
1376 let parent1 = metrics.sensor("test.parent1").unwrap();
1377 parent1
1378 .add_with_metric_name(
1379 metrics.metric_name("test.parent1.count", "grp1"),
1380 Box::new(CumulativeCount::new()),
1381 )
1382 .unwrap();
1383 let parent2 = metrics.sensor("test.parent2").unwrap();
1384 parent2
1385 .add_with_metric_name(
1386 metrics.metric_name("test.parent2.count", "grp1"),
1387 Box::new(CumulativeCount::new()),
1388 )
1389 .unwrap();
1390 let child1 = metrics
1391 .sensor_with_parents("test.child1", &[Arc::clone(&parent1), Arc::clone(&parent2)])
1392 .unwrap();
1393 child1
1394 .add_with_metric_name(
1395 metrics.metric_name("test.child1.count", "grp1"),
1396 Box::new(CumulativeCount::new()),
1397 )
1398 .unwrap();
1399 let child2 = metrics.sensor_with_parents("test.child2", &[Arc::clone(&parent2)]).unwrap();
1400 child2
1401 .add_with_metric_name(
1402 metrics.metric_name("test.child2.count", "grp1"),
1403 Box::new(CumulativeCount::new()),
1404 )
1405 .unwrap();
1406 let gchild1 = metrics.sensor_with_parents("test.gchild2", &[Arc::clone(&child2)]).unwrap();
1407 gchild1
1408 .add_with_metric_name(
1409 metrics.metric_name("test.gchild2.count", "grp1"),
1410 Box::new(CumulativeCount::new()),
1411 )
1412 .unwrap();
1413
1414 assert!(metrics.get_sensor("test.parent1").is_some());
1415 metrics.remove_sensor("test.parent1");
1416 assert!(metrics.get_sensor("test.parent1").is_none());
1417 assert!(metrics.metric(&metrics.metric_name("test.parent1.count", "grp1")).is_none());
1418 assert!(metrics.get_sensor("test.child1").is_none());
1420 assert!(metrics.children_sensors(&parent1).is_none());
1421 assert!(metrics.metric(&metrics.metric_name("test.child1.count", "grp1")).is_none());
1422
1423 assert!(metrics.get_sensor("test.gchild2").is_some());
1424 metrics.remove_sensor("test.gchild2");
1425 assert!(metrics.get_sensor("test.gchild2").is_none());
1426 assert!(metrics.children_sensors(&gchild1).is_none());
1427 assert!(metrics.metric(&metrics.metric_name("test.gchild2.count", "grp1")).is_none());
1428
1429 assert!(metrics.get_sensor("test.child2").is_some());
1430 metrics.remove_sensor("test.child2");
1431 assert!(metrics.get_sensor("test.child2").is_none());
1432 assert!(metrics.children_sensors(&child2).is_none());
1433 assert!(metrics.metric(&metrics.metric_name("test.child2.count", "grp1")).is_none());
1434
1435 assert!(metrics.get_sensor("test.parent2").is_some());
1436 metrics.remove_sensor("test.parent2");
1437 assert!(metrics.get_sensor("test.parent2").is_none());
1438 assert!(metrics.children_sensors(&parent2).is_none());
1439 assert!(metrics.metric(&metrics.metric_name("test.parent2.count", "grp1")).is_none());
1440
1441 assert_eq!(size, metrics.metrics().len());
1442 }
1443
1444 #[test]
1446 #[doc(alias = "org.apache.kafka.common.metrics.MetricsTest#testRemoveMetric")]
1447 fn test_remove_metric() {
1448 let metrics = Metrics::new();
1449 let size = metrics.metrics().len();
1450 metrics
1451 .add_metric_with_measurable(metrics.metric_name("test1", "grp1"), Box::new(CumulativeCount::new()))
1452 .unwrap();
1453 metrics
1454 .add_metric_with_measurable(metrics.metric_name("test2", "grp1"), Box::new(CumulativeCount::new()))
1455 .unwrap();
1456
1457 assert!(metrics.remove_metric(&metrics.metric_name("test1", "grp1")).is_some());
1458 assert!(metrics.metric(&metrics.metric_name("test1", "grp1")).is_none());
1459 assert!(metrics.metric(&metrics.metric_name("test2", "grp1")).is_some());
1460
1461 assert!(metrics.remove_metric(&metrics.metric_name("test2", "grp1")).is_some());
1462 assert!(metrics.metric(&metrics.metric_name("test2", "grp1")).is_none());
1463
1464 assert_eq!(size, metrics.metrics().len());
1465 }
1466
1467 #[test]
1469 #[doc(alias = "org.apache.kafka.common.metrics.MetricsTest#testDuplicateMetricName")]
1470 fn test_duplicate_metric_name() {
1471 let metrics = Metrics::new();
1472 metrics
1473 .sensor("test")
1474 .unwrap()
1475 .add_with_metric_name(metrics.metric_name("test", "grp1"), Box::new(Value::new()))
1476 .unwrap();
1477 let err = metrics
1478 .sensor("test2")
1479 .unwrap()
1480 .add_with_metric_name(metrics.metric_name("test", "grp1"), Box::new(CumulativeSum::new()))
1481 .unwrap_err();
1482 assert!(err.to_string().contains("already exists"));
1483 }
1484
1485 #[test]
1487 #[doc(alias = "org.apache.kafka.common.metrics.MetricsTest#testRemoveInactiveMetrics")]
1488 fn test_remove_inactive_metrics() {
1489 let (metrics, time) = metrics_with_mock();
1490
1491 let s1 = metrics
1492 .sensor_with_options(
1493 SensorOptionsBuilder::new()
1494 .set_name("test.s1")
1495 .set_inactive_sensor_expiration_time_seconds(1)
1496 .build()
1497 .unwrap(),
1498 )
1499 .unwrap();
1500 s1.add_with_metric_name(metrics.metric_name("test.s1.count", "grp1"), Box::new(CumulativeCount::new()))
1501 .unwrap();
1502
1503 let s2 = metrics
1504 .sensor_with_options(
1505 SensorOptionsBuilder::new()
1506 .set_name("test.s2")
1507 .set_inactive_sensor_expiration_time_seconds(3)
1508 .build()
1509 .unwrap(),
1510 )
1511 .unwrap();
1512 s2.add_with_metric_name(metrics.metric_name("test.s2.count", "grp1"), Box::new(CumulativeCount::new()))
1513 .unwrap();
1514
1515 metrics.expire_sensors();
1516 assert!(metrics.get_sensor("test.s1").is_some(), "Sensor test.s1 must be present");
1517 assert!(metrics.metric(&metrics.metric_name("test.s1.count", "grp1")).is_some());
1518 assert!(metrics.get_sensor("test.s2").is_some(), "Sensor test.s2 must be present");
1519 assert!(metrics.metric(&metrics.metric_name("test.s2.count", "grp1")).is_some());
1520
1521 time.sleep(1001);
1522 metrics.expire_sensors();
1523 assert!(
1524 metrics.get_sensor("test.s1").is_none(),
1525 "Sensor test.s1 should have been purged"
1526 );
1527 assert!(metrics.metric(&metrics.metric_name("test.s1.count", "grp1")).is_none());
1528 assert!(metrics.get_sensor("test.s2").is_some(), "Sensor test.s2 must be present");
1529 assert!(metrics.metric(&metrics.metric_name("test.s2.count", "grp1")).is_some());
1530
1531 s2.record().unwrap();
1533 time.sleep(2000);
1534 metrics.expire_sensors();
1535 assert!(metrics.get_sensor("test.s2").is_some(), "Sensor test.s2 must be present");
1536 assert!(metrics.metric(&metrics.metric_name("test.s2.count", "grp1")).is_some());
1537
1538 time.sleep(1001);
1540 metrics.expire_sensors();
1541 assert!(
1542 metrics.get_sensor("test.s2").is_none(),
1543 "Sensor test.s2 should have been purged"
1544 );
1545 assert!(metrics.metric(&metrics.metric_name("test.s2.count", "grp1")).is_none());
1546
1547 let s1 = metrics
1549 .sensor_with_options(
1550 SensorOptionsBuilder::new()
1551 .set_name("test.s1")
1552 .set_inactive_sensor_expiration_time_seconds(1)
1553 .build()
1554 .unwrap(),
1555 )
1556 .unwrap();
1557 s1.add_with_metric_name(metrics.metric_name("test.s1.count", "grp1"), Box::new(CumulativeCount::new()))
1558 .unwrap();
1559 assert!(metrics.get_sensor("test.s1").is_some(), "Sensor test.s1 must be present");
1560 assert!(metrics.metric(&metrics.metric_name("test.s1.count", "grp1")).is_some());
1561 }
1562
1563 #[test]
1565 #[doc(alias = "org.apache.kafka.common.metrics.MetricsTest#testQuotas")]
1566 fn test_quotas() {
1567 use crate::common::metrics::Quota;
1568 let metrics = Metrics::new();
1569 let sensor = metrics.sensor("test").unwrap();
1570 sensor
1571 .add_with_metric_name_config(
1572 metrics.metric_name("test1.total", "grp1"),
1573 Box::new(CumulativeSum::new()),
1574 Some(Arc::new(MetricConfig::new().set_quota(Quota::upper_bound(5.0)))),
1575 )
1576 .unwrap();
1577 sensor
1578 .add_with_metric_name_config(
1579 metrics.metric_name("test2.total", "grp1"),
1580 Box::new(CumulativeSum::new()),
1581 Some(Arc::new(MetricConfig::new().set_quota(Quota::lower_bound(0.0)))),
1582 )
1583 .unwrap();
1584 sensor.record_with_value(5.0).unwrap();
1585 let error = sensor
1586 .record_with_value(1.0)
1587 .expect_err("Should have gotten a quota violation.");
1588 assert!(error.is_quota_violation_error());
1589 assert_eq!(
1590 error.to_string(),
1591 format!(
1592 "QuotaViolationError: '{}' violated quota. Actual: 6.0, Threshold: 5.0",
1593 metrics.metric_name("test1.total", "grp1")
1594 )
1595 );
1596 assert!(
1597 (6.0 - double_value(&metrics.metric(&metrics.metric_name("test1.total", "grp1")).unwrap())).abs() < EPS
1598 );
1599 sensor.record_with_value(-6.0).unwrap();
1600 let error = sensor
1601 .record_with_value(-1.0)
1602 .expect_err("Should have gotten a quota violation.");
1603 assert!(error.is_quota_violation_error());
1604 }
1605
1606 #[test]
1608 fn test_simple_stats_cumulative() {
1609 let metrics = Metrics::new();
1610 let s2 = metrics.sensor("test.sensor2").unwrap();
1611 s2.add_with_metric_name(metrics.metric_name("s2.total", "grp1"), Box::new(CumulativeSum::new()))
1612 .unwrap();
1613 s2.record_with_value(5.0).unwrap();
1614 assert_eq!(
1615 5.0,
1616 double_value(&metrics.metric(&metrics.metric_name("s2.total", "grp1")).unwrap()),
1617 "s2 reflects the constant value"
1618 );
1619
1620 let s = metrics.sensor("test.sensor").unwrap();
1622 s.add_with_metric_name(metrics.metric_name("test.count", "grp1"), Box::new(CumulativeCount::new()))
1623 .unwrap();
1624 for i in 0..10 {
1625 s.record_with_value(i as f64).unwrap();
1626 }
1627 assert_eq!(
1628 10.0,
1629 double_value(&metrics.metric(&metrics.metric_name("test.count", "grp1")).unwrap()),
1630 "Count(0...9) = 10"
1631 );
1632 }
1633
1634 #[test]
1640 #[doc(alias = "org.apache.kafka.common.metrics.MetricsTest#testSimpleStats")]
1641 fn test_simple_stats() {
1642 use crate::common::metrics::TimeUnit;
1643 use crate::common::metrics::stats::{Avg, Max, Meter, Min, WindowedCount};
1644
1645 let (metrics, time) = metrics_with_mock();
1646 let config = metrics.config().clone();
1647
1648 let s = metrics.sensor("test.sensor").unwrap();
1649 s.add_with_metric_name(metrics.metric_name("test.avg", "grp1"), Box::new(Avg::new()))
1650 .unwrap();
1651 s.add_with_metric_name(metrics.metric_name("test.max", "grp1"), Box::new(Max::new()))
1652 .unwrap();
1653 s.add_with_metric_name(metrics.metric_name("test.min", "grp1"), Box::new(Min::new()))
1654 .unwrap();
1655 s.add(Box::new(Meter::with_unit(
1656 TimeUnit::Seconds,
1657 metrics.metric_name("test.rate", "grp1"),
1658 metrics.metric_name("test.total", "grp1"),
1659 )))
1660 .unwrap();
1661 s.add(Box::new(Meter::with_rate_stat(
1662 std::sync::Arc::new(WindowedCount::new().into_sampled_stat()),
1663 metrics.metric_name("test.occurrences", "grp1"),
1664 metrics.metric_name("test.occurrences.total", "grp1"),
1665 )))
1666 .unwrap();
1667 s.add_with_metric_name(metrics.metric_name("test.count", "grp1"), Box::new(WindowedCount::new()))
1668 .unwrap();
1669
1670 let s2 = metrics.sensor("test.sensor2").unwrap();
1671 s2.add_with_metric_name(metrics.metric_name("s2.total", "grp1"), Box::new(CumulativeSum::new()))
1672 .unwrap();
1673 s2.record_with_value(5.0).unwrap();
1674
1675 let mut sum = 0i64;
1676 let count = 10i64;
1677 for i in 0..count {
1678 s.record_with_value(i as f64).unwrap();
1679 sum += i;
1680 }
1681
1682 let mut elapsed_secs = (config.time_window_ms() * (config.samples() as i64 - 1)) as f64 / 1000.0;
1684 assert!(
1685 (count as f64 / elapsed_secs
1686 - double_value(&metrics.metric(&metrics.metric_name("test.occurrences", "grp1")).unwrap()))
1687 .abs()
1688 <= EPS,
1689 "Occurrences(0...{count})"
1690 );
1691
1692 let sleep_time_ms = 2i64;
1694 time.sleep(sleep_time_ms * 1000);
1695 elapsed_secs += sleep_time_ms as f64;
1696
1697 assert!(
1698 (5.0 - double_value(&metrics.metric(&metrics.metric_name("s2.total", "grp1")).unwrap())).abs() <= EPS,
1699 "s2 reflects the constant value"
1700 );
1701 assert!(
1702 (4.5 - double_value(&metrics.metric(&metrics.metric_name("test.avg", "grp1")).unwrap())).abs() <= EPS,
1703 "Avg(0...9) = 4.5"
1704 );
1705 assert!(
1706 ((count - 1) as f64 - double_value(&metrics.metric(&metrics.metric_name("test.max", "grp1")).unwrap()))
1707 .abs()
1708 <= EPS,
1709 "Max(0...9) = 9"
1710 );
1711 assert!(
1712 (0.0 - double_value(&metrics.metric(&metrics.metric_name("test.min", "grp1")).unwrap())).abs() <= EPS,
1713 "Min(0...9) = 0"
1714 );
1715 assert!(
1716 (sum as f64 / elapsed_secs
1717 - double_value(&metrics.metric(&metrics.metric_name("test.rate", "grp1")).unwrap()))
1718 .abs()
1719 <= EPS,
1720 "Rate(0...9)"
1721 );
1722 assert!(
1723 (count as f64 / elapsed_secs
1724 - double_value(&metrics.metric(&metrics.metric_name("test.occurrences", "grp1")).unwrap()))
1725 .abs()
1726 <= EPS,
1727 "Occurrences(0...{count})"
1728 );
1729 assert!(
1730 (count as f64 - double_value(&metrics.metric(&metrics.metric_name("test.count", "grp1")).unwrap())).abs()
1731 <= EPS,
1732 "Count(0...9) = 10"
1733 );
1734 }
1735
1736 #[test]
1738 #[doc(alias = "org.apache.kafka.common.metrics.MetricsTest#testRateWindowing")]
1739 fn test_rate_windowing() {
1740 use crate::common::metrics::TimeUnit;
1741 use crate::common::metrics::internals::MetricsUtils;
1742 use crate::common::metrics::stats::{Meter, WindowedCount};
1743
1744 let time = Arc::new(MockTime::new());
1745 let cfg = Arc::new(MetricConfig::new().set_samples(3));
1747 let metrics = Metrics::with_default_config_reporters_time(
1748 Arc::clone(&cfg),
1749 Vec::new(),
1750 Arc::clone(&time) as Arc<dyn Time>,
1751 );
1752
1753 let s = metrics
1754 .sensor_with_options(
1755 SensorOptionsBuilder::new()
1756 .set_name("test.sensor")
1757 .set_config(Some(Arc::clone(&cfg)))
1758 .build()
1759 .unwrap(),
1760 )
1761 .unwrap();
1762 let rate_metric_name = metrics.metric_name("test.rate", "grp1");
1763 let total_metric_name = metrics.metric_name("test.total", "grp1");
1764 let count_rate_metric_name = metrics.metric_name("test.count.rate", "grp1");
1765 let count_total_metric_name = metrics.metric_name("test.count.total", "grp1");
1766 s.add(Box::new(Meter::with_unit(
1767 TimeUnit::Seconds,
1768 rate_metric_name.clone(),
1769 total_metric_name.clone(),
1770 )))
1771 .unwrap();
1772 s.add(Box::new(Meter::with_rate_stat(
1773 Arc::new(WindowedCount::new().into_sampled_stat()),
1774 count_rate_metric_name.clone(),
1775 count_total_metric_name.clone(),
1776 )))
1777 .unwrap();
1778 let total_metric = metrics.metrics().get(&total_metric_name).unwrap().clone();
1779 let count_total_metric = metrics.metrics().get(&count_total_metric_name).unwrap().clone();
1780
1781 let mut sum = 0i64;
1782 let count = cfg.samples() as i64 - 1;
1783 for _ in 0..count {
1785 s.record_with_value(100.0).unwrap();
1786 sum += 100;
1787 time.sleep(cfg.time_window_ms());
1788 assert!((sum as f64 - double_value(&total_metric)).abs() <= EPS);
1789 }
1790
1791 time.sleep(cfg.time_window_ms() / 2);
1793
1794 let elapsed_secs =
1796 MetricsUtils::convert(cfg.time_window_ms(), TimeUnit::Seconds) * (cfg.samples() as f64 - 0.5);
1797
1798 let rate_metric = metrics.metrics().get(&rate_metric_name).unwrap().clone();
1799 let count_rate_metric = metrics.metrics().get(&count_rate_metric_name).unwrap().clone();
1800 assert!(
1801 (sum as f64 / elapsed_secs - double_value(&rate_metric)).abs() <= EPS,
1802 "Rate(0...2)"
1803 );
1804 assert!(
1805 (count as f64 / elapsed_secs - double_value(&count_rate_metric)).abs() <= EPS,
1806 "Count rate(0...2)"
1807 );
1808 assert!((sum as f64 - double_value(&total_metric)).abs() <= EPS);
1818 assert!((count as f64 - double_value(&count_total_metric)).abs() <= EPS);
1819
1820 time.sleep(cfg.time_window_ms() * cfg.samples() as i64);
1822 assert!((0.0 - double_value(&rate_metric)).abs() <= EPS);
1823 assert!((0.0 - double_value(&count_rate_metric)).abs() <= EPS);
1824 assert!((sum as f64 - double_value(&total_metric)).abs() <= EPS);
1825 assert!((count as f64 - double_value(&count_total_metric)).abs() <= EPS);
1826 }
1827
1828 #[test]
1830 fn count_metric_registered_on_construction() {
1831 let metrics = Metrics::new();
1832 let count_name = metrics.metric_name_with_description_tags(
1833 "count",
1834 "kafka-metrics-count",
1835 "total number of registered metrics",
1836 BTreeMap::new(),
1837 );
1838 let count_metric = metrics.metric(&count_name).expect("count metric must exist");
1839 assert_eq!(double_value(&count_metric), 1.0);
1841 metrics
1843 .add_metric_with_measurable(metrics.metric_name("extra", "grp1"), Box::new(Value::new()))
1844 .unwrap();
1845 assert_eq!(double_value(&count_metric), 2.0);
1846 }
1847
1848 #[test]
1853 fn dropping_metrics_frees_the_registry_and_its_metrics() {
1854 let metrics = Metrics::new();
1855 let shared = Arc::downgrade(&metrics.shared);
1856 let count_name = metrics.metric_name_with_description_tags(
1857 "count",
1858 "kafka-metrics-count",
1859 "total number of registered metrics",
1860 BTreeMap::new(),
1861 );
1862 let extra_name = metrics.metric_name("extra", "grp1");
1863 metrics
1864 .add_metric_with_measurable(extra_name.clone(), Box::new(Value::new()))
1865 .unwrap();
1866 let extra = Arc::downgrade(&metrics.metric(&extra_name).expect("extra metric must exist"));
1867 let count_metric = metrics.metric(&count_name).expect("count metric must exist");
1868 assert_eq!(
1869 double_value(&count_metric),
1870 2.0,
1871 "Java's gauge reports metrics.size() while alive"
1872 );
1873
1874 metrics.close();
1875 drop(metrics);
1876 assert!(
1877 shared.upgrade().is_none(),
1878 "MetricsShared must be freed once the Metrics is dropped"
1879 );
1880 assert!(
1881 extra.upgrade().is_none(),
1882 "registered metrics must be freed with their registry"
1883 );
1884 assert_eq!(double_value(&count_metric), 0.0);
1886 }
1887
1888 #[test]
1890 fn test_metric_instance() {
1891 use crate::common::MetricNameTemplate;
1892 use indexmap::IndexSet;
1893 let metrics = Metrics::new();
1894 let mut tag_names = IndexSet::new();
1895 tag_names.insert("client-id".to_string());
1896 let template = MetricNameTemplate::new("name", "group", "desc", tag_names);
1897
1898 let name = metrics
1899 .metric_instance_with_key_value(&template, &["client-id", "client-1"])
1900 .unwrap();
1901 assert_eq!(name.name(), "name");
1902 assert_eq!(name.tags().get("client-id").map(String::as_str), Some("client-1"));
1903
1904 let err = metrics.metric_instance_with_key_value(&template, &["wrong", "v"]).unwrap_err();
1906 assert!(err.to_string().contains("do not match the tags in the template"));
1907 }
1908
1909 #[test]
1917 fn test_new_default_config_time_forwards() {
1918 let config = Arc::new(MetricConfig::new().set_samples(7));
1919 let time = Arc::new(MockTime::new());
1920 let metrics = Metrics::with_default_config_time(Arc::clone(&config), Arc::clone(&time) as Arc<dyn Time>);
1921
1922 assert!(Arc::ptr_eq(metrics.config(), &config));
1924 assert_eq!(metrics.config().samples(), 7);
1925
1926 let s = metrics
1929 .sensor_with_config_inactive_sensor_expiration_time_seconds_parents("s", None, 1, &[])
1930 .unwrap();
1931 assert!(!s.has_expired());
1932 time.sleep(2_000);
1933 assert!(s.has_expired());
1934 }
1935
1936 #[test]
1938 fn test_metric_name_description_forwards() {
1939 let mut default_tags = BTreeMap::new();
1940 default_tags.insert("client-id".to_string(), "c1".to_string());
1941 let metrics = Metrics::with_default_config(Arc::new(MetricConfig::new().set_tags(default_tags)));
1942
1943 let added = metrics.metric_name_with_description("n", "g", "the description");
1944 let forwarded = metrics.metric_name_with_description_tags("n", "g", "the description", BTreeMap::new());
1945 assert_eq!(added, forwarded);
1946 assert_eq!(added.description(), "the description");
1949 assert_eq!(added.name(), "n");
1950 assert_eq!(added.group(), "g");
1951 assert_eq!(added.tags().get("client-id").map(String::as_str), Some("c1"));
1952 }
1953
1954 #[test]
1956 fn test_metric_name_tags_forwards() {
1957 let mut default_tags = BTreeMap::new();
1958 default_tags.insert("client-id".to_string(), "c1".to_string());
1959 let metrics = Metrics::with_default_config(Arc::new(MetricConfig::new().set_tags(default_tags)));
1960
1961 let mut tags = BTreeMap::new();
1962 tags.insert("node-id".to_string(), "n7".to_string());
1963
1964 let added = metrics.metric_name_with_tags("n", "g", tags.clone());
1965 assert_eq!(added, metrics.metric_name_with_description_tags("n", "g", "", tags));
1966 assert_eq!(added.description(), "");
1967 assert_eq!(added.tags().get("client-id").map(String::as_str), Some("c1"));
1969 assert_eq!(added.tags().get("node-id").map(String::as_str), Some("n7"));
1970 }
1971
1972 #[test]
1980 fn test_sensor_forwarding_overloads() {
1981 let (metrics, time) = metrics_with_mock();
1982 let config = Arc::new(MetricConfig::new().set_samples(9));
1983 let parent = metrics.sensor("parent").unwrap();
1984
1985 let s = metrics
1987 .sensor_with_recording_level_parents("a", RecordingLevel::Debug, std::slice::from_ref(&parent))
1988 .unwrap();
1989 assert!(!s.should_record(), "DEBUG sensor must not record under the default INFO config");
1990 assert_eq!(s.parents().len(), 1);
1991 assert!(Arc::ptr_eq(&s.parents()[0], &parent));
1992
1993 let s = metrics
1995 .sensor_with_config_parents("b", Some(Arc::clone(&config)), std::slice::from_ref(&parent))
1996 .unwrap();
1997 assert!(s.should_record());
1998 assert!(Arc::ptr_eq(&s.parents()[0], &parent));
1999 s.add_with_metric_name(metrics.metric_name("b.count", "grp"), Box::new(CumulativeCount::new()))
2000 .unwrap();
2001 assert!(Arc::ptr_eq(
2002 &metrics.metric(&metrics.metric_name("b.count", "grp")).unwrap().config(),
2003 &config
2004 ));
2005
2006 let s = metrics
2008 .sensor_with_config_recording_level_parents(
2009 "c",
2010 Some(Arc::clone(&config)),
2011 RecordingLevel::Debug,
2012 std::slice::from_ref(&parent),
2013 )
2014 .unwrap();
2015 assert!(!s.should_record());
2016 assert!(Arc::ptr_eq(&s.parents()[0], &parent));
2017 s.add_with_metric_name(metrics.metric_name("c.count", "grp"), Box::new(CumulativeCount::new()))
2018 .unwrap();
2019 assert!(Arc::ptr_eq(
2020 &metrics.metric(&metrics.metric_name("c.count", "grp")).unwrap().config(),
2021 &config
2022 ));
2023
2024 let s = metrics
2026 .sensor_with_config_inactive_sensor_expiration_time_seconds_parents(
2027 "d",
2028 Some(Arc::clone(&config)),
2029 1,
2030 std::slice::from_ref(&parent),
2031 )
2032 .unwrap();
2033 assert!(s.should_record());
2034 assert!(Arc::ptr_eq(&s.parents()[0], &parent));
2035 assert!(!s.has_expired());
2036 time.sleep(2_000);
2037 assert!(s.has_expired(), "the 1s inactivity window must reach the underlying sensor");
2038
2039 let canonical = metrics
2041 .sensor_with_options(
2042 SensorOptionsBuilder::new()
2043 .set_name("e")
2044 .set_config(Some(Arc::clone(&config)))
2045 .set_inactive_sensor_expiration_time_seconds(1)
2046 .set_parents(std::slice::from_ref(&parent))
2047 .build()
2048 .unwrap(),
2049 )
2050 .unwrap();
2051 assert!(canonical.should_record());
2052 assert!(Arc::ptr_eq(&canonical.parents()[0], &parent));
2053 }
2054
2055 #[test]
2059 fn sensor_options_builder_build_errors_when_no_mandatory_parameter_is_set() {
2060 let Err(error) = SensorOptionsBuilder::new().build() else {
2061 panic!("build must reject the unset mandatory parameter");
2062 };
2063 assert!(matches!(error, Error::LocalIllegalArgument(_)), "{error:?}");
2064 assert_eq!(
2065 error.message(),
2066 "SensorOptionsBuilder::build: mandatory parameter `name` was not set"
2067 );
2068 }
2069
2070 #[test]
2078 fn test_concurrent_sensor_creation_returns_one_sensor() {
2079 use std::sync::Barrier;
2080 const THREADS: usize = 8;
2081 const ITERATIONS: usize = 200;
2082 for iteration in 0..ITERATIONS {
2083 let metrics = Arc::new(Metrics::new());
2084 let parent = metrics.sensor("parent").unwrap();
2085 let barrier = Arc::new(Barrier::new(THREADS));
2086 let handles: Vec<_> = (0..THREADS)
2087 .map(|_| {
2088 let metrics = Arc::clone(&metrics);
2089 let parent = Arc::clone(&parent);
2090 let barrier = Arc::clone(&barrier);
2091 std::thread::spawn(move || {
2092 barrier.wait();
2093 metrics.sensor_with_parents("child", &[parent])
2094 })
2095 })
2096 .collect();
2097 let sensors: Vec<Arc<Sensor>> = handles
2098 .into_iter()
2099 .map(|h| h.join().expect("thread panicked").expect("sensor created"))
2100 .collect();
2101 let registered = metrics.get_sensor("child").expect("child registered");
2102 for sensor in &sensors {
2103 assert!(
2104 Arc::ptr_eq(sensor, ®istered),
2105 "iteration {iteration}: caller got an orphaned sensor"
2106 );
2107 }
2108 let children = metrics.children_sensors(&parent).expect("parent has children");
2109 assert_eq!(1, children.len(), "iteration {iteration}: duplicate child registration");
2110 assert!(Arc::ptr_eq(&children[0], ®istered));
2111 let name = metrics.metric_name("child-count", "group");
2113 assert!(
2114 registered
2115 .add_with_metric_name(name.clone(), Box::new(CumulativeCount::new()))
2116 .expect("first add")
2117 );
2118 assert!(metrics.metric(&name).is_some());
2119 }
2120 }
2121
2122 #[test]
2126 fn test_dropped_registry_is_freed() {
2127 let metrics = Metrics::new();
2128 let sensor = metrics.sensor("s").expect("sensor");
2129 sensor
2130 .add_with_metric_name(metrics.metric_name("s-count", "grp1"), Box::new(CumulativeCount::new()))
2131 .expect("add");
2132 assert_eq!(
2133 Some(MetricValue::Double(2.0)),
2134 metrics
2135 .metric(&metrics.metric_name("count", "kafka-metrics-count"))
2136 .map(|metric| metric.metric_value())
2137 );
2138 let shared = Arc::downgrade(&metrics.shared);
2139 drop(metrics);
2140 assert!(shared.upgrade().is_none(), "the registry's shared state is still referenced");
2141 assert!(
2143 sensor
2144 .add_with_metric_name(MetricName::new("late", "grp1", "", BTreeMap::new()), Box::new(Value::new()))
2145 .expect("add after the registry is gone")
2146 );
2147 sensor.record_with_value(1.0).unwrap();
2148 }
2149
2150 #[test]
2155 fn test_provider_capturing_a_sensor_does_not_keep_the_registry() {
2156 let metrics = Metrics::new();
2157 let sensor = metrics.sensor("wait-time").expect("sensor");
2158 sensor
2159 .add_with_metric_name(metrics.metric_name("wait-time-total", "grp1"), Box::new(CumulativeSum::new()))
2160 .expect("add");
2161 let sensor_for_gauge = Arc::clone(&sensor);
2162 metrics
2163 .add_metric_with_measurable(
2164 metrics.metric_name("gauge", "grp1"),
2165 Box::new(crate::common::metrics::ClosureMeasurable::new(move |_config, _now| {
2166 sensor_for_gauge.name().len() as f64
2167 })),
2168 )
2169 .expect("add gauge");
2170 let shared = Arc::downgrade(&metrics.shared);
2171 let weak_sensor = Arc::downgrade(&sensor);
2172 drop(sensor);
2173 drop(metrics);
2174 assert!(shared.upgrade().is_none(), "the registry's shared state is still referenced");
2175 assert!(
2176 weak_sensor.upgrade().is_none(),
2177 "the sensor captured by the gauge is still referenced"
2178 );
2179 }
2180}