Skip to content

Commit

Permalink
feat(o11y): adding Tenderly provider monitoring (#873)
Browse files Browse the repository at this point in the history
  • Loading branch information
geekbrother authored Dec 17, 2024
1 parent 455629e commit dc44fba
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 2 deletions.
3 changes: 3 additions & 0 deletions src/handlers/chain_agnostic/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use {
SimulationProvider,
},
utils::crypto::get_erc20_contract_balance,
Metrics,
},
alloy::primitives::{address, Address, B256, U256},
ethers::{types::H160 as EthersH160, utils::keccak256},
Expand Down Expand Up @@ -203,6 +204,7 @@ pub struct Erc20AssetChange {
pub async fn get_assets_changes_from_simulation(
simulation_provider: Arc<dyn SimulationProvider>,
transaction: &Transaction,
metrics: Arc<Metrics>,
) -> Result<(Vec<Erc20AssetChange>, u64), RpcError> {
// Fill the state overrides for the source address for each of the supported
// assets on the initial tx chain
Expand Down Expand Up @@ -233,6 +235,7 @@ pub async fn get_assets_changes_from_simulation(
transaction.to,
transaction.data.clone(),
state_overrides,
metrics,
)
.await?;
let gas_used = simulation_result.transaction.gas_used;
Expand Down
2 changes: 2 additions & 0 deletions src/handlers/chain_agnostic/route.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ async fn handler_internal(
let (_, simulated_gas_used) = get_assets_changes_from_simulation(
state.providers.simulation_provider.clone(),
&initial_transaction,
state.metrics.clone(),
)
.await?;
state
Expand All @@ -156,6 +157,7 @@ async fn handler_internal(
let (simulation_assets_changes, gas_used) = get_assets_changes_from_simulation(
state.providers.simulation_provider.clone(),
&initial_transaction,
state.metrics.clone(),
)
.await?;
let mut asset_transfer_value = U256::ZERO;
Expand Down
1 change: 1 addition & 0 deletions src/providers/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -878,6 +878,7 @@ pub trait SimulationProvider: Send + Sync {
to: Address,
input: Bytes,
state_overrides: HashMap<Address, HashMap<B256, B256>>,
metrics: Arc<Metrics>,
) -> Result<tenderly::SimulationResponse, RpcError>;

/// Get the cached gas estimation for ERC20 transfer
Expand Down
19 changes: 17 additions & 2 deletions src/providers/tenderly.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
use {
crate::{
error::RpcError, providers::SimulationProvider, storage::error::StorageError,
error::RpcError,
providers::{ProviderKind, SimulationProvider},
storage::error::StorageError,
utils::crypto::disassemble_caip2,
Metrics,
},
alloy::primitives::{Address, Bytes, B256, U256},
async_trait::async_trait,
deadpool_redis::{redis::AsyncCommands, Pool},
reqwest::Url,
serde::{Deserialize, Serialize},
std::{collections::HashMap, sync::Arc},
std::{collections::HashMap, sync::Arc, time::SystemTime},
tracing::error,
};

Expand Down Expand Up @@ -79,6 +82,7 @@ pub enum TokenStandard {
}

pub struct TenderlyProvider {
provider_kind: ProviderKind,
api_key: String,
base_api_url: String,
http_client: reqwest::Client,
Expand All @@ -98,6 +102,7 @@ impl TenderlyProvider {
);
let http_client = reqwest::Client::new();
Self {
provider_kind: ProviderKind::Tenderly,
api_key,
base_api_url,
http_client,
Expand Down Expand Up @@ -166,6 +171,7 @@ impl SimulationProvider for TenderlyProvider {
to: Address,
input: Bytes,
state_overrides: HashMap<Address, HashMap<B256, B256>>,
metrics: Arc<Metrics>,
) -> Result<SimulationResponse, RpcError> {
let url = Url::parse(format!("{}/simulate", &self.base_api_url).as_str())
.map_err(|_| RpcError::ConversionParseURLError)?;
Expand All @@ -183,6 +189,7 @@ impl SimulationProvider for TenderlyProvider {
state_objects.insert(address, account_state);
}

let latency_start = SystemTime::now();
let response = self
.send_post_request(
url,
Expand All @@ -197,6 +204,14 @@ impl SimulationProvider for TenderlyProvider {
},
)
.await?;
metrics.add_latency_and_status_code_for_provider(
self.provider_kind,
response.status().into(),
latency_start,
None,
Some("simulate".to_string()),
);

if !response.status().is_success() {
error!(
"Failed to get the transaction simulation response from Tenderly with status: {}",
Expand Down
2 changes: 2 additions & 0 deletions terraform/monitoring/dashboard.jsonnet
Original file line number Diff line number Diff line change
Expand Up @@ -139,13 +139,15 @@ dashboard.new(
panels.status.provider(ds, vars, 'OneInch') { gridPos: pos._4 },
panels.status.provider(ds, vars, 'Coinbase') { gridPos: pos._4 },
panels.status.provider(ds, vars, 'Bungee') { gridPos: pos._4 },
panels.status.provider(ds, vars, 'Tenderly') { gridPos: pos._4 },

row.new('Non-RPC providers Latency'),
panels.non_rpc.endpoints_latency(ds, vars, 'Zerion') { gridPos: pos._4 },
panels.non_rpc.endpoints_latency(ds, vars, 'SolScan') { gridPos: pos._4 },
panels.non_rpc.endpoints_latency(ds, vars, 'OneInch') { gridPos: pos._4 },
panels.non_rpc.endpoints_latency(ds, vars, 'Coinbase') { gridPos: pos._4 },
panels.non_rpc.endpoints_latency(ds, vars, 'Bungee') { gridPos: pos._4 },
panels.non_rpc.endpoints_latency(ds, vars, 'Tenderly') { gridPos: pos._4 },

row.new('Non-RPC providers Cache'),
panels.non_rpc.cache_latency(ds, vars) { gridPos: pos._2 },
Expand Down

0 comments on commit dc44fba

Please sign in to comment.