@@ -10,6 +10,8 @@ use jsonrpsee::{
1010 server:: ServerBuilder ,
1111 { core:: async_trait, server:: ServerHandle } ,
1212} ;
13+ use lazy_static:: lazy_static;
14+ use prometheus:: { register_counter, register_int_counter, Counter , IntCounter } ;
1315
1416use crate :: aggregator:: check_and_aggregate_receipts;
1517use crate :: api_versioning:: {
@@ -23,6 +25,51 @@ use tap_core::{
2325 receipt_aggregate_voucher:: ReceiptAggregateVoucher , tap_receipt:: Receipt ,
2426} ;
2527
28+ // Register the metrics into the global metrics registry.
29+ lazy_static ! {
30+ static ref AGGREGATION_SUCCESS_COUNTER : IntCounter = register_int_counter!(
31+ "aggregation_success_count" ,
32+ "Number of successful receipt aggregation requests."
33+ )
34+ . unwrap( ) ;
35+ }
36+ lazy_static ! {
37+ static ref AGGREGATION_FAILURE_COUNTER : IntCounter = register_int_counter!(
38+ "aggregation_failure_count" ,
39+ "Number of failed receipt aggregation requests (for any reason)."
40+ )
41+ . unwrap( ) ;
42+ }
43+ lazy_static ! {
44+ static ref DEPRECATION_WARNING_COUNT : IntCounter = register_int_counter!(
45+ "deprecation_warning_count" ,
46+ "Number of deprecation warnings sent to clients."
47+ )
48+ . unwrap( ) ;
49+ }
50+ lazy_static ! {
51+ static ref VERSION_ERROR_COUNT : IntCounter = register_int_counter!(
52+ "version_error_count" ,
53+ "Number of API version errors sent to clients."
54+ )
55+ . unwrap( ) ;
56+ }
57+ lazy_static ! {
58+ static ref TOTAL_AGGREGATED_RECEIPTS : IntCounter = register_int_counter!(
59+ "total_aggregated_receipts" ,
60+ "Total number of receipts successfully aggregated."
61+ )
62+ . unwrap( ) ;
63+ }
64+ // Using float for the GRT value because it can somewhat easily exceed the maximum value of int64.
65+ lazy_static ! {
66+ static ref TOTAL_GRT_AGGREGATED : Counter = register_counter!(
67+ "total_aggregated_grt" ,
68+ "Total successfully aggregated GRT value (wei)."
69+ )
70+ . unwrap( ) ;
71+ }
72+
2673/// Generates the `RpcServer` trait that is used to define the JSON-RPC API.
2774///
2875/// Note that because of the way the `rpc` macro works, we cannot document the RpcServer trait here.
@@ -80,6 +127,45 @@ fn check_api_version_deprecation(api_version: &TapRpcApiVersion) -> Option<JsonR
80127 }
81128}
82129
130+ async fn aggregate_receipts_ (
131+ api_version : String ,
132+ wallet : & LocalWallet ,
133+ receipts : Vec < EIP712SignedMessage < Receipt > > ,
134+ previous_rav : Option < EIP712SignedMessage < ReceiptAggregateVoucher > > ,
135+ ) -> JsonRpcResult < EIP712SignedMessage < ReceiptAggregateVoucher > > {
136+ // Return an error if the API version is not supported.
137+ let api_version = match parse_api_version ( api_version. as_str ( ) ) {
138+ Ok ( v) => v,
139+ Err ( e) => {
140+ VERSION_ERROR_COUNT . inc ( ) ;
141+ return Err ( e) ;
142+ }
143+ } ;
144+
145+ // Add a warning if the API version is to be deprecated.
146+ let mut warnings: Vec < JsonRpcWarning > = Vec :: new ( ) ;
147+ if let Some ( w) = check_api_version_deprecation ( & api_version) {
148+ warnings. push ( w) ;
149+ DEPRECATION_WARNING_COUNT . inc ( ) ;
150+ }
151+
152+ let res = match api_version {
153+ TapRpcApiVersion :: V0_0 => {
154+ check_and_aggregate_receipts ( & receipts, previous_rav, wallet) . await
155+ }
156+ } ;
157+
158+ // Handle aggregation error
159+ match res {
160+ Ok ( res) => Ok ( JsonRpcResponse :: warn ( res, warnings) ) ,
161+ Err ( e) => Err ( jsonrpsee:: types:: ErrorObject :: owned (
162+ JsonRpcErrorCode :: Aggregation as i32 ,
163+ e. to_string ( ) ,
164+ None :: < ( ) > ,
165+ ) ) ,
166+ }
167+ }
168+
83169#[ async_trait]
84170impl RpcServer for RpcImpl {
85171 async fn api_versions ( & self ) -> JsonRpcResult < TapRpcApiVersionsInfo > {
@@ -92,29 +178,21 @@ impl RpcServer for RpcImpl {
92178 receipts : Vec < EIP712SignedMessage < Receipt > > ,
93179 previous_rav : Option < EIP712SignedMessage < ReceiptAggregateVoucher > > ,
94180 ) -> JsonRpcResult < EIP712SignedMessage < ReceiptAggregateVoucher > > {
95- // Return an error if the API version is not supported.
96- let api_version = parse_api_version ( api_version. as_str ( ) ) ?;
97-
98- // Add a warning if the API version is to be deprecated.
99- let mut warnings: Vec < JsonRpcWarning > = Vec :: new ( ) ;
100- if let Some ( w) = check_api_version_deprecation ( & api_version) {
101- warnings. push ( w) ;
102- }
103-
104- let res = match api_version {
105- TapRpcApiVersion :: V0_0 => {
106- check_and_aggregate_receipts ( & receipts, previous_rav, & self . wallet ) . await
181+ // Values for Prometheus metrics
182+ let receipts_grt: u128 = receipts. iter ( ) . map ( |r| r. message . value ) . sum ( ) ;
183+ let receipts_count: u64 = receipts. len ( ) as u64 ;
184+
185+ match aggregate_receipts_ ( api_version, & self . wallet , receipts, previous_rav) . await {
186+ Ok ( res) => {
187+ TOTAL_GRT_AGGREGATED . inc_by ( receipts_grt as f64 ) ;
188+ TOTAL_AGGREGATED_RECEIPTS . inc_by ( receipts_count) ;
189+ AGGREGATION_SUCCESS_COUNTER . inc ( ) ;
190+ Ok ( res)
191+ }
192+ Err ( e) => {
193+ AGGREGATION_FAILURE_COUNTER . inc ( ) ;
194+ Err ( e)
107195 }
108- } ;
109-
110- // Handle aggregation error
111- match res {
112- Ok ( res) => Ok ( JsonRpcResponse :: warn ( res, warnings) ) ,
113- Err ( e) => Err ( jsonrpsee:: types:: ErrorObject :: owned (
114- JsonRpcErrorCode :: Aggregation as i32 ,
115- e. to_string ( ) ,
116- None :: < ( ) > ,
117- ) ) ,
118196 }
119197 }
120198}
0 commit comments