-
Notifications
You must be signed in to change notification settings - Fork 50
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: add rpc logging #75
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
use std::collections::HashMap; | ||
use std::fmt::Debug; | ||
|
||
use tokio_retry::strategy::{jitter, ExponentialBackoff}; | ||
use tokio_retry::Retry; | ||
|
@@ -37,11 +38,84 @@ impl Client { | |
Self { rpc_addr } | ||
} | ||
|
||
pub(crate) async fn query<M: methods::RpcMethod>( | ||
async fn query_retry<M: methods::RpcMethod, F, T>( | ||
&self, | ||
method: &M, | ||
) -> MethodCallResult<M::Response, M::Error> { | ||
retry(|| async { JsonRpcClient::connect(&self.rpc_addr).call(method).await }).await | ||
f: F, | ||
) -> MethodCallResult<M::Response, M::Error> | ||
where | ||
F: Fn() -> T, | ||
T: core::future::Future<Output = MethodCallResult<M::Response, M::Error>>, | ||
{ | ||
retry(|| async { f().await }).await | ||
} | ||
|
||
pub(crate) async fn query_broadcast_tx( | ||
&self, | ||
method: &methods::broadcast_tx_commit::RpcBroadcastTxCommitRequest, | ||
) -> MethodCallResult< | ||
FinalExecutionOutcomeView, | ||
near_jsonrpc_primitives::types::transactions::RpcTransactionError, | ||
> { | ||
self.query_retry::<methods::broadcast_tx_commit::RpcBroadcastTxCommitRequest, _, _>(|| async { | ||
itegulov marked this conversation as resolved.
Show resolved
Hide resolved
|
||
let result = JsonRpcClient::connect(&self.rpc_addr).call(method).await; | ||
match &result { | ||
Ok(response) => { | ||
if tracing::level_enabled!(tracing::Level::DEBUG) { | ||
itegulov marked this conversation as resolved.
Show resolved
Hide resolved
|
||
tracing::debug!( | ||
target: "workspaces", | ||
"Calling RPC method {:?} succeeded with {:?}", | ||
method, | ||
response | ||
); | ||
} else { | ||
tracing::info!( | ||
target: "workspaces", | ||
"Submitting transaction with actions {:?} succeeded with status {:?}", | ||
method.signed_transaction.transaction.actions, | ||
response.status | ||
); | ||
} | ||
} | ||
Err(error) => { | ||
if tracing::level_enabled!(tracing::Level::DEBUG) { | ||
tracing::error!( | ||
target: "workspaces", | ||
"Calling RPC method {:?} resulted in error {:?}", | ||
method, | ||
error | ||
); | ||
} else { | ||
tracing::error!( | ||
target: "workspaces", | ||
"Submitting transaction with actions {:?} resulted in error {:?}", | ||
method.signed_transaction.transaction.actions, | ||
error | ||
); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. hmm, I'm debating this, but for errors, we should be as verbose as possible anyways. There could be cases where errors are hard to reproduce There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You are right. Since errors should not happen very often and they could be hard to reproduce I think it makes sense to print them verbosely all the time. |
||
} | ||
}; | ||
result | ||
}) | ||
.await | ||
} | ||
|
||
pub(crate) async fn query<M>(&self, method: &M) -> MethodCallResult<M::Response, M::Error> | ||
where | ||
M: methods::RpcMethod + Debug, | ||
M::Response: Debug, | ||
M::Error: Debug, | ||
{ | ||
self.query_retry::<M, _, _>(|| async { | ||
let result = JsonRpcClient::connect(&self.rpc_addr).call(method).await; | ||
tracing::debug!( | ||
target: "workspaces", | ||
"Querying RPC with {:?} resulted in {:?}", | ||
method, | ||
result | ||
); | ||
result | ||
}) | ||
.await | ||
} | ||
|
||
async fn send_tx_and_retry( | ||
|
@@ -323,7 +397,7 @@ pub(crate) async fn send_tx( | |
tx: SignedTransaction, | ||
) -> anyhow::Result<FinalExecutionOutcomeView> { | ||
client | ||
.query(&methods::broadcast_tx_commit::RpcBroadcastTxCommitRequest { | ||
.query_broadcast_tx(&methods::broadcast_tx_commit::RpcBroadcastTxCommitRequest { | ||
signed_transaction: tx, | ||
}) | ||
.await | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
#![recursion_limit = "256"] | ||
use test_log::test; | ||
use workspaces::prelude::*; | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't really like how I had to create a separate function for broadcast txs, but it seems like Rust does not support specialization, so I do not know if there is a better way... Any suggestions are welcome
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yeah there's not much you can do here besides this. The alternative is creating some traits to specialize over, but that's not gonna be great either