@@ -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 {
@@ -109,6 +137,7 @@ async fn main() -> Result<()> {
109137 }
110138
111139 let pccs_url = config. pccs_url . clone ( ) ;
140+ let metrics_enabled = config. metrics . enabled ;
112141 let state = main_service:: KmsState :: new ( config) . context ( "Failed to initialize KMS state" ) ?;
113142 let figment = figment
114143 . clone ( )
@@ -125,6 +154,16 @@ async fn main() -> Result<()> {
125154 )
126155 . manage ( state) ;
127156
157+ if metrics_enabled {
158+ info ! ( "Prometheus metrics endpoint enabled at /metrics" ) ;
159+ rocket = rocket
160+ . attach ( AdHoc :: on_response (
161+ "Record KMS attestation metrics" ,
162+ |req, res| Box :: pin ( async move { record_attestation_metrics ( req, res) } ) ,
163+ ) )
164+ . mount ( "/" , rocket:: routes![ metrics] ) ;
165+ }
166+
128167 let verifier = QuoteVerifier :: new ( pccs_url) ;
129168 rocket = rocket. manage ( verifier) ;
130169
0 commit comments