@@ -23,6 +23,7 @@ pub struct BlockStateMetrics {
2323 pub op_counter : HashMap < CounterKey , u64 > ,
2424 pub read_bytes_counter : HashMap < CounterKey , u64 > ,
2525 pub write_bytes_counter : HashMap < CounterKey , u64 > ,
26+ pub entity_count_changes : HashMap < CounterKey , u64 > ,
2627}
2728
2829#[ derive( Hash , PartialEq , Eq , Debug , Clone ) ]
@@ -44,6 +45,7 @@ impl BlockStateMetrics {
4445 write_bytes_counter : HashMap :: new ( ) ,
4546 gas_counter : HashMap :: new ( ) ,
4647 op_counter : HashMap :: new ( ) ,
48+ entity_count_changes : HashMap :: new ( ) ,
4749 }
4850 }
4951
@@ -63,6 +65,10 @@ impl BlockStateMetrics {
6365 for ( key, value) in other. op_counter {
6466 * self . op_counter . entry ( key) . or_insert ( 0 ) += value;
6567 }
68+
69+ for ( key, value) in other. entity_count_changes {
70+ * self . entity_count_changes . entry ( key) . or_insert ( 0 ) = value;
71+ }
6672 }
6773
6874 fn serialize_to_csv < T : Serialize , U : Serialize , I : IntoIterator < Item = T > > (
@@ -97,6 +103,25 @@ impl BlockStateMetrics {
97103 )
98104 }
99105
106+ pub fn counter_to_csv_i32 (
107+ data : & HashMap < CounterKey , i32 > ,
108+ column_names : Vec < & str > ,
109+ ) -> Result < String > {
110+ Self :: serialize_to_csv (
111+ data. iter ( ) . map ( |( key, value) | match key {
112+ CounterKey :: Entity ( typename, id) => {
113+ vec ! [
114+ typename. typename( ) . to_string( ) ,
115+ id. to_string( ) ,
116+ value. to_string( ) ,
117+ ]
118+ }
119+ CounterKey :: String ( key) => vec ! [ key. to_string( ) , value. to_string( ) ] ,
120+ } ) ,
121+ column_names,
122+ )
123+ }
124+
100125 async fn write_csv_to_store ( bucket : & str , path : & str , data : String ) -> Result < ( ) > {
101126 let data_bytes = data. into_bytes ( ) ;
102127
@@ -158,6 +183,18 @@ impl BlockStateMetrics {
158183 }
159184 }
160185
186+ pub fn track_entity_count_change ( & mut self , entity_type : & EntityType , change : i32 ) {
187+ if ENV_VARS . enable_dips_metrics {
188+ let key = CounterKey :: Entity ( entity_type. clone ( ) , Id :: String ( Word :: from ( "total" ) ) ) ;
189+ let counter = self . entity_count_changes . entry ( key) . or_insert ( 0 ) ;
190+ if change < 0 {
191+ * counter = counter. saturating_sub ( ( -change) as u64 ) ;
192+ } else {
193+ * counter = counter. saturating_add ( change as u64 ) ;
194+ }
195+ }
196+ }
197+
161198 pub fn flush_metrics_to_store (
162199 & self ,
163200 logger : & Logger ,
@@ -180,6 +217,7 @@ impl BlockStateMetrics {
180217 let op_counter = self . op_counter . clone ( ) ;
181218 let read_bytes_counter = self . read_bytes_counter . clone ( ) ;
182219 let write_bytes_counter = self . write_bytes_counter . clone ( ) ;
220+ let entity_count_changes = self . entity_count_changes . clone ( ) ;
183221
184222 // Spawn the async task
185223 crate :: spawn ( async move {
@@ -203,6 +241,11 @@ impl BlockStateMetrics {
203241 Self :: counter_to_csv( & write_bytes_counter, vec![ "entity" , "id" , "bytes" ] )
204242 . unwrap( ) ,
205243 ) ,
244+ (
245+ "entities" ,
246+ Self :: counter_to_csv( & entity_count_changes, vec![ "entity" , "id" , "count" ] )
247+ . unwrap( ) ,
248+ ) ,
206249 ] ;
207250
208251 // Convert each metrics upload into a future
0 commit comments