@@ -182,10 +182,10 @@ pub type IntGaugeVec = DeleteOnDropWrapper<prometheus::IntGaugeVec>;
182182/// Delete-on-drop shadow of Prometheus [raw::UIntGaugeVec].
183183pub type UIntGaugeVec = DeleteOnDropWrapper < raw:: UIntGaugeVec > ;
184184
185- pub use prometheus:: { Counter , Histogram , IntCounter , IntGauge } ;
186-
187185use crate :: assert_none;
188186
187+ pub use prometheus:: { Counter , Histogram , IntCounter , IntGauge } ;
188+
189189/// Access to non-delete-on-drop vector types
190190pub mod raw {
191191 use prometheus:: core:: { AtomicU64 , GenericGaugeVec } ;
@@ -217,13 +217,12 @@ impl MetricsRegistry {
217217 }
218218
219219 /// Registers a gauge whose value is computed when observed.
220- pub fn register_computed_gauge < F , P > (
220+ pub fn register_computed_gauge < P > (
221221 & self ,
222222 opts : MakeCollectorOpts ,
223- f : F ,
223+ f : impl Fn ( ) -> P :: T + Send + Sync + ' static ,
224224 ) -> ComputedGenericGauge < P >
225225 where
226- F : Fn ( ) -> P :: T + Send + Sync + ' static ,
227226 P : Atomic + ' static ,
228227 {
229228 let gauge = ComputedGenericGauge {
@@ -769,6 +768,65 @@ impl DurationMetric for &'_ mut f64 {
769768 }
770769}
771770
771+ /// Register the Tokio runtime's metrics in our metrics registry.
772+ #[ cfg( feature = "async" ) ]
773+ pub fn register_runtime_metrics (
774+ name : & ' static str ,
775+ runtime_metrics : tokio:: runtime:: RuntimeMetrics ,
776+ registry : & MetricsRegistry ,
777+ ) {
778+ macro_rules! register {
779+ ( $method: ident, $doc: literal) => {
780+ let metrics = runtime_metrics. clone( ) ;
781+ registry. register_computed_gauge:: <prometheus:: core:: AtomicU64 >(
782+ crate :: metric!(
783+ name: concat!( "mz_tokio_" , stringify!( $method) ) ,
784+ help: $doc,
785+ const_labels: { "runtime" => name} ,
786+ ) ,
787+ move || <u64 as crate :: cast:: CastFrom <_>>:: cast_from( metrics. $method( ) ) ,
788+ ) ;
789+ } ;
790+ }
791+
792+ register ! (
793+ num_workers,
794+ "The number of worker threads used by the runtime."
795+ ) ;
796+ register ! (
797+ num_alive_tasks,
798+ "The current number of alive tasks in the runtime."
799+ ) ;
800+ register ! (
801+ global_queue_depth,
802+ "The number of tasks currently scheduled in the runtime's global queue."
803+ ) ;
804+ register ! (
805+ num_blocking_threads,
806+ "The number of additional threads spawned by the runtime."
807+ ) ;
808+ register ! (
809+ num_idle_blocking_threads,
810+ "The number of idle threads which have spawned by the runtime for spawn_blocking calls."
811+ ) ;
812+ register ! (
813+ spawned_tasks_count,
814+ "The number of tasks spawned in this runtime since it was created."
815+ ) ;
816+ register ! (
817+ remote_schedule_count,
818+ "The number of tasks scheduled from outside of the runtime."
819+ ) ;
820+ register ! (
821+ budget_forced_yield_count,
822+ "The number of times that tasks have been forced to yield back to the scheduler after exhausting their task budgets."
823+ ) ;
824+ register ! (
825+ blocking_queue_depth,
826+ "The number of tasks currently scheduled in the blocking thread pool, spawned using spawn_blocking."
827+ ) ;
828+ }
829+
772830#[ cfg( test) ]
773831mod tests {
774832 use std:: time:: Duration ;
0 commit comments