Skip to content

Commit

Permalink
refactor(starknet_sequencer_metrics): move sqmr metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
AlonLStarkWare committed Feb 11, 2025
1 parent 15bbc50 commit 004f4f7
Show file tree
Hide file tree
Showing 9 changed files with 42 additions and 39 deletions.
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 0 additions & 24 deletions crates/papyrus_common/src/metrics.rs
Original file line number Diff line number Diff line change
@@ -1,29 +1,5 @@
use std::sync::OnceLock;

/// The central marker is the first block number that doesn't exist yet.
pub const PAPYRUS_CENTRAL_BLOCK_MARKER: &str = "papyrus_central_block_marker";

/// The header marker is the first block number for which the node does not have a header.
pub const PAPYRUS_HEADER_MARKER: &str = "papyrus_header_marker";

/// The body marker is the first block number for which the node does not have a body.
pub const PAPYRUS_BODY_MARKER: &str = "papyrus_body_marker";

/// The state marker is the first block number for which the node does not have a state body.
pub const PAPYRUS_STATE_MARKER: &str = "papyrus_state_marker";

/// The compiled class marker is the first block number for which the node does not have all of the
/// corresponding compiled classes.
pub const PAPYRUS_COMPILED_CLASS_MARKER: &str = "papyrus_compiled_class_marker";

/// The base layer marker is the first block number for which the node does not guarantee L1
/// finality.
pub const PAPYRUS_BASE_LAYER_MARKER: &str = "papyrus_base_layer_marker";

/// The latency, in seconds, between a block timestamp (as state in its header) and the time the
/// node stores the header.
pub const PAPYRUS_HEADER_LATENCY_SEC: &str = "papyrus_header_latency";

// TODO(Shahak): consider making this value non static and add a way to change this while the app is
// running. e.g via a monitoring endpoint.
/// Global variable set by the main config to enable collecting profiling metrics.
Expand Down
1 change: 1 addition & 0 deletions crates/papyrus_p2p_sync/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ starknet_state_sync_types.workspace = true
starknet-types-core.workspace = true
thiserror.workspace = true
starknet_class_manager_types.workspace = true
starknet_sequencer_metrics.workspace = true
async-trait.workspace = true
tokio.workspace = true
tokio-stream.workspace = true
Expand Down
11 changes: 8 additions & 3 deletions crates/papyrus_p2p_sync/src/client/header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,17 @@ use chrono::{TimeZone, Utc};
use futures::future::BoxFuture;
use futures::{FutureExt, StreamExt};
use metrics::gauge;
use papyrus_common::metrics as papyrus_metrics;
use papyrus_network::network_manager::ClientResponsesManager;
use papyrus_protobuf::sync::{DataOrFin, SignedBlockHeader};
use papyrus_storage::header::{HeaderStorageReader, HeaderStorageWriter};
use papyrus_storage::{StorageError, StorageReader, StorageWriter};
use starknet_api::block::{BlockHash, BlockHeader, BlockNumber, BlockSignature};
use starknet_api::hash::StarkHash;
use starknet_class_manager_types::SharedClassManagerClient;
use starknet_sequencer_metrics::metric_definitions::{
PAPYRUS_HEADER_LATENCY_SEC,
PAPYRUS_HEADER_MARKER,
};
use starknet_state_sync_types::state_sync_types::SyncBlock;
use tracing::debug;

