sc_transaction_pool/
metrics.rs1use std::sync::Arc;
22
23use prometheus_endpoint::{register, Counter, PrometheusError, Registry, U64};
24
25#[derive(Clone, Default)]
26pub struct MetricsLink(Arc<Option<Metrics>>);
27
28impl MetricsLink {
29 pub fn new(registry: Option<&Registry>) -> Self {
30 Self(Arc::new(registry.and_then(|registry| {
31 Metrics::register(registry)
32 .map_err(|err| {
33 log::warn!("Failed to register prometheus metrics: {}", err);
34 })
35 .ok()
36 })))
37 }
38
39 pub fn report(&self, do_this: impl FnOnce(&Metrics)) {
40 if let Some(metrics) = self.0.as_ref() {
41 do_this(metrics);
42 }
43 }
44}
45
46pub struct Metrics {
48 pub submitted_transactions: Counter<U64>,
49 pub validations_invalid: Counter<U64>,
50 pub block_transactions_pruned: Counter<U64>,
51 pub block_transactions_resubmitted: Counter<U64>,
52}
53
54impl Metrics {
55 pub fn register(registry: &Registry) -> Result<Self, PrometheusError> {
56 Ok(Self {
57 submitted_transactions: register(
58 Counter::new(
59 "substrate_sub_txpool_submitted_transactions",
60 "Total number of transactions submitted",
61 )?,
62 registry,
63 )?,
64 validations_invalid: register(
65 Counter::new(
66 "substrate_sub_txpool_validations_invalid",
67 "Total number of transactions that were removed from the pool as invalid",
68 )?,
69 registry,
70 )?,
71 block_transactions_pruned: register(
72 Counter::new(
73 "substrate_sub_txpool_block_transactions_pruned",
74 "Total number of transactions that was requested to be pruned by block events",
75 )?,
76 registry,
77 )?,
78 block_transactions_resubmitted: register(
79 Counter::new(
80 "substrate_sub_txpool_block_transactions_resubmitted",
81 "Total number of transactions that was requested to be resubmitted by block events",
82 )?,
83 registry,
84 )?,
85 })
86 }
87}
88
89pub struct ApiMetrics {
91 pub validations_scheduled: Counter<U64>,
92 pub validations_finished: Counter<U64>,
93}
94
95impl ApiMetrics {
96 pub fn register(registry: &Registry) -> Result<Self, PrometheusError> {
98 Ok(Self {
99 validations_scheduled: register(
100 Counter::new(
101 "substrate_sub_txpool_validations_scheduled",
102 "Total number of transactions scheduled for validation",
103 )?,
104 registry,
105 )?,
106 validations_finished: register(
107 Counter::new(
108 "substrate_sub_txpool_validations_finished",
109 "Total number of transactions that finished validation",
110 )?,
111 registry,
112 )?,
113 })
114 }
115}
116
117pub trait ApiMetricsExt {
119 fn report(&self, report: impl FnOnce(&ApiMetrics));
121}
122
123impl ApiMetricsExt for Option<Arc<ApiMetrics>> {
124 fn report(&self, report: impl FnOnce(&ApiMetrics)) {
125 if let Some(metrics) = self.as_ref() {
126 report(metrics)
127 }
128 }
129}