Repository: MystenLabs/sui
File: crates/sui-indexer-alt-metrics/src/db.rs (Rust)

Code before:
                &self.desc[6].1,
                &[
                    ("reason", "broken", stats.connections_closed_broken as f64),
                    ("reason", "invalid", stats.connections_closed_invalid as f64),
                    (
                        "reason",
                        "max_lifetime",
                        stats.connections_closed_max_lifetime as f64,
                    ),
                    (
                        "reason",
                        "idle_timeout",
                        stats.connections_closed_idle_timeout as f64,
                    ),
                ],
            ),
            counter(&self.desc[7].1, requested.load(Ordering::Relaxed) as f64),
            counter(&self.desc[8].1, acquired.load(Ordering::Relaxed) as f64),
            counter(
                &self.desc[9].1,
                unacquired_error.load(Ordering::Relaxed) as f64,
            ),
            counter(
                &self.desc[10].1,
                unacquired_canceled.load(Ordering::Relaxed) as f64,
            ),
        ]
    }
}

fn desc(name: String, help: &str) -> Desc {
    desc_with_labels(name, help, &[])
}

fn desc_with_labels(name: String, help: &str, labels: &[&str]) -> Desc {
    Desc::new(
        name,
        help.to_string(),
        labels.iter().map(|s| s.to_string()).collect(),
        Default::default(),
    )
    .expect("Bad metric description")
}

fn gauge(desc: &Desc, value: f64) -> MetricFamily {
    let mut g = Gauge::default();
    let mut m = Metric::default();
    let mut mf = MetricFamily::new();

    g.set_value(value);
    m.set_gauge(g);

Review comment (suggestion):
We only introduce metrics like this when we don't control where they are incremented/decremented, which is not the case here. We can/should use regular metrics for this, rather than the `DbConnectionStatsCollector`.

Code after:
    let mut m = Metric::default();
    let mut mf = MetricFamily::new();

    g.set_value(value);
    m.set_gauge(g);

    mf.mut_metric().push(m);
    mf.set_name(desc.fq_name.clone());
    mf.set_help(desc.help.clone());
    mf.set_field_type(MetricType::GAUGE);
    mf
}

fn counter(desc: &Desc, value: f64) -> MetricFamily {
    let mut c = Counter::default();
    let mut m = Metric::default();
    let mut mf = MetricFamily::new();

    c.set_value(value);
    m.set_counter(c);

    mf.mut_metric().push(m);
    mf.set_name(desc.fq_name.clone());
    mf.set_help(desc.help.clone());
    mf.set_field_type(MetricType::COUNTER);
    mf
}

fn counter_with_labels(desc: &Desc, values: &[(&str, &str, f64)]) -> MetricFamily {
    let mut mf = MetricFamily::new();

    for (name, label, value) in values {
        let mut c = Counter::default();
        let mut l = LabelPair::default();
        let mut m = Metric::default();

        c.set_value(*value);
        l.set_name(name.to_string());
        l.set_value(label.to_string());

        m.set_counter(c);
        m.mut_label().push(l);
        mf.mut_metric().push(m);
    }

    mf.set_name(desc.fq_name.clone());
    mf.set_help(desc.help.clone());
    mf.set_field_type(MetricType::COUNTER);
    mf
}