|
| 1 | +use serde::{Deserialize, Serialize}; |
| 2 | + |
| 3 | +use crate::{ |
| 4 | + conditions::{Condition, ConditionConfig, ConditionDescription}, |
| 5 | + Event, |
| 6 | +}; |
| 7 | + |
| 8 | +//------------------------------------------------------------------------------ |
| 9 | + |
| 10 | +#[derive(Deserialize, Serialize, Debug, Default, Clone)] |
| 11 | +pub struct IsMetricConfig {} |
| 12 | + |
| 13 | +inventory::submit! { |
| 14 | + ConditionDescription::new::<IsMetricConfig>("is_metric") |
| 15 | +} |
| 16 | + |
| 17 | +#[typetag::serde(name = "is_metric")] |
| 18 | +impl ConditionConfig for IsMetricConfig { |
| 19 | + fn build(&self) -> crate::Result<Box<dyn Condition>> { |
| 20 | + Ok(Box::new(IsMetric {})) |
| 21 | + } |
| 22 | +} |
| 23 | + |
| 24 | +//------------------------------------------------------------------------------ |
| 25 | + |
| 26 | +pub struct IsMetric {} |
| 27 | + |
| 28 | +impl Condition for IsMetric { |
| 29 | + fn check(&self, e: &Event) -> bool { |
| 30 | + match e { |
| 31 | + Event::Metric(_) => true, |
| 32 | + _ => false, |
| 33 | + } |
| 34 | + } |
| 35 | + |
| 36 | + fn check_with_context(&self, e: &Event) -> Result<(), String> { |
| 37 | + if self.check(e) { |
| 38 | + Ok(()) |
| 39 | + } else { |
| 40 | + Err("event is not a metric type".to_string()) |
| 41 | + } |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +//------------------------------------------------------------------------------ |
| 46 | + |
| 47 | +#[cfg(test)] |
| 48 | +mod test { |
| 49 | + use super::*; |
| 50 | + use crate::{ |
| 51 | + event::metric::{Metric, MetricKind, MetricValue}, |
| 52 | + Event, |
| 53 | + }; |
| 54 | + |
| 55 | + #[test] |
| 56 | + fn is_metric_basic() { |
| 57 | + let cond = IsMetricConfig {}.build().unwrap(); |
| 58 | + |
| 59 | + assert_eq!(cond.check(&Event::from("just a log")), false); |
| 60 | + assert_eq!( |
| 61 | + cond.check(&Event::from(Metric { |
| 62 | + name: "test metric".to_string(), |
| 63 | + timestamp: None, |
| 64 | + tags: None, |
| 65 | + kind: MetricKind::Incremental, |
| 66 | + value: MetricValue::Counter { value: 1.0 }, |
| 67 | + })), |
| 68 | + true |
| 69 | + ); |
| 70 | + } |
| 71 | +} |
0 commit comments