Skip to content

Commit 9616717

Browse files
committed
Correctly label the orderbook orders metric
1 parent fec0394 commit 9616717

File tree

3 files changed

+57
-30
lines changed

3 files changed

+57
-30
lines changed

Cargo.lock

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/orderbook/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ serde = { workspace = true }
4848
serde_json = { workspace = true }
4949
serde_with = { workspace = true }
5050
shared = { path = "../shared" }
51+
strum_macros = "0.26.4"
5152
sqlx = { workspace = true }
5253
thiserror = { workspace = true }
5354
tokio = { workspace = true, features = ["macros", "rt-multi-thread", "signal", "sync", "time"] }

crates/orderbook/src/orderbook.rs

+55-30
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ use {
1313
order::{
1414
Order,
1515
OrderCancellation,
16-
OrderClass,
1716
OrderCreation,
1817
OrderCreationAppData,
1918
OrderStatus,
@@ -28,9 +27,15 @@ use {
2827
shared::{
2928
metrics::LivenessChecking,
3029
order_quoting::Quote,
31-
order_validation::{OrderValidating, ValidationError},
30+
order_validation::{
31+
is_order_outside_market_price,
32+
Amounts,
33+
OrderValidating,
34+
ValidationError,
35+
},
3236
},
3337
std::{borrow::Cow, sync::Arc},
38+
strum_macros::Display,
3439
thiserror::Error,
3540
};
3641

@@ -42,24 +47,18 @@ struct Metrics {
4247
orders: prometheus::IntCounterVec,
4348
}
4449

50+
#[derive(Display)]
51+
#[strum(serialize_all = "snake_case")]
4552
enum OrderOperation {
4653
Created,
4754
Cancelled,
4855
}
4956

50-
fn operation_label(op: &OrderOperation) -> &'static str {
51-
match op {
52-
OrderOperation::Created => "created",
53-
OrderOperation::Cancelled => "cancelled",
54-
}
55-
}
56-
57-
fn order_class_label(class: &OrderClass) -> &'static str {
58-
match class {
59-
OrderClass::Market => "user",
60-
OrderClass::Liquidity => "liquidity",
61-
OrderClass::Limit => "limit",
62-
}
57+
#[derive(Display)]
58+
#[strum(serialize_all = "snake_case")]
59+
enum OrderClass {
60+
User,
61+
Limit,
6362
}
6463

6564
impl Metrics {
@@ -68,20 +67,42 @@ impl Metrics {
6867
.expect("unexpected error getting metrics instance")
6968
}
7069

71-
fn on_order_operation(order: &Order, operation: OrderOperation) {
72-
let class = order_class_label(&order.metadata.class);
73-
let op = operation_label(&operation);
74-
Self::get().orders.with_label_values(&[class, op]).inc();
70+
fn on_order_operation(order: &Order, operation: OrderOperation, quote: Option<&Quote>) {
71+
let class = match quote {
72+
Some(ref quote) if
73+
// Check if the order at the submission time was billable (`user` order)
74+
is_order_outside_market_price(
75+
&Amounts {
76+
sell: order.data.sell_amount,
77+
buy: order.data.buy_amount,
78+
fee: order.data.fee_amount,
79+
},
80+
&Amounts {
81+
sell: quote.sell_amount,
82+
buy: quote.buy_amount,
83+
fee: quote.fee_amount,
84+
},
85+
order.data.kind) =>
86+
{
87+
OrderClass::User
88+
}
89+
_ => OrderClass::Limit,
90+
};
91+
Self::get()
92+
.orders
93+
.with_label_values(&[&class.to_string(), &operation.to_string()])
94+
.inc();
7595
}
7696

7797
// Resets all the counters to 0 so we can always use them in Grafana queries.
7898
fn initialize() {
7999
let metrics = Self::get();
80100
for op in &[OrderOperation::Created, OrderOperation::Cancelled] {
81-
let op = operation_label(op);
82-
for class in &[OrderClass::Market, OrderClass::Liquidity, OrderClass::Limit] {
83-
let class = order_class_label(class);
84-
metrics.orders.with_label_values(&[class, op]).reset();
101+
for class in &[OrderClass::User, OrderClass::Limit] {
102+
metrics
103+
.orders
104+
.with_label_values(&[&class.to_string(), &op.to_string()])
105+
.reset();
85106
}
86107
}
87108
}
@@ -220,10 +241,10 @@ impl Orderbook {
220241
let quote_id = quote.as_ref().and_then(|quote| quote.id);
221242

222243
self.database
223-
.insert_order(&order, quote)
244+
.insert_order(&order, quote.clone())
224245
.await
225246
.map_err(|err| AddOrderError::from_insertion(err, &order))?;
226-
Metrics::on_order_operation(&order, OrderOperation::Created);
247+
Metrics::on_order_operation(&order, OrderOperation::Created, quote.as_ref());
227248

228249
Ok((order.metadata.uid, quote_id))
229250
}
@@ -281,7 +302,7 @@ impl Orderbook {
281302

282303
for order in &orders {
283304
tracing::debug!(order_uid =% order.metadata.uid, "order cancelled");
284-
Metrics::on_order_operation(order, OrderOperation::Cancelled);
305+
Metrics::on_order_operation(order, OrderOperation::Cancelled, None);
285306
}
286307

287308
Ok(())
@@ -310,7 +331,7 @@ impl Orderbook {
310331
.await?;
311332

312333
tracing::debug!(order_uid =% order.metadata.uid, "order cancelled");
313-
Metrics::on_order_operation(&order, OrderOperation::Cancelled);
334+
Metrics::on_order_operation(&order, OrderOperation::Cancelled, None);
314335

315336
Ok(())
316337
}
@@ -367,11 +388,15 @@ impl Orderbook {
367388
let quote_id = quote.as_ref().and_then(|quote| quote.id);
368389

369390
self.database
370-
.replace_order(&old_order.metadata.uid, &validated_new_order, quote)
391+
.replace_order(&old_order.metadata.uid, &validated_new_order, quote.clone())
371392
.await
372393
.map_err(|err| AddOrderError::from_insertion(err, &validated_new_order))?;
373-
Metrics::on_order_operation(&old_order, OrderOperation::Cancelled);
374-
Metrics::on_order_operation(&validated_new_order, OrderOperation::Created);
394+
Metrics::on_order_operation(&old_order, OrderOperation::Cancelled, quote.as_ref());
395+
Metrics::on_order_operation(
396+
&validated_new_order,
397+
OrderOperation::Created,
398+
quote.as_ref(),
399+
);
375400

376401
Ok((validated_new_order.metadata.uid, quote_id))
377402
}

0 commit comments

Comments
 (0)