22
33use crate :: external_resources:: prover_config:: ProverServiceConfig ;
44use crate :: request_handler:: handler:: is_known_path;
5+ use crate :: request_handler:: types:: VerifiedInput ;
56use aptos_logger:: { error, info, warn} ;
67use aptos_metrics_core:: {
78 exponential_buckets, register_histogram_vec, register_int_counter_vec, Encoder , HistogramVec ,
@@ -17,8 +18,6 @@ use std::net::SocketAddr;
1718use std:: sync:: Arc ;
1819use std:: time:: { Duration , Instant } ;
1920
20- // TODO: sanity check and expand these metrics!
21-
2221// Constants for the metrics endpoint and response type
2322const METRICS_ENDPOINT : & str = "/metrics" ;
2423const PLAIN_CONTENT_TYPE : & str = "text/plain" ;
@@ -39,6 +38,16 @@ pub const PROVER_RESPONSE_GENERATION_LABEL: &str = "prover_response_generation";
3938pub const VALIDATE_PROVE_REQUEST_LABEL : & str = "validate_prove_request" ;
4039pub const WITNESS_GENERATION_LABEL : & str = "witness_generation" ;
4140
41+ // Useful metric labels for JWT attribute sizes
42+ const JWT_HEADER_SIZE : & str = "jwt_header_size" ;
43+ const JWT_PAYLOAD_SIZE : & str = "jwt_payload_size" ;
44+ const JWT_SIGNATURE_SIZE : & str = "jwt_signature_size" ;
45+ const JWT_ISS_SIZE : & str = "jwt_iss_size" ;
46+ const JWT_NONCE_SIZE : & str = "jwt_nonce_size" ;
47+ const JWT_SUB_SIZE : & str = "jwt_sub_size" ;
48+ const JWT_EMAIL_SIZE : & str = "jwt_email_size" ;
49+ const JWT_AUD_SIZE : & str = "jwt_aud_size" ;
50+
4251// Invalid request path label
4352const INVALID_PATH : & str = "invalid-path" ;
4453
@@ -61,6 +70,14 @@ static LATENCY_BUCKETS: Lazy<Vec<f64>> = Lazy::new(|| {
6170 . unwrap ( )
6271} ) ;
6372
73+ // Buckets for tracking sizes (1 byte to 256 KB)
74+ static SIZE_BUCKETS : Lazy < Vec < f64 > > = Lazy :: new ( || {
75+ exponential_buckets (
76+ /*start=*/ 1.0 , /*factor=*/ 2.0 , /*count=*/ 19 ,
77+ )
78+ . unwrap ( )
79+ } ) ;
80+
6481// Counter for the number of prover metrics in various states
6582pub static NUM_TOTAL_METRICS : Lazy < IntCounterVec > = Lazy :: new ( || {
6683 register_int_counter_vec ! (
@@ -93,6 +110,17 @@ static REQUEST_HANDLING_SECONDS: Lazy<HistogramVec> = Lazy::new(|| {
93110 . unwrap ( )
94111} ) ;
95112
113+ // Histogram for tracking the attribute sizes of JWT requests
114+ static REQUEST_JWT_ATTRIBUTE_SIZES : Lazy < HistogramVec > = Lazy :: new ( || {
115+ register_histogram_vec ! (
116+ "keyless_prover_service_request_jwt_attribute_sizes" ,
117+ "Sizes of request JWT attributes" ,
118+ & [ "attribute" ] ,
119+ SIZE_BUCKETS . clone( )
120+ )
121+ . unwrap ( )
122+ } ) ;
123+
96124/// Handles incoming HTTP requests for the metrics server
97125async fn handle_metrics_request (
98126 request : hyper:: Request < Body > ,
@@ -227,3 +255,31 @@ pub fn update_request_handling_metrics(
227255 ] )
228256 . observe ( elapsed. as_secs_f64 ( ) ) ;
229257}
258+
259+ /// Updates the JWT attribute size metrics for the given attribute and size
260+ fn update_jwt_attribute_size_metrics ( attribute : & str , size_bytes : usize ) {
261+ REQUEST_JWT_ATTRIBUTE_SIZES
262+ . with_label_values ( & [ attribute] )
263+ . observe ( size_bytes as f64 ) ;
264+ }
265+
266+ /// Updates the JWT attribute metrics based on the verified input
267+ pub fn update_jwt_attribute_metrics ( verified_input : & VerifiedInput ) {
268+ // Update the JWT parts metrics
269+ let jwt_parts = & verified_input. jwt_parts ;
270+ update_jwt_attribute_size_metrics ( JWT_HEADER_SIZE , jwt_parts. header_undecoded ( ) . len ( ) ) ;
271+ update_jwt_attribute_size_metrics ( JWT_PAYLOAD_SIZE , jwt_parts. payload_undecoded ( ) . len ( ) ) ;
272+ update_jwt_attribute_size_metrics ( JWT_SIGNATURE_SIZE , jwt_parts. signature_undecoded ( ) . len ( ) ) ;
273+
274+ // Update the JWT field metrics
275+ let jwt_payload = & verified_input. jwt . payload ;
276+ update_jwt_attribute_size_metrics ( JWT_ISS_SIZE , jwt_payload. iss . len ( ) ) ;
277+ update_jwt_attribute_size_metrics ( JWT_NONCE_SIZE , jwt_payload. nonce . len ( ) ) ;
278+ if let Some ( sub) = & jwt_payload. sub {
279+ update_jwt_attribute_size_metrics ( JWT_SUB_SIZE , sub. len ( ) ) ;
280+ }
281+ if let Some ( email) = & jwt_payload. email {
282+ update_jwt_attribute_size_metrics ( JWT_EMAIL_SIZE , email. len ( ) ) ;
283+ }
284+ update_jwt_attribute_size_metrics ( JWT_AUD_SIZE , jwt_payload. aud . len ( ) ) ;
285+ }
0 commit comments