Skip to content
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

enha: capture error metrics in rpc_subscriptions_active and remove the method wrapper #1942

Merged
merged 3 commits into from
Jan 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions crates/stratus_macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use syn::Fields;
use syn::Lit;
use syn::Meta;

#[proc_macro_derive(ErrorCode, attributes(error_code))]
#[proc_macro_derive(ErrorCode, attributes(error_code, major_error_code))]
pub fn derive_error_code(input: TokenStream) -> TokenStream {
let input = parse_macro_input!(input as DeriveInput);
derive_error_code_impl(input).into()
Expand Down Expand Up @@ -86,7 +86,7 @@ fn derive_error_code_impl(input: DeriveInput) -> proc_macro2::TokenStream {

// Add reverse mapping
reverse_match_arms.push(quote! {
#error_code => stringify!(#variant_name)
#error_code => Some(stringify!(#variant_name))
});
}

Expand All @@ -98,10 +98,10 @@ fn derive_error_code_impl(input: DeriveInput) -> proc_macro2::TokenStream {
}
}

fn str_repr_from_err_code(code: i32) -> &'static str {
fn str_repr_from_err_code(code: i32) -> Option<&'static str> {
match code {
#(#reverse_match_arms),*,
_ => "Unknown"
_ => None
}
}
}
Expand Down Expand Up @@ -144,12 +144,12 @@ mod tests {
}
}

fn str_repr_from_err_code(code: i32) -> &'static str {
fn str_repr_from_err_code(code: i32) -> Option<&'static str> {
match code {
103i32 => stringify ! (First),
104i32 => stringify ! (Second),
100i32 => stringify ! (Third),
_ => "Unknown"
103i32 => Some(stringify ! (First)),
104i32 => Some(stringify ! (Second)),
100i32 => Some(stringify ! (Third)),
_ => None
}
}
}
Expand Down
47 changes: 30 additions & 17 deletions src/eth/primitives/stratus_error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ use crate::ext::to_json_value;

pub trait ErrorCode {
fn error_code(&self) -> i32;
#[allow(unused)]
fn str_repr_from_err_code(code: i32) -> &'static str;
fn str_repr_from_err_code(code: i32) -> Option<&'static str>;
}