Expand Down Expand Up @@ -47,7 +50,8 @@ impl BlockData for SignedBlockHeader {
.expect("Vec::first should return a value on a vector of size 1"),
)?
.commit()?;
gauge!(papyrus_metrics::PAPYRUS_HEADER_MARKER).set(
// TODO(alonl): fix this metric
gauge!(PAPYRUS_HEADER_MARKER.get_name()).set(
self.block_header.block_header_without_hash.block_number.unchecked_next().0 as f64,
);
// TODO(shahak): Fix code dup with central sync
Expand All @@ -62,7 +66,8 @@ impl BlockData for SignedBlockHeader {
let header_latency = time_delta.num_seconds();
debug!("Header latency: {}.", header_latency);
if header_latency >= 0 {
gauge!(papyrus_metrics::PAPYRUS_HEADER_LATENCY_SEC).set(header_latency as f64);
// TODO(alonl): fix this metric
gauge!(PAPYRUS_HEADER_LATENCY_SEC.get_name()).set(header_latency as f64);
}
Ok(())
}
Expand Down
5 changes: 3 additions & 2 deletions crates/papyrus_p2p_sync/src/client/state_diff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ use std::collections::HashSet;
use futures::future::BoxFuture;
use futures::{FutureExt, StreamExt};
use metrics::gauge;
use papyrus_common::metrics as papyrus_metrics;
use papyrus_network::network_manager::ClientResponsesManager;
use papyrus_proc_macros::latency_histogram;
use papyrus_protobuf::sync::{DataOrFin, StateDiffChunk};
Expand All @@ -13,6 +12,7 @@ use papyrus_storage::{StorageError, StorageReader, StorageWriter};
use starknet_api::block::BlockNumber;
use starknet_api::state::ThinStateDiff;
use starknet_class_manager_types::SharedClassManagerClient;
use starknet_sequencer_metrics::metric_definitions::PAPYRUS_STATE_MARKER;
use starknet_state_sync_types::state_sync_types::SyncBlock;

use super::block_data_stream_builder::BadPeerError;
Expand All @@ -34,7 +34,8 @@ impl BlockData for (ThinStateDiff, BlockNumber) {
) -> BoxFuture<'a, Result<(), P2pSyncClientError>> {
async move {
storage_writer.begin_rw_txn()?.append_state_diff(self.1, self.0)?.commit()?;
gauge!(papyrus_metrics::PAPYRUS_STATE_MARKER).set(self.1.unchecked_next().0 as f64);
// TODO(alonl): fix this metric
gauge!(PAPYRUS_STATE_MARKER.get_name()).set(self.1.unchecked_next().0 as f64);
Ok(())
}
.boxed()
Expand Down
1 change: 1 addition & 0 deletions crates/papyrus_sync/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ starknet-types-core.workspace = true
starknet_api.workspace = true
starknet_class_manager_types.workspace = true
starknet_client.workspace = true
starknet_sequencer_metrics.workspace = true
thiserror.workspace = true
tokio = { workspace = true, features = ["full", "sync"] }
tracing.workspace = true
Expand Down
27 changes: 17 additions & 10 deletions crates/papyrus_sync/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ use chrono::{TimeZone, Utc};
use futures::stream;
use futures_util::{pin_mut, select, Stream, StreamExt};
use indexmap::IndexMap;
use papyrus_common::metrics as papyrus_metrics;
use papyrus_common::pending_classes::PendingClasses;
use papyrus_config::converters::deserialize_seconds_to_duration;
use papyrus_config::dumping::{ser_param, SerializeConfig};
Expand All @@ -42,6 +41,15 @@ use starknet_api::deprecated_contract_class::ContractClass as DeprecatedContract
use starknet_api::state::{StateDiff, ThinStateDiff};
use starknet_class_manager_types::{ClassManagerClientError, SharedClassManagerClient};
use starknet_client::reader::PendingData;
use starknet_sequencer_metrics::metric_definitions::{
PAPYRUS_BASE_LAYER_MARKER,
PAPYRUS_BODY_MARKER,
PAPYRUS_CENTRAL_BLOCK_MARKER,
PAPYRUS_COMPILED_CLASS_MARKER,
PAPYRUS_HEADER_LATENCY_SEC,
PAPYRUS_HEADER_MARKER,
PAPYRUS_STATE_MARKER,
};
use tokio::sync::RwLock;
use tracing::{debug, error, info, instrument, trace, warn};

Expand Down Expand Up @@ -438,10 +446,9 @@ impl<
.append_block_signature(block_number, signature)?
.append_body(block_number, block.body)?
.commit()?;
metrics::gauge!(papyrus_metrics::PAPYRUS_HEADER_MARKER)
.set(block_number.unchecked_next().0 as f64);
metrics::gauge!(papyrus_metrics::PAPYRUS_BODY_MARKER)
metrics::gauge!(PAPYRUS_HEADER_MARKER.get_name())
.set(block_number.unchecked_next().0 as f64);
metrics::gauge!(PAPYRUS_BODY_MARKER.get_name()).set(block_number.unchecked_next().0 as f64);
let time_delta = Utc::now()
- Utc
.timestamp_opt(block.header.block_header_without_hash.timestamp.0 as i64, 0)
Expand All @@ -450,7 +457,7 @@ impl<
let header_latency = time_delta.num_seconds();
debug!("Header latency: {}.", header_latency);
if header_latency >= 0 {
metrics::gauge!(papyrus_metrics::PAPYRUS_HEADER_LATENCY_SEC).set(header_latency as f64);
metrics::gauge!(PAPYRUS_HEADER_LATENCY_SEC.get_name()).set(header_latency as f64);
}
Ok(())
}
Expand Down Expand Up @@ -504,10 +511,10 @@ impl<
.update_class_manager_block_marker(&block_number)?
.commit()?;
}
metrics::gauge!(papyrus_metrics::PAPYRUS_STATE_MARKER)
metrics::gauge!(PAPYRUS_STATE_MARKER.get_name())
.set(block_number.unchecked_next().0 as f64);
let compiled_class_marker = self.reader.begin_ro_txn()?.get_compiled_class_marker()?;
metrics::gauge!(papyrus_metrics::PAPYRUS_COMPILED_CLASS_MARKER)
metrics::gauge!(PAPYRUS_COMPILED_CLASS_MARKER.get_name())
.set(compiled_class_marker.0 as f64);

