|
| 1 | +use std::time::Instant; |
| 2 | +use tokio::{ |
| 3 | + sync::oneshot, |
| 4 | + time::{Duration, timeout}, |
| 5 | +}; |
| 6 | +use vector_lib::metrics::Controller; |
| 7 | + |
| 8 | +use crate::{ |
| 9 | + config::Config, |
| 10 | + event::{Event, LogEvent, Metric, MetricValue}, |
| 11 | + test_util::{ |
| 12 | + mock::{ |
| 13 | + basic_source, |
| 14 | + sinks::CompletionSinkConfig, |
| 15 | + transforms::{NoopTransformConfig, TransformType}, |
| 16 | + }, |
| 17 | + start_topology, trace_init, |
| 18 | + }, |
| 19 | +}; |
| 20 | + |
| 21 | +const EVENT_COUNT: usize = 100; |
| 22 | +const TRANSFORM_DELAY_MS: u64 = 10; |
| 23 | +const SOURCE_ID: &str = "latency_source"; |
| 24 | +const TRANSFORM_ID: &str = "latency_delay"; |
| 25 | +const TRANSFORM_TYPE: &str = "test_noop"; |
| 26 | +const TRANSFORM_KIND: &str = "transform"; |
| 27 | +const SINK_ID: &str = "latency_sink"; |
| 28 | + |
| 29 | +struct LatencyTestRun { |
| 30 | + metrics: Vec<Metric>, |
| 31 | + elapsed_time: f64, |
| 32 | +} |
| 33 | + |
| 34 | +#[tokio::test] |
| 35 | +async fn component_latency_metrics_emitted() { |
| 36 | + let run = run_latency_topology().await; |
| 37 | + |
| 38 | + assert_histogram_count( |
| 39 | + &run.metrics, |
| 40 | + "component_latency_seconds", |
| 41 | + has_component_tags, |
| 42 | + ); |
| 43 | + assert_gauge_range( |
| 44 | + &run.metrics, |
| 45 | + "component_latency_mean_seconds", |
| 46 | + has_component_tags, |
| 47 | + TRANSFORM_DELAY_MS as f64 / 1000.0, |
| 48 | + run.elapsed_time, |
| 49 | + ); |
| 50 | +} |
| 51 | + |
| 52 | +async fn run_latency_topology() -> LatencyTestRun { |
| 53 | + trace_init(); |
| 54 | + |
| 55 | + let controller = Controller::get().expect("metrics controller"); |
| 56 | + controller.reset(); |
| 57 | + |
| 58 | + let (mut source_tx, source_config) = basic_source(); |
| 59 | + let transform_config = |
| 60 | + NoopTransformConfig::from(TransformType::Task).with_delay_ms(TRANSFORM_DELAY_MS); |
| 61 | + let (sink_done_tx, sink_done_rx) = oneshot::channel(); |
| 62 | + let sink_config = CompletionSinkConfig::new(EVENT_COUNT, sink_done_tx); |
| 63 | + |
| 64 | + let mut config = Config::builder(); |
| 65 | + config.add_source(SOURCE_ID, source_config); |
| 66 | + config.add_transform(TRANSFORM_ID, &[SOURCE_ID], transform_config); |
| 67 | + config.add_sink(SINK_ID, &[TRANSFORM_ID], sink_config); |
| 68 | + |
| 69 | + let start_time = Instant::now(); |
| 70 | + let (topology, _) = start_topology(config.build().unwrap(), false).await; |
| 71 | + |
| 72 | + for idx in 0..EVENT_COUNT { |
| 73 | + let event = Event::Log(LogEvent::from(format!("payload-{idx}"))); |
| 74 | + source_tx.send_event(event).await.unwrap(); |
| 75 | + } |
| 76 | + |
| 77 | + drop(source_tx); |
| 78 | + |
| 79 | + let completed = timeout(Duration::from_secs(5), sink_done_rx) |
| 80 | + .await |
| 81 | + .expect("timed out waiting for completion sink to finish") |
| 82 | + .expect("completion sink sender dropped"); |
| 83 | + assert!( |
| 84 | + completed, |
| 85 | + "completion sink finished before receiving all events" |
| 86 | + ); |
| 87 | + |
| 88 | + topology.stop().await; |
| 89 | + let elapsed_time = start_time.elapsed().as_secs_f64(); |
| 90 | + |
| 91 | + LatencyTestRun { |
| 92 | + metrics: controller.capture_metrics(), |
| 93 | + elapsed_time, |
| 94 | + } |
| 95 | +} |
| 96 | + |
| 97 | +fn assert_histogram_count(metrics: &[Metric], metric_name: &str, tags_match: fn(&Metric) -> bool) { |
| 98 | + let histogram = metrics |
| 99 | + .iter() |
| 100 | + .find(|metric| metric.name() == metric_name && tags_match(metric)) |
| 101 | + .unwrap_or_else(|| panic!("{metric_name} histogram missing")); |
| 102 | + |
| 103 | + match histogram.value() { |
| 104 | + MetricValue::AggregatedHistogram { count, .. } => { |
| 105 | + assert_eq!( |
| 106 | + *count, EVENT_COUNT as u64, |
| 107 | + "histogram count should match number of events" |
| 108 | + ); |
| 109 | + } |
| 110 | + other => panic!("expected aggregated histogram, got {other:?}"), |
| 111 | + } |
| 112 | +} |
| 113 | + |
| 114 | +fn assert_gauge_range( |
| 115 | + metrics: &[Metric], |
| 116 | + metric_name: &str, |
| 117 | + tags_match: fn(&Metric) -> bool, |
| 118 | + expected_min: f64, |
| 119 | + elapsed_time: f64, |
| 120 | +) { |
| 121 | + let gauge = metrics |
| 122 | + .iter() |
| 123 | + .find(|metric| metric.name() == metric_name && tags_match(metric)) |
| 124 | + .unwrap_or_else(|| panic!("{metric_name} gauge missing")); |
| 125 | + |
| 126 | + match gauge.value() { |
| 127 | + MetricValue::Gauge { value } => { |
| 128 | + assert!( |
| 129 | + *value >= expected_min, |
| 130 | + "expected mean latency to be >= {expected_min}, got {value}" |
| 131 | + ); |
| 132 | + assert!( |
| 133 | + *value < elapsed_time, |
| 134 | + "expected mean latency ({value}) to be less than elapsed time ({elapsed_time})" |
| 135 | + ); |
| 136 | + } |
| 137 | + other => panic!("expected gauge metric, got {other:?}"), |
| 138 | + } |
| 139 | +} |
| 140 | + |
| 141 | +fn has_component_tags(metric: &Metric) -> bool { |
| 142 | + metric.tags().is_some_and(|tags| { |
| 143 | + tags.get("component_id") == Some(TRANSFORM_ID) |
| 144 | + && tags.get("component_type") == Some(TRANSFORM_TYPE) |
| 145 | + && tags.get("component_kind") == Some(TRANSFORM_KIND) |
| 146 | + }) |
| 147 | +} |
0 commit comments