From 1ecac7ebe33f86ffe63070ac3ba04b78965f771a Mon Sep 17 00:00:00 2001 From: Henry de Valence Date: Thu, 23 May 2024 21:42:36 -0700 Subject: [PATCH] dex: add duration metric for SimulateTrade RPC (#4454) Backports #4454 for inclusion in v0.76.0. Refs #4402. Closes #4457. (cherry picked from commit e1d8b2c60b984942d310dcd32698919a2817638c) --- .../dex/src/component/action_handler/swap.rs | 7 +------ crates/core/component/dex/src/component/metrics.rs | 7 ++++--- crates/core/component/dex/src/component/rpc.rs | 12 ++++++++++-- 3 files changed, 15 insertions(+), 11 deletions(-) diff --git a/crates/core/component/dex/src/component/action_handler/swap.rs b/crates/core/component/dex/src/component/action_handler/swap.rs index 86d4591c8c..8eb5e7a388 100644 --- a/crates/core/component/dex/src/component/action_handler/swap.rs +++ b/crates/core/component/dex/src/component/action_handler/swap.rs @@ -9,9 +9,7 @@ use penumbra_proto::StateWriteProto; use penumbra_sct::component::source::SourceContext; use crate::{ - component::{ - metrics, position_manager::PositionManager as _, StateReadExt, StateWriteExt, SwapManager, - }, + component::{position_manager::PositionManager as _, StateReadExt, StateWriteExt, SwapManager}, event, swap::{proof::SwapProofPublic, Swap}, }; @@ -46,7 +44,6 @@ impl ActionHandler for Swap { "Dex MUST be enabled to process swap actions." ); - let swap_start = std::time::Instant::now(); let swap = self; // All swaps will be tallied for the block so the @@ -77,8 +74,6 @@ impl ActionHandler for Swap { ); state.add_recently_accessed_asset(swap.body.trading_pair.asset_2(), fixed_candidates); - metrics::histogram!(crate::component::metrics::DEX_SWAP_DURATION) - .record(swap_start.elapsed()); state.record_proto(event::swap(self)); Ok(()) diff --git a/crates/core/component/dex/src/component/metrics.rs b/crates/core/component/dex/src/component/metrics.rs index 547707ba17..056f3bd172 100644 --- a/crates/core/component/dex/src/component/metrics.rs +++ b/crates/core/component/dex/src/component/metrics.rs @@ -36,9 +36,9 @@ pub fn register_metrics() { "The time spent filling routes while executing trades within the DEX" ); describe_histogram!( - DEX_SWAP_DURATION, + DEX_RPC_SIMULATE_TRADE_DURATION, Unit::Seconds, - "The time spent processing swaps within the DEX" + "The time spent processing a SimulateTrade RPC request" ); } @@ -52,4 +52,5 @@ pub const DEX_PATH_SEARCH_DURATION: &str = "penumbra_dex_path_search_duration_se pub const DEX_ROUTE_FILL_DURATION: &str = "penumbra_dex_route_fill_duration_seconds"; pub const DEX_ARB_DURATION: &str = "penumbra_dex_arb_duration_seconds"; pub const DEX_BATCH_DURATION: &str = "penumbra_dex_batch_duration_seconds"; -pub const DEX_SWAP_DURATION: &str = "penumbra_dex_swap_duration_seconds"; +pub const DEX_RPC_SIMULATE_TRADE_DURATION: &str = + "penumbra_dex_rpc_simulate_trade_duration_seconds"; diff --git a/crates/core/component/dex/src/component/rpc.rs b/crates/core/component/dex/src/component/rpc.rs index fd76162d84..5d02b7fa4b 100644 --- a/crates/core/component/dex/src/component/rpc.rs +++ b/crates/core/component/dex/src/component/rpc.rs @@ -25,6 +25,7 @@ use penumbra_proto::{ use super::ExecutionCircuitBreaker; use crate::{ + component::metrics, lp::position::{self, Position}, state_key, DirectedTradingPair, SwapExecution, TradingPair, }; @@ -522,6 +523,7 @@ impl SimulationService for Server { tonic::Status::invalid_argument(format!("error parsing output id: {:#}", e)) })?; + let start_time = std::time::Instant::now(); let state = self.storage.latest_snapshot(); let mut routing_params = state.routing_params().await.expect("routing params unset"); @@ -560,9 +562,15 @@ impl SimulationService for Server { asset_id: input.asset_id, }; - Ok(tonic::Response::new(SimulateTradeResponse { + let rsp = tonic::Response::new(SimulateTradeResponse { unfilled: Some(unfilled.into()), output: Some(swap_execution.into()), - })) + }); + + let duration = start_time.elapsed(); + + metrics::histogram!(metrics::DEX_RPC_SIMULATE_TRADE_DURATION).record(duration); + + Ok(rsp) } }