sui_json_rpc/
logger.rs

1// Copyright (c) Mysten Labs, Inc.
2// SPDX-License-Identifier: Apache-2.0
3
4#[macro_export]
5macro_rules! with_tracing {
6    ($time_spent_threshold:expr, $future:expr) => {{
7        use tracing::{debug, Instrument, Span};
8        use jsonrpsee::core::{RpcResult};
9        use jsonrpsee::types::error::ErrorObjectOwned;
10        use $crate::error::RpcInterimResult;
11        use anyhow::anyhow;
12
13        async move {
14            let start = std::time::Instant::now();
15            let interim_result: RpcInterimResult<_> = $future.await;
16            let elapsed = start.elapsed();
17            let result: RpcResult<_> = interim_result.map_err(|e: Error| {
18                let anyhow_error = anyhow!("{:?}", e);
19
20                let rpc_error: ErrorObjectOwned = e.into();
21
22                debug!(error=%anyhow_error);
23                rpc_error
24            });
25
26            if elapsed > $time_spent_threshold {
27                debug!(?elapsed, "RPC took longer than threshold to complete.");
28            }
29            result
30        }
31        .instrument(Span::current())
32        .await
33    }};
34
35    ($future:expr) => {{
36        with_tracing!(std::time::Duration::from_secs(1), $future)
37    }};
38}