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

[EASY] Add metrics for logging native price requests #3006

Merged
merged 6 commits into from
Sep 26, 2024

Conversation

m-lord-renkse
Copy link
Contributor

Description

Add more metrics for native price requests. So we can properly assess how many requests are sent for each estimator.

@@ -104,16 +112,6 @@ impl Inner {
}
}

impl Clone for TradeEstimator {
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 guess this was done because it required in the pass a specific Clone implementation. But this currently does what the #[derive(Clone)] should do.

@m-lord-renkse m-lord-renkse marked this pull request as ready for review September 19, 2024 12:49
@m-lord-renkse m-lord-renkse requested a review from a team as a code owner September 19, 2024 12:49
@m-lord-renkse m-lord-renkse force-pushed the add-metrics-for-native-price-requests branch from 9311449 to 28b38c9 Compare September 19, 2024 13:04
Copy link
Contributor

@MartinquaXD MartinquaXD left a comment

Choose a reason for hiding this comment

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

Already nitted on a few implementation details when I remembered that we already have InstrumentedPriceEstimator which already abstracted that logic.
Probably just needs a few adjustments to work with native price estimators as well.

crates/shared/src/price_estimation.rs Outdated Show resolved Hide resolved
crates/shared/src/price_estimation.rs Outdated Show resolved Hide resolved
#[derive(prometheus_metric_storage::MetricStorage)]
pub(in crate::price_estimation) struct Metrics {
/// number of requests done by each estimator
#[metric(labels("requests"))]
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
#[metric(labels("requests"))]
#[metric(labels("estimator"))]

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 am unifying it with the same naming in InstrumentedPriceEstimator to be consistent.

crates/shared/src/price_estimation.rs Outdated Show resolved Hide resolved
@@ -91,6 +92,7 @@ where
token: H160,
) -> futures::future::BoxFuture<'_, NativePriceEstimateResult> {
async move {
Metrics::inc_estimator("buffered");
Copy link
Contributor

Choose a reason for hiding this comment

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

This would become problematic as soon as there are multiple different APIs that support buffering.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

You are definitely right, let's add it to CoinGecko

Copy link
Contributor

Choose a reason for hiding this comment

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

Btw if you use the InstrumentedPriceEstimator wrapper thingy you don't have to add anything to coingecko. You just wrap it in that struct and it starts instrumenting it.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yup, that's what I am doing, but I am trying to do it as nicer as possible, because InstrumentedPriceEstimator is hard-coded for PriceEstimating

@m-lord-renkse
Copy link
Contributor Author

InstrumentedPriceEstimator

Sounds good, let's give it a try.

@m-lord-renkse m-lord-renkse force-pushed the add-metrics-for-native-price-requests branch from 3771174 to 2fb58e4 Compare September 24, 2024 11:58
Copy link
Contributor

@MartinquaXD MartinquaXD left a comment

Choose a reason for hiding this comment

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

Nice!
Just a few nits

pub struct InstrumentedPriceEstimator {
inner: Box<dyn PriceEstimating>,
pub struct InstrumentedPriceEstimator<T> {
inner: Arc<T>,
Copy link
Contributor

Choose a reason for hiding this comment

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

Was there a reason to turn this into an Arc? Doesn't look necessary to me.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good question! The reason I did it is because during the development I wanted to fully remove Box<> and to have just the type T, I put Arc<> temporarily to sanitize some errors and I forgot to delete it during the clean up. Good catch!

The type should be:

inner: T

We do not need any wrapper at all!

// Count as a successful request if the answer is ok (no error) or if the error
// is No Liquidity
let success =
estimate.is_ok() || matches!(&estimate, Err(PriceEstimationError::NoLiquidity));
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 counting NoLiquidity as a success makes sense but then we should also change that in the regular estimator implementation to not have a drift for no reason here.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good point! I added a common function to evaluate the result! 👌

crates/shared/src/price_estimation/instrumented.rs Outdated Show resolved Hide resolved
@@ -155,7 +155,7 @@ impl<'a> PriceEstimatorFactory<'a> {
.as_ref()
.and_then(|trade_verifier| estimator.verified(trade_verifier));

let fast = instrument(estimator, name);
let fast = instrument::<T>(estimator, name);
Copy link
Contributor

Choose a reason for hiding this comment

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

is ::<T> actually needed here? I thought the compiler would be smart enough to deduce T on it's own.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good catch! 👀 it was needed for a middle version I did, but now it is not needed anymore 🙌

@m-lord-renkse m-lord-renkse force-pushed the add-metrics-for-native-price-requests branch from b0a06d1 to 4e3ecee Compare September 24, 2024 14:07
))
}
NativePriceEstimatorSource::OneInchSpotPriceApi => {
let name = "OneInchSpotPriceApi".to_string();
Copy link
Contributor

@squadgazzz squadgazzz Sep 24, 2024

Choose a reason for hiding this comment

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

UpperCamelCase should be avoided since driver.name uses flatcase. We should use a similar format for other names as well. If not flatcase everywhere, maybe snake_case.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah, I agree with you, but this is something systematic within the natice prices, we should change that in another PR alongside the configuration, because this happens also in the configuration of the services :/

Comment on lines 83 to 91
self.metrics
.native_price_estimation_times
.with_label_values(&[self.name.as_str()])
.observe(start.elapsed().as_secs_f64());
let result = self.estimate_result(estimate.as_ref());
self.metrics
.native_price_estimates
.with_label_values(&[self.name.as_str(), result])
.inc();
Copy link
Contributor

@squadgazzz squadgazzz Sep 24, 2024

Choose a reason for hiding this comment

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

Do we really need 2 metrics? Can we use only the native_price_estimation_times with 2 labels and use the _count metric instead of declaring one more with increment?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@squadgazzz in the end, because of this comment: #3006 (comment)
it was decided to directly reuse the current metrics 👌

Copy link
Contributor

@squadgazzz squadgazzz 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 add-metrics-for-native-price-requests branch from a243cdb to 3f137fc Compare September 26, 2024 10:44
@m-lord-renkse m-lord-renkse enabled auto-merge (squash) September 26, 2024 10:47
@m-lord-renkse m-lord-renkse merged commit 1b6ed89 into main Sep 26, 2024
11 checks passed
@m-lord-renkse m-lord-renkse deleted the add-metrics-for-native-price-requests branch September 26, 2024 10:51
@github-actions github-actions bot locked and limited conversation to collaborators Sep 26, 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.

3 participants