Skip to content

Commit

Permalink
Track metrics for user and liquidity orders separately
Browse files Browse the repository at this point in the history
  • Loading branch information
MartinquaXD committed May 6, 2022
1 parent b028a87 commit d68d924
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 9 deletions.
13 changes: 8 additions & 5 deletions crates/orderbook/src/orderbook.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ use thiserror::Error;
struct Metrics {
/// Number of user (non-liquidity) orders created.
user_orders_created: prometheus::Counter,
/// Number of liquidity orders created.
liquidity_orders_created: prometheus::Counter,
}

#[derive(Debug, Error)]
Expand Down Expand Up @@ -137,11 +139,12 @@ impl Orderbook {

self.database.insert_order(&order, fee).await?;

if !order.metadata.is_liquidity_order {
Metrics::instance(metrics::get_metric_storage_registry())
.expect("unexpected error getting metrics instance")
.user_orders_created
.inc();
let metrics = Metrics::instance(metrics::get_metric_storage_registry())
.expect("unexpected error getting metrics instance");
if order.metadata.is_liquidity_order {
metrics.liquidity_orders_created.inc();
} else {
metrics.user_orders_created.inc();
}

self.solvable_orders.request_update();
Expand Down
31 changes: 29 additions & 2 deletions crates/solver/src/driver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,12 @@ use contracts::GPv2Settlement;
use futures::future::join_all;
use gas_estimation::{EstimatedGasPrice, GasPriceEstimating};
use itertools::{Either, Itertools};
use model::order::{Order, OrderKind};
use model::solver_competition::{self, Objective, SolverCompetitionResponse, SolverSettlement};
use num::{rational::Ratio, BigInt, BigRational, ToPrimitive};
use num::{rational::Ratio, BigInt, BigRational, BigUint, ToPrimitive};
use primitive_types::{H160, H256};
use rand::prelude::SliceRandom;
use shared::conversions::u256_to_big_uint;
use shared::{
current_block::{self, CurrentBlockStream},
recent_block_cache::Block,
Expand Down Expand Up @@ -165,14 +167,39 @@ impl Driver {
.await
}

/// Collects all orders which got traded in the settlement. Tapping into partially fillable
/// orders multiple times will not result in duplicates. Partially fillable orders get
/// considered as traded only the first time we tap into their liquidity.
fn get_traded_orders(settlement: &Settlement) -> Vec<Order> {
let mut traded_orders = Vec::new();
for (_, group) in &settlement
.executed_trades()
.group_by(|(trade, _)| trade.order.metadata.uid)
{
let mut group = group.into_iter().peekable();
let order = &group.peek().unwrap().0.order;
let previously_filled = match order.creation.kind {
OrderKind::Buy => order.metadata.executed_buy_amount.clone(),
OrderKind::Sell => order.metadata.executed_sell_amount.clone(),
};
let newly_filled = group.fold(BigUint::default(), |acc, (trade, _)| {
acc + u256_to_big_uint(&trade.executed_amount)
});
if previously_filled == 0u8.into() && newly_filled > 0u8.into() {
traded_orders.push(order.clone());
}
}
traded_orders
}

async fn submit_settlement(
&self,
auction_id: u64,
solver: Arc<dyn Solver>,
rated_settlement: RatedSettlement,
) -> Result<TransactionReceipt> {
let settlement = rated_settlement.settlement;
let traded_orders = settlement.traded_orders().cloned().collect::<Vec<_>>();
let traded_orders = Self::get_traded_orders(&settlement);

self.metrics
.settlement_revertable_status(settlement.revertable(), solver.name());
Expand Down
10 changes: 8 additions & 2 deletions crates/solver/src/metrics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ impl Metrics {

let trade_counter = IntCounterVec::new(
Opts::new("trade_counter", "Number of trades settled"),
&["solver_type"],
&["solver_type", "trade_type"],
)?;
registry.register(Box::new(trade_counter.clone()))?;

Expand Down Expand Up @@ -306,7 +306,13 @@ impl SolverMetrics for Metrics {
fn order_settled(&self, order: &Order, solver: &str) {
let time_to_settlement =
chrono::offset::Utc::now().signed_duration_since(order.metadata.creation_date);
self.trade_counter.with_label_values(&[solver]).inc();
let order_type = match order.metadata.is_liquidity_order {
true => "liquidity_order",
false => "user_order",
};
self.trade_counter
.with_label_values(&[solver, order_type])
.inc();
self.order_settlement_time.inc_by(
time_to_settlement
.num_seconds()
Expand Down

0 comments on commit d68d924

Please sign in to comment.