@@ -39,7 +39,7 @@ use sp_runtime::generic::BlockId;
3939use sp_runtime:: traits:: {
4040 Block as BlockT , Header as HeaderT , NumberFor , One , Zero ,
4141} ;
42- use sc_telemetry:: { telemetry, CONSENSUS_INFO } ;
42+ use sc_telemetry:: { telemetry, CONSENSUS_DEBUG , CONSENSUS_INFO } ;
4343
4444use crate :: {
4545 CommandOrError , Commit , Config , Error , Precommit , Prevote ,
@@ -59,7 +59,7 @@ use sp_finality_grandpa::{
5959 AuthorityId , AuthoritySignature , Equivocation , EquivocationProof ,
6060 GrandpaApi , RoundNumber , SetId ,
6161} ;
62- use prometheus_endpoint:: { Gauge , U64 , register , PrometheusError } ;
62+ use prometheus_endpoint:: { register , Counter , Gauge , PrometheusError , U64 } ;
6363
6464type HistoricalVotes < Block > = finality_grandpa:: HistoricalVotes <
6565 <Block as BlockT >:: Hash ,
@@ -378,14 +378,32 @@ impl<Block: BlockT> SharedVoterSetState<Block> {
378378#[ derive( Clone ) ]
379379pub ( crate ) struct Metrics {
380380 finality_grandpa_round : Gauge < U64 > ,
381+ finality_grandpa_prevotes : Counter < U64 > ,
382+ finality_grandpa_precommits : Counter < U64 > ,
381383}
382384
383385impl Metrics {
384- pub ( crate ) fn register ( registry : & prometheus_endpoint:: Registry ) -> Result < Self , PrometheusError > {
386+ pub ( crate ) fn register (
387+ registry : & prometheus_endpoint:: Registry ,
388+ ) -> Result < Self , PrometheusError > {
385389 Ok ( Self {
386390 finality_grandpa_round : register (
387391 Gauge :: new ( "finality_grandpa_round" , "Highest completed GRANDPA round." ) ?,
388- registry
392+ registry,
393+ ) ?,
394+ finality_grandpa_prevotes : register (
395+ Counter :: new (
396+ "finality_grandpa_prevotes_total" ,
397+ "Total number of GRANDPA prevotes cast locally." ,
398+ ) ?,
399+ registry,
400+ ) ?,
401+ finality_grandpa_precommits : register (
402+ Counter :: new (
403+ "finality_grandpa_precommits_total" ,
404+ "Total number of GRANDPA precommits cast locally." ,
405+ ) ?,
406+ registry,
389407 ) ?,
390408 } )
391409 }
@@ -804,9 +822,22 @@ where
804822 None => return Ok ( ( ) ) ,
805823 } ;
806824
825+ let report_prevote_metrics = |prevote : & Prevote < Block > | {
826+ telemetry ! ( CONSENSUS_DEBUG ; "afg.prevote_issued" ;
827+ "round" => round,
828+ "target_number" => ?prevote. target_number,
829+ "target_hash" => ?prevote. target_hash,
830+ ) ;
831+
832+ if let Some ( metrics) = self . metrics . as_ref ( ) {
833+ metrics. finality_grandpa_prevotes . inc ( ) ;
834+ }
835+ } ;
836+
807837 self . update_voter_set_state ( |voter_set_state| {
808838 let ( completed_rounds, current_rounds) = voter_set_state. with_current_round ( round) ?;
809- let current_round = current_rounds. get ( & round)
839+ let current_round = current_rounds
840+ . get ( & round)
810841 . expect ( "checked in with_current_round that key exists; qed." ) ;
811842
812843 if !current_round. can_prevote ( ) {
@@ -816,6 +847,9 @@ where
816847 return Ok ( None ) ;
817848 }
818849
850+ // report to telemetry and prometheus
851+ report_prevote_metrics ( & prevote) ;
852+
819853 let propose = current_round. propose ( ) ;
820854
821855 let mut current_rounds = current_rounds. clone ( ) ;
@@ -837,17 +871,34 @@ where
837871 Ok ( ( ) )
838872 }
839873
840- fn precommitted ( & self , round : RoundNumber , precommit : Precommit < Block > ) -> Result < ( ) , Self :: Error > {
874+ fn precommitted (
875+ & self ,
876+ round : RoundNumber ,
877+ precommit : Precommit < Block > ,
878+ ) -> Result < ( ) , Self :: Error > {
841879 let local_id = crate :: is_voter ( & self . voters , self . config . keystore . as_ref ( ) ) ;
842880
843881 let local_id = match local_id {
844882 Some ( id) => id,
845883 None => return Ok ( ( ) ) ,
846884 } ;
847885
886+ let report_precommit_metrics = |precommit : & Precommit < Block > | {
887+ telemetry ! ( CONSENSUS_DEBUG ; "afg.precommit_issued" ;
888+ "round" => round,
889+ "target_number" => ?precommit. target_number,
890+ "target_hash" => ?precommit. target_hash,
891+ ) ;
892+
893+ if let Some ( metrics) = self . metrics . as_ref ( ) {
894+ metrics. finality_grandpa_precommits . inc ( ) ;
895+ }
896+ } ;
897+
848898 self . update_voter_set_state ( |voter_set_state| {
849899 let ( completed_rounds, current_rounds) = voter_set_state. with_current_round ( round) ?;
850- let current_round = current_rounds. get ( & round)
900+ let current_round = current_rounds
901+ . get ( & round)
851902 . expect ( "checked in with_current_round that key exists; qed." ) ;
852903
853904 if !current_round. can_precommit ( ) {
@@ -857,13 +908,16 @@ where
857908 return Ok ( None ) ;
858909 }
859910
911+ // report to telemetry and prometheus
912+ report_precommit_metrics ( & precommit) ;
913+
860914 let propose = current_round. propose ( ) ;
861915 let prevote = match current_round {
862916 HasVoted :: Yes ( _, Vote :: Prevote ( _, prevote) ) => prevote,
863917 _ => {
864918 let msg = "Voter precommitting before prevoting." ;
865919 return Err ( Error :: Safety ( msg. to_string ( ) ) ) ;
866- } ,
920+ }
867921 } ;
868922
869923 let mut current_rounds = current_rounds. clone ( ) ;
0 commit comments