Skip to content

Commit 08394ed

Browse files
authored
Integrate derived metrics with metrics.rs (#104)
1 parent a610c22 commit 08394ed

File tree

8 files changed

+689
-503
lines changed

8 files changed

+689
-503
lines changed

src/derived_metrics.rs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
macro_rules! derived_metrics {
2+
(
3+
[$metrics_name:ty] {
4+
stable {
5+
$(
6+
$(#[$($attributes:tt)*])*
7+
$vis:vis fn $name:ident($($args:tt)*) -> $ty:ty $body:block
8+
)*
9+
}
10+
unstable {
11+
$(
12+
$(#[$($unstable_attributes:tt)*])*
13+
$unstable_vis:vis fn $unstable_name:ident($($unstable_args:tt)*) -> $unstable_ty:ty $unstable_body:block
14+
)*
15+
}
16+
}
17+
) => {
18+
impl $metrics_name {
19+
$(
20+
$(#[$($attributes)*])*
21+
$vis fn $name($($args)*) -> $ty $body
22+
)*
23+
$(
24+
$(#[$($unstable_attributes)*])*
25+
#[cfg(tokio_unstable)]
26+
$unstable_vis fn $unstable_name($($unstable_args)*) -> $unstable_ty $unstable_body
27+
)*
28+
29+
#[cfg(all(test, feature = "metrics-rs-integration"))]
30+
const DERIVED_METRICS: &[&str] = &[$(stringify!($name),)*];
31+
#[cfg(all(test, tokio_unstable, feature = "metrics-rs-integration"))]
32+
const UNSTABLE_DERIVED_METRICS: &[&str] = &[$(stringify!($unstable_name),)*];
33+
}
34+
};
35+
}
36+
37+
pub(crate) use derived_metrics;

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,7 @@ cfg_rt! {
179179
)]
180180
pub use runtime::metrics_rs_integration::{RuntimeMetricsReporter, RuntimeMetricsReporterBuilder};
181181

182+
mod derived_metrics;
182183
#[cfg(all(feature = "rt", feature = "metrics-rs-integration"))]
183184
mod metrics_rs;
184185
mod task;

src/metrics_rs.rs

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,23 +76,44 @@ macro_rules! metric_refs {
7676
),*
7777
$(,)?
7878
}
79+
stable_derived {
80+
$(
81+
#[doc = $derived_doc:tt]
82+
$derived_name:ident: $derived_kind:tt <$derived_unit:ident> $derived_opts:tt
83+
),*
84+
$(,)?
85+
}
7986
unstable {
8087
$(
8188
#[doc = $unstable_doc:tt]
8289
$unstable_name:ident: $unstable_kind:tt <$unstable_unit:ident> $unstable_opts:tt
8390
),*
8491
$(,)?
8592
}
93+
unstable_derived {
94+
$(
95+
#[doc = $unstable_derived_doc:tt]
96+
$unstable_derived_name:ident: $unstable_derived_kind:tt <$unstable_derived_unit:ident> $unstable_derived_opts:tt
97+
),*
98+
$(,)?
99+
}
86100
}
87101
) => {
88102
struct $struct_name {
89103
$(
90104
$name: crate::metrics_rs::kind_to_type!($kind),
91105
)*
106+
$(
107+
$derived_name: crate::metrics_rs::kind_to_type!($derived_kind),
108+
)*
92109
$(
93110
#[cfg(tokio_unstable)]
94111
$unstable_name: crate::metrics_rs::kind_to_type!($unstable_kind),
95112
)*
113+
$(
114+
#[cfg(tokio_unstable)]
115+
$unstable_derived_name: crate::metrics_rs::kind_to_type!($unstable_derived_kind),
116+
)*
96117
}
97118

98119
impl $struct_name {
@@ -101,14 +122,30 @@ macro_rules! metric_refs {
101122
$(
102123
$name: crate::metrics_rs::capture_metric_ref!(transform_fn, $name: $kind $opts),
103124
)*
125+
$(
126+
$derived_name: crate::metrics_rs::capture_metric_ref!(transform_fn, $derived_name: $derived_kind $derived_opts),
127+
)*
104128
$(
105129
#[cfg(tokio_unstable)]
106130
$unstable_name: crate::metrics_rs::capture_metric_ref!(transform_fn, $unstable_name: $unstable_kind $unstable_opts),
107131
)*
132+
$(
133+
#[cfg(tokio_unstable)]
134+
$unstable_derived_name: crate::metrics_rs::capture_metric_ref!(transform_fn, $unstable_derived_name: $unstable_derived_kind $unstable_derived_opts),
135+
)*
108136
}
109137
}
110138

111139
fn emit(&self, metrics: $metrics_name, emit_arg: $emit_arg_type) {
140+
// Emit derived metrics before base metrics because emitting base metrics may move
141+
// out of `$metrics`.
142+
$(
143+
crate::metrics_rs::MyMetricOp::op((&self.$derived_name, metrics.$derived_name()), emit_arg);
144+
)*
145+
$(
146+
#[cfg(tokio_unstable)]
147+
crate::metrics_rs::MyMetricOp::op((&self.$unstable_derived_name, metrics.$unstable_derived_name()), emit_arg);
148+
)*
112149
$(
113150
crate::metrics_rs::MyMetricOp::op((&self.$name, metrics.$name), emit_arg);
114151
)*
@@ -122,10 +159,17 @@ macro_rules! metric_refs {
122159
$(
123160
crate::metrics_rs::describe_metric_ref!(transform_fn, $doc, $name: $kind<$unit> $opts);
124161
)*
162+
$(
163+
crate::metrics_rs::describe_metric_ref!(transform_fn, $derived_doc, $derived_name: $derived_kind<$derived_unit> $derived_opts);
164+
)*
125165
$(
126166
#[cfg(tokio_unstable)]
127167
crate::metrics_rs::describe_metric_ref!(transform_fn, $unstable_doc, $unstable_name: $unstable_kind<$unstable_unit> $unstable_opts);
128168
)*
169+
$(
170+
#[cfg(tokio_unstable)]
171+
crate::metrics_rs::describe_metric_ref!(transform_fn, $unstable_derived_doc, $unstable_derived_name: $unstable_derived_kind<$unstable_derived_unit> $unstable_derived_opts);
172+
)*
129173
}
130174
}
131175

@@ -161,6 +205,28 @@ macro_rules! metric_refs {
161205
panic!("missing metric {:?}", line);
162206
}
163207
}
208+
209+
#[test]
210+
fn test_no_derived_metrics_missing() {
211+
// test that no derived metrics are missing.
212+
for derived_metric in <$metrics_name>::DERIVED_METRICS {
213+
$(
214+
if *derived_metric == stringify!($derived_name) {
215+
continue
216+
}
217+
);*
218+
panic!("missing metric {:?}", derived_metric);
219+
}
220+
#[cfg(tokio_unstable)]
221+
for unstable_derived_metric in <$metrics_name>::UNSTABLE_DERIVED_METRICS {
222+
$(
223+
if *unstable_derived_metric == stringify!($unstable_derived_name) {
224+
continue
225+
}
226+
);*
227+
panic!("missing metric {:?}", unstable_derived_metric);
228+
}
229+
}
164230
}
165231
}
166232

