Browse Source

some refactoring

0xfirefist 1 year ago
parent
commit
2001274c7b
3 changed files with 52 additions and 46 deletions
  1. 18 9
      apps/fortuna/src/command/run.rs
  2. 15 14
      apps/fortuna/src/keeper.rs
  3. 19 23
      apps/fortuna/src/metrics.rs

+ 18 - 9
apps/fortuna/src/command/run.rs

@@ -16,7 +16,7 @@ use {
         keeper,
         metrics::{
             self,
-            ProviderLabel,
+            AccountLabel,
         },
         state::{
             HashChainState,
@@ -274,7 +274,7 @@ pub async fn run(opts: &RunOptions) -> Result<()> {
 
 pub async fn track_balance(
     config: Config,
-    keeper_address: Address,
+    address: Address,
     metrics_registry: Arc<metrics::Metrics>,
 ) {
     loop {
@@ -284,17 +284,21 @@ pub async fn track_balance(
                 Err(_e) => continue,
             };
 
-            let balance = match provider.get_balance(keeper_address, None).await {
+            let balance = match provider.get_balance(address, None).await {
+                // This conversion to u128 is fine as the total balance will never cross the limits
+                // of u128 practically.
                 Ok(r) => r.as_u128(),
                 Err(_e) => continue,
             };
+            // The f64 conversion is made to be able to serve metrics with the constraints of Prometheus.
+            // The balance is in wei, so we need to divide by 1e18 to convert it to eth.
             let balance = balance as f64 / 1e18;
 
             metrics_registry
                 .balance
-                .get_or_create(&ProviderLabel {
+                .get_or_create(&AccountLabel {
                     chain_id: chain_id.clone(),
-                    address:  keeper_address.to_string(),
+                    address:  address.to_string(),
                 })
                 // comment on why is this ok
                 .set(balance);
@@ -321,11 +325,14 @@ pub async fn track_collected_fee(
                     continue;
                 }
             };
+
+            // The f64 conversion is made to be able to serve metrics with the constraints of Prometheus.
+            // The fee is in wei, so we need to divide by 1e18 to convert it to eth.
             let collected_fee = provider_info.accrued_fees_in_wei as f64 / 1e18;
 
             metrics_registry
                 .collected_fee
-                .get_or_create(&ProviderLabel {
+                .get_or_create(&AccountLabel {
                     chain_id: chain_id.clone(),
                     address:  provider_address.to_string(),
                 })
@@ -359,15 +366,17 @@ pub async fn track_hashchain(
 
             metrics_registry
                 .current_sequence_number
-                .get_or_create(&ProviderLabel {
+                .get_or_create(&AccountLabel {
                     chain_id: chain_id.clone(),
                     address:  provider_address.to_string(),
                 })
-                // TODO: comment on i64 to u64 conversion
+                // sequence_number type on chain is u64 but practically it will take
+                // a long time for it to cross the limits of i64.
+                // currently prometheus only supports i64 for Gauge types
                 .set(current_sequence_number as i64);
             metrics_registry
                 .end_sequence_number
-                .get_or_create(&ProviderLabel {
+                .get_or_create(&AccountLabel {
                     chain_id: chain_id.clone(),
                     address:  provider_address.to_string(),
                 })

+ 15 - 14
apps/fortuna/src/keeper.rs

@@ -13,8 +13,8 @@ use {
         },
         config::EthereumConfig,
         metrics::{
+            AccountLabel,
             Metrics,
-            ProviderLabel,
         },
     },
     anyhow::{
@@ -29,6 +29,7 @@ use {
             Ws,
         },
         types::U256,
+        signers::Signer,
     },
     futures::StreamExt,
     std::sync::Arc,
@@ -239,20 +240,20 @@ pub async fn process_event(
                                 res
                             );
 
-                           if let Some(gas_used) = res.gas_used {
-                               let gas_used = gas_used.as_u128() as f64 / 1e18;
-                               metrics
-                                   .total_gas_spent
-                                   .get_or_create(&ProviderLabel {
-                                       chain_id: chain_config.id.clone(),
-                                       address: chain_config.provider_address.to_string(),
-                                   })
-                                   .inc_by(gas_used);
-                           }
+                            if let Some(gas_used) = res.gas_used {
+                                let gas_used = gas_used.as_u128() as f64 / 1e18;
+                                metrics
+                                    .total_gas_spent
+                                    .get_or_create(&AccountLabel {
+                                        chain_id: chain_config.id.clone(),
+                                        address:  contract.client().inner().inner().signer().address().to_string(),
+                                    })
+                                    .inc_by(gas_used);
+                            }
 
                             metrics
                                 .reveals
-                                .get_or_create(&ProviderLabel {
+                                .get_or_create(&AccountLabel {
                                     chain_id: chain_config.id.clone(),
                                     address:  chain_config.provider_address.to_string(),
                                 })
@@ -359,7 +360,7 @@ pub async fn process_single_block_batch(
                 for event in &events {
                     metrics
                         .requests
-                        .get_or_create(&ProviderLabel {
+                        .get_or_create(&AccountLabel {
                             chain_id: chain_state.id.clone(),
                             address:  chain_state.provider_address.to_string(),
                         })
@@ -386,7 +387,7 @@ pub async fn process_single_block_batch(
                     tracing::info!(sequence_number = &event.sequence_number, "Processed event",);
                     metrics
                         .requests_processed
-                        .get_or_create(&ProviderLabel {
+                        .get_or_create(&AccountLabel {
                             chain_id: chain_state.id.clone(),
                             address:  chain_state.provider_address.to_string(),
                         })

+ 19 - 23
apps/fortuna/src/metrics.rs

@@ -25,7 +25,7 @@ pub struct RpcLabel {
 }
 
 #[derive(Clone, Debug, Hash, PartialEq, Eq, EncodeLabelSet)]
-pub struct ProviderLabel {
+pub struct AccountLabel {
     pub chain_id: String,
     pub address:  String,
 }
@@ -35,18 +35,14 @@ pub struct Metrics {
 
     pub request_counter: Family<RequestLabel, Counter>,
 
-    pub current_sequence_number: Family<ProviderLabel, Gauge>,
-    pub end_sequence_number:     Family<ProviderLabel, Gauge>,
-    pub balance:                 Family<ProviderLabel, Gauge<f64, AtomicU64>>,
-    pub collected_fee:           Family<ProviderLabel, Gauge<f64, AtomicU64>>,
-    pub total_gas_spent:         Family<ProviderLabel, Gauge<f64, AtomicU64>>,
-    pub requests:                Family<ProviderLabel, Counter>,
-    pub requests_processed:      Family<ProviderLabel, Counter>,
-    pub reveals:                 Family<ProviderLabel, Counter>,
-    // NOTE: rpc is not part of metrics.
-    // why?
-    // - which metric type should we use to track it?
-    // - let's just use fetched latest safe block from logs
+    pub current_sequence_number: Family<AccountLabel, Gauge>,
+    pub end_sequence_number:     Family<AccountLabel, Gauge>,
+    pub balance:                 Family<AccountLabel, Gauge<f64, AtomicU64>>,
+    pub collected_fee:           Family<AccountLabel, Gauge<f64, AtomicU64>>,
+    pub total_gas_spent:         Family<AccountLabel, Gauge<f64, AtomicU64>>,
+    pub requests:                Family<AccountLabel, Counter>,
+    pub requests_processed:      Family<AccountLabel, Counter>,
+    pub reveals:                 Family<AccountLabel, Counter>,
 }
 
 impl Metrics {
@@ -62,7 +58,7 @@ impl Metrics {
             http_requests.clone(),
         );
 
-        let current_sequence_number = Family::<ProviderLabel, Gauge>::default();
+        let current_sequence_number = Family::<AccountLabel, Gauge>::default();
         metrics_registry.register(
             // With the metric name.
             "current_sequence_number",
@@ -71,25 +67,25 @@ impl Metrics {
             current_sequence_number.clone(),
         );
 
-        let end_sequence_number = Family::<ProviderLabel, Gauge>::default();
+        let end_sequence_number = Family::<AccountLabel, Gauge>::default();
         metrics_registry.register(
             // With the metric name.
             "end_sequence_number",
             // And the metric help text.
-            "The sequence number for the last request.",
+            "The sequence number for the end request.",
             end_sequence_number.clone(),
         );
 
-        let requests = Family::<ProviderLabel, Counter>::default();
+        let requests = Family::<AccountLabel, Counter>::default();
         metrics_registry.register(
             // With the metric name.
             "requests",
             // And the metric help text.
-            "Number of requests received",
+            "Number of requests received through events",
             requests.clone(),
         );
 
-        let requests_processed = Family::<ProviderLabel, Counter>::default();
+        let requests_processed = Family::<AccountLabel, Counter>::default();
         metrics_registry.register(
             // With the metric name.
             "requests_processed",
@@ -98,7 +94,7 @@ impl Metrics {
             requests_processed.clone(),
         );
 
-        let reveals = Family::<ProviderLabel, Counter>::default();
+        let reveals = Family::<AccountLabel, Counter>::default();
         metrics_registry.register(
             // With the metric name.
             "reveal",
@@ -107,7 +103,7 @@ impl Metrics {
             reveals.clone(),
         );
 
-        let balance = Family::<ProviderLabel, Gauge<f64, AtomicU64>>::default();
+        let balance = Family::<AccountLabel, Gauge<f64, AtomicU64>>::default();
         metrics_registry.register(
             // With the metric name.
             "balance",
@@ -116,7 +112,7 @@ impl Metrics {
             balance.clone(),
         );
 
-        let collected_fee = Family::<ProviderLabel, Gauge<f64, AtomicU64>>::default();
+        let collected_fee = Family::<AccountLabel, Gauge<f64, AtomicU64>>::default();
         metrics_registry.register(
             // With the metric name.
             "collected_fee",
@@ -125,7 +121,7 @@ impl Metrics {
             collected_fee.clone(),
         );
 
-        let total_gas_spent = Family::<ProviderLabel, Gauge<f64, AtomicU64>>::default();
+        let total_gas_spent = Family::<AccountLabel, Gauge<f64, AtomicU64>>::default();
         metrics_registry.register(
             // With the metric name.
             "total_gas_spent",