@@ -3,6 +3,8 @@ mod executor;
33mod keyset;
44mod squash_noise;
55
6+ pub mod metrics;
7+
68#[ cfg( test) ]
79mod tests;
810
@@ -21,7 +23,6 @@ use fhevm_engine_common::{
2123 metrics_server,
2224 pg_pool:: { PostgresPoolManager , ServiceError } ,
2325 telemetry:: { self , OtelTracer } ,
24- telemetry:: { register_histogram, MetricsConfig } ,
2526 types:: FhevmError ,
2627 utils:: { to_hex, DatabaseURL } ,
2728} ;
@@ -43,11 +44,9 @@ use tracing::{error, info, Level};
4344use crate :: {
4445 aws_upload:: { check_is_ready, spawn_resubmit_task, spawn_uploader} ,
4546 executor:: SwitchNSquashService ,
47+ metrics:: spawn_gauge_update_routine,
4648} ;
4749
48- use prometheus:: Histogram ;
49- use std:: sync:: { LazyLock , OnceLock } ;
50-
5150pub const UPLOAD_QUEUE_SIZE : usize = 20 ;
5251pub const SAFE_SER_LIMIT : u64 = 1024 * 1024 * 66 ;
5352pub type InternalEvents = Option < tokio:: sync:: mpsc:: Sender < & ' static str > > ;
@@ -75,6 +74,12 @@ pub struct DBConfig {
7574 pub lifo : bool ,
7675}
7776
77+ #[ derive( Clone , Default , Debug ) ]
78+ pub struct SNSMetricsConfig {
79+ pub addr : Option < String > ,
80+ pub gauge_update_interval_secs : Option < u32 > ,
81+ }
82+
7883#[ derive( Clone , Default , Debug ) ]
7984pub struct S3Config {
8085 pub bucket_ct128 : String ,
@@ -106,7 +111,7 @@ pub struct Config {
106111 pub s3 : S3Config ,
107112 pub log_level : Level ,
108113 pub health_checks : HealthCheckConfig ,
109- pub metrics_addr : Option < String > ,
114+ pub metrics : SNSMetricsConfig ,
110115 pub enable_compression : bool ,
111116 pub schedule_policy : SchedulePolicy ,
112117 pub pg_auto_explain_with_min_duration : Option < Duration > ,
@@ -390,9 +395,6 @@ pub async fn run_computation_loop(
390395) -> Result < ( ) , Box < dyn std:: error:: Error + Send + Sync > > {
391396 let port = conf. health_checks . port ;
392397
393- // Start metrics server
394- metrics_server:: spawn ( conf. metrics_addr . clone ( ) , token. child_token ( ) ) ;
395-
396398 let service = Arc :: new (
397399 SwitchNSquashService :: create (
398400 pool_mngr,
@@ -532,6 +534,21 @@ pub async fn run_all(
532534
533535 let pg_mngr = pool_mngr. clone ( ) ;
534536
537+ // Start metrics server
538+ metrics_server:: spawn ( conf. metrics . addr . clone ( ) , token. child_token ( ) ) ;
539+
540+ // Start gauge update routine.
541+ if let Some ( interval_secs) = conf. metrics . gauge_update_interval_secs {
542+ info ! (
543+ interval_secs = interval_secs,
544+ "Starting gauge update routine"
545+ ) ;
546+ spawn_gauge_update_routine (
547+ Duration :: from_secs ( interval_secs. into ( ) ) ,
548+ pg_mngr. pool ( ) . clone ( ) ,
549+ ) ;
550+ }
551+
535552 // Spawns a task to handle S3 uploads
536553 spawn ( async move {
537554 if let Err ( err) = run_uploader_loop ( & pg_mngr, & conf, jobs_rx, tx, s3, is_ready) . await {
@@ -552,12 +569,3 @@ pub async fn run_all(
552569
553570 Ok ( ( ) )
554571}
555-
556- pub static SNS_LATENCY_OP_HISTOGRAM_CONF : OnceLock < MetricsConfig > = OnceLock :: new ( ) ;
557- pub static SNS_LATENCY_OP_HISTOGRAM : LazyLock < Histogram > = LazyLock :: new ( || {
558- register_histogram (
559- SNS_LATENCY_OP_HISTOGRAM_CONF . get ( ) ,
560- "coprocessor_sns_op_latency_seconds" ,
561- "Squash_noise computation latencies in seconds" ,
562- )
563- } ) ;
0 commit comments