Skip to content
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
46eaa28
service/builder: Expose metrics to the RPC layers
lexnv Apr 25, 2025
89fe6ae
cargo: Add prometheus endpoint to the RPC layers
lexnv Apr 25, 2025
c56660a
tx: Initialize a subset of counter metrics
lexnv Apr 25, 2025
830b95d
tx: Increment metrics as counter vec
lexnv Apr 25, 2025
bf88c82
tx: Propagate transition times as histogram
lexnv Apr 25, 2025
bf4a235
tx: Replace unvalidated initial state with submitted
lexnv Apr 25, 2025
a009f37
tx: Introduce a metrics module
lexnv Apr 25, 2025
5b558a0
tx/metrics: Increment the status counter on internal advancement
lexnv Apr 25, 2025
6ab0c8c
tx/metrics: Simplify code by relying on the transaction event directly
lexnv Apr 25, 2025
2cc6fd1
tx/metrics: Provide clean API for metric control
lexnv Apr 25, 2025
a07ca93
tx: Propagate metrics on error
lexnv Apr 25, 2025
260e601
tx/metrics: Adjust internal labels
lexnv Apr 25, 2025
b0510d3
tx/metrics: Elapsed time since start to finalized
lexnv Apr 28, 2025
fe971b7
tx/event: Add wrapper for event state transitioning into final states
lexnv Apr 28, 2025
04d24c2
tx/metrics: Propagate start to final metrics as well
lexnv Apr 28, 2025
9b0febd
tx/tests: Adjust testing to the new interface
lexnv Apr 28, 2025
ca78079
tx: Register the rpc-v2 metrics only once
lexnv Apr 28, 2025
4a8b0c0
tx/metrics: Simplify reported metrics and code
lexnv Apr 28, 2025
96b0cbd
tx/metrics: Remove the counter since it can dededuced by histogram
lexnv Apr 28, 2025
5540857
Merge branch 'master' into lexnv/tx-metrics
lexnv Apr 28, 2025
8c2c9a4
tx/metrics: Fix unused imports
lexnv Apr 28, 2025
7c44dc1
tx/metrics: Fix docs references
lexnv Apr 28, 2025
1b21650
tx/metrics: Simplify labels since they are not used externally
lexnv Apr 29, 2025
516d2b3
tx/metrics: Replace HistogramVec with individual Histograms for granual
lexnv Apr 29, 2025
d45d520
tx/metrics: Add proper elapsed time
lexnv Apr 29, 2025
8ad08b8
tx/metrics: Add unit of seconds to metric description
lexnv May 23, 2025
c975366
tx/metrics: Apply feedback
lexnv May 23, 2025
6ac204d
Update from github-actions[bot] running command 'prdoc --audience nod…
github-actions[bot] May 25, 2025
793825f
cargo: Sort deps in alphabetical order
lexnv May 26, 2025
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
1 change: 1 addition & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions substrate/client/rpc-spec-v2/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ sp-version = { workspace = true, default-features = true }
thiserror = { workspace = true }
tokio = { features = ["sync"], workspace = true, default-features = true }
tokio-stream = { features = ["sync"], workspace = true }
prometheus-endpoint = { workspace = true, default-features = true }

[dev-dependencies]
assert_matches = { workspace = true }
Expand Down
184 changes: 184 additions & 0 deletions substrate/client/rpc-spec-v2/src/transaction/metrics.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,184 @@
// This file is part of Substrate.

// Copyright (C) Parity Technologies (UK) Ltd.
// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0

// This program 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.

// This program 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 this program. If not, see <https://www.gnu.org/licenses/>.

//! Metrics for recording transaction events.

use std::time::Instant;

use prometheus_endpoint::{
register, CounterVec, HistogramOpts, HistogramVec, Opts, PrometheusError, Registry, U64,
};

use super::TransactionEvent;

/// Histogram time buckets in microseconds.
const HISTOGRAM_BUCKETS: [f64; 11] = [
5.0,
25.0,
100.0,
500.0,
1_000.0,
2_500.0,
10_000.0,
25_000.0,
100_000.0,
1_000_000.0,
10_000_000.0,
];

