Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

Add node authority status metric #4699

Merged
merged 21 commits into from
Jan 13, 2022
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 20 additions & 2 deletions node/network/gossip-support/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ use polkadot_primitives::v1::{AuthorityDiscoveryId, Hash, SessionIndex};
#[cfg(test)]
mod tests;

mod metrics;

use metrics::Metrics;

const LOG_TARGET: &str = "parachain::gossip-support";
// How much time should we wait to reissue a connection request
// since the last authority discovery resolution failure.
Expand Down Expand Up @@ -104,14 +108,20 @@ pub struct GossipSupport<AD> {
connected_authorities_by_peer_id: HashMap<PeerId, HashSet<AuthorityDiscoveryId>>,
/// Authority discovery service.
authority_discovery: AD,

/// Subsystem metrics.
metrics: Metrics,
}

impl<AD> GossipSupport<AD>
where
AD: AuthorityDiscovery,
{
/// Create a new instance of the [`GossipSupport`] subsystem.
pub fn new(keystore: SyncCryptoStorePtr, authority_discovery: AD) -> Self {
pub fn new(keystore: SyncCryptoStorePtr, authority_discovery: AD, metrics: Metrics) -> Self {
// Initialize the `polkadot_node_is_authority` metric.
metrics.on_is_not_authority();

Self {
keystore,
last_session_index: None,
Expand All @@ -121,6 +131,7 @@ where
connected_authorities: HashMap::new(),
connected_authorities_by_peer_id: HashMap::new(),
authority_discovery,
metrics,
}
}

Expand Down Expand Up @@ -212,7 +223,14 @@ where
}

let all_authorities = determine_relevant_authorities(ctx, relay_parent).await?;
let our_index = ensure_i_am_an_authority(&self.keystore, &all_authorities).await?;
let our_index = ensure_i_am_an_authority(&self.keystore, &all_authorities)
.await
.map_err(|e| {
self.metrics.on_is_not_authority();
sandreim marked this conversation as resolved.
Show resolved Hide resolved
e
})?;
self.metrics.on_is_authority();

let other_authorities = {
let mut authorities = all_authorities.clone();
authorities.swap_remove(our_index);
Expand Down
67 changes: 67 additions & 0 deletions node/network/gossip-support/src/metrics.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
// Copyright 2021 Parity Technologies (UK) Ltd.
// This file is part of Polkadot.

// Polkadot is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// Polkadot is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with Polkadot. If not, see <http://www.gnu.org/licenses/>.

use polkadot_node_subsystem_util::{
metrics,
metrics::{
prometheus,
prometheus::{Gauge, PrometheusError, Registry, U64},
},
};

/// Dispute Distribution metrics.
#[derive(Clone, Default)]
pub struct Metrics(Option<MetricsInner>);

#[derive(Clone)]
struct MetricsInner {
/// Tracks authority status.
is_authority: Gauge<U64>,
}

impl Metrics {
#[cfg(test)]
/// Dummy constructor for testing.
sandreim marked this conversation as resolved.
Show resolved Hide resolved
pub fn new_dummy() -> Self {
Self(None)
}

/// Set the authority flag.
pub fn on_is_authority(&self) {
ordian marked this conversation as resolved.
Show resolved Hide resolved
if let Some(metrics) = &self.0 {
metrics.is_authority.set(1);
}
}

/// Unset the authority flag.
pub fn on_is_not_authority(&self) {
sandreim marked this conversation as resolved.
Show resolved Hide resolved
if let Some(metrics) = &self.0 {
metrics.is_authority.set(0);
}
}
}

impl metrics::Metrics for Metrics {
fn try_register(registry: &Registry) -> Result<Self, PrometheusError> {
let metrics = MetricsInner {
is_authority: prometheus::register(
Gauge::new("polkadot_node_is_authority", "Tracks the node authority status.")?,
registry,
)?,
};
Ok(Metrics(Some(metrics)))
}
}
6 changes: 5 additions & 1 deletion node/network/gossip-support/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,11 @@ async fn get_other_authorities_addrs_map() -> HashMap<AuthorityDiscoveryId, Hash
}

fn make_subsystem() -> GossipSupport<MockAuthorityDiscovery> {
GossipSupport::new(make_ferdie_keystore(), MOCK_AUTHORITY_DISCOVERY.clone())
GossipSupport::new(
make_ferdie_keystore(),
MOCK_AUTHORITY_DISCOVERY.clone(),
Metrics::new_dummy(),
)
}

fn test_harness<T: Future<Output = VirtualOverseer>, AD: AuthorityDiscovery>(
Expand Down
1 change: 1 addition & 0 deletions node/service/src/overseer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,7 @@ where
.gossip_support(GossipSupportSubsystem::new(
keystore.clone(),
authority_discovery_service.clone(),
Metrics::register(registry)?,
))
.dispute_coordinator(if disputes_enabled {
DisputeCoordinatorSubsystem::new(
Expand Down