Skip to content

Commit

Permalink
Add recording of metrics to gas price service
Browse files Browse the repository at this point in the history
  • Loading branch information
MitchTurner committed Jan 26, 2025
1 parent 9c3c9fe commit 2301bcd
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 4 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 2 additions & 3 deletions crates/metrics/src/gas_price_metrics.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use std::sync::OnceLock;
use prometheus_client::metrics::gauge::Gauge;

use std::sync::OnceLock;

#[derive(Debug, Default)]
pub struct GasPriceMetrics {
Expand All @@ -18,4 +17,4 @@ static GAS_PRICE_METRICS: OnceLock<GasPriceMetrics> = OnceLock::new();

pub fn gas_price_metrics() -> &'static GasPriceMetrics {
GAS_PRICE_METRICS.get_or_init(GasPriceMetrics::default)
}
}
1 change: 1 addition & 0 deletions crates/services/gas_price_service/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ repository = { workspace = true }
anyhow = { workspace = true }
async-trait = { workspace = true }
enum-iterator = { workspace = true }
fuel-core-metrics = { workspace = true }
fuel-core-services = { workspace = true }
fuel-core-storage = { workspace = true, features = ["std"] }
fuel-core-types = { workspace = true, features = ["std"] }
Expand Down
12 changes: 12 additions & 0 deletions crates/services/gas_price_service/src/v1/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,18 @@ impl V1Metadata {
};
Ok(metadata)
}

pub fn new_exec_gas_price(&self) -> u64 {
self.new_scaled_exec_price
.checked_div(self.gas_price_factor.get())
.unwrap_or(0)
}

pub fn new_da_gas_price(&self) -> u64 {
self.new_scaled_da_gas_price
.checked_div(self.gas_price_factor.get())
.unwrap_or(0)
}
}

#[derive(Debug, Clone, PartialEq)]
Expand Down
49 changes: 48 additions & 1 deletion crates/services/gas_price_service/src/v1/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ use crate::{
};
use anyhow::anyhow;
use async_trait::async_trait;
use fuel_core_metrics::gas_price_metrics::gas_price_metrics;
use fuel_core_services::{
RunnableService,
RunnableTask,
Expand All @@ -48,7 +49,10 @@ use fuel_core_services::{
StateWatcher,
TaskNextAction,
};
use fuel_core_types::fuel_types::BlockHeight;
use fuel_core_types::{
fuel_types::BlockHeight,
services::txpool::Metadata,
};
use fuel_gas_price_algorithm::{
v0::AlgorithmUpdaterV0,
v1::{
Expand Down Expand Up @@ -252,6 +256,7 @@ where
block_gas_capacity: u64,
block_bytes: u64,
block_fees: u64,
gas_price: u64,
) -> anyhow::Result<()> {
let capacity = Self::validate_block_gas_capacity(block_gas_capacity)?;
let mut storage_tx = self.storage_tx_provider.begin_transaction()?;
Expand Down Expand Up @@ -305,11 +310,51 @@ where
let new_algo = self.algorithm_updater.algorithm();
tracing::debug!("Updating gas price: {}", &new_algo.calculate());
self.shared_algo.update(new_algo).await;
Self::record_metrics(&metadata, gas_price);
// Clear the buffer after committing changes
self.da_block_costs_buffer.clear();
Ok(())
}

fn record_metrics(metadata: &UpdaterMetadata, gas_price: u64) {
if let UpdaterMetadata::V1(v1_metadata) = metadata {
let metrics = gas_price_metrics();
let real_gas_price_i64 = gas_price.try_into().unwrap_or(i64::MAX);
let exec_gas_price_i64 = v1_metadata
.new_exec_gas_price()
.try_into()
.unwrap_or(i64::MAX);
let da_gas_price_i64 = v1_metadata
.new_da_gas_price()
.try_into()
.unwrap_or(i64::MAX);
let total_reward_i64 =
v1_metadata.total_da_rewards.try_into().unwrap_or(i64::MAX);
let total_known_costs_i64 = v1_metadata
.latest_known_total_da_cost
.try_into()
.unwrap_or(i64::MAX);
let predicted_profit_i64 =
v1_metadata.last_profit.try_into().unwrap_or(i64::MAX);
let unrecorded_bytes_i64 = v1_metadata
.unrecorded_block_bytes
.try_into()
.unwrap_or(i64::MAX);
let latest_cost_per_byte_i64 = v1_metadata
.latest_da_cost_per_byte
.try_into()
.unwrap_or(i64::MAX);
metrics.real_gas_price.set(real_gas_price_i64);
metrics.exec_gas_price.set(exec_gas_price_i64);
metrics.da_gas_price.set(da_gas_price_i64);
metrics.total_reward.set(total_reward_i64);
metrics.total_known_costs.set(total_known_costs_i64);
metrics.predicted_profit.set(predicted_profit_i64);
metrics.unrecorded_bytes.set(unrecorded_bytes_i64);
metrics.latest_cost_per_byte.set(latest_cost_per_byte_i64);
}
}

async fn apply_block_info_to_gas_algorithm(
&mut self,
l2_block: BlockInfo,
Expand All @@ -329,6 +374,7 @@ where
block_gas_capacity,
block_bytes,
block_fees,
gas_price,
..
} => {
self.handle_normal_block(
Expand All @@ -337,6 +383,7 @@ where
block_gas_capacity,
block_bytes,
block_fees,
gas_price,
)
.await?;
}
Expand Down

0 comments on commit 2301bcd

Please sign in to comment.