Skip to content

Commit a31b2ab

Browse files
committed
feat: add monitoring for signer agreement state change reason, #5918
1 parent 88a946a commit a31b2ab

File tree

3 files changed

+68
-1
lines changed

3 files changed

+68
-1
lines changed

stacks-signer/src/monitoring/mod.rs

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,33 @@ mod prometheus;
2020
#[cfg(feature = "monitoring_prom")]
2121
mod server;
2222

23+
/// Represent different state change reason on signer agreement protocol
24+
pub enum SignerAgreementStateChangeReason {
25+
/// A new burn block has arrived
26+
BurnBlockArrival,
27+
/// A new stacks block has arrived
28+
StacksBlockArrival,
29+
/// A miner is inactive when it should be starting its tenure
30+
InactiveMiner,
31+
/// Signer agreement protocol version has been upgraded
32+
ProtocalUpgrade,
33+
}
34+
35+
impl std::fmt::Display for SignerAgreementStateChangeReason {
36+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
37+
write!(
38+
f,
39+
"{}",
40+
match self {
41+
SignerAgreementStateChangeReason::BurnBlockArrival => "burn_block_arrival",
42+
SignerAgreementStateChangeReason::StacksBlockArrival => "stacks_block_arrival",
43+
SignerAgreementStateChangeReason::InactiveMiner => "inactive_miner",
44+
SignerAgreementStateChangeReason::ProtocalUpgrade => "protocol_upgrade",
45+
}
46+
)
47+
}
48+
}
49+
2350
/// Actions for updating metrics
2451
#[cfg(feature = "monitoring_prom")]
2552
pub mod actions {
@@ -29,6 +56,7 @@ pub mod actions {
2956

3057
use crate::config::GlobalConfig;
3158
use crate::monitoring::prometheus::*;
59+
use crate::monitoring::SignerAgreementStateChangeReason;
3260
use crate::v0::signer_state::LocalStateMachine;
3361

3462
/// Update stacks tip height gauge
@@ -108,6 +136,16 @@ pub mod actions {
108136
.replace(state);
109137
}
110138

139+
/// Increment signer agreement state change reason counter
140+
pub fn increment_signer_agreement_state_change_reason(
141+
reason: SignerAgreementStateChangeReason,
142+
) {
143+
let label_value = reason.to_string();
144+
SIGNER_AGREEMENT_STATE_CHANGE_REASONS
145+
.with_label_values(&[&label_value])
146+
.inc();
147+
}
148+
111149
/// Start serving monitoring metrics.
112150
/// This will only serve the metrics if the `monitoring_prom` feature is enabled.
113151
pub fn start_serving_monitoring_metrics(config: GlobalConfig) -> Result<(), String> {
@@ -131,6 +169,7 @@ pub mod actions {
131169
use blockstack_lib::chainstate::nakamoto::NakamotoBlock;
132170
use stacks_common::info;
133171

172+
use crate::monitoring::SignerAgreementStateChangeReason;
134173
use crate::v0::signer_state::LocalStateMachine;
135174
use crate::GlobalConfig;
136175

@@ -179,6 +218,12 @@ pub mod actions {
179218
/// Record the current local state machine
180219
pub fn record_local_state(_state: LocalStateMachine) {}
181220

221+
/// Increment signer agreement state change reason counter
222+
pub fn increment_signer_agreement_state_change_reason(
223+
_reason: SignerAgreementStateChangeReason,
224+
) {
225+
}
226+
182227
/// Start serving monitoring metrics.
183228
/// This will only serve the metrics if the `monitoring_prom` feature is enabled.
184229
pub fn start_serving_monitoring_metrics(config: GlobalConfig) -> Result<(), String> {

stacks-signer/src/monitoring/prometheus.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,12 @@ lazy_static! {
7979
vec![0.005, 0.1, 0.25, 0.5, 1.0, 2.5, 5.0, 10.0, 20.0, 30.0, 60.0, 120.0]
8080
), &[]).unwrap();
8181

82+
pub static ref SIGNER_AGREEMENT_STATE_CHANGE_REASONS: IntCounterVec = register_int_counter_vec!(
83+
"stacks_signer_agreement_state_change_reasons",
84+
"The number of state machine changes in signer agreement protocol. `reason` can be one of: 'burn_block_arrival', 'stacks_block_arrival', 'inactive_miner', 'protocol_upgrade'",
85+
&["reason"]
86+
).unwrap();
87+
8288
pub static ref SIGNER_LOCAL_STATE_MACHINE: Mutex<Option<LocalStateMachine>> = Mutex::new(None);
8389
}
8490

stacks-signer/src/v0/signer_state.rs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,11 @@ impl LocalStateMachine {
271271
"inactive_tenure_ch" => %inactive_tenure_ch,
272272
"new_active_tenure_ch" => %new_active_tenure_ch
273273
);
274+
275+
crate::monitoring::actions::increment_signer_agreement_state_change_reason(
276+
crate::monitoring::SignerAgreementStateChangeReason::InactiveMiner,
277+
);
278+
274279
Ok(())
275280
} else {
276281
warn!("Current miner timed out due to inactivity, but prior miner is not valid. Allowing current miner to continue");
@@ -393,6 +398,11 @@ impl LocalStateMachine {
393398
*parent_tenure_last_block = *block_id;
394399
*parent_tenure_last_block_height = height;
395400
*self = LocalStateMachine::Initialized(prior_state_machine);
401+
402+
crate::monitoring::actions::increment_signer_agreement_state_change_reason(
403+
crate::monitoring::SignerAgreementStateChangeReason::StacksBlockArrival,
404+
);
405+
396406
Ok(())
397407
}
398408

@@ -447,7 +457,7 @@ impl LocalStateMachine {
447457
// set self to uninitialized so that if this function errors,
448458
// self is left as uninitialized.
449459
let prior_state = std::mem::replace(self, Self::Uninitialized);
450-
let prior_state_machine = match prior_state {
460+
let prior_state_machine = match prior_state.clone() {
451461
// if the local state machine was uninitialized, just initialize it
452462
LocalStateMachine::Uninitialized => Self::place_holder(),
453463
LocalStateMachine::Initialized(signer_state_machine) => signer_state_machine,
@@ -526,6 +536,12 @@ impl LocalStateMachine {
526536
active_signer_protocol_version: prior_state_machine.active_signer_protocol_version,
527537
});
528538

539+
if prior_state != *self {
540+
crate::monitoring::actions::increment_signer_agreement_state_change_reason(
541+
crate::monitoring::SignerAgreementStateChangeReason::BurnBlockArrival,
542+
);
543+
}
544+
529545
Ok(())
530546
}
531547
}

0 commit comments

Comments
 (0)