@@ -10,8 +10,8 @@ use ra_rpc::rocket_helper::QuoteVerifier;
1010use rocket:: {
1111 fairing:: AdHoc ,
1212 figment:: { providers:: Serialized , Figment } ,
13- response:: content:: RawHtml ,
14- Shutdown ,
13+ response:: content:: { RawHtml , RawText } ,
14+ Shutdown , State ,
1515} ;
1616use tracing:: { info, warn} ;
1717
@@ -77,6 +77,34 @@ async fn run_onboard_service(kms_config: KmsConfig, figment: Figment) -> Result<
7777 Ok ( ( ) )
7878}
7979
80+ #[ rocket:: get( "/metrics" ) ]
81+ fn metrics ( state : & State < KmsState > ) -> RawText < String > {
82+ RawText ( state. metrics ( ) . render_prometheus ( ) )
83+ }
84+
85+ // Count only RPCs whose primary job is to verify caller/app attestation.
86+ // Recording in a response fairing also catches failures that happen before
87+ // RpcHandler is constructed, such as malformed RA-TLS attestation.
88+ fn is_attestation_rpc_path ( path : & str ) -> bool {
89+ let Some ( method) = path. strip_prefix ( "/prpc/" ) else {
90+ return false ;
91+ } ;
92+ let method = method. trim_start_matches ( "KMS." ) ;
93+ matches ! ( method, "GetAppKey" | "GetKmsKey" | "SignCert" )
94+ }
95+
96+ fn record_attestation_metrics ( req : & rocket:: Request < ' _ > , res : & rocket:: Response < ' _ > ) {
97+ if !is_attestation_rpc_path ( req. uri ( ) . path ( ) . as_str ( ) ) {
98+ return ;
99+ }
100+ let Some ( state) = req. rocket ( ) . state :: < KmsState > ( ) else {
101+ return ;
102+ } ;
103+ state
104+ . metrics ( )
105+ . record_attestation_request ( res. status ( ) . code >= 400 ) ;
106+ }
107+
80108#[ rocket:: main]
81109async fn main ( ) -> Result < ( ) > {
82110 {
@@ -119,10 +147,15 @@ async fn main() -> Result<()> {
119147 res. set_raw_header ( "X-App-Version" , app_version ( ) ) ;
120148 } )
121149 } ) )
150+ . attach ( AdHoc :: on_response (
151+ "Record KMS attestation metrics" ,
152+ |req, res| Box :: pin ( async move { record_attestation_metrics ( req, res) } ) ,
153+ ) )
122154 . mount (
123155 "/prpc" ,
124156 ra_rpc:: prpc_routes!( KmsState , RpcHandler , trim: "KMS." ) ,
125157 )
158+ . mount ( "/" , rocket:: routes![ metrics] )
126159 . manage ( state) ;
127160
128161 let verifier = QuoteVerifier :: new ( pccs_url) ;
0 commit comments