Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
14 changes: 14 additions & 0 deletions prdoc/pr_10105.prdoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
title: 'rpc-v2/metrics: Align RPC metrics with tx pool metrics'
doc:
- audience: Node Dev
description: |-
This PR syncs the tx pool metrics with the RPC layer metrics.

The TX Pool metrics are re-exported under a `histogram` module, such that the RPC layer can reuse the same settings. This ensures that the RPC will be in sync with the Tx Pool metrics for the reliability dashboards.

Closes: https://github.com/paritytech/polkadot-sdk/issues/10067
crates:
- name: sc-rpc-spec-v2
bump: patch
- name: sc-transaction-pool
bump: patch
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 @@ -33,6 +33,7 @@ rand = { workspace = true, default-features = true }
sc-client-api = { workspace = true, default-features = true }
sc-rpc = { workspace = true, default-features = true }
sc-transaction-pool-api = { workspace = true, default-features = true }
sc-transaction-pool = { workspace = true, default-features = true }
schnellru = { workspace = true }
serde = { workspace = true, default-features = true }
sp-api = { workspace = true, default-features = true }
Expand Down
61 changes: 21 additions & 40 deletions substrate/client/rpc-spec-v2/src/transaction/metrics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,9 @@

use std::{collections::HashSet, time::Instant};

use prometheus_endpoint::{
exponential_buckets, linear_buckets, register, Histogram, HistogramOpts, PrometheusError,
Registry,
};
use prometheus_endpoint::{register, Histogram, PrometheusError, Registry};

use sc_transaction_pool::histograms as tx_pool_histograms;

use super::TransactionEvent;

