Skip to content

Commit 48f80e3

Browse files
committed
feat: add more prometheus metrics for mempool statistics
Miners (and mock-miners) can report how many transactions are (could be) included in a block and the reason that they stopped building a block. This information is very helpful to understand whether the block was full, the miner ran out of time, the miner was preempted, or there were no transactions available to mine.
1 parent 42faabe commit 48f80e3

File tree

6 files changed

+49
-1
lines changed

6 files changed

+49
-1
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ and this project adheres to the versioning scheme outlined in the [README.md](RE
1010
### Added
1111

1212
- Added a new RPC endpoint `/v3/health` to query the node's health status. The endpoint returns a 200 status code with relevant synchronization information (including the node's current Stacks tip height, the maximum Stacks tip height among its neighbors, and the difference between these two). A user can use the `difference_from_max_peer` value to decide what is a good threshold for them before considering the node out of sync. The endpoint returns a 500 status code if the query cannot retrieve viable data.
13+
- Improve prometheus metrics to gain more insights into the current state of the mempool
14+
- `stacks_node_miner_stop_reason_total`: Counts the number of times the miner stopped mining due to various reasons.
15+
- Always report the number of transactions mined in the last attempt, even if there were 0
1316

1417
## [3.1.0.0.12]
1518

stackslib/src/chainstate/nakamoto/miner.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -602,6 +602,7 @@ impl NakamotoBlockBuilder {
602602
}
603603

604604
if builder.txs.is_empty() {
605+
set_last_mined_block_transaction_count(0);
605606
return Err(Error::NoTransactionsToMine);
606607
}
607608

stackslib/src/chainstate/stacks/miner.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,8 @@ use crate::clarity_vm::clarity::{ClarityInstance, Error as clarity_error};
5454
use crate::core::mempool::*;
5555
use crate::core::*;
5656
use crate::monitoring::{
57-
set_last_mined_block_transaction_count, set_last_mined_execution_cost_observed,
57+
increment_miner_stop_reason, set_last_mined_block_transaction_count,
58+
set_last_mined_execution_cost_observed, MinerStopReason,
5859
};
5960
use crate::net::relay::Relayer;
6061

@@ -2771,6 +2772,7 @@ fn select_and_apply_transactions_from_mempool<B: BlockBuilder>(
27712772
(*settings.miner_status.lock().expect("FATAL: mutex poisoned")).is_blocked();
27722773
if blocked {
27732774
info!("Miner stopping due to preemption");
2775+
increment_miner_stop_reason(MinerStopReason::Preempted);
27742776
return Ok(None);
27752777
}
27762778

@@ -2779,6 +2781,7 @@ fn select_and_apply_transactions_from_mempool<B: BlockBuilder>(
27792781

27802782
if block_limit_hit == BlockLimitFunction::LIMIT_REACHED {
27812783
info!("Miner stopping due to limit reached");
2784+
increment_miner_stop_reason(MinerStopReason::LimitReached);
27822785
return Ok(None);
27832786
}
27842787
let time_now = get_epoch_time_ms();
@@ -2787,6 +2790,7 @@ fn select_and_apply_transactions_from_mempool<B: BlockBuilder>(
27872790
"Miner stopping due to mining time exceeded ({} ms)",
27882791
max_miner_time_ms
27892792
);
2793+
increment_miner_stop_reason(MinerStopReason::DeadlineReached);
27902794
return Ok(None);
27912795
}
27922796
if let Some(time_estimate) = txinfo.metadata.time_estimate_ms {
@@ -2896,6 +2900,7 @@ fn select_and_apply_transactions_from_mempool<B: BlockBuilder>(
28962900
{
28972901
info!("Miner stopping due to limit reached");
28982902
block_limit_hit = BlockLimitFunction::LIMIT_REACHED;
2903+
increment_miner_stop_reason(MinerStopReason::LimitReached);
28992904
return Ok(None);
29002905
}
29012906
}

stackslib/src/core/mempool.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1722,6 +1722,9 @@ impl MemPoolDB {
17221722
!start_with_no_estimate,
17231723
),
17241724
None => {
1725+
monitoring::increment_miner_stop_reason(
1726+
monitoring::MinerStopReason::NoTransactions,
1727+
);
17251728
break MempoolIterationStopReason::NoMoreCandidates;
17261729
}
17271730
}
@@ -1738,6 +1741,9 @@ impl MemPoolDB {
17381741
(tx, update_estimate)
17391742
}
17401743
None => {
1744+
monitoring::increment_miner_stop_reason(
1745+
monitoring::MinerStopReason::NoTransactions,
1746+
);
17411747
break MempoolIterationStopReason::NoMoreCandidates;
17421748
}
17431749
}

stackslib/src/monitoring/mod.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -462,6 +462,33 @@ pub fn get_burnchain_signer() -> Option<BurnchainSigner> {
462462
None
463463
}
464464

465+
pub enum MinerStopReason {
466+
NoTransactions,
467+
Preempted,
468+
LimitReached,
469+
DeadlineReached,
470+
}
471+
472+
impl MinerStopReason {
473+
pub fn as_str(&self) -> &'static str {
474+
match self {
475+
MinerStopReason::NoTransactions => "no_transactions",
476+
MinerStopReason::Preempted => "preempted",
477+
MinerStopReason::LimitReached => "limit_reached",
478+
MinerStopReason::DeadlineReached => "deadline_reached",
479+
}
480+
}
481+
}
482+
483+
// Increment the counter for the given miner stop reason.
484+
#[allow(unused_variables)]
485+
pub fn increment_miner_stop_reason(reason: MinerStopReason) {
486+
#[cfg(feature = "monitoring_prom")]
487+
prometheus::MINER_STOP_REASON_TOTAL
488+
.with_label_values(&[reason.as_str()])
489+
.inc();
490+
}
491+
465492
#[derive(Debug)]
466493
pub struct SetGlobalBurnchainSignerError;
467494

stackslib/src/monitoring/prometheus.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,12 @@ lazy_static! {
254254
"stacks_node_miner_current_median_commitment_low",
255255
"Low 64 bits of a miner's median commitment over the mining commitment window."
256256
)).unwrap();
257+
258+
pub static ref MINER_STOP_REASON_TOTAL: IntCounterVec = register_int_counter_vec!(
259+
"stacks_node_miner_stop_reason_total",
260+
"Total number of times the miner stopped for each reason",
261+
&["reason"]
262+
).unwrap();
257263
}
258264

259265
pub fn new_rpc_call_timer(path: &str) -> HistogramTimer {

0 commit comments

Comments
 (0)