Skip to content
10 changes: 10 additions & 0 deletions src/eth/rpc/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,11 @@ impl Server {
let is_healthy = self.health().await;
GlobalState::set_health(is_healthy);
metrics::set_consensus_is_ready(if is_healthy { 1u64 } else { 0u64 });

let current_mode = GlobalState::get_node_mode();
for mode in NodeMode::ALL {
metrics::set_node_mode(u64::from(mode == current_mode), mode.to_string());
}
}

fn read_importer(&self) -> Option<Arc<ImporterConsensus>> {
Expand Down Expand Up @@ -530,6 +535,8 @@ async fn stratus_change_to_leader(_: Params<'_>, ctx: Arc<RpcContext>, ext: Exte
ctx.server.storage.clear_cache();
tracing::info!("node mode changed to leader successfully, cache cleared");

ctx.server.update_health().await;

Ok(json!(true))
}

Expand Down Expand Up @@ -583,11 +590,14 @@ async fn stratus_change_to_follower(params: Params<'_>, ctx: Arc<RpcContext>, ex
Err(e) => {
tracing::error!(reason = ?e, "failed to initialize importer, reverting node mode to leader");
GlobalState::set_node_mode(NodeMode::Leader);
ctx.server.update_health().await;
return Err(e);
}
}
tracing::info!("node mode changed to follower successfully");

ctx.server.update_health().await;

Ok(json!(true))
}

Expand Down
4 changes: 4 additions & 0 deletions src/globals.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,10 @@ pub enum NodeMode {
FakeLeader,
}

impl NodeMode {
pub const ALL: [NodeMode; 3] = [NodeMode::Leader, NodeMode::Follower, NodeMode::FakeLeader];
}

// -----------------------------------------------------------------------------
// Global state
// -----------------------------------------------------------------------------
Expand Down
5 changes: 4 additions & 1 deletion src/infra/metrics/metrics_definitions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,10 @@ metrics! {
histogram_duration consensus_forward{},

"The readiness of Stratus."
gauge consensus_is_ready{}
gauge_no_auto_label consensus_is_ready{},

"Current node mode: 1 for the active mode, 0 for the others."
gauge_no_auto_label node_mode{mode}
}

// Kafka Metrics
Expand Down
19 changes: 19 additions & 0 deletions src/infra/metrics/metrics_macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,4 +174,23 @@ macro_rules! metrics_impl_fn_inc {
}
}
};
(gauge_no_auto_label $name:ident $group:ident $($label:ident)*) => {
paste::paste! {
#[allow(clippy::too_many_arguments)]
#[doc = "Set `" $name "` gauge. Does not carry the automatic `node_mode` label."]
pub fn [<set_ $name>](n: u64, $( $label: impl Into<super::MetricLabelValue> ),*) {
let labels = super::into_labels(
vec![
("group", stringify!($group).into()),

$(
(stringify!($label), $label.into()),
)*
]
);
let gauge = metrics::gauge!(stringify!([<stratus_$name>]), labels);
gauge.set(n as f64);
}
}
};
}
2 changes: 1 addition & 1 deletion src/infra/metrics/metrics_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ impl Metric {
match self.kind {
"counter" => describe_counter!(self.name, self.description),
"histogram_duration" | "histogram_counter" => describe_histogram!(self.name, self.description),
"gauge" => describe_gauge!(self.name, self.description),
"gauge" | "gauge_no_auto_label" => describe_gauge!(self.name, self.description),
_ => {}
}
}
Expand Down