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

Code before:
    pub async fn first<'q, Q, ST, U>(&mut self, query: Q) -> anyhow::Result<U>
    where
        Q: LimitDsl,
        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);

        let query_debug = diesel::debug_query(&query).to_string();
        debug!("{query_debug}");

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

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

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

        match query.get_results(&mut self.conn).await {
            Ok(results) => {
                self.metrics.requests_succeeded.inc();

Review comment (bug):
hmm, in the original code, the intention was to include the `query_debug` if it was a timeout error. I think we should continue doing that? 

Code after:
    }
}

impl Connection<'_> {
    pub async fn first<'q, Q, ST, U>(&mut self, query: Q) -> anyhow::Result<U>
    where
        Q: LimitDsl,
        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();