Repository: risingwavelabs/risingwave
File: src/meta/src/hummock/metrics_utils.rs (Rust)

Code before:
    for (table_id, logs) in &checkpoint.version.table_change_log {
        let object_count = logs
            .iter()
            .map(|l| l.old_value.len() + l.new_value.len())
            .sum::<usize>();
        let object_size = logs
            .iter()
            .map(|l| {
                l.old_value
                    .iter()
                    .chain(l.new_value.iter())
                    .map(|s| s.file_size as usize)
                    .sum::<usize>()
            })
            .sum::<usize>();
        metrics
            .table_change_log_object_count
            .with_label_values(&[&format!("{table_id}")])
            .set(object_count as _);
        metrics
            .table_change_log_object_size
            .with_label_values(&[&format!("{table_id}")])
            .set(object_size as _);
        let min_epoch = logs.epochs().min().unwrap_or_default();
        metrics
            .crossdb_changelog_min_epoch
            .with_label_values(&[&format!("{table_id}")])
            .set(min_epoch as _);
    }
}

// Triggers a report on compact_pending_bytes_needed
pub fn trigger_lsm_stat(
    metrics: &MetaMetrics,
    compaction_config: Arc<CompactionConfig>,
    levels: &Levels,
    compaction_group_id: CompactionGroupId,
) {
    let group_label = compaction_group_id.to_string();
    // compact_pending_bytes
    // we don't actually generate a compaction task here so developer config can be ignored.
    let dynamic_level_core = DynamicLevelSelectorCore::new(
        compaction_config.clone(),
        Arc::new(CompactionDeveloperConfig::default()),
    );
    let ctx = dynamic_level_core.calculate_level_base_size(levels);
    {
        let compact_pending_bytes_needed =
            dynamic_level_core.compact_pending_bytes_needed_with_ctx(levels, &ctx);

        metrics

Review comment (suggestion):
May generate `table_id_string` before the code to set `table_change_log_object_count`, and then always reuse the table_id_string.

Code after:
    for (table_id, logs) in &checkpoint.version.table_change_log {
        let table_id_label = table_id.to_string();
        let labels = [table_id_label.as_str()];
        let object_count = logs
            .iter()
            .map(|l| l.old_value.len() + l.new_value.len())
            .sum::<usize>();
        let object_size = logs
            .iter()
            .map(|l| {
                l.old_value
                    .iter()
                    .chain(l.new_value.iter())
                    .map(|s| s.file_size as usize)
                    .sum::<usize>()
            })
            .sum::<usize>();
        metrics
            .table_change_log_object_count
            .with_label_values(&labels)
            .set(object_count as _);
        metrics
            .table_change_log_object_size
            .with_label_values(&labels)
            .set(object_size as _);
        let min_epoch = logs.epochs().min().unwrap_or_default();
        metrics
            .table_change_log_min_epoch
            .with_label_values(&labels)
            .set(min_epoch as _);
    }
}

// Triggers a report on compact_pending_bytes_needed
pub fn trigger_lsm_stat(
    metrics: &MetaMetrics,
    compaction_config: Arc<CompactionConfig>,
    levels: &Levels,
    compaction_group_id: CompactionGroupId,
) {
    let group_label = compaction_group_id.to_string();
    // compact_pending_bytes
    // we don't actually generate a compaction task here so developer config can be ignored.
    let dynamic_level_core = DynamicLevelSelectorCore::new(
        compaction_config.clone(),
        Arc::new(CompactionDeveloperConfig::default()),
    );
    let ctx = dynamic_level_core.calculate_level_base_size(levels);
    {
        let compact_pending_bytes_needed =
            dynamic_level_core.compact_pending_bytes_needed_with_ctx(levels, &ctx);