#[derive(Debug, thiserror::Error, strum::EnumProperty, strum::IntoStaticStr, ErrorCode)]
#[major_error_code = 1000]
pub enum RpcError {
#[error("Block filter does not point to a valid block.")]
#[error_code = 1]
Expand Down Expand Up @@ -60,7 +60,9 @@ pub enum RpcError {
#[error_code = 9]
MinerModeParamInvalid,
}

#[derive(Debug, thiserror::Error, strum::EnumProperty, strum::IntoStaticStr, ErrorCode)]
#[major_error_code = 2000]
pub enum TransactionError {
#[error("Account at {address} is not a contract.")]
#[error_code = 1]
Expand Down Expand Up @@ -92,6 +94,7 @@ pub enum TransactionError {
}

#[derive(Debug, thiserror::Error, strum::EnumProperty, strum::IntoStaticStr, ErrorCode)]
#[major_error_code = 3000]
pub enum StorageError {
#[error("Block conflict: {number} already exists in the permanent storage.")]
#[error_code = 1]
Expand Down Expand Up @@ -131,6 +134,7 @@ pub enum StorageError {
}

#[derive(Debug, thiserror::Error, strum::EnumProperty, strum::IntoStaticStr, ErrorCode)]
#[major_error_code = 4000]
pub enum ImporterError {
#[error("Importer is already running.")]
#[error_code = 1]
Expand All @@ -150,6 +154,7 @@ pub enum ImporterError {
}

#[derive(Debug, thiserror::Error, strum::EnumProperty, strum::IntoStaticStr, ErrorCode)]
#[major_error_code = 5000]
pub enum ConsensusError {
#[error("Consensus is temporarily unavailable for follower node.")]
#[error_code = 1]
Expand All @@ -165,6 +170,7 @@ pub enum ConsensusError {
}

#[derive(Debug, thiserror::Error, strum::EnumProperty, strum::IntoStaticStr, ErrorCode)]
#[major_error_code = 6000]
pub enum UnexpectedError {
#[error("Unexpected channel {channel} closed.")]
#[error_code = 1]
Expand All @@ -176,6 +182,7 @@ pub enum UnexpectedError {
}

#[derive(Debug, thiserror::Error, strum::EnumProperty, strum::IntoStaticStr, ErrorCode)]
#[major_error_code = 7000]
pub enum StateError {
#[error("Stratus is not ready to start servicing requests.")]
#[error_code = 1]
Expand Down Expand Up @@ -206,53 +213,59 @@ pub enum StateError {
TransactionsEnabled,
}

#[derive(Debug, thiserror::Error, strum::EnumProperty, strum::IntoStaticStr, ErrorCode)]
#[derive(Debug, thiserror::Error, strum::EnumProperty, strum::IntoStaticStr)]
pub enum StratusError {
#[error(transparent)]
#[error_code = 1000]
RPC(#[from] RpcError),

#[error(transparent)]
#[error_code = 2000]
Transaction(#[from] TransactionError),

#[error(transparent)]
#[error_code = 3000]
Storage(#[from] StorageError),

#[error(transparent)]
#[error_code = 4000]
Importer(#[from] ImporterError),

#[error(transparent)]
#[error_code = 5000]
Consensus(#[from] ConsensusError),

#[error(transparent)]
#[error_code = 6000]
Unexpected(#[from] UnexpectedError),

#[error(transparent)]
#[error_code = 7000]
State(#[from] StateError),
}

impl StratusError {
/// Error code to be used in JSON-RPC response.
pub fn rpc_code(&self) -> i32 {
let inner_error = match self {
impl ErrorCode for StratusError {
fn error_code(&self) -> i32 {
match self {
Self::RPC(err) => err.error_code(),
Self::Transaction(err) => err.error_code(),
Self::Storage(err) => err.error_code(),
Self::Importer(err) => err.error_code(),
Self::Consensus(err) => err.error_code(),
Self::Unexpected(err) => err.error_code(),
Self::State(err) => err.error_code(),
};
}
}

self.error_code() + inner_error
fn str_repr_from_err_code(code: i32) -> Option<&'static str> {
let major = code % 1000;
match major {
1 => RpcError::str_repr_from_err_code(code),
2 => TransactionError::str_repr_from_err_code(code),
3 => StorageError::str_repr_from_err_code(code),
4 => ImporterError::str_repr_from_err_code(code),
5 => ConsensusError::str_repr_from_err_code(code),
6 => UnexpectedError::str_repr_from_err_code(code),
7 => StateError::str_repr_from_err_code(code),
_ => None,
}
}
}

impl StratusError {
/// Error message to be used in JSON-RPC response.
pub fn rpc_message(&self) -> String {
self.to_string()
Expand Down Expand Up @@ -312,6 +325,6 @@ impl From<StratusError> for ErrorObjectOwned {
data => data,
};

Self::owned(value.rpc_code(), value.rpc_message(), Some(data))
Self::owned(value.error_code(), value.rpc_message(), Some(data))
}
}
1 change: 0 additions & 1 deletion src/eth/rpc/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ mod rpc_client_app;
mod rpc_config;
mod rpc_context;
mod rpc_http_middleware;
mod rpc_method_wrapper;
mod rpc_middleware;
mod rpc_parser;
mod rpc_server;
Expand Down
39 changes: 0 additions & 39 deletions src/eth/rpc/rpc_method_wrapper.rs

This file was deleted.

3 changes: 2 additions & 1 deletion src/eth/rpc/rpc_middleware.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ use crate::eth::codegen::SoliditySignature;
use crate::eth::primitives::Address;
use crate::eth::primitives::Bytes;
use crate::eth::primitives::CallInput;
use crate::eth::primitives::ErrorCode;
use crate::eth::primitives::Hash;
use crate::eth::primitives::Nonce;
use crate::eth::primitives::RpcError;
Expand Down Expand Up @@ -246,7 +247,7 @@ impl<'a> Future for RpcResponse<'a> {
{
let rpc_result = match response_result.get("result") {
Some(result) => if_else!(result.is_null(), metrics::LABEL_MISSING, metrics::LABEL_PRESENT),
None => metrics::LABEL_ERROR,
None => StratusError::str_repr_from_err_code(error_code).unwrap_or("Unknown"),
};

let tx_ref = resp.tx.as_ref();
Expand Down
Loading
Loading