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

[PROPOSAL] Correctly label the orderbook orders metric #2917

Merged
merged 17 commits into from
Sep 12, 2024

Conversation

m-lord-renkse
Copy link
Contributor

Description

We need to properly adjust the orderbook orders metric. The reason is that we got a false alarm alert, because as it is now, everything is a limit order, so we got a period of time in which we got orders (real limit ones) and no trades (because those orders were out of market).

This could be solved in many ways, as discussed with @MartinquaXD , one way could be to only report "market" orders. But after giving it a deep thought, I think this is the best way, because we don't lose any type of order, and we report all of them labelled correctly.

Changes

  • Since all the orders are limit orders, I changed the OrderClass of the orders metric to reflect real limit orders or user orders (order not outside the market price). This way we keep all the orders tracked. I meant this class to be exclusively of the metric system, as we don't intent to store user order (aka market order) in the domain. But it is handy to have it in the metrics.
  • Use strum to serialize properly the enums
  • Classify the metric's OrderClass accordingly to the quote price if present.

How to test

  1. Checking / adjusting the metrics in grafana in staging

@m-lord-renkse m-lord-renkse force-pushed the improve-grafana-orderbook-order-metric branch from 9616717 to 41adcd3 Compare August 21, 2024 14:20
@m-lord-renkse m-lord-renkse marked this pull request as ready for review August 21, 2024 14:22
@m-lord-renkse m-lord-renkse requested a review from a team as a code owner August 21, 2024 14:22
#[derive(Display)]
#[strum(serialize_all = "snake_case")]
enum OrderClass {
User,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think user was probably a misnomer to begin with. Let's call it market instead and adjust the grafana alerts accordingly.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we rename this, why don't we call it Market and OutOfMarket or somethinglike this? Technically a market order is also a limit order.

In fact, it could maybe simply become a boolean inMarket?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since we have everywhere "market" and "limit" still, I would suggest to stick with that, otherwise we add inconsistencies. We could clean up all the "market" and "limit" code, and then unify it properly (we also have the "liquidity" there...)

Copy link
Contributor

@fleupold fleupold left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the approach is good, but maybe we can make the enum more clear or even reduce it to a simple boolean?

#[derive(Display)]
#[strum(serialize_all = "snake_case")]
enum OrderClass {
User,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we rename this, why don't we call it Market and OutOfMarket or somethinglike this? Technically a market order is also a limit order.

In fact, it could maybe simply become a boolean inMarket?

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

#[derive(Display)]
#[strum(serialize_all = "snake_case")]
enum OrderOperation {
Created,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like Created should take an Option<Quote> (since quotes are not relevant for cancelled operations). This would also make on_order_operation a bit cleaner (since right now there is an implicit assumption that quote is always None for Cancellation events)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will try to retrieve the quote for the cancelation events, so we can correctly label them.

m-lord-renkse and others added 3 commits August 22, 2024 11:47
Co-authored-by: Martin Beckmann <martin.beckmann@protonmail.com>
Co-authored-by: Martin Beckmann <martin.beckmann@protonmail.com>
@m-lord-renkse m-lord-renkse marked this pull request as draft August 23, 2024 11:39
@m-lord-renkse m-lord-renkse force-pushed the improve-grafana-orderbook-order-metric branch from 514c2f0 to bf58f16 Compare August 23, 2024 11:41
@m-lord-renkse m-lord-renkse force-pushed the improve-grafana-orderbook-order-metric branch from bf58f16 to bed0d84 Compare August 23, 2024 12:00
.zip(order_with_quote.quote_sell_token_price)
.zip(order_with_quote.solver)
.map(
|(
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Weird formatting :/

@@ -567,6 +579,26 @@ pub async fn single_full_order(
sqlx::query_as(QUERY).bind(uid).fetch_optional(ex).await
}

pub async fn single_full_order_with_quote(
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Create a new query so we don't need to do two roundtrip to get an order with its quote data.

}

pub struct SolvableOrders {
pub orders: Vec<Order>,
pub latest_settlement_block: u64,
}

pub struct OrderWithQuote {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Created a new object which contains an order with the quote information (the quote information corresponding to the one stored in order_quotes table, which is the only info we need, not the full quote).

@m-lord-renkse m-lord-renkse marked this pull request as ready for review August 23, 2024 12:54
Copy link

This pull request has been marked as stale because it has been inactive a while. Please update this pull request or it will be automatically closed.

@github-actions github-actions bot added the stale label Aug 31, 2024
@github-actions github-actions bot removed the stale label Sep 3, 2024
}
}

impl Deref for OrderWithQuote {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's not try to get fancy here. Deref is mostly "reserved" for smart pointer types and things like drop guards.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will change it for simplicity, but that actually is not true. Deref is for whatever use you want to use it, as long as it suits you. And it is definitely used for structs in which you have a main object, and then extra appended data, then you want to deref to the main struct instead of indexing it all the time. There are even crates created on top of Deref for cleaner code (see delegate crate).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

😮‍💨 now it is super ugly with the order.order...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I am going to revert it, that's why deref was created...

Copy link
Contributor

@MartinquaXD MartinquaXD Sep 9, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. Initially the docs specifically mentioned to only use if for smart pointers.
  2. since then the phrasing was relaxed to users of that type should not get surprised by deref which arguable is not the case here because we almost never use Deref (mostly a point for code consistency)

now it is super ugly with the order.order
Sure, but it also doesn't look like you tried hard to avoid it looking ugly...

let OrderWithQuote{order, quote} = weird_new_struct;

Ultimately not a hill I'm willing to die an but IMO requiring this "hack" rather indicates that the chosen abstraction doesn't work great.
For example you could also consider making the quote part of the FullOrder which probably is useful in more than just this specific case.

Copy link
Contributor Author

@m-lord-renkse m-lord-renkse Sep 9, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@MartinquaXD yup, I am aware of that PR that removed the comment, I remember it back in the day when it came out. It was a "ok guys, use it as you want" kind of thing 😄 and then many crates like derive_deref, delegate, deref_into, shrinkwraprs, ambassador crates came up (and for sure there are more, but those are the ones I remember, some of them already existed from before like deref_into IIRC). So, that's why I don't agree it is for smart pointers only, it used to be, but now it is quite accepted for many other usages.

Sure, but it also doesn't look like you tried hard to avoid it looking ugly...
😬 I wanted to make a point, that with just adding the deref implementation, we need to avoid order.order or like the code you propose. It is extra lines/overhead.

I agree with you that adding a deref in a codebase it wasn't used before (it is used before but little) may create confusion (that's why I reverted in the first place), but I don't think it adds inconsistency. If we cannot add new crates/patterns because it is the first one, then we will never add anything 🤔

Ultimately not a hill I'm willing to die an but IMO requiring this "hack" rather indicates that the chosen abstraction doesn't work great.

Yup, I agree with you here to be honest. I am not convinced of the struct myself either. But since it was used "just" for the metrics, and it is a pattern I already saw in the codebase, I was like "ok". But I am not super happy about it.

For example you could also consider making the quote part of the FullOrder which probably is useful in more than just this specific case.

That'd be great, if you tell me it is worth doing it. I will definitely do it! my only concern with that is that now for each single full order we also fetch the quote (same roundtrip at least).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@@ -323,6 +354,51 @@ impl OrderStoring for Postgres {
order.map(full_order_into_model_order).transpose()
}

async fn single_order_with_quote(&self, uid: &OrderUid) -> Result<Option<OrderWithQuote>> {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could probably be part of an existing DB test which tests quote fetching in general.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Really good idea, adding them!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added a test at OrderStoring level and database level 👌

Copy link
Contributor

@sunce86 sunce86 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lg

@m-lord-renkse m-lord-renkse force-pushed the improve-grafana-orderbook-order-metric branch from abb7bcc to 3ec80de Compare September 12, 2024 14:38
@m-lord-renkse m-lord-renkse merged commit 61240bc into main Sep 12, 2024
11 checks passed
@m-lord-renkse m-lord-renkse deleted the improve-grafana-orderbook-order-metric branch September 12, 2024 15:24
@github-actions github-actions bot locked and limited conversation to collaborators Sep 12, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants