Skip to content

Conversation

@jkosh44
Copy link
Contributor

@jkosh44 jkosh44 commented Nov 23, 2025

This commit provides integration of task metrics and metrics.rs.

Resolves #71

I hope that no one was already working on this, I saw the issue was unassigned so I thought I'd give it a try.

I split the PR into two commits for easier reviewing. The first commit moves shared macros and traits to a separate module, but doesn't change any code (other than imports, exports, and visibility modifiers). The second commit actually adds the task metrics integration.

There is still some duplicate across the XMetricsReporterBuilder and XMetricsReporter structs. I played around with making a generic struct that can be used by both, but the structs themselves are fairly simple, so it didn't seem worth the added complexity. There is a decent amount of duplicated doc-comments across the structs though. Let me know if you have any thoughts.

This commit moves the macros and traits used to implement the runtime
metrics integration with metrics_rs to a separate module. These will be
used in a later commit to implement the task metrics integration with
metrics_rs.

Works towards resolving tokio-rs#71

macro_rules! metric_refs {
(
[$struct_name:ident] [$($ignore:ident),* $(,)?] [$metrics_name:ty] [$emit_arg_type:ty] {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's probably possible to derive the struct_name from the metrics_name by appending Ref, but that didn't seem too worth it. Let me know if you disagree.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yea, better to pass the identifiers explicitly rather than constructing them

Comment on lines +208 to +221
#[cfg(tokio_unstable)]
impl MyMetricOp<&tokio::runtime::RuntimeMetrics> for (&metrics::Histogram, Vec<u64>) {
fn op(self, tokio: &tokio::runtime::RuntimeMetrics) {
for (i, bucket) in self.1.iter().enumerate() {
let range = tokio.poll_time_histogram_bucket_range(i);
if *bucket > 0 {
// emit using range.start to avoid very large numbers for open bucket
// FIXME: do we want to do something else here?
self.0
.record_many(range.start.as_micros() as f64, *bucket as usize);
}
}
}
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To be honest, I was a little confused by this impl. It seems like it's specific to a single histogram metric in the runtime. Does this make it impossible to add any other histogram metrics in the future?

Copy link
Contributor

@arielb1 arielb1 Nov 26, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you will add more histograms you will need to give the Vec<u64> special names or something. There are enough ways of changing the macro to get this to work so I'm not that worried about it.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we should rename the Histogram kind to a PollTimeHistogram kind for the meanwhile? (Not making this mandatory for approval)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea, I think that will help clarify. I've included this in my most recent commit.

@jkosh44 jkosh44 marked this pull request as ready for review November 23, 2025 15:23
This commit provides integration of task metrics and metrics.rs.

Resolves tokio-rs#71
@jkosh44 jkosh44 force-pushed the task-metrics-rs-integration branch from cd1bdf5 to 65042bd Compare November 23, 2025 15:58
Comment on lines 13 to 14
/// then it is strongly recommended to use [`with_metrics_transformer`] to give each [`TaskMonitor`]
/// a unique metric name.
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe actually the metric name doesn't need to be unique as long as each TaskMonitor has some unique tags?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

with_metrics_transformer can add dimensions. Maybe this should be "a unique metric name or dimensions"?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This definitely feels to me like a bad-ish API for the gauges, since if you have multiple TaskMonitors they will probably conflict in an annoying way.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've removed the default constructor in my most recent commit and make the metrics transformer mandatory.

@arielb1
Copy link
Contributor

arielb1 commented Nov 26, 2025

Nice PR!

I do think that API-wise, we don't want people to create task metrics without a transformer, since in that cause it is way too easy to end up with mixed gauges. Could you not add a default constructor for that?

@jkosh44
Copy link
Contributor Author

jkosh44 commented Nov 26, 2025

@arielb1 thanks so much for the review! I believe I've addressed all your comments in my most recent commit. Please let me know if I've missed something.

}
}

impl Default for TaskMetricsReporterBuilder {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should remove this?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

@arielb1 arielb1 merged commit 9354757 into tokio-rs:main Nov 27, 2025
7 checks passed
@jkosh44 jkosh44 deleted the task-metrics-rs-integration branch November 27, 2025 23:18
@kanpov
Copy link

kanpov commented Nov 28, 2025

Could this functionality be published in a release?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[metrics-rs-integration / task] Add metrics.rs integration for TaskMetrics

3 participants