@@ -4,6 +4,7 @@ use std::collections::{BTreeMap, HashMap, HashSet, VecDeque};
4
4
use crate :: types:: { PoolIterator , PoolKey , TransactionGroup } ;
5
5
use borsh:: BorshSerialize ;
6
6
use near_crypto:: PublicKey ;
7
+ use near_o11y:: metrics:: prometheus:: core:: { AtomicI64 , GenericGauge } ;
7
8
use near_primitives:: epoch_manager:: RngSeed ;
8
9
use near_primitives:: hash:: { hash, CryptoHash } ;
9
10
use near_primitives:: transaction:: SignedTransaction ;
@@ -39,26 +40,37 @@ pub struct TransactionPool {
39
40
total_transaction_size_limit : Option < u64 > ,
40
41
/// Total size of transactions in the pool measured in bytes.
41
42
total_transaction_size : u64 ,
43
+ /// Metrics tracked for transaction pool.
44
+ transaction_pool_count_metric : GenericGauge < AtomicI64 > ,
45
+ transaction_pool_size_metric : GenericGauge < AtomicI64 > ,
42
46
}
43
47
44
48
impl TransactionPool {
45
- pub fn new ( key_seed : RngSeed , total_transaction_size_limit : Option < u64 > ) -> Self {
49
+ pub fn new (
50
+ key_seed : RngSeed ,
51
+ total_transaction_size_limit : Option < u64 > ,
52
+ metrics_label : & str ,
53
+ ) -> Self {
54
+ let transaction_pool_count_metric =
55
+ metrics:: TRANSACTION_POOL_COUNT . with_label_values ( & [ metrics_label] ) ;
56
+ let transaction_pool_size_metric =
57
+ metrics:: TRANSACTION_POOL_SIZE . with_label_values ( & [ metrics_label] ) ;
58
+ // A `get()` call initializes a metric even if its value is zero.
59
+ transaction_pool_count_metric. get ( ) ;
60
+ transaction_pool_size_metric. get ( ) ;
61
+
46
62
Self {
47
63
key_seed,
48
64
transactions : BTreeMap :: new ( ) ,
49
65
unique_transactions : HashSet :: new ( ) ,
50
66
last_used_key : CryptoHash :: default ( ) ,
51
67
total_transaction_size_limit,
52
68
total_transaction_size : 0 ,
69
+ transaction_pool_count_metric,
70
+ transaction_pool_size_metric,
53
71
}
54
72
}
55
73
56
- pub fn init_metrics ( ) {
57
- // A `get()` call initializes a metric even if its value is zero.
58
- metrics:: TRANSACTION_POOL_COUNT . get ( ) ;
59
- metrics:: TRANSACTION_POOL_SIZE . get ( ) ;
60
- }
61
-
62
74
fn key ( & self , account_id : & AccountId , public_key : & PublicKey ) -> PoolKey {
63
75
let mut v = public_key. try_to_vec ( ) . unwrap ( ) ;
64
76
v. extend_from_slice ( & self . key_seed ) ;
@@ -98,8 +110,8 @@ impl TransactionPool {
98
110
. or_insert_with ( Vec :: new)
99
111
. push ( signed_transaction) ;
100
112
101
- metrics :: TRANSACTION_POOL_COUNT . inc ( ) ;
102
- metrics :: TRANSACTION_POOL_SIZE . set ( self . total_transaction_size as i64 ) ;
113
+ self . transaction_pool_count_metric . inc ( ) ;
114
+ self . transaction_pool_size_metric . set ( self . total_transaction_size as i64 ) ;
103
115
InsertTransactionResult :: Success
104
116
}
105
117
@@ -150,8 +162,8 @@ impl TransactionPool {
150
162
}
151
163
152
164
// We can update metrics only once for the whole batch of transactions.
153
- metrics :: TRANSACTION_POOL_COUNT . set ( self . unique_transactions . len ( ) as i64 ) ;
154
- metrics :: TRANSACTION_POOL_SIZE . set ( self . total_transaction_size as i64 ) ;
165
+ self . transaction_pool_count_metric . set ( self . unique_transactions . len ( ) as i64 ) ;
166
+ self . transaction_pool_size_metric . set ( self . total_transaction_size as i64 ) ;
155
167
}
156
168
157
169
/// Returns the number of unique transactions in the pool.
@@ -231,7 +243,7 @@ impl<'a> PoolIterator for PoolIteratorWrapper<'a> {
231
243
if sorted_group. transactions . is_empty ( ) {
232
244
for hash in sorted_group. removed_transaction_hashes {
233
245
if self . pool . unique_transactions . remove ( & hash) {
234
- metrics :: TRANSACTION_POOL_COUNT . dec ( ) ;
246
+ self . pool . transaction_pool_count_metric . dec ( ) ;
235
247
}
236
248
}
237
249
} else {
@@ -266,8 +278,8 @@ impl<'a> Drop for PoolIteratorWrapper<'a> {
266
278
}
267
279
}
268
280
// We can update metrics only once for the whole batch of transactions.
269
- metrics :: TRANSACTION_POOL_COUNT . set ( self . pool . unique_transactions . len ( ) as i64 ) ;
270
- metrics :: TRANSACTION_POOL_SIZE . set ( self . pool . transaction_size ( ) as i64 ) ;
281
+ self . pool . transaction_pool_count_metric . set ( self . pool . unique_transactions . len ( ) as i64 ) ;
282
+ self . pool . transaction_pool_size_metric . set ( self . pool . transaction_size ( ) as i64 ) ;
271
283
}
272
284
}
273
285
@@ -313,7 +325,7 @@ mod tests {
313
325
mut transactions : Vec < SignedTransaction > ,
314
326
expected_weight : u32 ,
315
327
) -> ( Vec < u64 > , TransactionPool ) {
316
- let mut pool = TransactionPool :: new ( TEST_SEED , None ) ;
328
+ let mut pool = TransactionPool :: new ( TEST_SEED , None , "" ) ;
317
329
let mut rng = thread_rng ( ) ;
318
330
transactions. shuffle ( & mut rng) ;
319
331
for tx in transactions {
@@ -424,7 +436,7 @@ mod tests {
424
436
} )
425
437
. collect :: < Vec < _ > > ( ) ;
426
438
427
- let mut pool = TransactionPool :: new ( TEST_SEED , None ) ;
439
+ let mut pool = TransactionPool :: new ( TEST_SEED , None , "" ) ;
428
440
let mut rng = thread_rng ( ) ;
429
441
transactions. shuffle ( & mut rng) ;
430
442
for tx in transactions. clone ( ) {
@@ -536,7 +548,7 @@ mod tests {
536
548
537
549
#[ test]
538
550
fn test_transaction_pool_size ( ) {
539
- let mut pool = TransactionPool :: new ( TEST_SEED , None ) ;
551
+ let mut pool = TransactionPool :: new ( TEST_SEED , None , "" ) ;
540
552
let transactions = generate_transactions ( "alice.near" , "alice.near" , 1 , 100 ) ;
541
553
let mut total_transaction_size = 0 ;
542
554
// Adding transactions increases the size.
@@ -560,7 +572,7 @@ mod tests {
560
572
// Each transaction is at least 1 byte in size, so the last transaction will not fit.
561
573
let pool_size_limit =
562
574
transactions. iter ( ) . map ( |tx| tx. get_size ( ) ) . sum :: < u64 > ( ) . checked_sub ( 1 ) . unwrap ( ) ;
563
- let mut pool = TransactionPool :: new ( TEST_SEED , Some ( pool_size_limit) ) ;
575
+ let mut pool = TransactionPool :: new ( TEST_SEED , Some ( pool_size_limit) , "" ) ;
564
576
for ( i, tx) in transactions. iter ( ) . cloned ( ) . enumerate ( ) {
565
577
if i + 1 < transactions. len ( ) {
566
578
assert_eq ! ( pool. insert_transaction( tx) , InsertTransactionResult :: Success ) ;
0 commit comments