Skip to content

Commit f7c7a2c

Browse files
authored
[Feat] impl runtime (#49)
* [Feat] impl runtime * [Fix] cargo sort
1 parent c7c3ab1 commit f7c7a2c

File tree

5 files changed

+407
-38
lines changed

5 files changed

+407
-38
lines changed

Cargo.lock

+48
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+2
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ log = { workspace = true }
4242
murmur2 = "0.1"
4343
mysql = "16.1"
4444
net2 = "0.2"
45+
pin-project-lite = "0.2"
4546
prometheus-client = { workspace = true }
4647
quick-error = "1.2"
4748
r2d2 = "0.8.3"
@@ -67,6 +68,7 @@ serial_test_derive = "2.0"
6768
tempfile = "3.0"
6869
test-log = "0.2"
6970
time = "0.3"
71+
tokio-test = "0.4"
7072

7173
[profile.release]
7274
debug = true

src/error.rs

+6
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,12 @@ quick_error! {
9797
display("Query database error, err:{}", e)
9898
cause(e)
9999
}
100+
JoinTask(e: tokio::task::JoinError) {
101+
from()
102+
description("Tokio join error")
103+
display("Tokio join error, err:{}", e)
104+
cause(e)
105+
}
100106
}
101107
}
102108

src/monitors/runtime_metrics.rs

+85-22
Original file line numberDiff line numberDiff line change
@@ -16,27 +16,30 @@
1616
*/
1717

1818
use 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)]
3740
pub 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

4245
impl 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

Comments
 (0)