Skip to content

Commit 29566bd

Browse files
authored
Merge pull request #5981 from fdefelici/feat/signer-mon-state-changes
feat: add monitoring for signer agreement state change reason
2 parents 88a946a + 30047be commit 29566bd

File tree

3 files changed

+56
-1
lines changed

3 files changed

+56
-1
lines changed

stacks-signer/src/monitoring/mod.rs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,27 @@
1414
// You should have received a copy of the GNU General Public License
1515
// along with this program. If not, see <http://www.gnu.org/licenses/>.
1616

17+
use stacks_common::define_named_enum;
18+
1719
#[cfg(feature = "monitoring_prom")]
1820
mod prometheus;
1921

2022
#[cfg(feature = "monitoring_prom")]
2123
mod server;
2224

25+
define_named_enum!(
26+
/// Represent different state change reason on signer agreement protocol
27+
SignerAgreementStateChangeReason {
28+
/// A new burn block has arrived
29+
BurnBlockArrival("burn_block_arrival"),
30+
/// A new stacks block has arrived
31+
StacksBlockArrival("stacks_block_arrival"),
32+
/// A miner is inactive when it should be starting its tenure
33+
InactiveMiner("inactive_miner"),
34+
/// Signer agreement protocol version has been upgraded
35+
ProtocolUpgrade("protocol_upgrade"),
36+
});
37+
2338
/// Actions for updating metrics
2439
#[cfg(feature = "monitoring_prom")]
2540
pub mod actions {
@@ -29,6 +44,7 @@ pub mod actions {
2944

3045
use crate::config::GlobalConfig;
3146
use crate::monitoring::prometheus::*;
47+
use crate::monitoring::SignerAgreementStateChangeReason;
3248
use crate::v0::signer_state::LocalStateMachine;
3349

3450
/// Update stacks tip height gauge
@@ -108,6 +124,16 @@ pub mod actions {
108124
.replace(state);
109125
}
110126

127+
/// Increment signer agreement state change reason counter
128+
pub fn increment_signer_agreement_state_change_reason(
129+
reason: SignerAgreementStateChangeReason,
130+
) {
131+
let label_value = reason.get_name();
132+
SIGNER_AGREEMENT_STATE_CHANGE_REASONS
133+
.with_label_values(&[&label_value])
134+
.inc();
135+
}
136+
111137
/// Start serving monitoring metrics.
112138
/// This will only serve the metrics if the `monitoring_prom` feature is enabled.
113139
pub fn start_serving_monitoring_metrics(config: GlobalConfig) -> Result<(), String> {
@@ -131,6 +157,7 @@ pub mod actions {
131157
use blockstack_lib::chainstate::nakamoto::NakamotoBlock;
132158
use stacks_common::info;
133159

160+
use crate::monitoring::SignerAgreementStateChangeReason;
134161
use crate::v0::signer_state::LocalStateMachine;
135162
use crate::GlobalConfig;
136163

@@ -179,6 +206,12 @@ pub mod actions {
179206
/// Record the current local state machine
180207
pub fn record_local_state(_state: LocalStateMachine) {}
181208

209+
/// Increment signer agreement state change reason counter
210+
pub fn increment_signer_agreement_state_change_reason(
211+
_reason: SignerAgreementStateChangeReason,
212+
) {
213+
}
214+
182215
/// Start serving monitoring metrics.
183216
/// This will only serve the metrics if the `monitoring_prom` feature is enabled.
184217
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)