@@ -205,6 +271,12 @@ impl<T> MyMetricOp<T> for (&metrics::Gauge, usize) {
205271
}
206272
}
207273

274+
impl<T> MyMetricOp<T> for (&metrics::Gauge, f64) {
275+
fn op(self, _t: T) {
276+
self.0.set(self.1);
277+
}
278+
}
279+
208280
#[cfg(tokio_unstable)]
209281
impl MyMetricOp<&tokio::runtime::RuntimeMetrics> for (&metrics::Histogram, Vec<u64>) {
210282
fn op(self, tokio: &tokio::runtime::RuntimeMetrics) {

src/runtime.rs

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use std::time::{Duration, Instant};
22
use tokio::runtime;
3+
use crate::derived_metrics::derived_metrics;
34

45
#[cfg(feature = "metrics-rs-integration")]
56
pub(crate) mod metrics_rs_integration;
@@ -1567,20 +1568,24 @@ impl Worker {
15671568
}
15681569
}
15691570

1570-
impl RuntimeMetrics {
1571-
/// Returns the ratio of the [`RuntimeMetrics::total_polls_count`] to the [`RuntimeMetrics::total_noop_count`].
1572-
#[cfg(tokio_unstable)]
1573-
pub fn mean_polls_per_park(&self) -> f64 {
1574-
let total_park_count = self.total_park_count - self.total_noop_count;
1575-
if total_park_count == 0 {
1576-
0.0
1577-
} else {
1578-
self.total_polls_count as f64 / total_park_count as f64
1571+
derived_metrics!(
1572+
[RuntimeMetrics] {
1573+
stable {
1574+
/// Returns the ratio of the [`RuntimeMetrics::total_busy_duration`] to the [`RuntimeMetrics::elapsed`].
1575+
pub fn busy_ratio(&self) -> f64 {
1576+
self.total_busy_duration.as_nanos() as f64 / self.elapsed.as_nanos() as f64
1577+
}
1578+
}
1579+
unstable {
1580+
/// Returns the ratio of the [`RuntimeMetrics::total_polls_count`] to the [`RuntimeMetrics::total_noop_count`].
1581+
pub fn mean_polls_per_park(&self) -> f64 {
1582+
let total_park_count = self.total_park_count - self.total_noop_count;
1583+
if total_park_count == 0 {
1584+
0.0
1585+
} else {
1586+
self.total_polls_count as f64 / total_park_count as f64
1587+
}
1588+
}
15791589
}
15801590
}
1581-
1582-
/// Returns the ratio of the [`RuntimeMetrics::total_busy_duration`] to the [`RuntimeMetrics::elapsed`].
1583-
pub fn busy_ratio(&self) -> f64 {
1584-
self.total_busy_duration.as_nanos() as f64 / self.elapsed.as_nanos() as f64
1585-
}
1586-
}
1591+
);

src/runtime/metrics_rs_integration.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,10 @@ metric_refs! {
264264
/// The number of tasks currently scheduled in the runtime's global queue
265265
global_queue_depth: Gauge<Count> [],
266266
}
267+
stable_derived {
268+
/// The ratio of the [`RuntimeMetrics::total_busy_duration`] to the [`RuntimeMetrics::elapsed`].
269+
busy_ratio: Gauge<Percent> [],
270+
}
267271
unstable {
268272
/// The average duration of a single invocation of poll on a task
269273
mean_poll_duration: Gauge<Microseconds> [],
@@ -330,6 +334,10 @@ metric_refs! {
330334
/// Returns the number of ready events processed by the runtime’s I/O driver
331335
io_driver_ready_count: Counter<Count> [],
332336
}
337+
unstable_derived {
338+
/// The ratio of the [`RuntimeMetrics::total_polls_count`] to the [`RuntimeMetrics::total_noop_count`].
339+
mean_polls_per_park: Gauge<Percent> [],
340+
}
333341
}
334342
}
335343

0 commit comments

Comments
 (0)