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

Code before:
                    "Connections that timed out waiting for a connection",
                ),
            ),
            (
                MetricType::COUNTER,
                desc(
                    name("connections_created"),
                    "Connections that have been created in the pool",
                ),
            ),
            (
                MetricType::COUNTER,
                desc_with_labels(
                    name("connections_closed"),
                    "Total connections that were closed",
                    &["reason"],
                ),
            ),
            (
                MetricType::COUNTER,
                desc(
                    name("connections_requested"),
                    "Total requested connections from the pool",
                ),
            ),
            (
                MetricType::COUNTER,
                desc(
                    name("connections_acquired"),
                    "Total requested connections from the pool that have been acquired",
                ),
            ),
            (
                MetricType::COUNTER,
                desc(
                    name("connections_unacquired_error"),
                    "Total requested connections from the pool that were not acquired due to an error",
                ),
            ),
            (
                MetricType::COUNTER,
                desc(
                    name("connections_unacquired_canceled"),
                    "Total requested connections from the pool that were not acquired due task cancellation",
                ),
            ),
        ];

        Self { db, desc }
    }
}

Review comment (suggestion):
maybe I'm missing something, but I think some of these metrics are covered by bb8's statistics:

- `requested <- get_direct + get_waited + get_timed_out`
- acquired = get_direct + get_waited

`unacquired_error` might be interesting, although perhaps also covered by `Statistics` depending on the intended use case

`unacquired_canceled` is interesting as this is a caller-side metric, since bb8 wouldn't know when a caller drops the future mid-flight





Code after:
            ),
            (
                MetricType::COUNTER,
                desc(
                    name("connections_created"),
                    "Connections that have been created in the pool",
                ),
            ),
            (
                MetricType::COUNTER,
                desc_with_labels(
                    name("connections_closed"),
                    "Total connections that were closed",
                    &["reason"],
                ),
            ),
        ];

        Self { db, desc }
    }
}

impl Collector for DbConnectionStatsCollector {
    fn desc(&self) -> Vec<&Desc> {
        self.desc.iter().map(|d| &d.1).collect()
    }

    fn collect(&self) -> Vec<MetricFamily> {
        let state = self.db.state();
        let stats = state.statistics;

        vec![
            gauge(&self.desc[0].1, state.connections as f64),
            gauge(&self.desc[1].1, state.idle_connections as f64),
            counter(&self.desc[2].1, stats.get_direct as f64),
            summary(
                &self.desc[3].1,
                stats.get_wait_time.as_millis() as f64,
                stats.get_waited + stats.get_timed_out,
            ),
            counter(&self.desc[4].1, stats.get_timed_out as f64),
            counter(&self.desc[5].1, stats.connections_created as f64),
            counter_with_labels(
                &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,