/// Labels for transaction status.
mod labels {
/// The initial state of the transaction.
pub const SUBMITTED: &str = "submitted";

/// Represents the `TransactionEvent::Validated` event.
pub const VALIDATED: &str = "validated";

/// Represents the `TransactionEvent::BestChainBlockIncluded(Some (..))` event.
pub const IN_BLOCK: &str = "in_block";

/// Represents the `TransactionEvent::BestChainBlockIncluded(None)` event.
pub const RETRACTED: &str = "retracted";

/// Represents the `TransactionEvent::Finalized` event.
pub const FINALIZED: &str = "finalized";

/// Represents the `TransactionEvent::Error` event.
pub const ERROR: &str = "error";

/// Represents the `TransactionEvent::Invalid` event.
pub const INVALID: &str = "invalid";

/// Represents the `TransactionEvent::Dropped` event.
pub const DROPPED: &str = "dropped";
}

/// Convert a transaction event to a metric label.
fn transaction_event_label<Hash>(event: &TransactionEvent<Hash>) -> &'static str {
match event {
TransactionEvent::Validated => labels::VALIDATED,
TransactionEvent::BestChainBlockIncluded(Some(_)) => labels::IN_BLOCK,
TransactionEvent::BestChainBlockIncluded(None) => labels::RETRACTED,
TransactionEvent::Finalized(..) => labels::FINALIZED,
TransactionEvent::Error(..) => labels::ERROR,
TransactionEvent::Dropped(..) => labels::DROPPED,
TransactionEvent::Invalid(..) => labels::INVALID,
}
}

#[derive(Debug, Clone)]
pub struct ExecutionState {
/// The time when the transaction entered this state.
started_at: Instant,
/// The initial state.
initial_state: &'static str,
}

impl ExecutionState {
/// Creates a new [`ExecutionState`].
pub fn new() -> Self {
Self { started_at: Instant::now(), initial_state: labels::SUBMITTED }
}

/// Advance the state of the transaction.
fn advance_state(&mut self, state: &'static str) {
self.initial_state = state;
self.started_at = Instant::now();
}
}

/// RPC layer metrics for transaction pool.
#[derive(Debug, Clone)]
pub struct Metrics {
/// Counter for transaction status.
pub status: CounterVec<U64>,

/// Histogram for transaction execution time in each event.
execution_time: HistogramVec,
}

impl Metrics {
/// Creates a new [`TransactionMetrics`] instance.
pub fn new(registry: &Registry) -> Result<Self, PrometheusError> {
let status = register(
CounterVec::new(
Opts::new("rpc_transaction_status", "Number of transactions by status"),
&["state"],
)?,
registry,
)?;

let execution_time = register(
HistogramVec::new(
HistogramOpts::new(
"rpc_transaction_execution_time",
"Transaction execution time in each event",
)
.buckets(HISTOGRAM_BUCKETS.to_vec()),
&["initial_state", "final_state"],
)?,
registry,
)?;

// The execution state will be initialized when the transaction is submitted.
Ok(Metrics { status, execution_time })
}
}

/// Transaction metrics for a single transaction instance.
pub struct InstanceMetrics {
metrics: Option<Metrics>,

/// The execution state of the transaction.
execution_state: ExecutionState,
}

impl InstanceMetrics {
/// Creates a new [`InstanceMetrics`] instance.
pub fn new(metrics: Option<Metrics>) -> Self {
if let Some(ref metrics) = metrics {
// Register the initial state of the transaction.
metrics.status.with_label_values(&[labels::SUBMITTED]).inc();
}

Self { metrics, execution_state: ExecutionState::new() }
}

/// Record the execution time of a transaction state.
///
/// This represents how long it took for the transaction to move to the next state.
///
/// The method must be called before the transaction event is provided to the user.
pub fn register_event<Hash>(&mut self, event: &TransactionEvent<Hash>) {
let Some(ref metrics) = self.metrics else {
return;
};

let final_state = transaction_event_label(event);

metrics.status.with_label_values(&[final_state]).inc();

let elapsed = self.execution_state.started_at.elapsed().as_micros() as f64;
metrics
.execution_time
.with_label_values(&[self.execution_state.initial_state, final_state])
.observe(elapsed);

self.execution_state.advance_state(final_state);
}
}
2 changes: 2 additions & 0 deletions substrate/client/rpc-spec-v2/src/transaction/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
#[cfg(test)]
mod tests;

mod metrics;

