@@ -646,10 +646,15 @@ pub type PrometheusUpdateCallback = Arc<dyn Fn() -> anyhow::Result<()> + Send +
646646pub type PrometheusExpositionFormatCallback =
647647 Arc < dyn Fn ( ) -> anyhow:: Result < String > + Send + Sync + ' static > ;
648648
649- /// Structure to hold Prometheus registries and associated callbacks for a given hierarchy
649+ /// Structure to hold Prometheus registries and associated callbacks for a given hierarchy.
650+ ///
651+ /// All fields are Arc-wrapped, so cloning shares state. This ensures metrics registered
652+ /// on cloned instances (e.g., cloned Client/Endpoint) are visible to the original.
653+ #[ derive( Clone ) ]
650654pub struct MetricsRegistry {
651- /// The Prometheus registry for this hierarchy (with interior mutability for thread-safe access)
652- pub prometheus_registry : std:: sync:: RwLock < prometheus:: Registry > ,
655+ /// The Prometheus registry for this hierarchy.
656+ /// Arc-wrapped so clones share the same registry (metrics registered on clones are visible everywhere).
657+ pub prometheus_registry : Arc < std:: sync:: RwLock < prometheus:: Registry > > ,
653658
654659 /// Update callbacks invoked before metrics are scraped.
655660 /// Wrapped in Arc to preserve callbacks across clones (prevents callback loss when MetricsRegistry is cloned).
@@ -683,25 +688,11 @@ impl std::fmt::Debug for MetricsRegistry {
683688 }
684689}
685690
686- impl Clone for MetricsRegistry {
687- fn clone ( & self ) -> Self {
688- Self {
689- prometheus_registry : std:: sync:: RwLock :: new (
690- self . prometheus_registry . read ( ) . unwrap ( ) . clone ( ) ,
691- ) ,
692- // Clone the Arc to share callbacks across all clones (prevents callback loss).
693- // Previously used Vec::new() here, which caused vllm: metrics to disappear.
694- prometheus_update_callbacks : Arc :: clone ( & self . prometheus_update_callbacks ) ,
695- prometheus_expfmt_callbacks : Arc :: clone ( & self . prometheus_expfmt_callbacks ) ,
696- }
697- }
698- }
699-
700691impl MetricsRegistry {
701692 /// Create a new metrics registry with an empty Prometheus registry and callback lists
702693 pub fn new ( ) -> Self {
703694 Self {
704- prometheus_registry : std:: sync:: RwLock :: new ( prometheus:: Registry :: new ( ) ) ,
695+ prometheus_registry : Arc :: new ( std:: sync:: RwLock :: new ( prometheus:: Registry :: new ( ) ) ) ,
705696 prometheus_update_callbacks : Arc :: new ( std:: sync:: RwLock :: new ( Vec :: new ( ) ) ) ,
706697 prometheus_expfmt_callbacks : Arc :: new ( std:: sync:: RwLock :: new ( Vec :: new ( ) ) ) ,
707698 }
0 commit comments