// Info the user on syncing the block once all the data is stored.
Expand All @@ -532,7 +539,7 @@ impl<
txn.commit()?;
let compiled_class_marker =
self.reader.begin_ro_txn()?.get_compiled_class_marker()?;
metrics::gauge!(papyrus_metrics::PAPYRUS_COMPILED_CLASS_MARKER)
metrics::gauge!(PAPYRUS_COMPILED_CLASS_MARKER.get_name())
.set(compiled_class_marker.0 as f64);
debug!("Added compiled class.");
Ok(())
Expand Down Expand Up @@ -575,7 +582,7 @@ impl<
if txn.get_base_layer_block_marker()? != block_number.unchecked_next() {
info!("Verified block {block_number} hash against base layer.");
txn.update_base_layer_block_marker(&block_number.unchecked_next())?.commit()?;
metrics::gauge!(papyrus_metrics::PAPYRUS_BASE_LAYER_MARKER)
metrics::gauge!(PAPYRUS_BASE_LAYER_MARKER.get_name())
.set(block_number.unchecked_next().0 as f64);
}
Ok(())
Expand Down Expand Up @@ -710,7 +717,7 @@ fn stream_new_blocks<
let central_block_marker = latest_central_block.map_or(
BlockNumber::default(), |block_hash_and_number| block_hash_and_number.number.unchecked_next()
);
metrics::gauge!(papyrus_metrics::PAPYRUS_CENTRAL_BLOCK_MARKER).set(central_block_marker.0 as f64);
metrics::gauge!(PAPYRUS_CENTRAL_BLOCK_MARKER.get_name()).set(central_block_marker.0 as f64);
if header_marker == central_block_marker {
// Only if the node have the last block and state (without casms), sync pending data.
if collect_pending_data && reader.begin_ro_txn()?.get_state_marker()? == header_marker{
Expand Down
9 changes: 9 additions & 0 deletions crates/starknet_sequencer_metrics/src/metric_definitions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,15 @@ define_gauge_metrics!(
{ STATE_SYNC_NUM_CONNECTED_PEERS, "apollo_sync_num_connected_peers", "The number of connected peers to the state sync p2p component" },
{ STATE_SYNC_NUM_ACTIVE_INBOUND_SESSIONS, "apollo_sync_num_active_inbound_sessions", "The number of inbound sessions to the state sync p2p component" },
{ STATE_SYNC_NUM_ACTIVE_OUTBOUND_SESSIONS, "apollo_sync_num_active_outbound_sessions", "The number of outbound sessions to the state sync p2p component" },
},
MetricScope::PapyrusSync => {
{ PAPYRUS_HEADER_MARKER, "papyrus_header_marker", "The first block number for which the node does not have a header" },
{ PAPYRUS_BODY_MARKER, "papyrus_body_marker", "The first block number for which the node does not have a body" },
{ PAPYRUS_STATE_MARKER, "papyrus_state_marker", "The first block number for which the node does not have a state body" },
{ PAPYRUS_COMPILED_CLASS_MARKER, "papyrus_compiled_class_marker", "The first block number for which the node does not have all of the corresponding compiled classes" },
{ PAPYRUS_BASE_LAYER_MARKER, "papyrus_base_layer_marker", "The first block number for which the node does not guarantee L1 finality" },
{ PAPYRUS_CENTRAL_BLOCK_MARKER, "papyrus_central_block_marker", "The first block number that doesn't exist yet" },
{ PAPYRUS_HEADER_LATENCY_SEC, "papyrus_header_latency", "The latency, in seconds, between a block timestamp (as state in its header) and the time the node stores the header" },
}
);

Expand Down
1 change: 1 addition & 0 deletions crates/starknet_sequencer_metrics/src/metrics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ pub enum MetricScope {
Batcher,
HttpServer,
Network,
PapyrusSync,
}

pub struct MetricCounter {
Expand Down

0 comments on commit 004f4f7

Please sign in to comment.