Repository: MystenLabs/sui
File: crates/sui-indexer-alt-graphql/src/extensions/logging.rs (Rust)

Code before:
    F: Future<Output = Response>,
{
    type Output = F::Output;

    fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
        let this = self.project();

        let Poll::Ready(mut resp) = this.inner.poll(cx) else {
            return Poll::Pending;
        };

        let Some(RequestMetrics { timer, ext }) = this.metrics.take() else {
            return Poll::Ready(resp);
        };

        let elapsed_ms = timer.stop_and_record() * 1000.0;
        ext.metrics.queries_in_flight.dec();

        // SAFETY: This is set by `prepare_request`.
        let Session { uuid, addr } = ext.session.get().unwrap();
        let request_id = uuid.to_string().try_into().unwrap();
        resp.http_headers.insert(REQUEST_ID_HEADER, request_id);

        if resp.is_ok() {
            if log::log_enabled!(Level::Debug) {
                info!(%uuid, %addr, elapsed_ms, response = %json!(resp), "Request succeeded");
            } else {
                info!(%uuid, %addr, elapsed_ms, "Request succeeded");
            }
            ext.metrics.queries_succeeded.inc();
        } else {
            let codes = error_codes(&resp);

            // Log internal errors, timeouts, and unknown errors at a higher log level than other errors.
            if is_loud_query(&codes) {
                warn!(%uuid, %addr, elapsed_ms, ?codes, response = %json!(resp), query = ext.query.get().unwrap(), "Request failed");
            } else if log::log_enabled!(Level::Debug) {
                info!(%uuid, %addr, elapsed_ms, ?codes, response = %json!(resp), query = ext.query.get().unwrap(), "Request failed");
            } else {
                info!(%uuid, %addr, elapsed_ms, ?codes, "Request failed");
            }

            if codes.is_empty() {
                ext.metrics
                    .queries_failed
                    .with_label_values(&["<UNKNOWN>"])
                    .inc();
            }

            for code in &codes {
                ext.metrics.queries_failed.with_label_values(&[code]).inc();

Review comment (bug):
```suggestion
                debug!(%uuid, %addr, elapsed_ms, response = %json!(resp), "Request succeeded");
```

But also, is the response helpful without the query?

Code after:
        if result.is_ok() {
            self.metrics.fields_succeeded.with_label_values(labels)
        } else {
            self.metrics.fields_failed.with_label_values(labels)
        }
        .inc();

        result
    }
}

impl<F> Future for MetricsFuture<F>
where
    F: Future<Output = Response>,
{
    type Output = F::Output;

    fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
        let this = self.project();

        let Poll::Ready(mut resp) = this.inner.poll(cx) else {
            return Poll::Pending;
        };

        let Some(RequestMetrics { timer, ext }) = this.metrics.take() else {
            return Poll::Ready(resp);
        };

        let elapsed_ms = timer.stop_and_record() * 1000.0;
        ext.metrics.queries_in_flight.dec();

        // SAFETY: This is set by `prepare_request`.
        let Session { uuid, addr } = ext.session.get().unwrap();
        let request_id = uuid.to_string().try_into().unwrap();
        resp.http_headers.insert(REQUEST_ID_HEADER, request_id);

        if resp.is_ok() {
            if log_enabled!(Level::Debug) {
                debug!(%uuid, %addr, elapsed_ms, query = ext.query.get().unwrap(), response = %json!(resp), "Request succeeded");
            } else {
                info!(%uuid, %addr, elapsed_ms, "Request succeeded");
            }
            ext.metrics.queries_succeeded.inc();
        } else {
            let codes = error_codes(&resp);

            // Log internal errors, timeouts, and unknown errors at a higher log level than other errors.
            if is_loud_query(&codes) {
                warn!(%uuid, %addr, elapsed_ms, query = ext.query.get().unwrap(), response = %json!(resp), "Request failed");
            } else if log_enabled!(Level::Debug) {
                debug!(%uuid, %addr, elapsed_ms, query = ext.query.get().unwrap(), response = %json!(resp), "Request failed");