Skip to content

Commit

Permalink
Move instrumented gas estimator into shared (#413)
Browse files Browse the repository at this point in the history
* Convert gas estimator metrics to MetricStorage

* Move InstrumentedGasPriceEstimator into shared
  • Loading branch information
vkgnosis authored Aug 5, 2022
1 parent e655138 commit adf888f
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 26 deletions.
1 change: 0 additions & 1 deletion crates/orderbook/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ pub mod api;
pub mod arguments;
pub mod database;
pub mod fee_subsidy;
pub mod gas_price;
pub mod metrics;
pub mod order_quoting;
pub mod order_validation;
Expand Down
3 changes: 1 addition & 2 deletions crates/orderbook/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ use orderbook::{
fee_subsidy::{
config::FeeSubsidyConfiguration, cow_token::CowSubsidy, FeeSubsidies, FeeSubsidizing,
},
gas_price::InstrumentedGasEstimator,
metrics::Metrics,
order_quoting::{Forget, OrderQuoter, QuoteHandler, QuoteStoring},
order_validation::{OrderValidator, SignatureConfiguration},
Expand All @@ -35,6 +34,7 @@ use shared::{
balancer_sor_api::DefaultBalancerSorApi,
baseline_solver::BaseTokens,
current_block::current_block_stream,
gas_price::InstrumentedGasEstimator,
http_solver::{DefaultHttpSolverApi, Objective, SolverConfig},
maintenance::ServiceMaintenance,
metrics::{serve_metrics, DEFAULT_METRICS_PORT},
Expand Down Expand Up @@ -163,7 +163,6 @@ async fn main() {
)
.await
.expect("failed to create gas price estimator"),
metrics.clone(),
));

let baseline_sources = args.shared.baseline_sources.unwrap_or_else(|| {
Expand Down
16 changes: 1 addition & 15 deletions crates/orderbook/src/metrics.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
use anyhow::Result;
use gas_estimation::GasPrice1559;
use prometheus::{Gauge, IntCounter, IntCounterVec, IntGauge, Opts};
use prometheus::{IntCounter, IntCounterVec, IntGauge, Opts};

pub struct Metrics {
/// Gas estimate metrics
gas_price: Gauge,
native_price_cache: IntCounterVec,
// auction metrics
auction_creations: IntCounter,
Expand All @@ -18,10 +15,6 @@ impl Metrics {
pub fn new() -> Result<Self> {
let registry = global_metrics::get_metrics_registry();

let opts = Opts::new("gas_price", "Gas price estimate over time.");
let gas_price = Gauge::with_opts(opts).unwrap();
registry.register(Box::new(gas_price.clone()))?;

let native_price_cache = IntCounterVec::new(
Opts::new("native_price_cache", "Native price cache hit/miss counter."),
&["result"],
Expand Down Expand Up @@ -59,7 +52,6 @@ impl Metrics {
registry.register(Box::new(auction_price_estimate_timeouts.clone()))?;

Ok(Self {
gas_price,
native_price_cache,
auction_creations,
auction_solvable_orders,
Expand Down Expand Up @@ -89,12 +81,6 @@ impl crate::solvable_orders::AuctionMetrics for Metrics {
}
}

impl crate::gas_price::Metrics for Metrics {
fn gas_price(&self, estimate: GasPrice1559) {
self.gas_price.set(estimate.effective_gas_price() / 1e9);
}
}

impl shared::price_estimation::native_price_cache::Metrics for Metrics {
fn native_price_cache(&self, misses: usize, hits: usize) {
self.native_price_cache
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,23 @@

use anyhow::Result;
use gas_estimation::{GasPrice1559, GasPriceEstimating};
use std::{sync::Arc, time::Duration};
use std::time::Duration;

/// An instrumented gas price estimator that wraps an inner one.
pub struct InstrumentedGasEstimator<T> {
inner: T,
metrics: Arc<dyn Metrics>,
metrics: &'static Metrics,
}

impl<T> InstrumentedGasEstimator<T>
where
T: GasPriceEstimating,
{
pub fn new(inner: T, metrics: Arc<dyn Metrics>) -> Self {
Self { inner, metrics }
pub fn new(inner: T) -> Self {
Self {
inner,
metrics: Metrics::instance(global_metrics::get_metric_storage_registry()).unwrap(),
}
}
}

Expand All @@ -40,12 +43,15 @@ where

async fn estimate(&self) -> Result<GasPrice1559> {
let estimate = self.inner.estimate().await?;
self.metrics.gas_price(estimate);
self.metrics
.gas_price
.set(estimate.effective_gas_price() / 1e9);
Ok(estimate)
}
}

/// Gas estimator metrics.
pub trait Metrics: Send + Sync + 'static {
fn gas_price(&self, estimate: GasPrice1559);
#[derive(prometheus_metric_storage::MetricStorage)]
struct Metrics {
/// Last measured gas price in gwei
gas_price: prometheus::Gauge,
}
1 change: 1 addition & 0 deletions crates/shared/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ pub mod conversions;
pub mod current_block;
pub mod ethcontract_error;
pub mod event_handling;
pub mod gas_price;
pub mod gas_price_estimation;
pub mod http_client;
pub mod http_solver;
Expand Down

0 comments on commit adf888f

Please sign in to comment.