@@ -2,8 +2,8 @@ use std::num::NonZeroUsize;
22
33use lru:: LruCache ;
44use prometheus_exporter:: prometheus:: {
5- core:: { AtomicU64 , GenericCounter } ,
6- Registry ,
5+ core:: { AtomicU64 , GenericCounter , GenericCounterVec } ,
6+ Histogram , HistogramOpts , IntCounterVec , Opts , Registry ,
77} ;
88
99use crate :: shared:: {
@@ -26,6 +26,11 @@ pub struct PrometheusMetrics {
2626 pub block_height_counter : GenericCounter < AtomicU64 > ,
2727 pub epoch_counter : GenericCounter < AtomicU64 > ,
2828 pub total_supply_native_token : GenericCounter < AtomicU64 > ,
29+ pub transaction_size : Histogram ,
30+ pub transaction_inner_size : Histogram ,
31+ pub bonds_per_epoch : GenericCounterVec < AtomicU64 > ,
32+ pub unbonds_per_epoch : GenericCounterVec < AtomicU64 > ,
33+ pub transaction_kind : GenericCounterVec < AtomicU64 > ,
2934 registry : Registry ,
3035}
3136
@@ -52,18 +57,65 @@ impl PrometheusMetrics {
5257 )
5358 . expect ( "unable to create counter total supply" ) ;
5459
60+ let transaction_size_opts = HistogramOpts :: new (
61+ "transaction_size_bytes" ,
62+ "The sizes of transactions in bytes" ,
63+ )
64+ . buckets ( vec ! [
65+ 10.0 , 50.0 , 100.0 , 500.0 , 1000.0 , 5000.0 , 10000.0 , 50000.0 ,
66+ ] ) ;
67+ let transaction_size = Histogram :: with_opts ( transaction_size_opts)
68+ . expect ( "unable to create histogram transaction sizes" ) ;
69+
70+ let transaction_inner_size_opts =
71+ HistogramOpts :: new ( "transaction_inners" , "The number of inner tx for a wrapper" )
72+ . buckets ( vec ! [ 2.0 , 4.0 , 8.0 , 16.0 , 32.0 , 64.0 , 128.0 ] ) ;
73+ let transaction_inner_size = Histogram :: with_opts ( transaction_inner_size_opts)
74+ . expect ( "unable to create histogram transaction sizes" ) ;
75+
76+ let bonds_per_epoch_opts = Opts :: new ( "bonds_per_epoch" , "Total bonds per epoch" ) ;
77+ let bonds_per_epoch = IntCounterVec :: new ( bonds_per_epoch_opts, & [ "epoch" ] )
78+ . expect ( "unable to create histogram transaction sizes" ) ;
79+
80+ let unbonds_per_epoch_opts = Opts :: new ( "unbonds_per_epoch" , "Total unbonds per epoch" ) ;
81+ let unbonds_per_epoch = IntCounterVec :: new ( unbonds_per_epoch_opts, & [ "epoch" ] )
82+ . expect ( "unable to create histogram transaction sizes" ) ;
83+
84+ let transaction_kind_opts =
85+ Opts :: new ( "transaction_kind" , "Total transaction per transaction kind" ) ;
86+ let transaction_kind =
87+ IntCounterVec :: new ( transaction_kind_opts, & [ "kind" , "epoch" , "height" ] )
88+ . expect ( "unable to create histogram transaction sizes" ) ;
89+
5590 registry
5691 . register ( Box :: new ( block_height_counter. clone ( ) ) )
5792 . unwrap ( ) ;
5893 registry. register ( Box :: new ( epoch_counter. clone ( ) ) ) . unwrap ( ) ;
5994 registry
6095 . register ( Box :: new ( total_supply_native_token. clone ( ) ) )
6196 . unwrap ( ) ;
97+ registry
98+ . register ( Box :: new ( transaction_size. clone ( ) ) )
99+ . unwrap ( ) ;
100+ registry
101+ . register ( Box :: new ( bonds_per_epoch. clone ( ) ) )
102+ . unwrap ( ) ;
103+ registry
104+ . register ( Box :: new ( unbonds_per_epoch. clone ( ) ) )
105+ . unwrap ( ) ;
106+ registry
107+ . register ( Box :: new ( transaction_kind. clone ( ) ) )
108+ . unwrap ( ) ;
62109
63110 Self {
64111 block_height_counter,
65112 epoch_counter,
66113 total_supply_native_token,
114+ transaction_size,
115+ transaction_inner_size,
116+ bonds_per_epoch,
117+ unbonds_per_epoch,
118+ transaction_kind,
67119 registry,
68120 }
69121 }
@@ -95,7 +147,13 @@ impl State {
95147 . unwrap_or ( 1 )
96148 }
97149
98- pub fn update ( & mut self , block : Block , total_supply_native : u64 ) {
150+ pub fn update (
151+ & mut self ,
152+ block : Block ,
153+ total_supply_native : u64 ,
154+ future_bonds : u64 ,
155+ future_unbonds : u64 ,
156+ ) {
99157 if let Some ( height) = self . latest_block_height {
100158 self . metrics
101159 . block_height_counter
@@ -110,7 +168,7 @@ impl State {
110168 . block_height_counter
111169 . inc_by ( block. epoch - epoch) ;
112170 } else {
113- self . metrics . block_height_counter . inc_by ( block. epoch ) ;
171+ self . metrics . epoch_counter . inc_by ( block. epoch ) ;
114172 }
115173 self . latest_epoch = Some ( block. epoch ) ;
116174
@@ -125,6 +183,49 @@ impl State {
125183 }
126184 self . latest_total_supply_native = Some ( total_supply_native) ;
127185
186+ for tx in & block. transactions {
187+ self . metrics
188+ . transaction_inner_size
189+ . observe ( tx. inners . len ( ) as f64 ) ;
190+ }
191+
192+ for tx in & block. transactions {
193+ for inner in & tx. inners {
194+ let inner_kind = inner. kind . to_string ( ) ;
195+
196+ self . metrics
197+ . transaction_kind
198+ . with_label_values ( & [
199+ & inner_kind,
200+ & block. epoch . to_string ( ) ,
201+ & block. height . to_string ( ) ,
202+ ] )
203+ . inc ( ) ;
204+
205+ self . metrics
206+ . transaction_size
207+ . observe ( inner. kind . size ( ) as f64 ) ;
208+ }
209+ }
210+
211+ self . metrics
212+ . bonds_per_epoch
213+ . with_label_values ( & [ & ( block. epoch + 1 ) . to_string ( ) ] )
214+ . reset ( ) ;
215+ self . metrics
216+ . bonds_per_epoch
217+ . with_label_values ( & [ & ( block. epoch + 1 ) . to_string ( ) ] )
218+ . inc_by ( future_bonds) ;
219+
220+ self . metrics
221+ . unbonds_per_epoch
222+ . with_label_values ( & [ & ( block. epoch + 1 ) . to_string ( ) ] )
223+ . reset ( ) ;
224+ self . metrics
225+ . unbonds_per_epoch
226+ . with_label_values ( & [ & ( block. epoch + 1 ) . to_string ( ) ] )
227+ . inc_by ( future_unbonds) ;
228+
128229 self . blocks . put ( block. height , block) ;
129230 }
130231
0 commit comments