Skip to main content

risingwave_stream/executor/monitor/
streaming_stats.rs

1// Copyright 2022 RisingWave Labs
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15use std::sync::OnceLock;
16
17use prometheus::{
18    Histogram, IntCounter, IntGauge, Registry, exponential_buckets, histogram_opts,
19    register_histogram_with_registry, register_int_counter_with_registry,
20    register_int_gauge_with_registry,
21};
22use risingwave_common::catalog::TableId;
23use risingwave_common::config::MetricLevel;
24use risingwave_common::metrics::{
25    GaugeAggregation, IntGaugeVecRelabelExt, LabelGuardedHistogramVec, LabelGuardedIntCounter,
26    LabelGuardedIntCounterVec, LabelGuardedIntGaugeVec, MetricVecRelabelExt,
27    RelabeledAggregatedIntGauge, RelabeledAggregatedIntGaugeVec, RelabeledGuardedHistogramVec,
28    RelabeledGuardedIntCounterVec,
29};
30use risingwave_common::monitor::GLOBAL_METRICS_REGISTRY;
31use risingwave_common::monitor::in_mem::CountMap;
32use risingwave_common::{
33    register_guarded_histogram_vec_with_registry, register_guarded_int_counter_vec_with_registry,
34    register_guarded_int_gauge_vec_with_registry,
35};
36use risingwave_connector::sink::catalog::SinkId;
37use risingwave_pb::id::ExecutorId;
38
39use crate::common::log_store_impl::kv_log_store::{
40    REWIND_BACKOFF_MULTIPLIER, REWIND_INITIAL_DELAY, REWIND_MAX_DELAY,
41};
42use crate::executor::prelude::ActorId;
43use crate::task::FragmentId;
44
45#[derive(Clone)]
46pub struct StreamingMetrics {
47    pub level: MetricLevel,
48
49    // Executor metrics (disabled by default)
50    pub executor_row_count: RelabeledGuardedIntCounterVec,
51
52    // Profiling Metrics:
53    // Aggregated per operator rather than per actor.
54    // These are purely in-memory, never collected by prometheus.
55    pub mem_stream_node_output_row_count: CountMap<ExecutorId>,
56    pub mem_stream_node_output_blocking_duration_ns: CountMap<ExecutorId>,
57
58    // Streaming actor metrics from tokio (disabled by default)
59    actor_scheduled_duration: RelabeledGuardedIntCounterVec,
60    actor_scheduled_cnt: RelabeledGuardedIntCounterVec,
61    actor_poll_duration: RelabeledGuardedIntCounterVec,
62    actor_poll_cnt: RelabeledGuardedIntCounterVec,
63    actor_idle_duration: RelabeledGuardedIntCounterVec,
64    actor_idle_cnt: RelabeledGuardedIntCounterVec,
65
66    // Streaming actor
67    pub actor_count: LabelGuardedIntGaugeVec,
68    pub actor_in_record_cnt: RelabeledGuardedIntCounterVec,
69    pub actor_out_record_cnt: RelabeledGuardedIntCounterVec,
70    pub fragment_channel_buffered_bytes: LabelGuardedIntGaugeVec,
71    pub actor_current_epoch: RelabeledAggregatedIntGaugeVec,
72    pub project_expr_inflight_window_size: RelabeledAggregatedIntGaugeVec,
73
74    // Source
75    pub source_output_row_count: RelabeledGuardedIntCounterVec,
76    pub source_split_change_count: RelabeledGuardedIntCounterVec,
77    pub source_backfill_row_count: RelabeledGuardedIntCounterVec,
78
79    // Sink
80    sink_input_row_count: RelabeledGuardedIntCounterVec,
81    sink_input_bytes: RelabeledGuardedIntCounterVec,
82    sink_chunk_buffer_size: RelabeledAggregatedIntGaugeVec,
83
84    // Exchange (see also `compute::ExchangeServiceMetrics`)
85    pub exchange_frag_recv_size: LabelGuardedIntCounterVec,
86
87    // Streaming Merge (We breakout this metric from `barrier_align_duration` because
88    // the alignment happens on different levels)
89    pub merge_barrier_align_duration: RelabeledGuardedIntCounterVec,
90
91    // Backpressure
92    pub actor_output_buffer_blocking_duration_ns: RelabeledGuardedIntCounterVec,
93    actor_input_buffer_blocking_duration_ns: RelabeledGuardedIntCounterVec,
94
95    // Streaming Join
96    pub join_lookup_miss_count: RelabeledGuardedIntCounterVec,
97    pub join_lookup_total_count: RelabeledGuardedIntCounterVec,
98    pub join_insert_cache_miss_count: RelabeledGuardedIntCounterVec,
99    pub join_actor_input_waiting_duration_ns: RelabeledGuardedIntCounterVec,
100    pub join_match_duration_ns: RelabeledGuardedIntCounterVec,
101    pub join_cached_entry_count: RelabeledAggregatedIntGaugeVec,
102    pub join_matched_join_keys: RelabeledGuardedHistogramVec,
103
104    // Streaming Join, Streaming Dynamic Filter and Streaming Union
105    pub barrier_align_duration: RelabeledGuardedIntCounterVec,
106
107    // Streaming Aggregation
108    agg_lookup_miss_count: RelabeledGuardedIntCounterVec,
109    agg_total_lookup_count: RelabeledGuardedIntCounterVec,
110    agg_cached_entry_count: RelabeledAggregatedIntGaugeVec,
111    agg_chunk_lookup_miss_count: RelabeledGuardedIntCounterVec,
112    agg_chunk_total_lookup_count: RelabeledGuardedIntCounterVec,
113    agg_dirty_groups_count: RelabeledAggregatedIntGaugeVec,
114    agg_dirty_groups_heap_size: RelabeledAggregatedIntGaugeVec,
115    agg_distinct_cache_miss_count: RelabeledGuardedIntCounterVec,
116    agg_distinct_total_cache_count: RelabeledGuardedIntCounterVec,
117    agg_distinct_cached_entry_count: RelabeledAggregatedIntGaugeVec,
118    agg_state_cache_lookup_count: RelabeledGuardedIntCounterVec,
119    agg_state_cache_miss_count: RelabeledGuardedIntCounterVec,
120
121    // Streaming TopN
122    group_top_n_cache_miss_count: RelabeledGuardedIntCounterVec,
123    group_top_n_total_query_cache_count: RelabeledGuardedIntCounterVec,
124    group_top_n_cached_entry_count: RelabeledAggregatedIntGaugeVec,
125    // TODO(rc): why not just use the above three?
126    group_top_n_appendonly_cache_miss_count: RelabeledGuardedIntCounterVec,
127    group_top_n_appendonly_total_query_cache_count: RelabeledGuardedIntCounterVec,
128    group_top_n_appendonly_cached_entry_count: RelabeledAggregatedIntGaugeVec,
129
130    // Lookup executor
131    lookup_cache_miss_count: RelabeledGuardedIntCounterVec,
132    lookup_total_query_cache_count: RelabeledGuardedIntCounterVec,
133    lookup_cached_entry_count: RelabeledAggregatedIntGaugeVec,
134
135    // temporal join
136    temporal_join_cache_miss_count: RelabeledGuardedIntCounterVec,
137    temporal_join_total_query_cache_count: RelabeledGuardedIntCounterVec,
138    temporal_join_cached_entry_count: RelabeledAggregatedIntGaugeVec,
139
140    // Backfill
141    backfill_snapshot_read_row_count: RelabeledGuardedIntCounterVec,
142    backfill_upstream_output_row_count: RelabeledGuardedIntCounterVec,
143
144    // CDC Backfill
145    cdc_backfill_snapshot_read_row_count: RelabeledGuardedIntCounterVec,
146    cdc_backfill_upstream_output_row_count: RelabeledGuardedIntCounterVec,
147
148    // Snapshot Backfill
149    pub(crate) snapshot_backfill_consume_row_count: RelabeledGuardedIntCounterVec,
150
151    // Over Window
152    over_window_cached_entry_count: RelabeledAggregatedIntGaugeVec,
153    over_window_cache_lookup_count: RelabeledGuardedIntCounterVec,
154    over_window_cache_miss_count: RelabeledGuardedIntCounterVec,
155    over_window_range_cache_entry_count: RelabeledAggregatedIntGaugeVec,
156    over_window_range_cache_lookup_count: RelabeledGuardedIntCounterVec,
157    over_window_range_cache_left_miss_count: RelabeledGuardedIntCounterVec,
158    over_window_range_cache_right_miss_count: RelabeledGuardedIntCounterVec,
159    over_window_accessed_entry_count: RelabeledGuardedIntCounterVec,
160    over_window_compute_count: RelabeledGuardedIntCounterVec,
161    over_window_same_output_count: RelabeledGuardedIntCounterVec,
162    over_window_state_cleaned_row_count: RelabeledGuardedIntCounterVec,
163
164    // Match recognize
165    match_recognize_matches_emitted_count: RelabeledGuardedIntCounterVec,
166    match_recognize_evicted_rows_count: RelabeledGuardedIntCounterVec,
167    match_recognize_scan_budget_exhausted_count: RelabeledGuardedIntCounterVec,
168    match_recognize_within_deadline_overflow_count: RelabeledGuardedIntCounterVec,
169    match_recognize_retained_rows: RelabeledAggregatedIntGaugeVec,
170    match_recognize_retained_partitions: RelabeledAggregatedIntGaugeVec,
171
172    /// The duration from receipt of barrier to all actors collection.
173    /// The max of all nodes' `barrier_inflight_latency` for a partial graph is the latency for a
174    /// barrier to flow through that partial graph.
175    pub barrier_inflight_latency: LabelGuardedHistogramVec,
176    /// The duration of sync to storage.
177    pub barrier_sync_latency: LabelGuardedHistogramVec,
178    pub barrier_batch_size: Histogram,
179    /// The progress made by the earliest in-flight barriers in the local barrier manager.
180    pub barrier_manager_progress: LabelGuardedIntCounterVec,
181
182    pub kv_log_store_storage_write_count: RelabeledGuardedIntCounterVec,
183    pub kv_log_store_storage_write_size: RelabeledGuardedIntCounterVec,
184    pub kv_log_store_rewind_count: RelabeledGuardedIntCounterVec,
185    pub kv_log_store_rewind_delay: RelabeledGuardedHistogramVec,
186    pub kv_log_store_storage_read_count: RelabeledGuardedIntCounterVec,
187    pub kv_log_store_storage_read_size: RelabeledGuardedIntCounterVec,
188    pub kv_log_store_buffer_unconsumed_item_count: RelabeledAggregatedIntGaugeVec,
189    pub kv_log_store_buffer_unconsumed_row_count: RelabeledAggregatedIntGaugeVec,
190    pub kv_log_store_buffer_unconsumed_epoch_count: RelabeledAggregatedIntGaugeVec,
191    pub kv_log_store_buffer_unconsumed_min_epoch: RelabeledAggregatedIntGaugeVec,
192    pub kv_log_store_buffer_memory_bytes: RelabeledAggregatedIntGaugeVec,
193
194    pub crossdb_last_consumed_min_epoch: RelabeledAggregatedIntGaugeVec,
195
196    pub sync_kv_log_store_read_count: RelabeledGuardedIntCounterVec,
197    pub sync_kv_log_store_read_size: RelabeledGuardedIntCounterVec,
198    pub sync_kv_log_store_write_pause_duration_ns: RelabeledGuardedIntCounterVec,
199    pub sync_kv_log_store_state: RelabeledGuardedIntCounterVec,
200    pub sync_kv_log_store_wait_next_poll_ns: RelabeledGuardedIntCounterVec,
201    pub sync_kv_log_store_storage_write_count: RelabeledGuardedIntCounterVec,
202    pub sync_kv_log_store_storage_write_size: RelabeledGuardedIntCounterVec,
203    pub sync_kv_log_store_buffer_unconsumed_item_count: RelabeledAggregatedIntGaugeVec,
204    pub sync_kv_log_store_buffer_unconsumed_row_count: RelabeledAggregatedIntGaugeVec,
205    pub sync_kv_log_store_buffer_unconsumed_epoch_count: RelabeledAggregatedIntGaugeVec,
206    pub sync_kv_log_store_buffer_unconsumed_min_epoch: RelabeledAggregatedIntGaugeVec,
207    pub sync_kv_log_store_buffer_memory_bytes: RelabeledAggregatedIntGaugeVec,
208
209    // Memory management
210    pub lru_runtime_loop_count: IntCounter,
211    pub lru_latest_sequence: IntGauge,
212    pub lru_watermark_sequence: IntGauge,
213    pub lru_eviction_policy: IntGauge,
214    pub jemalloc_allocated_bytes: IntGauge,
215    pub jemalloc_active_bytes: IntGauge,
216    pub jemalloc_resident_bytes: IntGauge,
217    pub jemalloc_metadata_bytes: IntGauge,
218    pub jvm_allocated_bytes: IntGauge,
219    pub jvm_active_bytes: IntGauge,
220    pub stream_memory_usage: RelabeledAggregatedIntGaugeVec,
221
222    // Materialized view
223    materialize_cache_hit_count: RelabeledGuardedIntCounterVec,
224    materialize_data_exist_count: RelabeledGuardedIntCounterVec,
225    materialize_cache_total_count: RelabeledGuardedIntCounterVec,
226    materialize_input_row_count: RelabeledGuardedIntCounterVec,
227    pub materialize_current_epoch: RelabeledAggregatedIntGaugeVec,
228
229    // PostgreSQL CDC LSN monitoring
230    pub pg_cdc_state_table_lsn: LabelGuardedIntGaugeVec,
231    pub pg_cdc_jni_commit_offset_lsn: LabelGuardedIntGaugeVec,
232
233    // MySQL CDC binlog monitoring
234    pub mysql_cdc_state_binlog_file_seq: LabelGuardedIntGaugeVec,
235    pub mysql_cdc_state_binlog_position: LabelGuardedIntGaugeVec,
236    pub mariadb_cdc_state_binlog_file_seq: LabelGuardedIntGaugeVec,
237    pub mariadb_cdc_state_binlog_position: LabelGuardedIntGaugeVec,
238
239    // SQL Server CDC LSN monitoring
240    pub sqlserver_cdc_state_change_lsn: LabelGuardedIntGaugeVec,
241    pub sqlserver_cdc_state_commit_lsn: LabelGuardedIntGaugeVec,
242    pub sqlserver_cdc_jni_commit_offset_lsn: LabelGuardedIntGaugeVec,
243
244    // Gap Fill
245    pub gap_fill_generated_rows_count: RelabeledGuardedIntCounterVec,
246
247    // Now (temporal filter clock)
248    pub now_streaming_clock_ms: LabelGuardedIntGaugeVec,
249    pub now_wall_clock_drift_ms: LabelGuardedIntGaugeVec,
250
251    // State Table
252    pub state_table_iter_count: RelabeledGuardedIntCounterVec,
253    pub state_table_get_count: RelabeledGuardedIntCounterVec,
254    pub state_table_iter_vnode_pruned_count: RelabeledGuardedIntCounterVec,
255    pub state_table_get_vnode_pruned_count: RelabeledGuardedIntCounterVec,
256}
257
258pub static GLOBAL_STREAMING_METRICS: OnceLock<StreamingMetrics> = OnceLock::new();
259
260fn latency_buckets(max: f64, count: usize) -> Vec<f64> {
261    const MIN: f64 = 0.1;
262
263    assert!(count > 1);
264    let factor = (max / MIN).powf(1.0 / (count - 1) as f64);
265    let mut buckets = exponential_buckets(MIN, factor, count).unwrap();
266    *buckets.last_mut().unwrap() = max;
267    buckets
268}
269
270pub fn global_streaming_metrics(metric_level: MetricLevel) -> StreamingMetrics {
271    GLOBAL_STREAMING_METRICS
272        .get_or_init(|| StreamingMetrics::new(&GLOBAL_METRICS_REGISTRY, metric_level))
273        .clone()
274}
275
276impl StreamingMetrics {
277    pub fn new(registry: &Registry, level: MetricLevel) -> Self {
278        let executor_row_count = register_guarded_int_counter_vec_with_registry!(
279            "stream_executor_row_count",
280            "Total number of rows that have been output from each executor",
281            &["actor_id", "fragment_id", "executor_identity"],
282            registry
283        )
284        .unwrap()
285        .relabel_debug_1(level);
286
287        let stream_node_output_row_count = CountMap::new();
288        let stream_node_output_blocking_duration_ns = CountMap::new();
289
290        let source_output_row_count = register_guarded_int_counter_vec_with_registry!(
291            "stream_source_output_rows_counts",
292            "Total number of rows that have been output from source",
293            &["actor_id", "source_id", "source_name", "fragment_id"],
294            registry
295        )
296        .unwrap()
297        .relabel_debug_1(level);
298
299        let source_split_change_count = register_guarded_int_counter_vec_with_registry!(
300            "stream_source_split_change_event_count",
301            "Total number of split change events that have been operated by source",
302            &["actor_id", "source_id", "source_name", "fragment_id"],
303            registry
304        )
305        .unwrap()
306        .relabel_debug_1(level);
307
308        let source_backfill_row_count = register_guarded_int_counter_vec_with_registry!(
309            "stream_source_backfill_rows_counts",
310            "Total number of rows that have been backfilled for source",
311            &["actor_id", "source_id", "source_name", "fragment_id"],
312            registry
313        )
314        .unwrap()
315        .relabel_debug_1(level);
316
317        let sink_input_row_count = register_guarded_int_counter_vec_with_registry!(
318            "stream_sink_input_row_count",
319            "Total number of rows streamed into sink executors",
320            &["actor_id", "sink_id", "fragment_id"],
321            registry
322        )
323        .unwrap()
324        .relabel_debug_1(level);
325
326        let sink_input_bytes = register_guarded_int_counter_vec_with_registry!(
327            "stream_sink_input_bytes",
328            "Total size of chunks streamed into sink executors",
329            &["actor_id", "sink_id", "fragment_id"],
330            registry
331        )
332        .unwrap()
333        .relabel_debug_1(level);
334
335        let materialize_input_row_count = register_guarded_int_counter_vec_with_registry!(
336            "stream_mview_input_row_count",
337            "Total number of rows streamed into materialize executors",
338            &["actor_id", "table_id", "fragment_id"],
339            registry
340        )
341        .unwrap()
342        .relabel_debug_1(level);
343
344        let materialize_current_epoch = register_guarded_int_gauge_vec_with_registry!(
345            "stream_mview_current_epoch",
346            "The current epoch of the materialized executor",
347            &["actor_id", "table_id", "fragment_id"],
348            registry
349        )
350        .unwrap()
351        .relabel_debug_1_with_aggregation(level, GaugeAggregation::Min);
352
353        let pg_cdc_state_table_lsn = register_guarded_int_gauge_vec_with_registry!(
354            "stream_pg_cdc_state_table_lsn",
355            "Current LSN value stored in PostgreSQL CDC state table",
356            &["source_id"],
357            registry,
358        )
359        .unwrap();
360
361        let pg_cdc_jni_commit_offset_lsn = register_guarded_int_gauge_vec_with_registry!(
362            "stream_pg_cdc_jni_commit_offset_lsn",
363            "LSN value when JNI commit offset is called for PostgreSQL CDC",
364            &["source_id"],
365            registry,
366        )
367        .unwrap();
368
369        let mysql_cdc_state_binlog_file_seq = register_guarded_int_gauge_vec_with_registry!(
370            "stream_mysql_cdc_state_binlog_file_seq",
371            "Current binlog file sequence number stored in MySQL CDC state table",
372            &["source_id"],
373            registry,
374        )
375        .unwrap();
376
377        let mysql_cdc_state_binlog_position = register_guarded_int_gauge_vec_with_registry!(
378            "stream_mysql_cdc_state_binlog_position",
379            "Current binlog position stored in MySQL CDC state table",
380            &["source_id"],
381            registry,
382        )
383        .unwrap();
384
385        let mariadb_cdc_state_binlog_file_seq = register_guarded_int_gauge_vec_with_registry!(
386            "stream_mariadb_cdc_state_binlog_file_seq",
387            "Current binlog file sequence number stored in MariaDB CDC state table",
388            &["source_id"],
389            registry,
390        )
391        .unwrap();
392
393        let mariadb_cdc_state_binlog_position = register_guarded_int_gauge_vec_with_registry!(
394            "stream_mariadb_cdc_state_binlog_position",
395            "Current binlog position stored in MariaDB CDC state table",
396            &["source_id"],
397            registry,
398        )
399        .unwrap();
400
401        let sqlserver_cdc_state_change_lsn = register_guarded_int_gauge_vec_with_registry!(
402            "stream_sqlserver_cdc_state_change_lsn",
403            "Current change_lsn value stored in SQL Server CDC state table",
404            &["source_id"],
405            registry,
406        )
407        .unwrap();
408
409        let sqlserver_cdc_state_commit_lsn = register_guarded_int_gauge_vec_with_registry!(
410            "stream_sqlserver_cdc_state_commit_lsn",
411            "Current commit_lsn value stored in SQL Server CDC state table",
412            &["source_id"],
413            registry,
414        )
415        .unwrap();
416
417        let sqlserver_cdc_jni_commit_offset_lsn = register_guarded_int_gauge_vec_with_registry!(
418            "stream_sqlserver_cdc_jni_commit_offset_lsn",
419            "LSN value when JNI commit offset is called for SQL Server CDC",
420            &["source_id"],
421            registry,
422        )
423        .unwrap();
424
425        let sink_chunk_buffer_size = register_guarded_int_gauge_vec_with_registry!(
426            "stream_sink_chunk_buffer_size",
427            "Total size of chunks buffered in a barrier",
428            &["actor_id", "sink_id", "fragment_id"],
429            registry
430        )
431        .unwrap()
432        .relabel_debug_1_with_aggregation(level, GaugeAggregation::Sum);
433        let actor_output_buffer_blocking_duration_ns =
434            register_guarded_int_counter_vec_with_registry!(
435                "stream_actor_output_buffer_blocking_duration_ns",
436                "Total blocking duration (ns) of output buffer",
437                &["actor_id", "fragment_id", "downstream_fragment_id"],
438                registry
439            )
440            .unwrap()
441            // mask the first label `actor_id` if the level is less verbose than `Debug`
442            .relabel_debug_1(level);
443
444        let actor_input_buffer_blocking_duration_ns =
445            register_guarded_int_counter_vec_with_registry!(
446                "stream_actor_input_buffer_blocking_duration_ns",
447                "Total blocking duration (ns) of input buffer",
448                &["actor_id", "fragment_id", "upstream_fragment_id"],
449                registry
450            )
451            .unwrap()
452            // mask the first label `actor_id` if the level is less verbose than `Debug`
453            .relabel_debug_1(level);
454
455        let fragment_channel_buffered_bytes = register_guarded_int_gauge_vec_with_registry!(
456            "stream_fragment_channel_buffered_bytes",
457            "Estimated buffered bytes for actor channels by fragment",
458            &["fragment_id"],
459            registry
460        )
461        .unwrap();
462
463        let exchange_frag_recv_size = register_guarded_int_counter_vec_with_registry!(
464            "stream_exchange_frag_recv_size",
465            "Total size of messages that have been received from upstream Fragment",
466            &["up_fragment_id", "down_fragment_id"],
467            registry
468        )
469        .unwrap();
470
471        let actor_poll_duration = register_guarded_int_counter_vec_with_registry!(
472            "stream_actor_poll_duration",
473            "tokio's metrics",
474            &["actor_id", "fragment_id"],
475            registry
476        )
477        .unwrap()
478        .relabel_debug_1(level);
479
480        let actor_poll_cnt = register_guarded_int_counter_vec_with_registry!(
481            "stream_actor_poll_cnt",
482            "tokio's metrics",
483            &["actor_id", "fragment_id"],
484            registry
485        )
486        .unwrap()
487        .relabel_debug_1(level);
488
489        let actor_scheduled_duration = register_guarded_int_counter_vec_with_registry!(
490            "stream_actor_scheduled_duration",
491            "tokio's metrics",
492            &["actor_id", "fragment_id"],
493            registry
494        )
495        .unwrap()
496        .relabel_debug_1(level);
497
498        let actor_scheduled_cnt = register_guarded_int_counter_vec_with_registry!(
499            "stream_actor_scheduled_cnt",
500            "tokio's metrics",
501            &["actor_id", "fragment_id"],
502            registry
503        )
504        .unwrap()
505        .relabel_debug_1(level);
506
507        let actor_idle_duration = register_guarded_int_counter_vec_with_registry!(
508            "stream_actor_idle_duration",
509            "tokio's metrics",
510            &["actor_id", "fragment_id"],
511            registry
512        )
513        .unwrap()
514        .relabel_debug_1(level);
515
516        let actor_idle_cnt = register_guarded_int_counter_vec_with_registry!(
517            "stream_actor_idle_cnt",
518            "tokio's metrics",
519            &["actor_id", "fragment_id"],
520            registry
521        )
522        .unwrap()
523        .relabel_debug_1(level);
524
525        let actor_in_record_cnt = register_guarded_int_counter_vec_with_registry!(
526            "stream_actor_in_record_cnt",
527            "Total number of rows actor received",
528            &["actor_id", "fragment_id", "upstream_fragment_id"],
529            registry
530        )
531        .unwrap()
532        .relabel_debug_1(level);
533
534        let actor_out_record_cnt = register_guarded_int_counter_vec_with_registry!(
535            "stream_actor_out_record_cnt",
536            "Total number of rows actor sent",
537            &["actor_id", "fragment_id"],
538            registry
539        )
540        .unwrap()
541        .relabel_debug_1(level);
542
543        let actor_current_epoch = register_guarded_int_gauge_vec_with_registry!(
544            "stream_actor_current_epoch",
545            "Current epoch of actor",
546            &["actor_id", "fragment_id"],
547            registry
548        )
549        .unwrap()
550        .relabel_debug_1_with_aggregation(level, GaugeAggregation::Min);
551
552        let project_expr_inflight_window_size = register_guarded_int_gauge_vec_with_registry!(
553            "stream_project_expr_inflight_window_size",
554            "Number of messages waiting in ProjectExecutor's ordered projection window",
555            &["actor_id", "fragment_id"],
556            registry
557        )
558        .unwrap()
559        .relabel_debug_1_with_aggregation(level, GaugeAggregation::Sum);
560
561        let actor_count = register_guarded_int_gauge_vec_with_registry!(
562            "stream_actor_count",
563            "Total number of actors (parallelism)",
564            &["fragment_id"],
565            registry
566        )
567        .unwrap();
568
569        let merge_barrier_align_duration = register_guarded_int_counter_vec_with_registry!(
570            "stream_merge_barrier_align_duration_ns",
571            "Total merge barrier alignment duration (ns)",
572            &["actor_id", "fragment_id"],
573            registry
574        )
575        .unwrap()
576        .relabel_debug_1(level);
577
578        let join_lookup_miss_count = register_guarded_int_counter_vec_with_registry!(
579            "stream_join_lookup_miss_count",
580            "Join executor lookup miss duration",
581            &["actor_id", "side", "join_table_id", "fragment_id"],
582            registry
583        )
584        .unwrap()
585        .relabel_debug_1(level);
586
587        let join_lookup_total_count = register_guarded_int_counter_vec_with_registry!(
588            "stream_join_lookup_total_count",
589            "Join executor lookup total operation",
590            &["actor_id", "side", "join_table_id", "fragment_id"],
591            registry
592        )
593        .unwrap()
594        .relabel_debug_1(level);
595
596        let join_insert_cache_miss_count = register_guarded_int_counter_vec_with_registry!(
597            "stream_join_insert_cache_miss_count",
598            "Join executor cache miss when insert operation",
599            &["actor_id", "side", "join_table_id", "fragment_id"],
600            registry
601        )
602        .unwrap()
603        .relabel_debug_1(level);
604
605        let join_actor_input_waiting_duration_ns = register_guarded_int_counter_vec_with_registry!(
606            "stream_join_actor_input_waiting_duration_ns",
607            "Total waiting duration (ns) of input buffer of join actor",
608            &["actor_id", "fragment_id"],
609            registry
610        )
611        .unwrap()
612        .relabel_debug_1(level);
613
614        let join_match_duration_ns = register_guarded_int_counter_vec_with_registry!(
615            "stream_join_match_duration_ns",
616            "Matching duration for each side",
617            &["actor_id", "fragment_id", "side"],
618            registry
619        )
620        .unwrap()
621        .relabel_debug_1(level);
622
623        let barrier_align_duration = register_guarded_int_counter_vec_with_registry!(
624            "stream_barrier_align_duration_ns",
625            "Duration of join align barrier",
626            &["actor_id", "fragment_id", "wait_side", "executor"],
627            registry
628        )
629        .unwrap()
630        .relabel_debug_1(level);
631
632        let join_cached_entry_count = register_guarded_int_gauge_vec_with_registry!(
633            "stream_join_cached_entry_count",
634            "Number of cached entries in streaming join operators",
635            &["actor_id", "fragment_id", "side"],
636            registry
637        )
638        .unwrap()
639        .relabel_debug_1_with_aggregation(level, GaugeAggregation::Sum);
640
641        let join_matched_join_keys_opts = histogram_opts!(
642            "stream_join_matched_join_keys",
643            "The number of keys matched in the opposite side",
644            exponential_buckets(16.0, 2.0, 28).unwrap() // max 2^31
645        );
646
647        let join_matched_join_keys = register_guarded_histogram_vec_with_registry!(
648            join_matched_join_keys_opts,
649            &["actor_id", "fragment_id", "table_id"],
650            registry
651        )
652        .unwrap()
653        .relabel_debug_1(level);
654
655        let agg_lookup_miss_count = register_guarded_int_counter_vec_with_registry!(
656            "stream_agg_lookup_miss_count",
657            "Aggregation executor lookup miss duration",
658            &["actor_id", "table_id", "fragment_id"],
659            registry
660        )
661        .unwrap()
662        .relabel_debug_1(level);
663
664        let agg_total_lookup_count = register_guarded_int_counter_vec_with_registry!(
665            "stream_agg_lookup_total_count",
666            "Aggregation executor lookup total operation",
667            &["actor_id", "table_id", "fragment_id"],
668            registry
669        )
670        .unwrap()
671        .relabel_debug_1(level);
672
673        let agg_distinct_cache_miss_count = register_guarded_int_counter_vec_with_registry!(
674            "stream_agg_distinct_cache_miss_count",
675            "Aggregation executor dinsinct miss duration",
676            &["actor_id", "table_id", "fragment_id"],
677            registry
678        )
679        .unwrap()
680        .relabel_debug_1(level);
681
682        let agg_distinct_total_cache_count = register_guarded_int_counter_vec_with_registry!(
683            "stream_agg_distinct_total_cache_count",
684            "Aggregation executor distinct total operation",
685            &["actor_id", "table_id", "fragment_id"],
686            registry
687        )
688        .unwrap()
689        .relabel_debug_1(level);
690
691        let agg_distinct_cached_entry_count = register_guarded_int_gauge_vec_with_registry!(
692            "stream_agg_distinct_cached_entry_count",
693            "Total entry counts in distinct aggregation executor cache",
694            &["actor_id", "table_id", "fragment_id"],
695            registry
696        )
697        .unwrap()
698        .relabel_debug_1_with_aggregation(level, GaugeAggregation::Sum);
699
700        let agg_dirty_groups_count = register_guarded_int_gauge_vec_with_registry!(
701            "stream_agg_dirty_groups_count",
702            "Total dirty group counts in aggregation executor",
703            &["actor_id", "table_id", "fragment_id"],
704            registry
705        )
706        .unwrap()
707        .relabel_debug_1_with_aggregation(level, GaugeAggregation::Sum);
708
709        let agg_dirty_groups_heap_size = register_guarded_int_gauge_vec_with_registry!(
710            "stream_agg_dirty_groups_heap_size",
711            "Total dirty group heap size in aggregation executor",
712            &["actor_id", "table_id", "fragment_id"],
713            registry
714        )
715        .unwrap()
716        .relabel_debug_1_with_aggregation(level, GaugeAggregation::Sum);
717
718        let agg_state_cache_lookup_count = register_guarded_int_counter_vec_with_registry!(
719            "stream_agg_state_cache_lookup_count",
720            "Aggregation executor state cache lookup count",
721            &["actor_id", "table_id", "fragment_id"],
722            registry
723        )
724        .unwrap()
725        .relabel_debug_1(level);
726
727        let agg_state_cache_miss_count = register_guarded_int_counter_vec_with_registry!(
728            "stream_agg_state_cache_miss_count",
729            "Aggregation executor state cache miss count",
730            &["actor_id", "table_id", "fragment_id"],
731            registry
732        )
733        .unwrap()
734        .relabel_debug_1(level);
735
736        let group_top_n_cache_miss_count = register_guarded_int_counter_vec_with_registry!(
737            "stream_group_top_n_cache_miss_count",
738            "Group top n executor cache miss count",
739            &["actor_id", "table_id", "fragment_id"],
740            registry
741        )
742        .unwrap()
743        .relabel_debug_1(level);
744
745        let group_top_n_total_query_cache_count = register_guarded_int_counter_vec_with_registry!(
746            "stream_group_top_n_total_query_cache_count",
747            "Group top n executor query cache total count",
748            &["actor_id", "table_id", "fragment_id"],
749            registry
750        )
751        .unwrap()
752        .relabel_debug_1(level);
753
754        let group_top_n_cached_entry_count = register_guarded_int_gauge_vec_with_registry!(
755            "stream_group_top_n_cached_entry_count",
756            "Total entry counts in group top n executor cache",
757            &["actor_id", "table_id", "fragment_id"],
758            registry
759        )
760        .unwrap()
761        .relabel_debug_1_with_aggregation(level, GaugeAggregation::Sum);
762
763        let group_top_n_appendonly_cache_miss_count =
764            register_guarded_int_counter_vec_with_registry!(
765                "stream_group_top_n_appendonly_cache_miss_count",
766                "Group top n appendonly executor cache miss count",
767                &["actor_id", "table_id", "fragment_id"],
768                registry
769            )
770            .unwrap()
771            .relabel_debug_1(level);
772
773        let group_top_n_appendonly_total_query_cache_count =
774            register_guarded_int_counter_vec_with_registry!(
775                "stream_group_top_n_appendonly_total_query_cache_count",
776                "Group top n appendonly executor total cache count",
777                &["actor_id", "table_id", "fragment_id"],
778                registry
779            )
780            .unwrap()
781            .relabel_debug_1(level);
782
783        let group_top_n_appendonly_cached_entry_count =
784            register_guarded_int_gauge_vec_with_registry!(
785                "stream_group_top_n_appendonly_cached_entry_count",
786                "Total entry counts in group top n appendonly executor cache",
787                &["actor_id", "table_id", "fragment_id"],
788                registry
789            )
790            .unwrap()
791            .relabel_debug_1_with_aggregation(level, GaugeAggregation::Sum);
792
793        let lookup_cache_miss_count = register_guarded_int_counter_vec_with_registry!(
794            "stream_lookup_cache_miss_count",
795            "Lookup executor cache miss count",
796            &["actor_id", "table_id", "fragment_id"],
797            registry
798        )
799        .unwrap()
800        .relabel_debug_1(level);
801
802        let lookup_total_query_cache_count = register_guarded_int_counter_vec_with_registry!(
803            "stream_lookup_total_query_cache_count",
804            "Lookup executor query cache total count",
805            &["actor_id", "table_id", "fragment_id"],
806            registry
807        )
808        .unwrap()
809        .relabel_debug_1(level);
810
811        let lookup_cached_entry_count = register_guarded_int_gauge_vec_with_registry!(
812            "stream_lookup_cached_entry_count",
813            "Total entry counts in lookup executor cache",
814            &["actor_id", "table_id", "fragment_id"],
815            registry
816        )
817        .unwrap()
818        .relabel_debug_1_with_aggregation(level, GaugeAggregation::Sum);
819
820        let temporal_join_cache_miss_count = register_guarded_int_counter_vec_with_registry!(
821            "stream_temporal_join_cache_miss_count",
822            "Temporal join executor cache miss count",
823            &["actor_id", "table_id", "fragment_id"],
824            registry
825        )
826        .unwrap()
827        .relabel_debug_1(level);
828
829        let temporal_join_total_query_cache_count =
830            register_guarded_int_counter_vec_with_registry!(
831                "stream_temporal_join_total_query_cache_count",
832                "Temporal join executor query cache total count",
833                &["actor_id", "table_id", "fragment_id"],
834                registry
835            )
836            .unwrap()
837            .relabel_debug_1(level);
838
839        let temporal_join_cached_entry_count = register_guarded_int_gauge_vec_with_registry!(
840            "stream_temporal_join_cached_entry_count",
841            "Total entry count in temporal join executor cache",
842            &["actor_id", "table_id", "fragment_id"],
843            registry
844        )
845        .unwrap()
846        .relabel_debug_1_with_aggregation(level, GaugeAggregation::Sum);
847
848        let agg_cached_entry_count = register_guarded_int_gauge_vec_with_registry!(
849            "stream_agg_cached_entry_count",
850            "Number of cached keys in streaming aggregation operators",
851            &["actor_id", "table_id", "fragment_id"],
852            registry
853        )
854        .unwrap()
855        .relabel_debug_1_with_aggregation(level, GaugeAggregation::Sum);
856
857        let agg_chunk_lookup_miss_count = register_guarded_int_counter_vec_with_registry!(
858            "stream_agg_chunk_lookup_miss_count",
859            "Aggregation executor chunk-level lookup miss duration",
860            &["actor_id", "table_id", "fragment_id"],
861            registry
862        )
863        .unwrap()
864        .relabel_debug_1(level);
865
866        let agg_chunk_total_lookup_count = register_guarded_int_counter_vec_with_registry!(
867            "stream_agg_chunk_lookup_total_count",
868            "Aggregation executor chunk-level lookup total operation",
869            &["actor_id", "table_id", "fragment_id"],
870            registry
871        )
872        .unwrap()
873        .relabel_debug_1(level);
874
875        let backfill_snapshot_read_row_count = register_guarded_int_counter_vec_with_registry!(
876            "stream_backfill_snapshot_read_row_count",
877            "Total number of rows that have been read from the backfill snapshot",
878            &["actor_id", "table_id"],
879            registry
880        )
881        .unwrap()
882        .relabel_debug_1(level);
883
884        let backfill_upstream_output_row_count = register_guarded_int_counter_vec_with_registry!(
885            "stream_backfill_upstream_output_row_count",
886            "Total number of rows that have been output from the backfill upstream",
887            &["actor_id", "table_id"],
888            registry
889        )
890        .unwrap()
891        .relabel_debug_1(level);
892
893        let cdc_backfill_snapshot_read_row_count = register_guarded_int_counter_vec_with_registry!(
894            "stream_cdc_backfill_snapshot_read_row_count",
895            "Total number of rows that have been read from the cdc_backfill snapshot",
896            &["actor_id", "table_id"],
897            registry
898        )
899        .unwrap()
900        .relabel_debug_1(level);
901
902        let cdc_backfill_upstream_output_row_count =
903            register_guarded_int_counter_vec_with_registry!(
904                "stream_cdc_backfill_upstream_output_row_count",
905                "Total number of rows that have been output from the cdc_backfill upstream",
906                &["actor_id", "table_id"],
907                registry
908            )
909            .unwrap()
910            .relabel_debug_1(level);
911
912        let snapshot_backfill_consume_row_count = register_guarded_int_counter_vec_with_registry!(
913            "stream_snapshot_backfill_consume_snapshot_row_count",
914            "Total number of rows that have been output from snapshot backfill",
915            &["actor_id", "table_id", "stage"],
916            registry
917        )
918        .unwrap()
919        .relabel_debug_1(level);
920
921        let over_window_cached_entry_count = register_guarded_int_gauge_vec_with_registry!(
922            "stream_over_window_cached_entry_count",
923            "Total entry (partition) count in over window executor cache",
924            &["actor_id", "table_id", "fragment_id"],
925            registry
926        )
927        .unwrap()
928        .relabel_debug_1_with_aggregation(level, GaugeAggregation::Sum);
929
930        let over_window_cache_lookup_count = register_guarded_int_counter_vec_with_registry!(
931            "stream_over_window_cache_lookup_count",
932            "Over window executor cache lookup count",
933            &["actor_id", "table_id", "fragment_id"],
934            registry
935        )
936        .unwrap()
937        .relabel_debug_1(level);
938
939        let over_window_cache_miss_count = register_guarded_int_counter_vec_with_registry!(
940            "stream_over_window_cache_miss_count",
941            "Over window executor cache miss count",
942            &["actor_id", "table_id", "fragment_id"],
943            registry
944        )
945        .unwrap()
946        .relabel_debug_1(level);
947
948        let over_window_range_cache_entry_count = register_guarded_int_gauge_vec_with_registry!(
949            "stream_over_window_range_cache_entry_count",
950            "Over window partition range cache entry count",
951            &["actor_id", "table_id", "fragment_id"],
952            registry,
953        )
954        .unwrap()
955        .relabel_debug_1_with_aggregation(level, GaugeAggregation::Sum);
956
957        let over_window_range_cache_lookup_count = register_guarded_int_counter_vec_with_registry!(
958            "stream_over_window_range_cache_lookup_count",
959            "Over window partition range cache lookup count",
960            &["actor_id", "table_id", "fragment_id"],
961            registry
962        )
963        .unwrap()
964        .relabel_debug_1(level);
965
966        let over_window_range_cache_left_miss_count =
967            register_guarded_int_counter_vec_with_registry!(
968                "stream_over_window_range_cache_left_miss_count",
969                "Over window partition range cache left miss count",
970                &["actor_id", "table_id", "fragment_id"],
971                registry
972            )
973            .unwrap()
974            .relabel_debug_1(level);
975
976        let over_window_range_cache_right_miss_count =
977            register_guarded_int_counter_vec_with_registry!(
978                "stream_over_window_range_cache_right_miss_count",
979                "Over window partition range cache right miss count",
980                &["actor_id", "table_id", "fragment_id"],
981                registry
982            )
983            .unwrap()
984            .relabel_debug_1(level);
985
986        let over_window_accessed_entry_count = register_guarded_int_counter_vec_with_registry!(
987            "stream_over_window_accessed_entry_count",
988            "Over window accessed entry count",
989            &["actor_id", "table_id", "fragment_id"],
990            registry
991        )
992        .unwrap()
993        .relabel_debug_1(level);
994
995        let over_window_compute_count = register_guarded_int_counter_vec_with_registry!(
996            "stream_over_window_compute_count",
997            "Over window compute count",
998            &["actor_id", "table_id", "fragment_id"],
999            registry
1000        )
1001        .unwrap()
1002        .relabel_debug_1(level);
1003
1004        let over_window_same_output_count = register_guarded_int_counter_vec_with_registry!(
1005            "stream_over_window_same_output_count",
1006            "Over window same output count",
1007            &["actor_id", "table_id", "fragment_id"],
1008            registry
1009        )
1010        .unwrap()
1011        .relabel_debug_1(level);
1012
1013        let over_window_state_cleaned_row_count = register_guarded_int_counter_vec_with_registry!(
1014            "stream_over_window_state_cleaned_row_count",
1015            "Over window number of stale state rows deleted by watermark-driven state cleaning",
1016            &["actor_id", "table_id", "fragment_id"],
1017            registry
1018        )
1019        .unwrap()
1020        .relabel_debug_1(level);
1021
1022        let match_recognize_matches_emitted_count =
1023            register_guarded_int_counter_vec_with_registry!(
1024                "stream_match_recognize_matches_emitted_count",
1025                "Matches emitted by the match recognize executor",
1026                &["actor_id", "table_id", "fragment_id"],
1027                registry
1028            )
1029            .unwrap()
1030            .relabel_debug_1(level);
1031
1032        let match_recognize_evicted_rows_count = register_guarded_int_counter_vec_with_registry!(
1033            "stream_match_recognize_evicted_rows_count",
1034            "Buffered rows evicted by the match recognize executor",
1035            &["actor_id", "table_id", "fragment_id"],
1036            registry
1037        )
1038        .unwrap()
1039        .relabel_debug_1(level);
1040
1041        let match_recognize_scan_budget_exhausted_count = register_guarded_int_counter_vec_with_registry!(
1042            "stream_match_recognize_scan_budget_exhausted_count",
1043            "Partition visits that exhausted the match recognize scan budget and degraded conservatively",
1044            &["actor_id", "table_id", "fragment_id"],
1045            registry
1046        )
1047        .unwrap()
1048        .relabel_debug_1(level);
1049
1050        let match_recognize_within_deadline_overflow_count = register_guarded_int_counter_vec_with_registry!(
1051            "stream_match_recognize_within_deadline_overflow_count",
1052            "Buffered rows whose WITHIN deadline lies past the ORDER BY type's range, so their window never closes",
1053            &["actor_id", "table_id", "fragment_id"],
1054            registry
1055        )
1056        .unwrap()
1057        .relabel_debug_1(level);
1058
1059        let match_recognize_retained_rows = register_guarded_int_gauge_vec_with_registry!(
1060            "stream_match_recognize_retained_rows",
1061            "Rows currently retained in memory by the match recognize executor, summed over the actor's partitions",
1062            &["actor_id", "table_id", "fragment_id"],
1063            registry
1064        )
1065        .unwrap()
1066        .relabel_debug_1_with_aggregation(level, GaugeAggregation::Sum);
1067
1068        let match_recognize_retained_partitions = register_guarded_int_gauge_vec_with_registry!(
1069            "stream_match_recognize_retained_partitions",
1070            "Partitions currently held in memory by the match recognize executor, including emptied entries awaiting the next watermark sweep",
1071            &["actor_id", "table_id", "fragment_id"],
1072            registry
1073        )
1074        .unwrap()
1075        .relabel_debug_1_with_aggregation(level, GaugeAggregation::Sum);
1076
1077        let barrier_inflight_latency = register_guarded_histogram_vec_with_registry!(
1078            "stream_barrier_inflight_duration_seconds",
1079            "barrier_inflight_latency",
1080            &["partial_graph"],
1081            latency_buckets(600.0, 20),
1082            registry
1083        )
1084        .unwrap();
1085
1086        let barrier_sync_latency = register_guarded_histogram_vec_with_registry!(
1087            "stream_barrier_sync_storage_duration_seconds",
1088            "barrier_sync_latency",
1089            &["partial_graph"],
1090            latency_buckets(600.0, 20),
1091            registry
1092        )
1093        .unwrap();
1094
1095        let opts = histogram_opts!(
1096            "stream_barrier_batch_size",
1097            "barrier_batch_size",
1098            exponential_buckets(1.0, 2.0, 8).unwrap()
1099        );
1100        let barrier_batch_size = register_histogram_with_registry!(opts, registry).unwrap();
1101
1102        let barrier_manager_progress = register_guarded_int_counter_vec_with_registry!(
1103            "stream_barrier_manager_progress",
1104            "The number of actors that have processed the earliest in-flight barriers",
1105            &["partial_graph"],
1106            registry
1107        )
1108        .unwrap();
1109
1110        let sync_kv_log_store_wait_next_poll_ns = register_guarded_int_counter_vec_with_registry!(
1111            "sync_kv_log_store_wait_next_poll_ns",
1112            "Total duration (ns) of waiting for next poll",
1113            &["actor_id", "target", "fragment_id", "relation"],
1114            registry
1115        )
1116        .unwrap()
1117        .relabel_debug_1(level);
1118
1119        let sync_kv_log_store_read_count = register_guarded_int_counter_vec_with_registry!(
1120            "sync_kv_log_store_read_count",
1121            "read row count throughput of sync_kv log store",
1122            &["actor_id", "type", "target", "fragment_id", "relation"],
1123            registry
1124        )
1125        .unwrap()
1126        .relabel_debug_1(level);
1127
1128        let sync_kv_log_store_read_size = register_guarded_int_counter_vec_with_registry!(
1129            "sync_kv_log_store_read_size",
1130            "read size throughput of sync_kv log store",
1131            &["actor_id", "type", "target", "fragment_id", "relation"],
1132            registry
1133        )
1134        .unwrap()
1135        .relabel_debug_1(level);
1136
1137        let sync_kv_log_store_write_pause_duration_ns =
1138            register_guarded_int_counter_vec_with_registry!(
1139                "sync_kv_log_store_write_pause_duration_ns",
1140                "Duration (ns) of sync_kv log store write pause",
1141                &["actor_id", "target", "fragment_id", "relation"],
1142                registry
1143            )
1144            .unwrap()
1145            .relabel_debug_1(level);
1146
1147        let sync_kv_log_store_state = register_guarded_int_counter_vec_with_registry!(
1148            "sync_kv_log_store_state",
1149            "clean/unclean state transition for sync_kv log store",
1150            &["actor_id", "state", "target", "fragment_id", "relation"],
1151            registry
1152        )
1153        .unwrap()
1154        .relabel_debug_1(level);
1155
1156        let sync_kv_log_store_storage_write_count =
1157            register_guarded_int_counter_vec_with_registry!(
1158                "sync_kv_log_store_storage_write_count",
1159                "Write row count throughput of sync_kv log store",
1160                &["actor_id", "target", "fragment_id", "relation"],
1161                registry
1162            )
1163            .unwrap()
1164            .relabel_debug_1(level);
1165
1166        let sync_kv_log_store_storage_write_size = register_guarded_int_counter_vec_with_registry!(
1167            "sync_kv_log_store_storage_write_size",
1168            "Write size throughput of sync_kv log store",
1169            &["actor_id", "target", "fragment_id", "relation"],
1170            registry
1171        )
1172        .unwrap()
1173        .relabel_debug_1(level);
1174
1175        let sync_kv_log_store_buffer_unconsumed_item_count =
1176            register_guarded_int_gauge_vec_with_registry!(
1177                "sync_kv_log_store_buffer_unconsumed_item_count",
1178                "Number of Unconsumed Item in buffer",
1179                &["actor_id", "target", "fragment_id", "relation"],
1180                registry
1181            )
1182            .unwrap()
1183            .relabel_debug_1_with_aggregation(level, GaugeAggregation::Sum);
1184
1185        let sync_kv_log_store_buffer_unconsumed_row_count =
1186            register_guarded_int_gauge_vec_with_registry!(
1187                "sync_kv_log_store_buffer_unconsumed_row_count",
1188                "Number of Unconsumed Row in buffer",
1189                &["actor_id", "target", "fragment_id", "relation"],
1190                registry
1191            )
1192            .unwrap()
1193            .relabel_debug_1_with_aggregation(level, GaugeAggregation::Sum);
1194
1195        let sync_kv_log_store_buffer_unconsumed_epoch_count =
1196            register_guarded_int_gauge_vec_with_registry!(
1197                "sync_kv_log_store_buffer_unconsumed_epoch_count",
1198                "Number of Unconsumed Epoch in buffer",
1199                &["actor_id", "target", "fragment_id", "relation"],
1200                registry
1201            )
1202            .unwrap()
1203            .relabel_debug_1_with_aggregation(level, GaugeAggregation::Sum);
1204
1205        let sync_kv_log_store_buffer_unconsumed_min_epoch =
1206            register_guarded_int_gauge_vec_with_registry!(
1207                "sync_kv_log_store_buffer_unconsumed_min_epoch",
1208                "Number of Unconsumed Epoch in buffer",
1209                &["actor_id", "target", "fragment_id", "relation"],
1210                registry
1211            )
1212            .unwrap()
1213            .relabel_debug_1_with_aggregation(level, GaugeAggregation::Min);
1214        let sync_kv_log_store_buffer_memory_bytes =
1215            register_guarded_int_gauge_vec_with_registry!(
1216                "sync_kv_log_store_buffer_memory_bytes",
1217                "Estimated heap bytes used by synced kv log store buffer (unconsumed + consumed but not truncated)",
1218                &["actor_id", "target", "fragment_id", "relation"],
1219                registry
1220            )
1221            .unwrap()
1222            .relabel_debug_1_with_aggregation(level, GaugeAggregation::Sum);
1223
1224        let kv_log_store_storage_write_count = register_guarded_int_counter_vec_with_registry!(
1225            "kv_log_store_storage_write_count",
1226            "Write row count throughput of kv log store",
1227            &["actor_id", "connector", "sink_id", "sink_name"],
1228            registry
1229        )
1230        .unwrap()
1231        .relabel_debug_1(level);
1232
1233        let kv_log_store_storage_write_size = register_guarded_int_counter_vec_with_registry!(
1234            "kv_log_store_storage_write_size",
1235            "Write size throughput of kv log store",
1236            &["actor_id", "connector", "sink_id", "sink_name"],
1237            registry
1238        )
1239        .unwrap()
1240        .relabel_debug_1(level);
1241
1242        let kv_log_store_storage_read_count = register_guarded_int_counter_vec_with_registry!(
1243            "kv_log_store_storage_read_count",
1244            "Write row count throughput of kv log store",
1245            &["actor_id", "connector", "sink_id", "sink_name", "read_type"],
1246            registry
1247        )
1248        .unwrap()
1249        .relabel_debug_1(level);
1250
1251        let kv_log_store_storage_read_size = register_guarded_int_counter_vec_with_registry!(
1252            "kv_log_store_storage_read_size",
1253            "Write size throughput of kv log store",
1254            &["actor_id", "connector", "sink_id", "sink_name", "read_type"],
1255            registry
1256        )
1257        .unwrap()
1258        .relabel_debug_1(level);
1259
1260        let kv_log_store_rewind_count = register_guarded_int_counter_vec_with_registry!(
1261            "kv_log_store_rewind_count",
1262            "Kv log store rewind rate",
1263            &["actor_id", "connector", "sink_id", "sink_name"],
1264            registry
1265        )
1266        .unwrap()
1267        .relabel_debug_1(level);
1268
1269        let kv_log_store_rewind_delay_opts = {
1270            let first_delay_secs = REWIND_INITIAL_DELAY.as_secs_f64();
1271            let base = REWIND_BACKOFF_MULTIPLIER as f64;
1272            let bucket_count = (REWIND_MAX_DELAY.as_secs_f64() / first_delay_secs)
1273                .log(base)
1274                .ceil() as usize;
1275            let buckets = exponential_buckets(first_delay_secs, base, bucket_count).unwrap();
1276            histogram_opts!(
1277                "kv_log_store_rewind_delay",
1278                "Kv log store rewind delay",
1279                buckets,
1280            )
1281        };
1282
1283        let kv_log_store_rewind_delay = register_guarded_histogram_vec_with_registry!(
1284            kv_log_store_rewind_delay_opts,
1285            &["actor_id", "connector", "sink_id", "sink_name"],
1286            registry
1287        )
1288        .unwrap()
1289        .relabel_debug_1(level);
1290
1291        let kv_log_store_buffer_unconsumed_item_count =
1292            register_guarded_int_gauge_vec_with_registry!(
1293                "kv_log_store_buffer_unconsumed_item_count",
1294                "Number of Unconsumed Item in buffer",
1295                &["actor_id", "connector", "sink_id", "sink_name"],
1296                registry
1297            )
1298            .unwrap()
1299            .relabel_debug_1_with_aggregation(level, GaugeAggregation::Sum);
1300
1301        let kv_log_store_buffer_unconsumed_row_count =
1302            register_guarded_int_gauge_vec_with_registry!(
1303                "kv_log_store_buffer_unconsumed_row_count",
1304                "Number of Unconsumed Row in buffer",
1305                &["actor_id", "connector", "sink_id", "sink_name"],
1306                registry
1307            )
1308            .unwrap()
1309            .relabel_debug_1_with_aggregation(level, GaugeAggregation::Sum);
1310
1311        let kv_log_store_buffer_unconsumed_epoch_count =
1312            register_guarded_int_gauge_vec_with_registry!(
1313                "kv_log_store_buffer_unconsumed_epoch_count",
1314                "Number of Unconsumed Epoch in buffer",
1315                &["actor_id", "connector", "sink_id", "sink_name"],
1316                registry
1317            )
1318            .unwrap()
1319            .relabel_debug_1_with_aggregation(level, GaugeAggregation::Sum);
1320
1321        let kv_log_store_buffer_unconsumed_min_epoch =
1322            register_guarded_int_gauge_vec_with_registry!(
1323                "kv_log_store_buffer_unconsumed_min_epoch",
1324                "Number of Unconsumed Epoch in buffer",
1325                &["actor_id", "connector", "sink_id", "sink_name"],
1326                registry
1327            )
1328            .unwrap()
1329            .relabel_debug_1_with_aggregation(level, GaugeAggregation::Min);
1330
1331        let crossdb_last_consumed_min_epoch = register_guarded_int_gauge_vec_with_registry!(
1332            "crossdb_last_consumed_min_epoch",
1333            "Last consumed min epoch for cross-database changelog stream scan",
1334            &["actor_id", "table_id", "fragment_id"],
1335            registry
1336        )
1337        .unwrap()
1338        .relabel_debug_1_with_aggregation(level, GaugeAggregation::Min);
1339
1340        let kv_log_store_buffer_memory_bytes =
1341            register_guarded_int_gauge_vec_with_registry!(
1342                "kv_log_store_buffer_memory_bytes",
1343                "Estimated heap bytes used by kv log store buffer (unconsumed + consumed but not truncated)",
1344                &[
1345                    "actor_id",
1346                    "fragment_id",
1347                    "connector",
1348                    "sink_id",
1349                    "sink_name",
1350                ],
1351                registry
1352            )
1353            .unwrap()
1354            .relabel_debug_1_with_aggregation(level, GaugeAggregation::Sum);
1355
1356        let lru_runtime_loop_count = register_int_counter_with_registry!(
1357            "lru_runtime_loop_count",
1358            "The counts of the eviction loop in LRU manager per second",
1359            registry
1360        )
1361        .unwrap();
1362
1363        let lru_latest_sequence = register_int_gauge_with_registry!(
1364            "lru_latest_sequence",
1365            "Current LRU global sequence",
1366            registry,
1367        )
1368        .unwrap();
1369
1370        let lru_watermark_sequence = register_int_gauge_with_registry!(
1371            "lru_watermark_sequence",
1372            "Current LRU watermark sequence",
1373            registry,
1374        )
1375        .unwrap();
1376
1377        let lru_eviction_policy = register_int_gauge_with_registry!(
1378            "lru_eviction_policy",
1379            "Current LRU eviction policy",
1380            registry,
1381        )
1382        .unwrap();
1383
1384        let jemalloc_allocated_bytes = register_int_gauge_with_registry!(
1385            "jemalloc_allocated_bytes",
1386            "The allocated memory jemalloc, got from jemalloc_ctl",
1387            registry
1388        )
1389        .unwrap();
1390
1391        let jemalloc_active_bytes = register_int_gauge_with_registry!(
1392            "jemalloc_active_bytes",
1393            "The active memory jemalloc, got from jemalloc_ctl",
1394            registry
1395        )
1396        .unwrap();
1397
1398        let jemalloc_resident_bytes = register_int_gauge_with_registry!(
1399            "jemalloc_resident_bytes",
1400            "The active memory jemalloc, got from jemalloc_ctl",
1401            registry
1402        )
1403        .unwrap();
1404
1405        let jemalloc_metadata_bytes = register_int_gauge_with_registry!(
1406            "jemalloc_metadata_bytes",
1407            "The active memory jemalloc, got from jemalloc_ctl",
1408            registry
1409        )
1410        .unwrap();
1411
1412        let jvm_allocated_bytes = register_int_gauge_with_registry!(
1413            "jvm_allocated_bytes",
1414            "The allocated jvm memory",
1415            registry
1416        )
1417        .unwrap();
1418
1419        let jvm_active_bytes = register_int_gauge_with_registry!(
1420            "jvm_active_bytes",
1421            "The active jvm memory",
1422            registry
1423        )
1424        .unwrap();
1425
1426        let materialize_cache_hit_count = register_guarded_int_counter_vec_with_registry!(
1427            "stream_materialize_cache_hit_count",
1428            "Materialize executor cache hit count",
1429            &["actor_id", "table_id", "fragment_id"],
1430            registry
1431        )
1432        .unwrap()
1433        .relabel_debug_1(level);
1434
1435        let materialize_data_exist_count = register_guarded_int_counter_vec_with_registry!(
1436            "stream_materialize_data_exist_count",
1437            "Materialize executor data exist count",
1438            &["actor_id", "table_id", "fragment_id"],
1439            registry
1440        )
1441        .unwrap()
1442        .relabel_debug_1(level);
1443
1444        let materialize_cache_total_count = register_guarded_int_counter_vec_with_registry!(
1445            "stream_materialize_cache_total_count",
1446            "Materialize executor cache total operation",
1447            &["actor_id", "table_id", "fragment_id"],
1448            registry
1449        )
1450        .unwrap()
1451        .relabel_debug_1(level);
1452
1453        let stream_memory_usage = register_guarded_int_gauge_vec_with_registry!(
1454            "stream_memory_usage",
1455            "Memory usage for stream executors",
1456            &["actor_id", "table_id", "desc"],
1457            registry
1458        )
1459        .unwrap()
1460        .relabel_debug_1_with_aggregation(level, GaugeAggregation::Sum);
1461
1462        let gap_fill_generated_rows_count = register_guarded_int_counter_vec_with_registry!(
1463            "gap_fill_generated_rows_count",
1464            "Total number of rows generated by gap fill executor",
1465            &["actor_id", "fragment_id"],
1466            registry
1467        )
1468        .unwrap()
1469        .relabel_debug_1(level);
1470
1471        let state_table_iter_count = register_guarded_int_counter_vec_with_registry!(
1472            "state_table_iter_count",
1473            "Total number of state table iter operations",
1474            &["actor_id", "fragment_id", "table_id"],
1475            registry
1476        )
1477        .unwrap()
1478        .relabel_debug_1(level);
1479
1480        let state_table_get_count = register_guarded_int_counter_vec_with_registry!(
1481            "state_table_get_count",
1482            "Total number of state table get operations",
1483            &["actor_id", "fragment_id", "table_id"],
1484            registry
1485        )
1486        .unwrap()
1487        .relabel_debug_1(level);
1488
1489        let state_table_iter_vnode_pruned_count = register_guarded_int_counter_vec_with_registry!(
1490            "state_table_iter_vnode_pruned_count",
1491            "Total number of state table iter operations pruned by vnode statistics",
1492            &["actor_id", "fragment_id", "table_id"],
1493            registry
1494        )
1495        .unwrap()
1496        .relabel_debug_1(level);
1497
1498        let state_table_get_vnode_pruned_count = register_guarded_int_counter_vec_with_registry!(
1499            "state_table_get_vnode_pruned_count",
1500            "Total number of state table get operations pruned by vnode statistics",
1501            &["actor_id", "fragment_id", "table_id"],
1502            registry
1503        )
1504        .unwrap()
1505        .relabel_debug_1(level);
1506
1507        let now_streaming_clock_ms = register_guarded_int_gauge_vec_with_registry!(
1508            "stream_now_streaming_clock_ms",
1509            "Latest streaming NOW() timestamp (milliseconds since Unix epoch) \
1510             emitted as a watermark by this fragment on this compute node.",
1511            &["fragment_id"],
1512            registry,
1513        )
1514        .unwrap();
1515        let now_wall_clock_drift_ms = register_guarded_int_gauge_vec_with_registry!(
1516            "stream_now_wall_clock_drift_ms",
1517            "Latest processed barrier epoch minus the latest streaming NOW() timestamp \
1518             emitted as a watermark by this fragment on this compute node, \
1519             in milliseconds. This is relative to processed barriers, not scrape-time wall clock.",
1520            &["fragment_id"],
1521            registry,
1522        )
1523        .unwrap();
1524
1525        Self {
1526            level,
1527            executor_row_count,
1528            mem_stream_node_output_row_count: stream_node_output_row_count,
1529            mem_stream_node_output_blocking_duration_ns: stream_node_output_blocking_duration_ns,
1530            actor_scheduled_duration,
1531            actor_scheduled_cnt,
1532            actor_poll_duration,
1533            actor_poll_cnt,
1534            actor_idle_duration,
1535            actor_idle_cnt,
1536            actor_count,
1537            actor_in_record_cnt,
1538            actor_out_record_cnt,
1539            fragment_channel_buffered_bytes,
1540            actor_current_epoch,
1541            project_expr_inflight_window_size,
1542            source_output_row_count,
1543            source_split_change_count,
1544            source_backfill_row_count,
1545            sink_input_row_count,
1546            sink_input_bytes,
1547            sink_chunk_buffer_size,
1548            exchange_frag_recv_size,
1549            merge_barrier_align_duration,
1550            actor_output_buffer_blocking_duration_ns,
1551            actor_input_buffer_blocking_duration_ns,
1552            join_lookup_miss_count,
1553            join_lookup_total_count,
1554            join_insert_cache_miss_count,
1555            join_actor_input_waiting_duration_ns,
1556            join_match_duration_ns,
1557            join_cached_entry_count,
1558            join_matched_join_keys,
1559            barrier_align_duration,
1560            agg_lookup_miss_count,
1561            agg_total_lookup_count,
1562            agg_cached_entry_count,
1563            agg_chunk_lookup_miss_count,
1564            agg_chunk_total_lookup_count,
1565            agg_dirty_groups_count,
1566            agg_dirty_groups_heap_size,
1567            agg_distinct_cache_miss_count,
1568            agg_distinct_total_cache_count,
1569            agg_distinct_cached_entry_count,
1570            agg_state_cache_lookup_count,
1571            agg_state_cache_miss_count,
1572            group_top_n_cache_miss_count,
1573            group_top_n_total_query_cache_count,
1574            group_top_n_cached_entry_count,
1575            group_top_n_appendonly_cache_miss_count,
1576            group_top_n_appendonly_total_query_cache_count,
1577            group_top_n_appendonly_cached_entry_count,
1578            lookup_cache_miss_count,
1579            lookup_total_query_cache_count,
1580            lookup_cached_entry_count,
1581            temporal_join_cache_miss_count,
1582            temporal_join_total_query_cache_count,
1583            temporal_join_cached_entry_count,
1584            backfill_snapshot_read_row_count,
1585            backfill_upstream_output_row_count,
1586            cdc_backfill_snapshot_read_row_count,
1587            cdc_backfill_upstream_output_row_count,
1588            snapshot_backfill_consume_row_count,
1589            over_window_cached_entry_count,
1590            over_window_cache_lookup_count,
1591            over_window_cache_miss_count,
1592            over_window_range_cache_entry_count,
1593            over_window_range_cache_lookup_count,
1594            over_window_range_cache_left_miss_count,
1595            over_window_range_cache_right_miss_count,
1596            over_window_accessed_entry_count,
1597            over_window_compute_count,
1598            over_window_same_output_count,
1599            over_window_state_cleaned_row_count,
1600            match_recognize_matches_emitted_count,
1601            match_recognize_evicted_rows_count,
1602            match_recognize_scan_budget_exhausted_count,
1603            match_recognize_within_deadline_overflow_count,
1604            match_recognize_retained_rows,
1605            match_recognize_retained_partitions,
1606            barrier_inflight_latency,
1607            barrier_sync_latency,
1608            barrier_batch_size,
1609            barrier_manager_progress,
1610            kv_log_store_storage_write_count,
1611            kv_log_store_storage_write_size,
1612            kv_log_store_rewind_count,
1613            kv_log_store_rewind_delay,
1614            kv_log_store_storage_read_count,
1615            kv_log_store_storage_read_size,
1616            kv_log_store_buffer_unconsumed_item_count,
1617            kv_log_store_buffer_unconsumed_row_count,
1618            kv_log_store_buffer_unconsumed_epoch_count,
1619            kv_log_store_buffer_unconsumed_min_epoch,
1620            kv_log_store_buffer_memory_bytes,
1621            crossdb_last_consumed_min_epoch,
1622            sync_kv_log_store_read_count,
1623            sync_kv_log_store_read_size,
1624            sync_kv_log_store_write_pause_duration_ns,
1625            sync_kv_log_store_state,
1626            sync_kv_log_store_wait_next_poll_ns,
1627            sync_kv_log_store_storage_write_count,
1628            sync_kv_log_store_storage_write_size,
1629            sync_kv_log_store_buffer_unconsumed_item_count,
1630            sync_kv_log_store_buffer_unconsumed_row_count,
1631            sync_kv_log_store_buffer_unconsumed_epoch_count,
1632            sync_kv_log_store_buffer_unconsumed_min_epoch,
1633            sync_kv_log_store_buffer_memory_bytes,
1634            lru_runtime_loop_count,
1635            lru_latest_sequence,
1636            lru_watermark_sequence,
1637            lru_eviction_policy,
1638            jemalloc_allocated_bytes,
1639            jemalloc_active_bytes,
1640            jemalloc_resident_bytes,
1641            jemalloc_metadata_bytes,
1642            jvm_allocated_bytes,
1643            jvm_active_bytes,
1644            stream_memory_usage,
1645            materialize_cache_hit_count,
1646            materialize_data_exist_count,
1647            materialize_cache_total_count,
1648            materialize_input_row_count,
1649            materialize_current_epoch,
1650            pg_cdc_state_table_lsn,
1651            pg_cdc_jni_commit_offset_lsn,
1652            mysql_cdc_state_binlog_file_seq,
1653            mysql_cdc_state_binlog_position,
1654            mariadb_cdc_state_binlog_file_seq,
1655            mariadb_cdc_state_binlog_position,
1656            sqlserver_cdc_state_change_lsn,
1657            sqlserver_cdc_state_commit_lsn,
1658            sqlserver_cdc_jni_commit_offset_lsn,
1659            gap_fill_generated_rows_count,
1660            now_streaming_clock_ms,
1661            now_wall_clock_drift_ms,
1662            state_table_iter_count,
1663            state_table_get_count,
1664            state_table_iter_vnode_pruned_count,
1665            state_table_get_vnode_pruned_count,
1666        }
1667    }
1668
1669    /// Create a new `StreamingMetrics` instance used in tests or other places.
1670    pub fn unused() -> Self {
1671        global_streaming_metrics(MetricLevel::Disabled)
1672    }
1673
1674    pub fn new_actor_metrics(&self, actor_id: ActorId, fragment_id: FragmentId) -> ActorMetrics {
1675        let label_list: &[&str; 2] = &[&actor_id.to_string(), &fragment_id.to_string()];
1676        let actor_scheduled_duration = self
1677            .actor_scheduled_duration
1678            .with_guarded_label_values(label_list);
1679        let actor_scheduled_cnt = self
1680            .actor_scheduled_cnt
1681            .with_guarded_label_values(label_list);
1682        let actor_poll_duration = self
1683            .actor_poll_duration
1684            .with_guarded_label_values(label_list);
1685        let actor_poll_cnt = self.actor_poll_cnt.with_guarded_label_values(label_list);
1686        let actor_idle_duration = self
1687            .actor_idle_duration
1688            .with_guarded_label_values(label_list);
1689        let actor_idle_cnt = self.actor_idle_cnt.with_guarded_label_values(label_list);
1690        ActorMetrics {
1691            actor_scheduled_duration,
1692            actor_scheduled_cnt,
1693            actor_poll_duration,
1694            actor_poll_cnt,
1695            actor_idle_duration,
1696            actor_idle_cnt,
1697        }
1698    }
1699
1700    pub(crate) fn new_actor_input_metrics(
1701        &self,
1702        actor_id: ActorId,
1703        fragment_id: FragmentId,
1704        upstream_fragment_id: FragmentId,
1705    ) -> ActorInputMetrics {
1706        let actor_id_str = actor_id.to_string();
1707        let fragment_id_str = fragment_id.to_string();
1708        let upstream_fragment_id_str = upstream_fragment_id.to_string();
1709        ActorInputMetrics {
1710            actor_in_record_cnt: self.actor_in_record_cnt.with_guarded_label_values(&[
1711                &actor_id_str,
1712                &fragment_id_str,
1713                &upstream_fragment_id_str,
1714            ]),
1715            actor_input_buffer_blocking_duration_ns: self
1716                .actor_input_buffer_blocking_duration_ns
1717                .with_guarded_label_values(&[
1718                    &actor_id_str,
1719                    &fragment_id_str,
1720                    &upstream_fragment_id_str,
1721                ]),
1722        }
1723    }
1724
1725    pub fn new_sink_exec_metrics(
1726        &self,
1727        id: SinkId,
1728        actor_id: ActorId,
1729        fragment_id: FragmentId,
1730    ) -> SinkExecutorMetrics {
1731        let label_list: &[&str; 3] = &[
1732            &actor_id.to_string(),
1733            &id.to_string(),
1734            &fragment_id.to_string(),
1735        ];
1736        SinkExecutorMetrics {
1737            sink_input_row_count: self
1738                .sink_input_row_count
1739                .with_guarded_label_values(label_list),
1740            sink_input_bytes: self.sink_input_bytes.with_guarded_label_values(label_list),
1741            sink_chunk_buffer_size: self
1742                .sink_chunk_buffer_size
1743                .with_guarded_label_values(label_list),
1744        }
1745    }
1746
1747    pub fn new_group_top_n_metrics(
1748        &self,
1749        table_id: TableId,
1750        actor_id: ActorId,
1751        fragment_id: FragmentId,
1752    ) -> GroupTopNMetrics {
1753        let label_list: &[&str; 3] = &[
1754            &actor_id.to_string(),
1755            &table_id.to_string(),
1756            &fragment_id.to_string(),
1757        ];
1758
1759        GroupTopNMetrics {
1760            group_top_n_cache_miss_count: self
1761                .group_top_n_cache_miss_count
1762                .with_guarded_label_values(label_list),
1763            group_top_n_total_query_cache_count: self
1764                .group_top_n_total_query_cache_count
1765                .with_guarded_label_values(label_list),
1766            group_top_n_cached_entry_count: self
1767                .group_top_n_cached_entry_count
1768                .with_guarded_label_values(label_list),
1769        }
1770    }
1771
1772    pub fn new_append_only_group_top_n_metrics(
1773        &self,
1774        table_id: TableId,
1775        actor_id: ActorId,
1776        fragment_id: FragmentId,
1777    ) -> GroupTopNMetrics {
1778        let label_list: &[&str; 3] = &[
1779            &actor_id.to_string(),
1780            &table_id.to_string(),
1781            &fragment_id.to_string(),
1782        ];
1783
1784        GroupTopNMetrics {
1785            group_top_n_cache_miss_count: self
1786                .group_top_n_appendonly_cache_miss_count
1787                .with_guarded_label_values(label_list),
1788            group_top_n_total_query_cache_count: self
1789                .group_top_n_appendonly_total_query_cache_count
1790                .with_guarded_label_values(label_list),
1791            group_top_n_cached_entry_count: self
1792                .group_top_n_appendonly_cached_entry_count
1793                .with_guarded_label_values(label_list),
1794        }
1795    }
1796
1797    pub fn new_lookup_executor_metrics(
1798        &self,
1799        table_id: TableId,
1800        actor_id: ActorId,
1801        fragment_id: FragmentId,
1802    ) -> LookupExecutorMetrics {
1803        let label_list: &[&str; 3] = &[
1804            &actor_id.to_string(),
1805            &table_id.to_string(),
1806            &fragment_id.to_string(),
1807        ];
1808
1809        LookupExecutorMetrics {
1810            lookup_cache_miss_count: self
1811                .lookup_cache_miss_count
1812                .with_guarded_label_values(label_list),
1813            lookup_total_query_cache_count: self
1814                .lookup_total_query_cache_count
1815                .with_guarded_label_values(label_list),
1816            lookup_cached_entry_count: self
1817                .lookup_cached_entry_count
1818                .with_guarded_label_values(label_list),
1819        }
1820    }
1821
1822    pub fn new_hash_agg_metrics(
1823        &self,
1824        table_id: TableId,
1825        actor_id: ActorId,
1826        fragment_id: FragmentId,
1827    ) -> HashAggMetrics {
1828        let label_list: &[&str; 3] = &[
1829            &actor_id.to_string(),
1830            &table_id.to_string(),
1831            &fragment_id.to_string(),
1832        ];
1833        HashAggMetrics {
1834            agg_lookup_miss_count: self
1835                .agg_lookup_miss_count
1836                .with_guarded_label_values(label_list),
1837            agg_total_lookup_count: self
1838                .agg_total_lookup_count
1839                .with_guarded_label_values(label_list),
1840            agg_cached_entry_count: self
1841                .agg_cached_entry_count
1842                .with_guarded_label_values(label_list),
1843            agg_chunk_lookup_miss_count: self
1844                .agg_chunk_lookup_miss_count
1845                .with_guarded_label_values(label_list),
1846            agg_chunk_total_lookup_count: self
1847                .agg_chunk_total_lookup_count
1848                .with_guarded_label_values(label_list),
1849            agg_dirty_groups_count: self
1850                .agg_dirty_groups_count
1851                .with_guarded_label_values(label_list),
1852            agg_dirty_groups_heap_size: self
1853                .agg_dirty_groups_heap_size
1854                .with_guarded_label_values(label_list),
1855            agg_state_cache_lookup_count: self
1856                .agg_state_cache_lookup_count
1857                .with_guarded_label_values(label_list),
1858            agg_state_cache_miss_count: self
1859                .agg_state_cache_miss_count
1860                .with_guarded_label_values(label_list),
1861        }
1862    }
1863
1864    pub fn new_agg_distinct_dedup_metrics(
1865        &self,
1866        table_id: TableId,
1867        actor_id: ActorId,
1868        fragment_id: FragmentId,
1869    ) -> AggDistinctDedupMetrics {
1870        let label_list: &[&str; 3] = &[
1871            &actor_id.to_string(),
1872            &table_id.to_string(),
1873            &fragment_id.to_string(),
1874        ];
1875        AggDistinctDedupMetrics {
1876            agg_distinct_cache_miss_count: self
1877                .agg_distinct_cache_miss_count
1878                .with_guarded_label_values(label_list),
1879            agg_distinct_total_cache_count: self
1880                .agg_distinct_total_cache_count
1881                .with_guarded_label_values(label_list),
1882            agg_distinct_cached_entry_count: self
1883                .agg_distinct_cached_entry_count
1884                .with_guarded_label_values(label_list),
1885        }
1886    }
1887
1888    pub fn new_temporal_join_metrics(
1889        &self,
1890        table_id: TableId,
1891        actor_id: ActorId,
1892        fragment_id: FragmentId,
1893    ) -> TemporalJoinMetrics {
1894        let label_list: &[&str; 3] = &[
1895            &actor_id.to_string(),
1896            &table_id.to_string(),
1897            &fragment_id.to_string(),
1898        ];
1899        TemporalJoinMetrics {
1900            temporal_join_cache_miss_count: self
1901                .temporal_join_cache_miss_count
1902                .with_guarded_label_values(label_list),
1903            temporal_join_total_query_cache_count: self
1904                .temporal_join_total_query_cache_count
1905                .with_guarded_label_values(label_list),
1906            temporal_join_cached_entry_count: self
1907                .temporal_join_cached_entry_count
1908                .with_guarded_label_values(label_list),
1909        }
1910    }
1911
1912    pub fn new_backfill_metrics(&self, table_id: TableId, actor_id: ActorId) -> BackfillMetrics {
1913        let label_list: &[&str; 2] = &[&actor_id.to_string(), &table_id.to_string()];
1914        BackfillMetrics {
1915            backfill_snapshot_read_row_count: self
1916                .backfill_snapshot_read_row_count
1917                .with_guarded_label_values(label_list),
1918            backfill_upstream_output_row_count: self
1919                .backfill_upstream_output_row_count
1920                .with_guarded_label_values(label_list),
1921        }
1922    }
1923
1924    pub fn new_cdc_backfill_metrics(
1925        &self,
1926        table_id: TableId,
1927        actor_id: ActorId,
1928    ) -> CdcBackfillMetrics {
1929        let label_list: &[&str; 2] = &[&actor_id.to_string(), &table_id.to_string()];
1930        CdcBackfillMetrics {
1931            cdc_backfill_snapshot_read_row_count: self
1932                .cdc_backfill_snapshot_read_row_count
1933                .with_guarded_label_values(label_list),
1934            cdc_backfill_upstream_output_row_count: self
1935                .cdc_backfill_upstream_output_row_count
1936                .with_guarded_label_values(label_list),
1937        }
1938    }
1939
1940    pub fn new_over_window_metrics(
1941        &self,
1942        table_id: TableId,
1943        actor_id: ActorId,
1944        fragment_id: FragmentId,
1945    ) -> OverWindowMetrics {
1946        let label_list: &[&str; 3] = &[
1947            &actor_id.to_string(),
1948            &table_id.to_string(),
1949            &fragment_id.to_string(),
1950        ];
1951        OverWindowMetrics {
1952            over_window_cached_entry_count: self
1953                .over_window_cached_entry_count
1954                .with_guarded_label_values(label_list),
1955            over_window_cache_lookup_count: self
1956                .over_window_cache_lookup_count
1957                .with_guarded_label_values(label_list),
1958            over_window_cache_miss_count: self
1959                .over_window_cache_miss_count
1960                .with_guarded_label_values(label_list),
1961            over_window_range_cache_entry_count: self
1962                .over_window_range_cache_entry_count
1963                .with_guarded_label_values(label_list),
1964            over_window_range_cache_lookup_count: self
1965                .over_window_range_cache_lookup_count
1966                .with_guarded_label_values(label_list),
1967            over_window_range_cache_left_miss_count: self
1968                .over_window_range_cache_left_miss_count
1969                .with_guarded_label_values(label_list),
1970            over_window_range_cache_right_miss_count: self
1971                .over_window_range_cache_right_miss_count
1972                .with_guarded_label_values(label_list),
1973            over_window_accessed_entry_count: self
1974                .over_window_accessed_entry_count
1975                .with_guarded_label_values(label_list),
1976            over_window_compute_count: self
1977                .over_window_compute_count
1978                .with_guarded_label_values(label_list),
1979            over_window_same_output_count: self
1980                .over_window_same_output_count
1981                .with_guarded_label_values(label_list),
1982            over_window_state_cleaned_row_count: self
1983                .over_window_state_cleaned_row_count
1984                .with_guarded_label_values(label_list),
1985        }
1986    }
1987
1988    pub fn new_match_recognize_metrics(
1989        &self,
1990        table_id: TableId,
1991        actor_id: ActorId,
1992        fragment_id: FragmentId,
1993    ) -> MatchRecognizeMetrics {
1994        let label_list: &[&str; 3] = &[
1995            &actor_id.to_string(),
1996            &table_id.to_string(),
1997            &fragment_id.to_string(),
1998        ];
1999        MatchRecognizeMetrics {
2000            match_recognize_matches_emitted_count: self
2001                .match_recognize_matches_emitted_count
2002                .with_guarded_label_values(label_list),
2003            match_recognize_evicted_rows_count: self
2004                .match_recognize_evicted_rows_count
2005                .with_guarded_label_values(label_list),
2006            match_recognize_scan_budget_exhausted_count: self
2007                .match_recognize_scan_budget_exhausted_count
2008                .with_guarded_label_values(label_list),
2009            match_recognize_within_deadline_overflow_count: self
2010                .match_recognize_within_deadline_overflow_count
2011                .with_guarded_label_values(label_list),
2012            match_recognize_retained_rows: self
2013                .match_recognize_retained_rows
2014                .with_guarded_label_values(label_list),
2015            match_recognize_retained_partitions: self
2016                .match_recognize_retained_partitions
2017                .with_guarded_label_values(label_list),
2018        }
2019    }
2020
2021    pub fn new_materialize_cache_metrics(
2022        &self,
2023        table_id: TableId,
2024        actor_id: ActorId,
2025        fragment_id: FragmentId,
2026    ) -> MaterializeCacheMetrics {
2027        let label_list: &[&str; 3] = &[
2028            &actor_id.to_string(),
2029            &table_id.to_string(),
2030            &fragment_id.to_string(),
2031        ];
2032        MaterializeCacheMetrics {
2033            materialize_cache_hit_count: self
2034                .materialize_cache_hit_count
2035                .with_guarded_label_values(label_list),
2036            materialize_data_exist_count: self
2037                .materialize_data_exist_count
2038                .with_guarded_label_values(label_list),
2039            materialize_cache_total_count: self
2040                .materialize_cache_total_count
2041                .with_guarded_label_values(label_list),
2042        }
2043    }
2044
2045    pub fn new_materialize_metrics(
2046        &self,
2047        table_id: TableId,
2048        actor_id: ActorId,
2049        fragment_id: FragmentId,
2050    ) -> MaterializeMetrics {
2051        let label_list: &[&str; 3] = &[
2052            &actor_id.to_string(),
2053            &table_id.to_string(),
2054            &fragment_id.to_string(),
2055        ];
2056        MaterializeMetrics {
2057            materialize_input_row_count: self
2058                .materialize_input_row_count
2059                .with_guarded_label_values(label_list),
2060            materialize_current_epoch: self
2061                .materialize_current_epoch
2062                .with_guarded_label_values(label_list),
2063        }
2064    }
2065
2066    pub fn new_state_table_metrics(
2067        &self,
2068        table_id: TableId,
2069        actor_id: ActorId,
2070        fragment_id: FragmentId,
2071    ) -> StateTableMetrics {
2072        let label_list: &[&str; 3] = &[
2073            &actor_id.to_string(),
2074            &fragment_id.to_string(),
2075            &table_id.to_string(),
2076        ];
2077        StateTableMetrics {
2078            iter_count: self
2079                .state_table_iter_count
2080                .with_guarded_label_values(label_list),
2081            get_count: self
2082                .state_table_get_count
2083                .with_guarded_label_values(label_list),
2084            iter_vnode_pruned_count: self
2085                .state_table_iter_vnode_pruned_count
2086                .with_guarded_label_values(label_list),
2087            get_vnode_pruned_count: self
2088                .state_table_get_vnode_pruned_count
2089                .with_guarded_label_values(label_list),
2090        }
2091    }
2092}
2093
2094pub(crate) struct ActorInputMetrics {
2095    pub(crate) actor_in_record_cnt: LabelGuardedIntCounter,
2096    pub(crate) actor_input_buffer_blocking_duration_ns: LabelGuardedIntCounter,
2097}
2098
2099/// Tokio metrics for actors
2100pub struct ActorMetrics {
2101    pub actor_scheduled_duration: LabelGuardedIntCounter,
2102    pub actor_scheduled_cnt: LabelGuardedIntCounter,
2103    pub actor_poll_duration: LabelGuardedIntCounter,
2104    pub actor_poll_cnt: LabelGuardedIntCounter,
2105    pub actor_idle_duration: LabelGuardedIntCounter,
2106    pub actor_idle_cnt: LabelGuardedIntCounter,
2107}
2108
2109pub struct SinkExecutorMetrics {
2110    pub sink_input_row_count: LabelGuardedIntCounter,
2111    pub sink_input_bytes: LabelGuardedIntCounter,
2112    pub sink_chunk_buffer_size: RelabeledAggregatedIntGauge,
2113}
2114
2115pub struct MaterializeCacheMetrics {
2116    pub materialize_cache_hit_count: LabelGuardedIntCounter,
2117    pub materialize_data_exist_count: LabelGuardedIntCounter,
2118    pub materialize_cache_total_count: LabelGuardedIntCounter,
2119}
2120
2121pub struct MaterializeMetrics {
2122    pub materialize_input_row_count: LabelGuardedIntCounter,
2123    pub materialize_current_epoch: RelabeledAggregatedIntGauge,
2124}
2125
2126pub struct GroupTopNMetrics {
2127    pub group_top_n_cache_miss_count: LabelGuardedIntCounter,
2128    pub group_top_n_total_query_cache_count: LabelGuardedIntCounter,
2129    pub group_top_n_cached_entry_count: RelabeledAggregatedIntGauge,
2130}
2131
2132pub struct LookupExecutorMetrics {
2133    pub lookup_cache_miss_count: LabelGuardedIntCounter,
2134    pub lookup_total_query_cache_count: LabelGuardedIntCounter,
2135    pub lookup_cached_entry_count: RelabeledAggregatedIntGauge,
2136}
2137
2138pub struct HashAggMetrics {
2139    pub agg_lookup_miss_count: LabelGuardedIntCounter,
2140    pub agg_total_lookup_count: LabelGuardedIntCounter,
2141    pub agg_cached_entry_count: RelabeledAggregatedIntGauge,
2142    pub agg_chunk_lookup_miss_count: LabelGuardedIntCounter,
2143    pub agg_chunk_total_lookup_count: LabelGuardedIntCounter,
2144    pub agg_dirty_groups_count: RelabeledAggregatedIntGauge,
2145    pub agg_dirty_groups_heap_size: RelabeledAggregatedIntGauge,
2146    pub agg_state_cache_lookup_count: LabelGuardedIntCounter,
2147    pub agg_state_cache_miss_count: LabelGuardedIntCounter,
2148}
2149
2150pub struct AggDistinctDedupMetrics {
2151    pub agg_distinct_cache_miss_count: LabelGuardedIntCounter,
2152    pub agg_distinct_total_cache_count: LabelGuardedIntCounter,
2153    pub agg_distinct_cached_entry_count: RelabeledAggregatedIntGauge,
2154}
2155
2156pub struct TemporalJoinMetrics {
2157    pub temporal_join_cache_miss_count: LabelGuardedIntCounter,
2158    pub temporal_join_total_query_cache_count: LabelGuardedIntCounter,
2159    pub temporal_join_cached_entry_count: RelabeledAggregatedIntGauge,
2160}
2161
2162pub struct BackfillMetrics {
2163    pub backfill_snapshot_read_row_count: LabelGuardedIntCounter,
2164    pub backfill_upstream_output_row_count: LabelGuardedIntCounter,
2165}
2166
2167#[derive(Clone)]
2168pub struct CdcBackfillMetrics {
2169    pub cdc_backfill_snapshot_read_row_count: LabelGuardedIntCounter,
2170    pub cdc_backfill_upstream_output_row_count: LabelGuardedIntCounter,
2171}
2172
2173pub struct OverWindowMetrics {
2174    pub over_window_cached_entry_count: RelabeledAggregatedIntGauge,
2175    pub over_window_cache_lookup_count: LabelGuardedIntCounter,
2176    pub over_window_cache_miss_count: LabelGuardedIntCounter,
2177    pub over_window_range_cache_entry_count: RelabeledAggregatedIntGauge,
2178    pub over_window_range_cache_lookup_count: LabelGuardedIntCounter,
2179    pub over_window_range_cache_left_miss_count: LabelGuardedIntCounter,
2180    pub over_window_range_cache_right_miss_count: LabelGuardedIntCounter,
2181    pub over_window_accessed_entry_count: LabelGuardedIntCounter,
2182    pub over_window_compute_count: LabelGuardedIntCounter,
2183    pub over_window_same_output_count: LabelGuardedIntCounter,
2184    pub over_window_state_cleaned_row_count: LabelGuardedIntCounter,
2185}
2186
2187pub struct MatchRecognizeMetrics {
2188    pub match_recognize_matches_emitted_count: LabelGuardedIntCounter,
2189    pub match_recognize_evicted_rows_count: LabelGuardedIntCounter,
2190    pub match_recognize_scan_budget_exhausted_count: LabelGuardedIntCounter,
2191    pub match_recognize_within_deadline_overflow_count: LabelGuardedIntCounter,
2192    /// Rows currently retained in memory across the actor's partitions. Retention is bounded only
2193    /// by match liveness and `WITHIN`, so this gauge is the one signal of a partition set growing
2194    /// toward memory exhaustion (a pattern whose closer never arrives retains its rows forever).
2195    pub match_recognize_retained_rows: RelabeledAggregatedIntGauge,
2196    /// Partitions currently held in memory, emptied entries included. The watermark pass visits
2197    /// only the partitions it can change, so an entry nothing reaches any more would be invisible
2198    /// to `retained_rows` (it holds no rows); this gauge is where such a leak would show.
2199    pub match_recognize_retained_partitions: RelabeledAggregatedIntGauge,
2200}
2201
2202#[derive(Clone)]
2203pub struct StateTableMetrics {
2204    pub iter_count: LabelGuardedIntCounter,
2205    pub get_count: LabelGuardedIntCounter,
2206    pub iter_vnode_pruned_count: LabelGuardedIntCounter,
2207    pub get_vnode_pruned_count: LabelGuardedIntCounter,
2208}
2209
2210#[cfg(test)]
2211mod tests {
2212    use risingwave_common::metrics::get_label;
2213
2214    use super::*;
2215
2216    fn assert_aggregation(
2217        gauge_vec: &RelabeledAggregatedIntGaugeVec,
2218        first_labels: &[&str],
2219        second_labels: &[&str],
2220        expected: i64,
2221    ) {
2222        let first = gauge_vec.with_guarded_label_values(first_labels);
2223        let second = gauge_vec.with_guarded_label_values(second_labels);
2224        first.set(5);
2225        second.set(7);
2226        assert_eq!(first.get(), expected);
2227        assert_eq!(second.get(), expected);
2228    }
2229
2230    fn assert_min_ignores_absent(
2231        gauge_vec: &RelabeledAggregatedIntGaugeVec,
2232        empty_labels: &[&str],
2233        pending_labels: &[&str],
2234    ) {
2235        let empty = gauge_vec.with_guarded_label_values(empty_labels);
2236        let pending = gauge_vec.with_guarded_label_values(pending_labels);
2237        empty.set_optional(None);
2238        pending.set_optional(Some(100));
2239        assert_eq!(empty.get(), 100);
2240        assert_eq!(pending.get(), 100);
2241    }
2242
2243    fn assert_metric_labels(registry: &Registry, metric_name: &str, expected: &[(&str, &str)]) {
2244        let metric_family = registry
2245            .gather()
2246            .into_iter()
2247            .find(|family| family.name() == metric_name)
2248            .unwrap();
2249        let metric = metric_family.get_metric().first().unwrap();
2250        for (name, value) in expected {
2251            assert_eq!(get_label::<String>(metric, name).as_deref(), Some(*value));
2252        }
2253    }
2254
2255    fn record_reordered_metrics(
2256        metrics: &StreamingMetrics,
2257    ) -> (
2258        LabelGuardedIntCounter,
2259        SinkExecutorMetrics,
2260        HashAggMetrics,
2261        OverWindowMetrics,
2262        MatchRecognizeMetrics,
2263    ) {
2264        let source_output_row_count = metrics
2265            .source_output_row_count
2266            .with_guarded_label_values(&["11", "22", "source", "33"]);
2267        source_output_row_count.inc();
2268        let sink_metrics =
2269            metrics.new_sink_exec_metrics(SinkId::new(44), ActorId::new(11), FragmentId::new(33));
2270        sink_metrics.sink_input_row_count.inc();
2271        let agg_metrics =
2272            metrics.new_hash_agg_metrics(TableId::new(55), ActorId::new(11), FragmentId::new(33));
2273        agg_metrics.agg_lookup_miss_count.inc();
2274        let over_window_metrics = metrics.new_over_window_metrics(
2275            TableId::new(66),
2276            ActorId::new(11),
2277            FragmentId::new(33),
2278        );
2279        over_window_metrics
2280            .over_window_state_cleaned_row_count
2281            .inc();
2282        let match_recognize_metrics = metrics.new_match_recognize_metrics(
2283            TableId::new(77),
2284            ActorId::new(11),
2285            FragmentId::new(33),
2286        );
2287        match_recognize_metrics
2288            .match_recognize_matches_emitted_count
2289            .inc();
2290        match_recognize_metrics.match_recognize_retained_rows.set(1);
2291        (
2292            source_output_row_count,
2293            sink_metrics,
2294            agg_metrics,
2295            over_window_metrics,
2296            match_recognize_metrics,
2297        )
2298    }
2299
2300    #[test]
2301    fn info_and_critical_metrics_blank_actor_id_without_shifting_other_labels() {
2302        for level in [MetricLevel::Critical, MetricLevel::Info] {
2303            let registry = Registry::new();
2304            let metrics = StreamingMetrics::new(&registry, level);
2305            let _recorded_metrics = record_reordered_metrics(&metrics);
2306
2307            assert_metric_labels(
2308                &registry,
2309                "stream_source_output_rows_counts",
2310                &[
2311                    ("actor_id", ""),
2312                    ("source_id", "22"),
2313                    ("source_name", "source"),
2314                    ("fragment_id", "33"),
2315                ],
2316            );
2317            assert_metric_labels(
2318                &registry,
2319                "stream_sink_input_row_count",
2320                &[("actor_id", ""), ("sink_id", "44"), ("fragment_id", "33")],
2321            );
2322            assert_metric_labels(
2323                &registry,
2324                "stream_agg_lookup_miss_count",
2325                &[("actor_id", ""), ("table_id", "55"), ("fragment_id", "33")],
2326            );
2327            assert_metric_labels(
2328                &registry,
2329                "stream_over_window_state_cleaned_row_count",
2330                &[("actor_id", ""), ("table_id", "66"), ("fragment_id", "33")],
2331            );
2332            assert_metric_labels(
2333                &registry,
2334                "stream_match_recognize_matches_emitted_count",
2335                &[("actor_id", ""), ("table_id", "77"), ("fragment_id", "33")],
2336            );
2337            assert_metric_labels(
2338                &registry,
2339                "stream_match_recognize_retained_rows",
2340                &[("actor_id", ""), ("table_id", "77"), ("fragment_id", "33")],
2341            );
2342        }
2343    }
2344
2345    #[test]
2346    fn debug_metrics_retain_actor_id() {
2347        let registry = Registry::new();
2348        let metrics = StreamingMetrics::new(&registry, MetricLevel::Debug);
2349        let _recorded_metrics = record_reordered_metrics(&metrics);
2350
2351        assert_metric_labels(
2352            &registry,
2353            "stream_sink_input_row_count",
2354            &[("actor_id", "11"), ("sink_id", "44"), ("fragment_id", "33")],
2355        );
2356    }
2357
2358    #[test]
2359    fn actor_relabeled_gauges_use_the_assigned_reducers() {
2360        for level in [MetricLevel::Critical, MetricLevel::Info] {
2361            let registry = Registry::new();
2362            let metrics = StreamingMetrics::new(&registry, level);
2363
2364            assert_aggregation(
2365                &metrics.actor_current_epoch,
2366                &["1", "fragment"],
2367                &["2", "fragment"],
2368                5,
2369            );
2370            for gauge_vec in [
2371                &metrics.materialize_current_epoch,
2372                &metrics.crossdb_last_consumed_min_epoch,
2373            ] {
2374                assert_aggregation(
2375                    gauge_vec,
2376                    &["1", "table", "fragment"],
2377                    &["2", "table", "fragment"],
2378                    5,
2379                );
2380            }
2381            assert_aggregation(
2382                &metrics.kv_log_store_buffer_unconsumed_min_epoch,
2383                &["1", "connector", "sink", "name"],
2384                &["2", "connector", "sink", "name"],
2385                5,
2386            );
2387            assert_aggregation(
2388                &metrics.sync_kv_log_store_buffer_unconsumed_min_epoch,
2389                &["1", "target", "fragment", "relation"],
2390                &["2", "target", "fragment", "relation"],
2391                5,
2392            );
2393
2394            assert_aggregation(
2395                &metrics.project_expr_inflight_window_size,
2396                &["1", "fragment"],
2397                &["2", "fragment"],
2398                12,
2399            );
2400            assert_aggregation(
2401                &metrics.sink_chunk_buffer_size,
2402                &["1", "sink", "fragment"],
2403                &["2", "sink", "fragment"],
2404                12,
2405            );
2406            assert_aggregation(
2407                &metrics.join_cached_entry_count,
2408                &["1", "fragment", "side"],
2409                &["2", "fragment", "side"],
2410                12,
2411            );
2412            for gauge_vec in [
2413                &metrics.agg_cached_entry_count,
2414                &metrics.agg_dirty_groups_count,
2415                &metrics.agg_dirty_groups_heap_size,
2416                &metrics.agg_distinct_cached_entry_count,
2417                &metrics.group_top_n_appendonly_cached_entry_count,
2418                &metrics.group_top_n_cached_entry_count,
2419                &metrics.lookup_cached_entry_count,
2420                &metrics.match_recognize_retained_partitions,
2421                &metrics.match_recognize_retained_rows,
2422                &metrics.over_window_cached_entry_count,
2423                &metrics.over_window_range_cache_entry_count,
2424                &metrics.temporal_join_cached_entry_count,
2425            ] {
2426                assert_aggregation(
2427                    gauge_vec,
2428                    &["1", "table", "fragment"],
2429                    &["2", "table", "fragment"],
2430                    12,
2431                );
2432            }
2433            assert_aggregation(
2434                &metrics.stream_memory_usage,
2435                &["1", "table", "description"],
2436                &["2", "table", "description"],
2437                12,
2438            );
2439            assert_aggregation(
2440                &metrics.kv_log_store_buffer_memory_bytes,
2441                &["1", "fragment", "connector", "sink", "name"],
2442                &["2", "fragment", "connector", "sink", "name"],
2443                12,
2444            );
2445            for gauge_vec in [
2446                &metrics.kv_log_store_buffer_unconsumed_epoch_count,
2447                &metrics.kv_log_store_buffer_unconsumed_item_count,
2448                &metrics.kv_log_store_buffer_unconsumed_row_count,
2449            ] {
2450                assert_aggregation(
2451                    gauge_vec,
2452                    &["1", "connector", "sink", "name"],
2453                    &["2", "connector", "sink", "name"],
2454                    12,
2455                );
2456            }
2457            for gauge_vec in [
2458                &metrics.sync_kv_log_store_buffer_memory_bytes,
2459                &metrics.sync_kv_log_store_buffer_unconsumed_epoch_count,
2460                &metrics.sync_kv_log_store_buffer_unconsumed_item_count,
2461                &metrics.sync_kv_log_store_buffer_unconsumed_row_count,
2462            ] {
2463                assert_aggregation(
2464                    gauge_vec,
2465                    &["1", "target", "fragment", "relation"],
2466                    &["2", "target", "fragment", "relation"],
2467                    12,
2468                );
2469            }
2470        }
2471    }
2472
2473    #[test]
2474    fn empty_log_store_buffers_do_not_mask_pending_epochs() {
2475        for level in [MetricLevel::Critical, MetricLevel::Info] {
2476            let registry = Registry::new();
2477            let metrics = StreamingMetrics::new(&registry, level);
2478
2479            assert_min_ignores_absent(
2480                &metrics.kv_log_store_buffer_unconsumed_min_epoch,
2481                &["1", "connector", "sink", "name"],
2482                &["2", "connector", "sink", "name"],
2483            );
2484            assert_min_ignores_absent(
2485                &metrics.sync_kv_log_store_buffer_unconsumed_min_epoch,
2486                &["1", "target", "fragment", "relation"],
2487                &["2", "target", "fragment", "relation"],
2488            );
2489        }
2490    }
2491}