1use std::collections::HashSet;
19use std::sync::atomic::{AtomicI64, Ordering};
20use std::sync::{Arc, Mutex, Weak};
21
22use crate::common::Metric;
23use crate::common::metrics::MetricsShared;
24use crate::common::metrics::{
25 CompoundStat, KafkaMetric, Measurable, MeasurableStat, MetricConfig, Quota, QuotaViolationError, Stat,
26};
27use crate::common::utils::Time;
28use crate::common::{Error, MetricName};
29
30#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
35#[non_exhaustive]
36#[doc(alias = "org.apache.kafka.common.metrics.Sensor$RecordingLevel")]
37pub enum RecordingLevel {
38 Info,
40 Debug,
42 Trace,
44}
45
46impl std::fmt::Display for RecordingLevel {
48 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
49 f.write_str(self.name())
50 }
51}
52
53impl RecordingLevel {
54 #[doc(alias = "org.apache.kafka.common.metrics.Sensor$RecordingLevel#id")]
56 pub fn id(&self) -> i16 {
57 match self {
58 RecordingLevel::Info => 0,
59 RecordingLevel::Debug => 1,
60 RecordingLevel::Trace => 2,
61 }
62 }
63
64 #[doc(alias = "org.apache.kafka.common.metrics.Sensor$RecordingLevel#name")]
66 pub fn name(&self) -> &'static str {
67 match self {
68 RecordingLevel::Info => "INFO",
69 RecordingLevel::Debug => "DEBUG",
70 RecordingLevel::Trace => "TRACE",
71 }
72 }
73
74 #[doc(alias = "org.apache.kafka.common.metrics.Sensor$RecordingLevel#forId")]
88 pub fn for_id(id: i16) -> Result<RecordingLevel, Error> {
89 match id {
90 0 => Ok(RecordingLevel::Info),
91 1 => Ok(RecordingLevel::Debug),
92 2 => Ok(RecordingLevel::Trace),
93 _ => Err(Error::local_illegal_argument(format!(
94 "Unexpected RecordLevel id `{id}`, it should be between `0` and `2` (inclusive)"
95 ))),
96 }
97 }
98
99 #[doc(alias = "org.apache.kafka.common.metrics.Sensor$RecordingLevel#forName")]
113 pub fn for_name(name: &str) -> Result<RecordingLevel, Error> {
114 match name.to_uppercase().as_str() {
115 "INFO" => Ok(RecordingLevel::Info),
116 "DEBUG" => Ok(RecordingLevel::Debug),
117 "TRACE" => Ok(RecordingLevel::Trace),
118 other => Err(Error::local_illegal_argument(format!(
119 "No enum constant RecordingLevel.{other}"
120 ))),
121 }
122 }
123
124 #[doc(alias = "org.apache.kafka.common.metrics.Sensor$RecordingLevel#shouldRecord")]
136 pub fn should_record(&self, config_id: i16) -> bool {
137 if config_id == RecordingLevel::Info.id() {
138 self.id() == RecordingLevel::Info.id()
139 } else if config_id == RecordingLevel::Debug.id() {
140 self.id() == RecordingLevel::Info.id() || self.id() == RecordingLevel::Debug.id()
141 } else if config_id == RecordingLevel::Trace.id() {
142 true
143 } else {
144 panic!("Did not recognize recording level {config_id}")
147 }
148 }
149}
150
151#[doc(alias = "org.apache.kafka.common.metrics.Sensor$StatAndConfig")]
159struct StatAndConfig {
160 stat: Box<dyn Stat>,
161 config: StatConfigSource,
162}
163
164enum StatConfigSource {
174 FromMetric(Arc<KafkaMetric>),
175 Constant(Arc<MetricConfig>),
176}
177
178impl StatAndConfig {
179 fn record(&self, value: f64, time_ms: i64) {
180 let config = match &self.config {
181 StatConfigSource::FromMetric(metric) => metric.config(),
182 StatConfigSource::Constant(config) => Arc::clone(config),
183 };
184 self.stat.record(&config, value, time_ms);
185 }
186}
187
188#[doc(alias = "org.apache.kafka.common.metrics.Sensor")]
193pub struct Sensor {
194 registry: Option<Weak<MetricsShared>>,
204 name: String,
205 parents: Vec<Arc<Sensor>>,
206 inner: Mutex<SensorInner>,
207 config: Arc<MetricConfig>,
208 time: Arc<dyn Time>,
209 last_record_time: AtomicI64,
210 inactive_sensor_expiration_time_ms: i64,
211 recording_level: RecordingLevel,
212}
213
214struct SensorInner {
217 stats: Vec<StatAndConfig>,
218 metrics: indexmap::IndexMap<MetricName, Arc<KafkaMetric>>,
219}
220
221impl std::fmt::Debug for Sensor {
222 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
224 f.debug_struct("Sensor").field("name", &self.name).finish_non_exhaustive()
225 }
226}
227
228impl Sensor {
229 #[doc(alias = "org.apache.kafka.common.metrics.Sensor#Sensor")]
235 pub(crate) fn new(
236 registry: Option<Weak<MetricsShared>>,
237 name: impl Into<String>,
238 parents: Vec<Arc<Sensor>>,
239 config: Arc<MetricConfig>,
240 time: Arc<dyn Time>,
241 inactive_sensor_expiration_time_seconds: i64,
242 recording_level: RecordingLevel,
243 ) -> Result<Self, Error> {
244 let now = time.milliseconds();
245 let sensor = Sensor {
246 registry,
247 name: name.into(),
248 parents,
249 inner: Mutex::new(SensorInner { stats: Vec::new(), metrics: indexmap::IndexMap::new() }),
250 config,
251 time,
252 last_record_time: AtomicI64::new(now),
253 inactive_sensor_expiration_time_ms: inactive_sensor_expiration_time_seconds.saturating_mul(1000),
254 recording_level,
255 };
256 let mut seen: HashSet<*const Sensor> = HashSet::new();
258 sensor.check_forest(&mut seen)?;
259 Ok(sensor)
260 }
261
262 #[doc(alias = "org.apache.kafka.common.metrics.Sensor#checkForest")]
264 fn check_forest(&self, sensors: &mut HashSet<*const Sensor>) -> Result<(), Error> {
265 if !sensors.insert(self as *const Sensor) {
266 return Err(Error::local_illegal_argument(format!(
267 "Circular dependency in sensors: {} is its own parent.",
268 self.name()
269 )));
270 }
271 for parent in &self.parents {
272 parent.check_forest(sensors)?;
273 }
274 Ok(())
275 }
276
277 #[doc(alias = "org.apache.kafka.common.metrics.Sensor#name")]
280 pub fn name(&self) -> &str {
281 &self.name
282 }
283
284 #[doc(alias = "org.apache.kafka.common.metrics.Sensor#parents")]
286 pub(crate) fn parents(&self) -> &[Arc<Sensor>] {
287 &self.parents
288 }
289
290 #[doc(alias = "org.apache.kafka.common.metrics.Sensor#shouldRecord")]
293 pub fn should_record(&self) -> bool {
294 self.recording_level.should_record(self.config.record_level().id())
295 }
296
297 #[doc(alias = "org.apache.kafka.common.metrics.Sensor#record")]
308 pub fn record(&self) -> Result<(), Error> {
309 if self.should_record() {
310 self.record_internal(1.0, self.time.milliseconds(), true)?;
311 }
312 Ok(())
313 }
314
315 #[doc(alias = "org.apache.kafka.common.metrics.Sensor#record")]
326 pub fn record_with_value(&self, value: f64) -> Result<(), Error> {
327 if self.should_record() {
328 self.record_internal(value, self.time.milliseconds(), true)?;
329 }
330 Ok(())
331 }
332
333 #[doc(alias = "org.apache.kafka.common.metrics.Sensor#record")]
346 pub fn record_with_value_time_ms(&self, value: f64, time_ms: i64) -> Result<(), Error> {
347 if self.should_record() {
348 self.record_internal(value, time_ms, true)?;
349 }
350 Ok(())
351 }
352
353 #[doc(alias = "org.apache.kafka.common.metrics.Sensor#record")]
367 pub fn record_with_value_time_ms_check_quotas(
368 &self,
369 value: f64,
370 time_ms: i64,
371 check_quotas: bool,
372 ) -> Result<(), Error> {
373 if self.should_record() {
374 self.record_internal(value, time_ms, check_quotas)?;
375 }
376 Ok(())
377 }
378
379 #[doc(alias = "org.apache.kafka.common.metrics.Sensor#recordInternal")]
380 fn record_internal(&self, value: f64, time_ms: i64, check_quotas: bool) -> Result<(), Error> {
381 self.last_record_time.store(time_ms, Ordering::SeqCst);
382 {
383 let inner = self.inner.lock().expect("sensor mutex poisoned");
384 for stat_and_config in &inner.stats {
386 stat_and_config.record(value, time_ms);
387 }
388 }
389 if check_quotas {
390 self.check_quotas_with_time_ms(time_ms)?;
391 }
392 for parent in &self.parents {
393 parent.record_with_value_time_ms_check_quotas(value, time_ms, check_quotas)?;
394 }
395 Ok(())
396 }
397
398 #[doc(alias = "org.apache.kafka.common.metrics.Sensor#checkQuotas")]
405 pub fn check_quotas(&self) -> Result<(), Error> {
406 self.check_quotas_with_time_ms(self.time.milliseconds())
407 }
408
409 #[doc(alias = "org.apache.kafka.common.metrics.Sensor#checkQuotas")]
420 pub fn check_quotas_with_time_ms(&self, time_ms: i64) -> Result<(), Error> {
426 let metrics: Vec<(Arc<KafkaMetric>, Quota)> = {
435 let inner = self.inner.lock().expect("sensor mutex poisoned");
436 inner
437 .metrics
438 .values()
439 .filter_map(|metric| metric.config().quota().map(|quota| (Arc::clone(metric), quota.clone())))
440 .collect()
441 };
442 for (metric, quota) in metrics {
443 let value = metric.measurable_value(time_ms);
444 if !quota.acceptable(value) {
445 return Err(Error::QuotaViolation(Box::new(QuotaViolationError::new(
446 metric.metric_name().clone(),
447 value,
448 quota.bound(),
449 ))));
450 }
451 }
452 Ok(())
453 }
454
455 #[doc(alias = "org.apache.kafka.common.metrics.Sensor#add")]
479 pub fn add_with_metric_name(&self, metric_name: MetricName, stat: Box<dyn MeasurableStat>) -> Result<bool, Error> {
480 self.add_with_metric_name_config(metric_name, stat, None)
481 }
482
483 #[doc(alias = "org.apache.kafka.common.metrics.Sensor#add")]
501 pub fn add_with_metric_name_config(
502 &self,
503 metric_name: MetricName,
504 stat: Box<dyn MeasurableStat>,
505 config: Option<Arc<MetricConfig>>,
506 ) -> Result<bool, Error> {
507 if self.has_expired() {
508 return Ok(false);
509 }
510 let mut inner = self.inner.lock().expect("sensor mutex poisoned");
511 if inner.metrics.contains_key(&metric_name) {
512 return Ok(true);
513 }
514 let stat_config = config.unwrap_or_else(|| Arc::clone(&self.config));
515
516 let stat: Arc<dyn MeasurableStat> = Arc::from(stat);
522 let measurable_view: Arc<dyn Measurable> = Arc::clone(&stat) as Arc<dyn Measurable>;
523 let metric = Arc::new(KafkaMetric::new(
524 metric_name.clone(),
525 Box::new(MeasurableArc(measurable_view)),
526 Arc::clone(&stat_config),
527 Arc::clone(&self.time),
528 ));
529
530 if let Some(registry) = self.registry.as_ref().and_then(Weak::upgrade) {
531 let existing = registry.register_metric(Arc::clone(&metric));
532 if existing.is_some() {
533 return Err(Error::local_illegal_argument(format!(
534 "A metric named '{metric_name}' already exists, can't register another one."
535 )));
536 }
537 }
538 inner.metrics.insert(metric_name, Arc::clone(&metric));
539 inner
540 .stats
541 .push(StatAndConfig { stat: Box::new(StatArc(stat)), config: StatConfigSource::FromMetric(metric) });
542 Ok(true)
543 }
544
545 #[doc(alias = "org.apache.kafka.common.metrics.Sensor#add")]
560 pub fn add(&self, stat: Box<dyn CompoundStat>) -> Result<bool, Error> {
561 self.add_with_config(stat, None)
562 }
563
564 #[doc(alias = "org.apache.kafka.common.metrics.Sensor#add")]
582 pub fn add_with_config(
586 &self,
587 stat: Box<dyn CompoundStat>,
588 config: Option<Arc<MetricConfig>>,
589 ) -> Result<bool, Error> {
590 if self.has_expired() {
591 return Ok(false);
592 }
593 let mut inner = self.inner.lock().expect("sensor mutex poisoned");
594 let stat_config = config.unwrap_or_else(|| Arc::clone(&self.config));
595
596 let children = stat.stats();
599
600 inner.stats.push(StatAndConfig {
601 stat: Box::new(CompoundStatBox(stat)),
602 config: StatConfigSource::Constant(Arc::clone(&stat_config)),
603 });
604
605 for child in children {
606 let metric = Arc::new(KafkaMetric::new(
607 child.name().clone(),
608 Box::new(MeasurableArc(child.stat())),
609 Arc::clone(&stat_config),
610 Arc::clone(&self.time),
611 ));
612 if !inner.metrics.contains_key(child.name()) {
613 if let Some(registry) = self.registry.as_ref().and_then(Weak::upgrade) {
614 let existing = registry.register_metric(Arc::clone(&metric));
615 if existing.is_some() {
616 return Err(Error::local_illegal_argument(format!(
617 "A metric named '{}' already exists, can't register another one.",
618 child.name()
619 )));
620 }
621 }
622 inner.metrics.insert(child.name().clone(), metric);
623 }
624 }
625 Ok(true)
626 }
627
628 #[doc(alias = "org.apache.kafka.common.metrics.Sensor#hasMetrics")]
634 pub fn has_metrics(&self) -> bool {
635 !self.inner.lock().expect("sensor mutex poisoned").metrics.is_empty()
636 }
637
638 #[doc(alias = "org.apache.kafka.common.metrics.Sensor#hasExpired")]
640 pub fn has_expired(&self) -> bool {
641 (self.time.milliseconds() - self.last_record_time.load(Ordering::SeqCst))
642 > self.inactive_sensor_expiration_time_ms
643 }
644
645 #[doc(alias = "org.apache.kafka.common.metrics.Sensor#metrics")]
647 pub(crate) fn metrics(&self) -> Vec<Arc<KafkaMetric>> {
648 self.inner
649 .lock()
650 .expect("sensor mutex poisoned")
651 .metrics
652 .values()
653 .cloned()
654 .collect()
655 }
656}
657
658struct StatArc(Arc<dyn MeasurableStat>);
661
662impl Stat for StatArc {
663 fn record(&self, config: &MetricConfig, value: f64, time_ms: i64) {
664 self.0.record(config, value, time_ms);
665 }
666}
667
668struct MeasurableArc(Arc<dyn Measurable>);
671
672impl Measurable for MeasurableArc {
673 fn measure(&self, config: &MetricConfig, now: i64) -> f64 {
674 self.0.measure(config, now)
675 }
676
677 fn type_name(&self) -> Option<&'static str> {
680 self.0.type_name()
681 }
682}
683
684struct CompoundStatBox(Box<dyn CompoundStat>);
687
688impl Stat for CompoundStatBox {
689 fn record(&self, config: &MetricConfig, value: f64, time_ms: i64) {
690 self.0.record(config, value, time_ms);
691 }
692}
693
694pub(crate) fn record_or_log(result: Result<(), Error>) {
702 if let Err(error) = result {
703 log::error!("Recording a client metric failed: {error}");
704 }
705}
706
707#[cfg(test)]
708mod tests {
709 use super::*;
710 use crate::common::metrics::Quota;
711 use crate::common::metrics::stats::{CumulativeCount, Value};
712 use crate::common::protocol::Errors;
713 use crate::common::utils::MockTime;
714 use crate::common::utils::SystemTime;
715 use std::collections::BTreeMap;
716
717 fn level_id(level: RecordingLevel) -> i16 {
718 level.id()
719 }
720
721 fn info_config() -> Arc<MetricConfig> {
722 Arc::new(MetricConfig::new().set_record_level(RecordingLevel::Info))
723 }
724 fn debug_config() -> Arc<MetricConfig> {
725 Arc::new(MetricConfig::new().set_record_level(RecordingLevel::Debug))
726 }
727 fn trace_config() -> Arc<MetricConfig> {
728 Arc::new(MetricConfig::new().set_record_level(RecordingLevel::Trace))
729 }
730
731 fn standalone(config: Arc<MetricConfig>, expiration_secs: i64, level: RecordingLevel) -> Sensor {
732 Sensor::new(None, "sensor", Vec::new(), config, Arc::new(SystemTime), expiration_secs, level).unwrap()
733 }
734
735 fn name(n: &str, g: &str) -> MetricName {
736 MetricName::new(n, g, "", BTreeMap::new())
737 }
738
739 fn quota_config(quota: Quota) -> Arc<MetricConfig> {
740 Arc::new(MetricConfig::new().set_quota(quota).set_record_level(RecordingLevel::Info))
741 }
742
743 #[test]
752 fn record_or_log_logs_the_violation() {
753 let sensor = standalone(quota_config(Quota::upper_bound(5.0)), 60, RecordingLevel::Info);
754 assert!(
755 sensor
756 .add_with_metric_name(name("value", "test-group"), Box::new(Value::new()))
757 .unwrap()
758 );
759 let ((), lines) = crate::test_log_capture::capture_logs(|| {
760 record_or_log(sensor.record_with_value_time_ms(4.0, 1));
761 record_or_log(sensor.record_with_value_time_ms(10.0, 2));
762 });
763 let errors: Vec<_> = lines.into_iter().filter(|(level, _)| *level == log::Level::Error).collect();
764 assert_eq!(errors.len(), 1, "{errors:?}");
765 assert!(
766 errors[0]
767 .1
768 .starts_with("Recording a client metric failed: QuotaViolationError: "),
769 "{}",
770 errors[0].1
771 );
772 assert!(errors[0].1.ends_with("Actual: 10.0, Threshold: 5.0"), "{}", errors[0].1);
773 }
774
775 #[test]
776 fn test_check_quotas_reports_an_upper_bound_violation() {
777 let sensor = standalone(quota_config(Quota::upper_bound(5.0)), 60, RecordingLevel::Info);
778 let metric_name = name("value", "test-group");
779 assert!(
780 sensor
781 .add_with_metric_name(metric_name.clone(), Box::new(Value::new()))
782 .unwrap()
783 );
784
785 sensor.record_with_value_time_ms(4.0, 1).unwrap();
787 assert!(sensor.check_quotas_with_time_ms(1).is_ok());
788 assert!(sensor.check_quotas().is_ok());
789
790 sensor.record_with_value_time_ms_check_quotas(10.0, 2, false).unwrap();
792 let error = sensor
793 .check_quotas_with_time_ms(2)
794 .expect_err("10.0 exceeds the upper bound of 5.0");
795 let Error::QuotaViolation(violation) = &error else {
796 panic!("expected a quota violation, got {error:?}");
797 };
798 assert_eq!(violation.metric_name().name(), "value");
799 assert_eq!(violation.value(), 10.0);
800 assert_eq!(violation.bound(), 5.0);
801
802 assert!(error.is_kafka_error());
805 assert!(!error.is_api_error());
806 assert!(!error.is_retriable_error());
807 assert_eq!(error.error(), Errors::UnknownServerError);
808 assert_eq!(error.message(), "");
811 assert_eq!(
812 error.to_string(),
813 format!("QuotaViolationError: '{metric_name}' violated quota. Actual: 10.0, Threshold: 5.0")
814 );
815 }
816
817 #[test]
821 fn test_check_quotas_reports_a_lower_bound_violation() {
822 let sensor = standalone(quota_config(Quota::lower_bound(5.0)), 60, RecordingLevel::Info);
823 assert!(
824 sensor
825 .add_with_metric_name(name("value", "test-group"), Box::new(Value::new()))
826 .unwrap()
827 );
828
829 sensor.record_with_value_time_ms(10.0, 1).unwrap();
830 assert!(sensor.check_quotas_with_time_ms(1).is_ok());
831
832 sensor.record_with_value_time_ms_check_quotas(1.0, 2, false).unwrap();
833 let error = sensor
834 .check_quotas_with_time_ms(2)
835 .expect_err("1.0 is below the lower bound of 5.0");
836 let Error::QuotaViolation(violation) = &error else {
837 panic!("expected a quota violation, got {error:?}");
838 };
839 assert_eq!(violation.value(), 1.0);
840 assert_eq!(violation.bound(), 5.0);
841 }
842
843 #[test]
848 fn test_record_violation_does_not_record_the_parents() {
849 use crate::common::metrics::Metrics;
850 let metrics = Metrics::new();
851 let parent = metrics.sensor("parent").unwrap();
852 let parent_count = metrics.metric_name("parent.count", "grp1");
853 parent
854 .add_with_metric_name(parent_count.clone(), Box::new(CumulativeCount::new()))
855 .unwrap();
856 let child = metrics
857 .sensor_with_config_parents("child", Some(quota_config(Quota::upper_bound(5.0))), &[Arc::clone(&parent)])
858 .unwrap();
859 let child_value = metrics.metric_name("child.value", "grp1");
860 child.add_with_metric_name(child_value.clone(), Box::new(Value::new())).unwrap();
861 let value_of = |name: &MetricName| metrics.metric(name).unwrap().measurable_value(0);
862
863 child.record_with_value(1.0).unwrap();
864 assert_eq!(value_of(&parent_count), 1.0);
865
866 let error = child.record_with_value(10.0).expect_err("10.0 exceeds the upper bound of 5.0");
867 assert!(error.is_quota_violation_error());
868 assert_eq!(
869 error.to_string(),
870 format!("QuotaViolationError: '{child_value}' violated quota. Actual: 10.0, Threshold: 5.0")
871 );
872 assert_eq!(value_of(&child_value), 10.0);
874 assert_eq!(value_of(&parent_count), 1.0);
875
876 child.record_with_value_time_ms_check_quotas(10.0, 0, false).unwrap();
877 assert_eq!(value_of(&parent_count), 2.0);
878 }
879
880 #[test]
882 #[doc(alias = "org.apache.kafka.common.metrics.SensorTest#testStrictQuotaEnforcementWithRate")]
883 fn test_strict_quota_enforcement_with_rate() {
884 use crate::common::metrics::stats::Rate;
885 use crate::common::metrics::{Metrics, TimeUnit};
886 let wall_clock_ms = SystemTime.milliseconds();
887 let time = Arc::new(MockTime::with_auto_tick_ms_current_time_ms_current_high_res_time_ns(
888 0,
889 wall_clock_ms,
890 0,
891 ));
892 let metrics = Metrics::with_time(Arc::clone(&time) as Arc<dyn Time>);
893 let config = MetricConfig::new()
894 .set_quota(Quota::upper_bound(2.0))
895 .set_time_window(1, TimeUnit::Seconds)
896 .set_samples(11);
897 let sensor = metrics
898 .sensor_with_config_parents("sensor", Some(Arc::new(config)), &[])
899 .unwrap();
900 let metric_name = metrics.metric_name("rate", "test-group");
901 assert!(sensor.add_with_metric_name(metric_name.clone(), Box::new(Rate::new())).unwrap());
902 let rate_metric = metrics.metric(&metric_name).unwrap();
903 let strict_record = |value: f64, time_ms: i64| -> Result<(), Error> {
904 sensor.check_quotas_with_time_ms(time_ms)?;
905 sensor.record_with_value_time_ms_check_quotas(value, time_ms, false)
906 };
907
908 strict_record(30.0, time.milliseconds()).unwrap();
911 assert!((rate_metric.measurable_value(time.milliseconds()) - 3.0).abs() <= 0.1);
912
913 time.sleep(5000);
916
917 assert!((rate_metric.measurable_value(time.milliseconds()) - 3.0).abs() <= 0.1);
920 let error = strict_record(30.0, time.milliseconds()).expect_err("the rate is still above the quota");
921 assert!(error.is_quota_violation_error());
922 }
923
924 struct RecordingStat(Arc<Mutex<Vec<StatCall>>>);
928
929 type StatCall = (&'static str, f64, f64, i64);
931
932 impl Stat for RecordingStat {
933 fn record(&self, config: &MetricConfig, value: f64, time_ms: i64) {
934 let bound = config.quota().map_or(f64::NAN, Quota::bound);
935 self.0.lock().unwrap().push(("record", bound, value, time_ms));
936 }
937 }
938
939 impl Measurable for RecordingStat {
940 fn measure(&self, config: &MetricConfig, now: i64) -> f64 {
941 let bound = config.quota().map_or(f64::NAN, Quota::bound);
942 self.0.lock().unwrap().push(("measure", bound, 0.0, now));
943 0.0
944 }
945 }
946
947 #[test]
949 #[doc(alias = "org.apache.kafka.common.metrics.SensorTest#testRecordAndCheckQuotaUseMetricConfigOfEachStat")]
950 fn test_record_and_check_quota_use_metric_config_of_each_stat() {
951 use crate::common::metrics::Metrics;
952 let metrics = Metrics::with_time(Arc::new(MockTime::new()));
953 let sensor = metrics.sensor("sensor").unwrap();
954
955 let calls1 = Arc::new(Mutex::new(Vec::new()));
956 let stat1_config = Arc::new(MetricConfig::new().set_quota(Quota::upper_bound(5.0)));
957 sensor
958 .add_with_metric_name_config(
959 metrics.metric_name("stat1", "test-group"),
960 Box::new(RecordingStat(Arc::clone(&calls1))),
961 Some(stat1_config),
962 )
963 .unwrap();
964 let calls2 = Arc::new(Mutex::new(Vec::new()));
965 let stat2_config = Arc::new(MetricConfig::new().set_quota(Quota::upper_bound(10.0)));
966 sensor
967 .add_with_metric_name_config(
968 metrics.metric_name("stat2", "test-group"),
969 Box::new(RecordingStat(Arc::clone(&calls2))),
970 Some(stat2_config),
971 )
972 .unwrap();
973
974 sensor.record_with_value_time_ms(10.0, 1).unwrap();
975 assert!(calls1.lock().unwrap().contains(&("record", 5.0, 10.0, 1)));
976 assert!(calls2.lock().unwrap().contains(&("record", 10.0, 10.0, 1)));
977
978 sensor.check_quotas_with_time_ms(2).unwrap();
979 assert!(calls1.lock().unwrap().contains(&("measure", 5.0, 0.0, 2)));
980 assert!(calls2.lock().unwrap().contains(&("measure", 10.0, 0.0, 2)));
981 }
982
983 #[test]
985 #[doc(alias = "org.apache.kafka.common.metrics.SensorTest#testUpdatingMetricConfigIsReflectedInTheSensor")]
986 fn test_updating_metric_config_is_reflected_in_the_sensor() {
987 use crate::common::metrics::Metrics;
988 let metrics = Metrics::with_time(Arc::new(MockTime::new()));
989 let sensor = metrics.sensor("sensor").unwrap();
990
991 let calls = Arc::new(Mutex::new(Vec::new()));
992 let stat_name = metrics.metric_name("stat", "test-group");
993 let stat_config = Arc::new(MetricConfig::new().set_quota(Quota::upper_bound(5.0)));
994 sensor
995 .add_with_metric_name_config(
996 stat_name.clone(),
997 Box::new(RecordingStat(Arc::clone(&calls))),
998 Some(stat_config),
999 )
1000 .unwrap();
1001
1002 sensor.record_with_value_time_ms(10.0, 1).unwrap();
1003 assert!(calls.lock().unwrap().contains(&("record", 5.0, 10.0, 1)));
1004
1005 sensor.check_quotas_with_time_ms(2).unwrap();
1006 assert!(calls.lock().unwrap().contains(&("measure", 5.0, 0.0, 2)));
1007
1008 let new_config = Arc::new(MetricConfig::new().set_quota(Quota::upper_bound(10.0)));
1010 metrics.metric(&stat_name).unwrap().set_config(new_config);
1011
1012 sensor.record_with_value_time_ms(10.0, 3).unwrap();
1013 assert!(calls.lock().unwrap().contains(&("record", 10.0, 10.0, 3)));
1014
1015 sensor.check_quotas_with_time_ms(4).unwrap();
1016 assert!(calls.lock().unwrap().contains(&("measure", 10.0, 0.0, 4)));
1017 }
1018
1019 #[test]
1022 fn test_check_quotas_is_a_no_op_without_a_quota() {
1023 let sensor = standalone(info_config(), 60, RecordingLevel::Info);
1024 assert!(
1025 sensor
1026 .add_with_metric_name(name("value", "test-group"), Box::new(Value::new()))
1027 .unwrap()
1028 );
1029 sensor.record_with_value_time_ms(f64::MAX, 1).unwrap();
1030 assert!(sensor.check_quotas_with_time_ms(1).is_ok());
1031 assert!(sensor.check_quotas().is_ok());
1032 }
1033
1034 #[test]
1038 fn test_check_quotas_in_multiple_threads() {
1039 let sensor = Arc::new(standalone(quota_config(Quota::upper_bound(f64::MAX)), 60, RecordingLevel::Info));
1040 assert!(
1041 sensor
1042 .add_with_metric_name(name("test-metric", "test-group"), Box::new(Value::new()))
1043 .unwrap()
1044 );
1045
1046 let mut handles = Vec::new();
1047 for index in 0..10i64 {
1048 let sensor = Arc::clone(&sensor);
1049 handles.push(std::thread::spawn(move || {
1050 for j in 0..20i64 {
1051 sensor
1052 .record_with_value_time_ms_check_quotas((j * index) as f64, j, false)
1053 .unwrap();
1054 sensor.check_quotas().expect("an upper bound of f64::MAX is never violated");
1055 }
1056 }));
1057 }
1058 for handle in handles {
1059 handle.join().expect("check_quotas must be safe to call from many tasks");
1060 }
1061 }
1062
1063 #[test]
1065 #[doc(alias = "org.apache.kafka.common.metrics.SensorTest#testRecordLevelEnum")]
1066 fn test_record_level_enum() {
1067 let config_level = RecordingLevel::Info;
1068 assert!(RecordingLevel::Info.should_record(level_id(config_level)));
1069 assert!(!RecordingLevel::Debug.should_record(level_id(config_level)));
1070 assert!(!RecordingLevel::Trace.should_record(level_id(config_level)));
1071
1072 let config_level = RecordingLevel::Debug;
1073 assert!(RecordingLevel::Info.should_record(level_id(config_level)));
1074 assert!(RecordingLevel::Debug.should_record(level_id(config_level)));
1075 assert!(!RecordingLevel::Trace.should_record(level_id(config_level)));
1076
1077 let config_level = RecordingLevel::Trace;
1078 assert!(RecordingLevel::Info.should_record(level_id(config_level)));
1079 assert!(RecordingLevel::Debug.should_record(level_id(config_level)));
1080 assert!(RecordingLevel::Trace.should_record(level_id(config_level)));
1081
1082 assert_eq!(
1083 RecordingLevel::Debug,
1084 RecordingLevel::for_name(RecordingLevel::Debug.name()).unwrap()
1085 );
1086 assert_eq!(
1087 RecordingLevel::Info,
1088 RecordingLevel::for_name(RecordingLevel::Info.name()).unwrap()
1089 );
1090 assert_eq!(
1091 RecordingLevel::Trace,
1092 RecordingLevel::for_name(RecordingLevel::Trace.name()).unwrap()
1093 );
1094 }
1095
1096 #[test]
1098 #[doc(alias = "org.apache.kafka.common.metrics.SensorTest#testShouldRecordForInfoLevelSensor")]
1099 fn test_should_record_for_info_level_sensor() {
1100 assert!(standalone(info_config(), 0, RecordingLevel::Info).should_record());
1101 assert!(standalone(debug_config(), 0, RecordingLevel::Info).should_record());
1102 assert!(standalone(trace_config(), 0, RecordingLevel::Info).should_record());
1103 }
1104
1105 #[test]
1107 #[doc(alias = "org.apache.kafka.common.metrics.SensorTest#testShouldRecordForDebugLevelSensor")]
1108 fn test_should_record_for_debug_level_sensor() {
1109 assert!(!standalone(info_config(), 0, RecordingLevel::Debug).should_record());
1110 assert!(standalone(debug_config(), 0, RecordingLevel::Debug).should_record());
1111 assert!(standalone(trace_config(), 0, RecordingLevel::Debug).should_record());
1112 }
1113
1114 #[test]
1116 #[doc(alias = "org.apache.kafka.common.metrics.SensorTest#testShouldRecordForTraceLevelSensor")]
1117 fn test_should_record_for_trace_level_sensor() {
1118 assert!(!standalone(info_config(), 0, RecordingLevel::Trace).should_record());
1119 assert!(!standalone(debug_config(), 0, RecordingLevel::Trace).should_record());
1120 assert!(standalone(trace_config(), 0, RecordingLevel::Trace).should_record());
1121 }
1122
1123 #[test]
1125 #[doc(alias = "org.apache.kafka.common.metrics.SensorTest#shouldReturnPresenceOfMetrics")]
1126 fn should_return_presence_of_metrics() {
1127 let sensor = standalone(Arc::new(MetricConfig::new()), i64::MAX, RecordingLevel::Info);
1128 assert!(!sensor.has_metrics());
1129 sensor
1130 .add_with_metric_name(name("name1", "group1"), Box::new(CumulativeCount::new()))
1131 .unwrap();
1132 assert!(sensor.has_metrics());
1133 sensor
1134 .add_with_metric_name(name("name2", "group2"), Box::new(Value::new()))
1135 .unwrap();
1136 assert!(sensor.has_metrics());
1137 }
1138
1139 #[test]
1150 #[doc(alias = "org.apache.kafka.common.metrics.SensorTest#testExpiredSensor")]
1151 fn test_expired_sensor() {
1152 let time = Arc::new(MockTime::new());
1153 let inactive_sensor_expiration_time_seconds = 60i64;
1154 let sensor = Sensor::new(
1155 None,
1156 "sensor",
1157 Vec::new(),
1158 Arc::new(MetricConfig::new()),
1159 Arc::clone(&time) as Arc<dyn Time>,
1160 inactive_sensor_expiration_time_seconds,
1161 RecordingLevel::Info,
1162 )
1163 .unwrap();
1164
1165 assert!(
1167 sensor
1168 .add_with_metric_name(name("test1", "grp1"), Box::new(CumulativeCount::new()))
1169 .unwrap()
1170 );
1171 assert!(
1172 sensor
1173 .add_with_metric_name(name("test2", "grp1"), Box::new(Value::new()))
1174 .unwrap()
1175 );
1176 assert_eq!(2, sensor.metrics().len());
1177 assert!(!sensor.has_expired());
1178
1179 time.sleep((inactive_sensor_expiration_time_seconds + 1) * 1000);
1181 assert!(sensor.has_expired());
1182
1183 assert!(
1186 !sensor
1187 .add_with_metric_name(name("test3", "grp1"), Box::new(CumulativeCount::new()))
1188 .unwrap()
1189 );
1190 assert!(
1191 !sensor
1192 .add_with_metric_name_config(name("test4", "grp1"), Box::new(Value::new()), None)
1193 .unwrap()
1194 );
1195 assert_eq!(2, sensor.metrics().len());
1196
1197 sensor.record_with_value(1.0).unwrap();
1201 assert!(!sensor.has_expired());
1202 assert_eq!(2, sensor.metrics().len());
1203 }
1204
1205 #[test]
1224 #[ignore = "micro-bench; run with --ignored --nocapture"]
1225 fn bench_sensor_record_ns() {
1226 use crate::common::metrics::stats::{Avg, Max, Meter};
1227 use std::time::Instant;
1228
1229 let time = Arc::new(MockTime::new());
1230 let sensor = Sensor::new(
1231 None,
1232 "bench",
1233 Vec::new(),
1234 info_config(),
1235 Arc::clone(&time) as Arc<dyn Time>,
1236 i64::MAX,
1237 RecordingLevel::Info,
1238 )
1239 .unwrap();
1240 sensor.add(Box::new(Meter::new(name("rate", "g"), name("total", "g")))).unwrap();
1242 sensor.add_with_metric_name(name("avg", "g"), Box::new(Avg::new())).unwrap();
1243 sensor.add_with_metric_name(name("max", "g"), Box::new(Max::new())).unwrap();
1244
1245 let mut now = time.milliseconds();
1248 for i in 0..10_000 {
1249 sensor.record_with_value_time_ms(i as f64, now).unwrap();
1250 }
1251
1252 const ITERS: u64 = 2_000_000;
1253 let start = Instant::now();
1254 for i in 0..ITERS {
1255 if i % 4096 == 0 {
1258 now += 1;
1259 }
1260 sensor.record_with_value_time_ms(i as f64, now).unwrap();
1261 }
1262 let elapsed = start.elapsed();
1263 let ns_per_call = elapsed.as_nanos() as f64 / ITERS as f64;
1264 eprintln!(
1265 "M8 micro-bench: Sensor::record_with_value_time_ms over Meter+Avg+Max = {ns_per_call:.1} ns/call \
1266 ({ITERS} iters in {elapsed:?})"
1267 );
1268 assert!(ns_per_call > 0.0);
1271 }
1272}