-
Notifications
You must be signed in to change notification settings - Fork 1.2k
tx/metrics: Add metrics for the RPC v2 transactionWatch_v1_submitAndWatch
#8345
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
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 89fe6ae
cargo: Add prometheus endpoint to the RPC layers
lexnv c56660a
tx: Initialize a subset of counter metrics
lexnv 830b95d
tx: Increment metrics as counter vec
lexnv bf88c82
tx: Propagate transition times as histogram
lexnv bf4a235
tx: Replace unvalidated initial state with submitted
lexnv a009f37
tx: Introduce a metrics module
lexnv 5b558a0
tx/metrics: Increment the status counter on internal advancement
lexnv 6ab0c8c
tx/metrics: Simplify code by relying on the transaction event directly
lexnv 2cc6fd1
tx/metrics: Provide clean API for metric control
lexnv a07ca93
tx: Propagate metrics on error
lexnv 260e601
tx/metrics: Adjust internal labels
lexnv b0510d3
tx/metrics: Elapsed time since start to finalized
lexnv fe971b7
tx/event: Add wrapper for event state transitioning into final states
lexnv 04d24c2
tx/metrics: Propagate start to final metrics as well
lexnv 9b0febd
tx/tests: Adjust testing to the new interface
lexnv ca78079
tx: Register the rpc-v2 metrics only once
lexnv 4a8b0c0
tx/metrics: Simplify reported metrics and code
lexnv 96b0cbd
tx/metrics: Remove the counter since it can dededuced by histogram
lexnv 5540857
Merge branch 'master' into lexnv/tx-metrics
lexnv 8c2c9a4
tx/metrics: Fix unused imports
lexnv 7c44dc1
tx/metrics: Fix docs references
lexnv 1b21650
tx/metrics: Simplify labels since they are not used externally
lexnv 516d2b3
tx/metrics: Replace HistogramVec with individual Histograms for granual
lexnv d45d520
tx/metrics: Add proper elapsed time
lexnv 8ad08b8
tx/metrics: Add unit of seconds to metric description
lexnv c975366
tx/metrics: Apply feedback
lexnv 6ac204d
Update from github-actions[bot] running command 'prdoc --audience nod…
github-actions[bot] 793825f
cargo: Sort deps in alphabetical order
lexnv File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
184 changes: 184 additions & 0 deletions
184
substrate/client/rpc-spec-v2/src/transaction/metrics.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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> { | ||
lexnv marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| 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); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -28,6 +28,8 @@ | |
| #[cfg(test)] | ||
| mod tests; | ||
|
|
||
| mod metrics; | ||
|
|
||
| pub mod api; | ||
| pub mod error; | ||
| pub mod event; | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.