1616 */
1717
1818use prometheus_client:: {
19- encoding:: { EncodeLabelSet , EncodeLabelValue } ,
19+ encoding:: EncodeLabelSet ,
2020 metrics:: { family:: Family , gauge} ,
2121 registry:: Registry ,
2222} ;
2323
24- #[ derive( Clone , Debug , Hash , PartialEq , Eq , EncodeLabelValue ) ]
25- pub enum ObClientRuntimeGaugeType {
26- Default = 0 ,
27- ConnWriter = 1 ,
28- ConnReader = 2 ,
24+ use crate :: monitors:: prometheus:: OBKV_CLIENT_REGISTRY ;
25+
26+ lazy_static ! {
27+ pub static ref OBKV_RUNTIME_GAUGE_METRICS : RuntimeGaugeMetrics = {
28+ let runtime_metrics = RuntimeGaugeMetrics :: default ( ) ;
29+ runtime_metrics. register( & mut OBKV_CLIENT_REGISTRY . lock( ) . unwrap( ) . registry) ;
30+ runtime_metrics
31+ } ;
2932}
3033
3134#[ derive( Clone , Debug , Hash , PartialEq , Eq , EncodeLabelSet ) ]
32- pub struct RuntimeThreadLabels {
33- pub rt_type : ObClientRuntimeGaugeType ,
35+ pub struct RuntimeNameLabels {
36+ pub string_type : String ,
3437}
3538
3639#[ derive( Default ) ]
3740pub struct RuntimeGaugeMetrics {
38- runtime_thread_alive_gauges : Family < RuntimeThreadLabels , gauge:: Gauge > ,
39- runtime_thread_idle_gauges : Family < RuntimeThreadLabels , gauge:: Gauge > ,
41+ runtime_thread_alive_gauges : Family < RuntimeNameLabels , gauge:: Gauge > ,
42+ runtime_thread_idle_gauges : Family < RuntimeNameLabels , gauge:: Gauge > ,
4043}
4144
4245impl RuntimeGaugeMetrics {
@@ -54,35 +57,95 @@ impl RuntimeGaugeMetrics {
5457 ) ;
5558 }
5659
57- pub fn on_thread_start ( & self , rt_type : ObClientRuntimeGaugeType ) {
60+ pub fn on_thread_start ( & self , rt_name : & str ) {
5861 self . runtime_thread_alive_gauges
59- . get_or_create ( & RuntimeThreadLabels { rt_type } )
62+ . get_or_create ( & RuntimeNameLabels {
63+ string_type : rt_name. to_string ( ) ,
64+ } )
6065 . inc ( ) ;
6166 }
6267
63- pub fn on_thread_stop ( & self , rt_type : ObClientRuntimeGaugeType ) {
68+ pub fn on_thread_stop ( & self , rt_name : & str ) {
6469 self . runtime_thread_alive_gauges
65- . get_or_create ( & RuntimeThreadLabels { rt_type } )
70+ . get_or_create ( & RuntimeNameLabels {
71+ string_type : rt_name. to_string ( ) ,
72+ } )
6673 . dec ( ) ;
6774 }
6875
69- pub fn on_thread_park ( & self , rt_type : ObClientRuntimeGaugeType ) {
70- self . runtime_thread_alive_gauges
71- . get_or_create ( & RuntimeThreadLabels { rt_type } )
76+ pub fn on_thread_park ( & self , rt_name : & str ) {
77+ self . runtime_thread_idle_gauges
78+ . get_or_create ( & RuntimeNameLabels {
79+ string_type : rt_name. to_string ( ) ,
80+ } )
7281 . inc ( ) ;
7382 }
7483
75- pub fn on_thread_unpark ( & self , rt_type : ObClientRuntimeGaugeType ) {
76- self . runtime_thread_alive_gauges
77- . get_or_create ( & RuntimeThreadLabels { rt_type } )
84+ pub fn on_thread_unpark ( & self , rt_name : & str ) {
85+ self . runtime_thread_idle_gauges
86+ . get_or_create ( & RuntimeNameLabels {
87+ string_type : rt_name. to_string ( ) ,
88+ } )
7889 . dec ( ) ;
7990 }
8091
81- pub fn get_runtime_thread_alive_gauges ( & self ) -> & Family < RuntimeThreadLabels , gauge:: Gauge > {
92+ pub fn get_runtime_thread_alive_gauges ( & self ) -> & Family < RuntimeNameLabels , gauge:: Gauge > {
8293 & self . runtime_thread_alive_gauges
8394 }
8495
85- pub fn get_runtime_thread_idle_gauges ( & self ) -> & Family < RuntimeThreadLabels , gauge:: Gauge > {
96+ pub fn get_runtime_thread_idle_gauges ( & self ) -> & Family < RuntimeNameLabels , gauge:: Gauge > {
8697 & self . runtime_thread_idle_gauges
8798 }
8899}
100+
101+ /// Runtime metrics.
102+ #[ derive( Debug ) ]
103+ pub struct RuntimeMetrics {
104+ pub runtime_name : String ,
105+ }
106+
107+ impl RuntimeMetrics {
108+ pub fn new ( runtime_name : & str ) -> Self {
109+ Self {
110+ runtime_name : runtime_name. to_owned ( ) ,
111+ }
112+ }
113+
114+ pub fn set_runtime_name ( & mut self , runtime_name : String ) {
115+ self . runtime_name = runtime_name;
116+ }
117+
118+ pub fn on_thread_start ( & self ) {
119+ OBKV_RUNTIME_GAUGE_METRICS . on_thread_start ( & self . runtime_name ) ;
120+ }
121+
122+ pub fn on_thread_stop ( & self ) {
123+ OBKV_RUNTIME_GAUGE_METRICS . on_thread_stop ( & self . runtime_name ) ;
124+ }
125+
126+ pub fn on_thread_park ( & self ) {
127+ OBKV_RUNTIME_GAUGE_METRICS . on_thread_park ( & self . runtime_name ) ;
128+ }
129+
130+ pub fn on_thread_unpark ( & self ) {
131+ OBKV_RUNTIME_GAUGE_METRICS . on_thread_unpark ( & self . runtime_name ) ;
132+ }
133+
134+ pub fn get_runtime_thread_alive_gauges ( & self ) -> i64 {
135+ OBKV_RUNTIME_GAUGE_METRICS
136+ . get_runtime_thread_alive_gauges ( )
137+ . get_or_create ( & RuntimeNameLabels {
138+ string_type : self . runtime_name . clone ( ) ,
139+ } )
140+ . get ( )
141+ }
142+
143+ pub fn get_runtime_thread_idle_gauges ( & self ) -> i64 {
144+ OBKV_RUNTIME_GAUGE_METRICS
145+ . get_runtime_thread_idle_gauges ( )
146+ . get_or_create ( & RuntimeNameLabels {
147+ string_type : self . runtime_name . clone ( ) ,
148+ } )
149+ . get ( )
150+ }
151+ }
0 commit comments