Expand All @@ -42,67 +41,49 @@ impl Metrics {
/// Creates a new [`Metrics`] instance.
pub fn new(registry: &Registry) -> Result<Self, PrometheusError> {
let validated = register(
Histogram::with_opts(
HistogramOpts::new(
"rpc_transaction_validation_time",
"RPC Transaction validation time in seconds",
)
.buckets(exponential_buckets(0.01, 2.0, 16).expect("Valid buckets; qed")),
tx_pool_histograms::ready_future(
"rpc_transaction_validation_time",
"RPC Transaction validation time in seconds",
)?,
registry,
)?;

let in_block = register(
Histogram::with_opts(
HistogramOpts::new(
"rpc_transaction_in_block_time",
"RPC Transaction in block time in seconds",
)
.buckets(linear_buckets(0.0, 3.0, 20).expect("Valid buckets; qed")),
tx_pool_histograms::in_block(
"rpc_transaction_in_block_time",
"RPC Transaction in block time in seconds",
)?,
registry,
)?;

let finalized = register(
Histogram::with_opts(
HistogramOpts::new(
"rpc_transaction_finalized_time",
"RPC Transaction finalized time in seconds",
)
.buckets(linear_buckets(0.01, 40.0, 20).expect("Valid buckets; qed")),
tx_pool_histograms::finalized(
"rpc_transaction_finalized_time",
"RPC Transaction finalized time in seconds",
)?,
registry,
)?;

let dropped = register(
Histogram::with_opts(
HistogramOpts::new(
"rpc_transaction_dropped_time",
"RPC Transaction dropped time in seconds",
)
.buckets(linear_buckets(0.01, 3.0, 20).expect("Valid buckets; qed")),
tx_pool_histograms::invalid(
"rpc_transaction_dropped_time",
"RPC Transaction dropped time in seconds",
)?,
registry,
)?;

let invalid = register(
Histogram::with_opts(
HistogramOpts::new(
"rpc_transaction_invalid_time",
"RPC Transaction invalid time in seconds",
)
.buckets(linear_buckets(0.01, 3.0, 20).expect("Valid buckets; qed")),
tx_pool_histograms::invalid(
"rpc_transaction_invalid_time",
"RPC Transaction invalid time in seconds",
)?,
registry,
)?;

let error = register(
Histogram::with_opts(
HistogramOpts::new(
"rpc_transaction_error_time",
"RPC Transaction error time in seconds",
)
.buckets(linear_buckets(0.01, 3.0, 20).expect("Valid buckets; qed")),
tx_pool_histograms::invalid(
"rpc_transaction_error_time",
"RPC Transaction error time in seconds",
)?,
registry,
)?;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
// This file is part of Substrate.

@michalkucharczyk michalkucharczyk Oct 27, 2025

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe metric_buckets.rs (or shared_metric_buckets.rs) would be a better name for this file?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

or timing_metric_buckets.rs ?


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

//! Transaction metrics for various transaction states.
//!
//! The histograms are shared between the transaction pool and the RPC layer.
//!
//! # Note
//!
//! The RPC layer will utilize a subset of these metrics for now, since the
//! RPC emitted events do not have a direct mapping to all transaction pool events.
//!
//! Changing these metrics will impact the RPC layer as well.

use prometheus_endpoint::{
exponential_buckets, histogram_opts, linear_buckets, Histogram, PrometheusError,
};

/// Histogram of timings for reporting `Ready`/`Future` events.
pub fn ready_future(name: &'static str, label: &'static str) -> Result<Histogram, PrometheusError> {
Histogram::with_opts(histogram_opts!(name, label, exponential_buckets(0.01, 2.0, 16).unwrap()))
}

/// Histogram of timings for reporting `Broadcast` event.
pub fn broadcast(name: &'static str, label: &'static str) -> Result<Histogram, PrometheusError> {
Histogram::with_opts(histogram_opts!(name, label, linear_buckets(0.01, 0.25, 16).unwrap()))

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this mean the first bucket is (-Inf .. 0.01)? Is it intentional, or it should be linear_buckets(0, 0.25, 16)?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would may be even write it as linear_buckets(0.25, 0.25, 16) to not waste a bucket on (-Inf .. 0), because there is no negative timings and bucket (-Inf .. 0.25) is essentially (0 .. 0.25). But this applies to other histograms as well.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And, honestly, I don't know how the histograms are rendered in the UI, and if it makes sense visualization-wise.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Im not entirely familiar with the reliability buckets 🤔 Maybe @michalkucharczyk or @olliecorbisiero could provide better answers

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dmitry-markin @lexnv do the buckets I outlined in issue #10067 help clear things up?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would leave it as is.

}

/// Histogram of timings for reporting `InBlock` event.
pub fn in_block(name: &'static str, label: &'static str) -> Result<Histogram, PrometheusError> {
Histogram::with_opts(
histogram_opts!(name, label).buckets(
[
linear_buckets(0.0, 3.0, 20).unwrap(),
// requested in #9158
vec![60.0, 75.0, 90.0, 120.0, 180.0],
]
.concat(),
),
)
}

/// Histogram of timings for reporting `Retracted` event.
pub fn retracted(name: &'static str, label: &'static str) -> Result<Histogram, PrometheusError> {
Histogram::with_opts(histogram_opts!(name, label, linear_buckets(0.0, 3.0, 20).unwrap()))
}

/// Histogram of timings for reporting `FinalityTimeout` event.
pub fn finalized_timeout(
name: &'static str,
label: &'static str,
) -> Result<Histogram, PrometheusError> {
Histogram::with_opts(histogram_opts!(name, label, linear_buckets(0.0, 40.0, 20).unwrap()))
}

/// Histogram of timings for reporting `Finalized` event.
pub fn finalized(name: &'static str, label: &'static str) -> Result<Histogram, PrometheusError> {
Histogram::with_opts(
histogram_opts!(name, label).buckets(
[
// requested in #9158
linear_buckets(0.0, 5.0, 8).unwrap(),
linear_buckets(40.0, 40.0, 19).unwrap(),
]
.concat(),
),
)
}

/// Histogram of timings for reporting `Invalid` / `Dropped` / `Usurped` events.
pub fn invalid(name: &'static str, label: &'static str) -> Result<Histogram, PrometheusError> {
Histogram::with_opts(
histogram_opts!(name, label).buckets(
[
linear_buckets(0.0, 3.0, 20).unwrap(),
// requested in PR 9158

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// requested in PR 9158
// requested in PR #9158

vec![60.0, 75.0, 90.0, 120.0, 180.0],
]
.concat(),
),
)
}
111 changes: 28 additions & 83 deletions substrate/client/transaction-pool/src/fork_aware_txpool/metrics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@ use super::tx_mem_pool::InsertionInfo;
use crate::{
common::metrics::{GenericMetricsLink, MetricsRegistrant},
graph::{self, BlockHash, ExtrinsicHash},
LOG_TARGET,
histograms, LOG_TARGET,
};
use futures::{FutureExt, StreamExt};
use prometheus_endpoint::{
exponential_buckets, histogram_opts, linear_buckets, register, Counter, Gauge, Histogram,
PrometheusError, Registry, U64,
histogram_opts, linear_buckets, register, Counter, Gauge, Histogram, PrometheusError, Registry,
U64,
};
#[cfg(doc)]
use sc_transaction_pool_api::TransactionPool;
Expand Down Expand Up @@ -115,127 +115,72 @@ impl EventsHistograms {
fn register(registry: &Registry) -> Result<Self, PrometheusError> {
Ok(Self {
future: register(
Histogram::with_opts(histogram_opts!(
histograms::ready_future(
"substrate_sub_txpool_timing_event_future",
"Histogram of timings for reporting Future event",
exponential_buckets(0.01, 2.0, 16).unwrap()
))?,
)?,
registry,
)?,
ready: register(
Histogram::with_opts(histogram_opts!(
histograms::ready_future(
"substrate_sub_txpool_timing_event_ready",
"Histogram of timings for reporting Ready event",
exponential_buckets(0.01, 2.0, 16).unwrap()
))?,
)?,
registry,
)?,
broadcast: register(
Histogram::with_opts(histogram_opts!(
histograms::broadcast(
"substrate_sub_txpool_timing_event_broadcast",
"Histogram of timings for reporting Broadcast event",
linear_buckets(0.01, 0.25, 16).unwrap()
))?,
)?,
registry,
)?,
in_block: register(
Histogram::with_opts(
histogram_opts!(
"substrate_sub_txpool_timing_event_in_block",
"Histogram of timings for reporting InBlock event"
)
.buckets(
[
linear_buckets(0.0, 3.0, 20).unwrap(),
// requested in #9158
vec![60.0, 75.0, 90.0, 120.0, 180.0],
]
.concat(),
),
histograms::in_block(
"substrate_sub_txpool_timing_event_in_block",
"Histogram of timings for reporting InBlock event",
)?,
registry,
)?,
retracted: register(
Histogram::with_opts(histogram_opts!(
histograms::retracted(
"substrate_sub_txpool_timing_event_retracted",
"Histogram of timings for reporting Retracted event",
linear_buckets(0.0, 3.0, 20).unwrap()
))?,
)?,
registry,
)?,
finality_timeout: register(
Histogram::with_opts(histogram_opts!(
histograms::finalized_timeout(
"substrate_sub_txpool_timing_event_finality_timeout",
"Histogram of timings for reporting FinalityTimeout event",
linear_buckets(0.0, 40.0, 20).unwrap()
))?,
)?,
registry,
)?,
finalized: register(
Histogram::with_opts(
histogram_opts!(
"substrate_sub_txpool_timing_event_finalized",
"Histogram of timings for reporting Finalized event"
)
.buckets(
[
// requested in #9158
linear_buckets(0.0, 5.0, 8).unwrap(),
linear_buckets(40.0, 40.0, 19).unwrap(),
]
.concat(),
),
histograms::finalized(
"substrate_sub_txpool_timing_event_finalized",
"Histogram of timings for reporting Finalized event",
)?,
registry,
)?,
usurped: register(
Histogram::with_opts(
histogram_opts!(
"substrate_sub_txpool_timing_event_usurped",
"Histogram of timings for reporting Usurped event"
)
.buckets(
[
linear_buckets(0.0, 3.0, 20).unwrap(),
// requested in #9158
vec![60.0, 75.0, 90.0, 120.0, 180.0],
]
.concat(),
),
histograms::invalid(
"substrate_sub_txpool_timing_event_usurped",
"Histogram of timings for reporting Usurped event",
)?,
registry,
)?,
dropped: register(
Histogram::with_opts(
histogram_opts!(
"substrate_sub_txpool_timing_event_dropped",
"Histogram of timings for reporting Dropped event"
)
.buckets(
[
linear_buckets(0.0, 3.0, 20).unwrap(),
// requested in #9158
vec![60.0, 75.0, 90.0, 120.0, 180.0],
]
.concat(),
),
histograms::invalid(
"substrate_sub_txpool_timing_event_dropped",
"Histogram of timings for reporting Dropped event",
)?,
registry,
)?,
invalid: register(
Histogram::with_opts(
histogram_opts!(
"substrate_sub_txpool_timing_event_invalid",
"Histogram of timings for reporting Invalid event"
)
.buckets(
[
linear_buckets(0.0, 3.0, 20).unwrap(),
// requested in #9158
vec![60.0, 75.0, 90.0, 120.0, 180.0],
]
.concat(),
),
histograms::invalid(
"substrate_sub_txpool_timing_event_invalid",
"Histogram of timings for reporting Invalid event",
)?,
registry,
)?,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,7 @@

mod dropped_watcher;
pub(crate) mod fork_aware_txpool;
pub mod histograms;
mod import_notification_sink;
mod metrics;
mod multi_view_listener;
Expand Down
Loading
Loading