@@ -14,24 +14,67 @@ use crate::{
1414 } ,
1515} ;
1616
17+ const EVENT_COUNT : usize = 100 ;
18+ const SOURCE_ID : & str = "latency_source" ;
19+ const TRANSFORM_ID : & str = "latency_delay" ;
20+ const TRANSFORM_TYPE : & str = "test_noop" ;
21+ const TRANSFORM_KIND : & str = "transform" ;
22+ const SINK_ID : & str = "latency_sink" ;
23+
24+ struct ProcessingTimeTestRun {
25+ metrics : Vec < Metric > ,
26+ elapsed_time : f64 ,
27+ }
28+
1729#[ tokio:: test]
1830async fn sink_processing_time_metrics_emitted ( ) {
31+ let run = run_processing_time_topology ( ) . await ;
32+
33+ assert_histogram_count (
34+ & run. metrics ,
35+ "event_processing_time_seconds" ,
36+ has_latency_tags,
37+ ) ;
38+ assert_gauge_range (
39+ & run. metrics ,
40+ "event_processing_time_mean_seconds" ,
41+ has_latency_tags,
42+ run. elapsed_time ,
43+ ) ;
44+ }
45+
46+ #[ tokio:: test]
47+ async fn component_processing_time_metrics_emitted ( ) {
48+ let run = run_processing_time_topology ( ) . await ;
49+
50+ assert_histogram_count (
51+ & run. metrics ,
52+ "component_processing_time_seconds" ,
53+ has_component_tags,
54+ ) ;
55+ assert_gauge_range (
56+ & run. metrics ,
57+ "component_processing_time_mean_seconds" ,
58+ has_component_tags,
59+ run. elapsed_time ,
60+ ) ;
61+ }
62+
63+ async fn run_processing_time_topology ( ) -> ProcessingTimeTestRun {
1964 trace_init ( ) ;
2065
2166 let controller = Controller :: get ( ) . expect ( "metrics controller" ) ;
2267 controller. reset ( ) ;
2368
24- const EVENT_COUNT : usize = 100 ;
25-
2669 let ( mut source_tx, source_config) = basic_source ( ) ;
2770 let transform_config = noop_transform ( ) ;
2871 let ( sink_done_tx, sink_done_rx) = oneshot:: channel ( ) ;
2972 let sink_config = completion_sink ( EVENT_COUNT , sink_done_tx) ;
3073
3174 let mut config = Config :: builder ( ) ;
32- config. add_source ( "latency_source" , source_config) ;
33- config. add_transform ( "latency_delay" , & [ "latency_source" ] , transform_config) ;
34- config. add_sink ( "latency_sink" , & [ "latency_delay" ] , sink_config) ;
75+ config. add_source ( SOURCE_ID , source_config) ;
76+ config. add_transform ( TRANSFORM_ID , & [ SOURCE_ID ] , transform_config) ;
77+ config. add_sink ( SINK_ID , & [ TRANSFORM_ID ] , sink_config) ;
3578
3679 let start_time = Instant :: now ( ) ;
3780 let ( topology, _) = start_topology ( config. build ( ) . unwrap ( ) , false ) . await ;
@@ -55,17 +98,17 @@ async fn sink_processing_time_metrics_emitted() {
5598 topology. stop ( ) . await ;
5699 let elapsed_time = start_time. elapsed ( ) . as_secs_f64 ( ) ;
57100
58- let metrics = controller. capture_metrics ( ) ;
59- let sink_id = "latency_sink" ;
60- let source_id = "latency_source" ;
101+ ProcessingTimeTestRun {
102+ metrics : controller. capture_metrics ( ) ,
103+ elapsed_time,
104+ }
105+ }
61106
107+ fn assert_histogram_count ( metrics : & [ Metric ] , metric_name : & str , tags_match : fn ( & Metric ) -> bool ) {
62108 let histogram = metrics
63109 . iter ( )
64- . find ( |metric| {
65- metric. name ( ) == "event_processing_time_seconds"
66- && has_latency_tags ( metric, sink_id, source_id)
67- } )
68- . expect ( "event_processing_time_seconds histogram missing" ) ;
110+ . find ( |metric| metric. name ( ) == metric_name && tags_match ( metric) )
111+ . unwrap_or_else ( || panic ! ( "{metric_name} histogram missing" ) ) ;
69112
70113 match histogram. value ( ) {
71114 MetricValue :: AggregatedHistogram { count, .. } => {
@@ -76,14 +119,18 @@ async fn sink_processing_time_metrics_emitted() {
76119 }
77120 other => panic ! ( "expected aggregated histogram, got {other:?}" ) ,
78121 }
122+ }
79123
124+ fn assert_gauge_range (
125+ metrics : & [ Metric ] ,
126+ metric_name : & str ,
127+ tags_match : fn ( & Metric ) -> bool ,
128+ elapsed_time : f64 ,
129+ ) {
80130 let gauge = metrics
81131 . iter ( )
82- . find ( |metric| {
83- metric. name ( ) == "event_processing_time_mean_seconds"
84- && has_latency_tags ( metric, sink_id, source_id)
85- } )
86- . expect ( "event_processing_time_mean_seconds gauge missing" ) ;
132+ . find ( |metric| metric. name ( ) == metric_name && tags_match ( metric) )
133+ . unwrap_or_else ( || panic ! ( "{metric_name} gauge missing" ) ) ;
87134
88135 match gauge. value ( ) {
89136 MetricValue :: Gauge { value } => {
@@ -100,9 +147,17 @@ async fn sink_processing_time_metrics_emitted() {
100147 }
101148}
102149
103- fn has_latency_tags ( metric : & Metric , sink : & str , source : & str ) -> bool {
150+ fn has_latency_tags ( metric : & Metric ) -> bool {
151+ metric. tags ( ) . is_some_and ( |tags| {
152+ tags. get ( "source_component_id" ) == Some ( SOURCE_ID )
153+ && tags. get ( "sink_component_id" ) == Some ( SINK_ID )
154+ } )
155+ }
156+
157+ fn has_component_tags ( metric : & Metric ) -> bool {
104158 metric. tags ( ) . is_some_and ( |tags| {
105- tags. get ( "source_component_id" ) == Some ( source)
106- && tags. get ( "sink_component_id" ) == Some ( sink)
159+ tags. get ( "component_id" ) == Some ( TRANSFORM_ID )
160+ && tags. get ( "component_type" ) == Some ( TRANSFORM_TYPE )
161+ && tags. get ( "component_kind" ) == Some ( TRANSFORM_KIND )
107162 } )
108163}
0 commit comments