Skip to content

Commit

Permalink
fix: add starting stacks height for prometheus metrics (#567)
Browse files Browse the repository at this point in the history
  • Loading branch information
MicaiahReid committed May 8, 2024
1 parent 6f7eba4 commit 6a8c086
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 25 deletions.
40 changes: 27 additions & 13 deletions components/chainhook-cli/src/service/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ use chainhook_sdk::chainhooks::types::{ChainhookConfig, ChainhookFullSpecificati
use chainhook_sdk::chainhooks::types::ChainhookSpecification;
use chainhook_sdk::observer::{
start_event_observer, HookExpirationData, ObserverCommand, ObserverEvent,
PredicateEvaluationReport,
PredicateEvaluationReport, StacksObserverStartupContext,
};
use chainhook_sdk::types::{Chain, StacksChainEvent};
use chainhook_sdk::types::{Chain, StacksBlockData, StacksChainEvent};
use chainhook_sdk::utils::Context;
use redis::{Commands, Connection};

Expand Down Expand Up @@ -243,24 +243,38 @@ impl Service {
let ctx = self.ctx.clone();
let stacks_db =
open_readonly_stacks_db_conn_with_retry(&config.expected_cache_path(), 3, &ctx)?;
let unconfirmed_blocks = match get_all_unconfirmed_blocks(&stacks_db, &ctx) {
let confirmed_tip = get_last_block_height_inserted(&stacks_db, &ctx).unwrap_or(0);
let stacks_startup_context = match get_all_unconfirmed_blocks(&stacks_db, &ctx) {
Ok(blocks) => {
let confirmed_tip = get_last_block_height_inserted(&stacks_db, &ctx).unwrap_or(0);
// any unconfirmed blocks that are earlier than confirmed blocks are invalid
Some(
blocks
.iter()
.filter(|&b| b.block_identifier.index > confirmed_tip)
.cloned()
.collect(),
)

let unconfirmed_blocks = blocks
.iter()
.filter(|&b| b.block_identifier.index > confirmed_tip)
.cloned()
.collect::<Vec<StacksBlockData>>();

let highest_appended = match unconfirmed_blocks
.iter()
.max_by_key(|b| b.block_identifier.index)
{
Some(highest_block) => highest_block.block_identifier.index,
None => confirmed_tip,
};
StacksObserverStartupContext {
block_pool_seed: unconfirmed_blocks,
last_block_height_appended: highest_appended,
}
}
Err(e) => {
info!(
self.ctx.expect_logger(),
"Failed to get stacks blocks from db to seed block pool: {}", e
);
None
StacksObserverStartupContext {
block_pool_seed: vec![],
last_block_height_appended: confirmed_tip,
}
}
};

Expand All @@ -272,7 +286,7 @@ impl Service {
observer_command_rx,
Some(observer_event_tx_moved),
None,
unconfirmed_blocks,
Some(stacks_startup_context),
self.ctx.clone(),
);

Expand Down
15 changes: 15 additions & 0 deletions components/chainhook-sdk/src/monitoring.rs
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,21 @@ impl PrometheusMonitoring {
g
}

pub fn initialize(
&self,
stx_predicates: u64,
btc_predicates: u64,
initial_stx_block: Option<u64>,
) {
self.stx_metrics_set_registered_predicates(stx_predicates);
self.btc_metrics_set_registered_predicates(btc_predicates);
if let Some(initial_stx_block) = initial_stx_block {
self.stx_metrics_block_received(initial_stx_block);
self.stx_metrics_block_appeneded(initial_stx_block);
self.stx_metrics_block_evaluated(initial_stx_block);
}
}

// stx helpers
pub fn stx_metrics_deregister_predicate(&self) {
self.stx_registered_predicates.dec();
Expand Down
27 changes: 15 additions & 12 deletions components/chainhook-sdk/src/observer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,12 @@ pub struct ObserverSidecar {
pub bitcoin_chain_event_notifier: Option<crossbeam_channel::Sender<HandleBlock>>,
}

#[derive(Debug, Clone, Default)]
pub struct StacksObserverStartupContext {
pub block_pool_seed: Vec<StacksBlockData>,
pub last_block_height_appended: u64,
}

impl ObserverSidecar {
fn perform_bitcoin_sidecar_mutations(
&self,
Expand Down Expand Up @@ -426,7 +432,7 @@ pub fn start_event_observer(
observer_commands_rx: Receiver<ObserverCommand>,
observer_events_tx: Option<crossbeam_channel::Sender<ObserverEvent>>,
observer_sidecar: Option<ObserverSidecar>,
stacks_block_pool_seed: Option<Vec<StacksBlockData>>,
stacks_startup_context: Option<StacksObserverStartupContext>,
ctx: Context,
) -> Result<(), Box<dyn Error>> {
match config.bitcoin_block_signaling {
Expand Down Expand Up @@ -478,7 +484,7 @@ pub fn start_event_observer(
observer_commands_rx,
observer_events_tx.clone(),
observer_sidecar,
stacks_block_pool_seed,
stacks_startup_context.unwrap_or_default(),
context_cloned.clone(),
);
match hiro_system_kit::nestable_block_on(future) {
Expand Down Expand Up @@ -536,11 +542,10 @@ pub async fn start_bitcoin_event_observer(
}

let prometheus_monitoring = PrometheusMonitoring::new();
prometheus_monitoring.stx_metrics_set_registered_predicates(
prometheus_monitoring.initialize(
chainhook_store.predicates.stacks_chainhooks.len() as u64,
);
prometheus_monitoring.btc_metrics_set_registered_predicates(
chainhook_store.predicates.bitcoin_chainhooks.len() as u64,
None,
);

if let Some(port) = config.prometheus_monitoring_port {
Expand Down Expand Up @@ -575,7 +580,7 @@ pub async fn start_stacks_event_observer(
observer_commands_rx: Receiver<ObserverCommand>,
observer_events_tx: Option<crossbeam_channel::Sender<ObserverEvent>>,
observer_sidecar: Option<ObserverSidecar>,
stacks_block_pool_seed: Option<Vec<StacksBlockData>>,
stacks_startup_context: StacksObserverStartupContext,
ctx: Context,
) -> Result<(), Box<dyn Error>> {
let indexer_config = IndexerConfig {
Expand All @@ -588,9 +593,8 @@ pub async fn start_stacks_event_observer(
};

let mut indexer = Indexer::new(indexer_config.clone());
if let Some(stacks_block_pool_seed) = stacks_block_pool_seed {
indexer.seed_stacks_block_pool(stacks_block_pool_seed, &ctx);
}

indexer.seed_stacks_block_pool(stacks_startup_context.block_pool_seed, &ctx);

let log_level = if config.display_logs {
if cfg!(feature = "cli") {
Expand All @@ -613,11 +617,10 @@ pub async fn start_stacks_event_observer(
let background_job_tx_mutex = Arc::new(Mutex::new(observer_commands_tx.clone()));

let prometheus_monitoring = PrometheusMonitoring::new();
prometheus_monitoring.stx_metrics_set_registered_predicates(
prometheus_monitoring.initialize(
chainhook_store.predicates.stacks_chainhooks.len() as u64,
);
prometheus_monitoring.btc_metrics_set_registered_predicates(
chainhook_store.predicates.bitcoin_chainhooks.len() as u64,
Some(stacks_startup_context.last_block_height_appended),
);

if let Some(port) = config.prometheus_monitoring_port {
Expand Down

0 comments on commit 6a8c086

Please sign in to comment.