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

Code before:
        <Q::Output as Query>::SqlType: CompatibleType<U, Pg, SqlType = ST>,
        U: Send + FromSqlRow<ST, Pg> + 'static,
        Pg: QueryMetadata<<Q::Output as Query>::SqlType>,
        ST: 'static,
    {
        let query = query.limit(1);

        self.metrics.requests_received.inc();
        let _guard = self.metrics.latency.start_timer();

        let id = self.conn.id;
        let query_debug = diesel::debug_query(&query).to_string();
        match query.get_result(&mut self.conn).await {
            Ok(results) => {
                self.metrics.requests_succeeded.inc();
                debug!(id, "{query_debug}");
                Ok(results)
            }
            Err(err) => {
                self.metrics.requests_failed.inc();
                let message = if is_timeout(&err) {
                    format!("First query timed out id={id} query=[{query_debug}]")
                } else {
                    format!("First query error id={id} query=[{query_debug}]")
                };
                Err(err).context(message)
            }
        }
    }

    pub async fn results<'q, Q, ST, U>(&mut self, query: Q) -> anyhow::Result<Vec<U>>
    where
        Q: Query + QueryFragment<Pg> + QueryId + Send + 'q,
        Q::SqlType: CompatibleType<U, Pg, SqlType = ST>,
        U: Send + FromSqlRow<ST, Pg> + 'static,
        Pg: QueryMetadata<Q::SqlType>,
        ST: 'static,
    {
        self.metrics.requests_received.inc();
        let _guard = self.metrics.latency.start_timer();

        let id = self.conn.id;
        let query_debug = diesel::debug_query(&query).to_string();
        match query.get_results(&mut self.conn).await {
            Ok(results) => {
                self.metrics.requests_succeeded.inc();
                debug!(id, "{query_debug}");
                Ok(results)
            }
            Err(err) => {
                self.metrics.requests_failed.inc();

Review comment (bug):
```suggestion
                self.metrics.requests_succeeded.inc();
                debug!(id, "Succeeded: {query_debug}");
                Ok(results)
            }
            Err(err) => {
                self.metrics.requests_failed.inc();
                if is_timeout(&err) {
                  warn!(id, "Timed out: {query_debug}");
                } else {
                  warn!(id, "Failed with '{err:?}': {query_debug});
                }
```

Code after:
        Q::Output: Query + QueryFragment<Pg> + QueryId + Send + 'q,
        <Q::Output as Query>::SqlType: CompatibleType<U, Pg, SqlType = ST>,
        U: Send + FromSqlRow<ST, Pg> + 'static,
        Pg: QueryMetadata<<Q::Output as Query>::SqlType>,
        ST: 'static,
    {
        let query = query.limit(1);

        self.metrics.requests_received.inc();
        let _guard = self.metrics.latency.start_timer();

        let pid = self.conn.pid;
        let query_debug = diesel::debug_query(&query).to_string();
        match query.get_result(&mut self.conn).await {
            Ok(results) => {
                self.metrics.requests_succeeded.inc();
                debug!(pid, "{query_debug}");
                Ok(results)
            }
            Err(err) => {
                self.metrics.requests_failed.inc();
                if is_timeout(&err) {
                    warn!(pid, "Timed out: {query_debug}");
                } else {
                    warn!(pid, "Failed with '{err:?}': {query_debug}");
                };
                Err(err).with_context(|| format!("First error from DB request pid={pid}"))
            }
        }
    }

    pub async fn results<'q, Q, ST, U>(&mut self, query: Q) -> anyhow::Result<Vec<U>>
    where
        Q: Query + QueryFragment<Pg> + QueryId + Send + 'q,
        Q::SqlType: CompatibleType<U, Pg, SqlType = ST>,
        U: Send + FromSqlRow<ST, Pg> + 'static,
        Pg: QueryMetadata<Q::SqlType>,
        ST: 'static,
    {
        self.metrics.requests_received.inc();
        let _guard = self.metrics.latency.start_timer();

        let pid = self.conn.pid;
        let query_debug = diesel::debug_query(&query).to_string();
        match query.get_results(&mut self.conn).await {
            Ok(results) => {
                self.metrics.requests_succeeded.inc();
                debug!(pid, "{query_debug}");
                Ok(results)
            }
            Err(err) => {