pub mod api;
pub mod error;
pub mod event;
Expand Down
52 changes: 42 additions & 10 deletions substrate/client/rpc-spec-v2/src/transaction/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ use crate::{
use codec::Decode;
use futures::{StreamExt, TryFutureExt};
use jsonrpsee::{core::async_trait, PendingSubscriptionSink};

use super::metrics::{InstanceMetrics, Metrics};

use prometheus_endpoint::{PrometheusError, Registry};

use sc_rpc::utils::{RingBuffer, Subscription};
use sc_transaction_pool_api::{
error::IntoPoolError, BlockHash, TransactionFor, TransactionPool, TransactionSource,
Expand All @@ -50,12 +55,22 @@ pub struct Transaction<Pool, Client> {
pool: Arc<Pool>,
/// Executor to spawn subscriptions.
executor: SubscriptionTaskExecutor,
/// Metrics for transactions.
metrics: Option<Metrics>,
}

impl<Pool, Client> Transaction<Pool, Client> {
/// Creates a new [`Transaction`].
pub fn new(client: Arc<Client>, pool: Arc<Pool>, executor: SubscriptionTaskExecutor) -> Self {
Transaction { client, pool, executor }
pub fn new(
client: Arc<Client>,
pool: Arc<Pool>,
executor: SubscriptionTaskExecutor,
registry: Option<&Registry>,
) -> Result<Self, PrometheusError> {
let metrics =
if let Some(registry) = registry { Some(Metrics::new(registry)?) } else { None };

Ok(Transaction { client, pool, executor, metrics })
}
}

Expand All @@ -78,6 +93,9 @@ where
let client = self.client.clone();
let pool = self.pool.clone();

// Get a new transaction metrics instance and increment the counter.
let mut metrics = InstanceMetrics::new(self.metrics.clone());

let fut = async move {
let decoded_extrinsic = match TransactionFor::<Pool>::decode(&mut &xt[..]) {
Ok(decoded_extrinsic) => decoded_extrinsic,
Expand All @@ -86,12 +104,14 @@ where

let Ok(sink) = pending.accept().await.map(Subscription::from) else { return };

let event = TransactionEvent::Invalid::<BlockHash<Pool>>(TransactionError {
error: "Extrinsic bytes cannot be decoded".into(),
});

metrics.register_event(&event);

// The transaction is invalid.
let _ = sink
.send(&TransactionEvent::Invalid::<BlockHash<Pool>>(TransactionError {
error: "Extrinsic bytes cannot be decoded".into(),
}))
.await;
let _ = sink.send(&event).await;
return
},
};
Expand All @@ -112,8 +132,17 @@ where

match submit.await {
Ok(stream) => {
let stream =
stream.filter_map(move |event| async move { handle_event(event) }).boxed();
let stream = stream
.filter_map(|event| {
let event = handle_event(event);

if let Some(ref event) = event {
metrics.register_event(event);
}

async move { event }
})
.boxed();

// If the subscription is too slow older events will be overwritten.
sink.pipe_from_stream(stream, RingBuffer::new(3)).await;
Expand All @@ -122,6 +151,9 @@ where
// We have not created an `Watcher` for the tx. Make sure the
// error is still propagated as an event.
let event: TransactionEvent<<Pool::Block as BlockT>::Hash> = err.into();

metrics.register_event(&event);

_ = sink.send(&event).await;
},
};
Expand All @@ -134,7 +166,7 @@ where
/// Handle events generated by the transaction-pool and convert them
/// to the new API expected state.
#[inline]
pub fn handle_event<Hash: Clone, BlockHash: Clone>(
fn handle_event<Hash: Clone, BlockHash: Clone>(
event: TransactionStatus<Hash, BlockHash>,
) -> Option<TransactionEvent<BlockHash>> {
match event {
Expand Down
5 changes: 4 additions & 1 deletion substrate/client/service/src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -529,6 +529,7 @@ where
config.blocks_pruning,
backend.clone(),
&*rpc_builder,
config.prometheus_registry(),
)
};

Expand Down Expand Up @@ -676,6 +677,7 @@ pub fn gen_rpc_module<TBl, TBackend, TCl, TRpc, TExPool>(
blocks_pruning: BlocksPruning,
backend: Arc<TBackend>,
rpc_builder: &(dyn Fn(SubscriptionTaskExecutor) -> Result<RpcModule<TRpc>, Error>),
registry: Option<&Registry>,
) -> Result<RpcModule<()>, Error>
where
TBl: BlockT,
Expand Down Expand Up @@ -731,7 +733,8 @@ where
client.clone(),
transaction_pool.clone(),
task_executor.clone(),
)
registry,
)?
.into_rpc();

let chain_head_v2 = sc_rpc_spec_v2::chain_head::ChainHead::new(
Expand Down
Loading