-
Notifications
You must be signed in to change notification settings - Fork 475
feat(runtime): add RuntimeTracer trait for task instrumentation
#2467
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,86 @@ | ||
| // Licensed to the Apache Software Foundation (ASF) under one | ||
| // or more contributor license agreements. See the NOTICE file | ||
| // distributed with this work for additional information | ||
| // regarding copyright ownership. The ASF licenses this file | ||
| // to you under the Apache License, Version 2.0 (the | ||
| // "License"); you may not use this file except in compliance | ||
| // with the License. You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, | ||
| // software distributed under the License is distributed on an | ||
| // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| // KIND, either express or implied. See the License for the | ||
| // specific language governing permissions and limitations | ||
| // under the License. | ||
|
|
||
| use std::any::Any; | ||
| use std::future::Future; | ||
|
|
||
| use futures::FutureExt; | ||
| use futures::future::BoxFuture; | ||
|
|
||
| /// A trait for injecting instrumentation into spawned tasks. | ||
| /// | ||
| /// Implementations can wrap futures or blocking closures with tracing spans, | ||
| /// metrics, or other observability hooks. The tracer receives type-erased | ||
| /// values and must preserve the output without modification. | ||
| pub trait RuntimeTracer: Send + Sync + 'static { | ||
| /// Wraps a type-erased future with instrumentation. | ||
| /// | ||
| /// The implementation must not alter the future's output value. | ||
| fn trace_future( | ||
| &self, | ||
| fut: BoxFuture<'static, Box<dyn Any + Send>>, | ||
| ) -> BoxFuture<'static, Box<dyn Any + Send>>; | ||
|
|
||
| /// Wraps a type-erased blocking closure with instrumentation. | ||
| /// | ||
| /// The implementation must not alter the closure's return value. | ||
| fn trace_block( | ||
| &self, | ||
| f: Box<dyn FnOnce() -> Box<dyn Any + Send> + Send>, | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Because this is blocking do we really need the
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Tokio
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Right but not all Rust run times support work stealing and cross thread moves. Should the Tracer check that Tokio is being used to be safe from a future scenario where the run time being used is composable? For example Glommio from Datadog doesn't use Send on spawn https://docs.rs/glommio/latest/glommio/fn.spawn_local.html |
||
| ) -> Box<dyn FnOnce() -> Box<dyn Any + Send> + Send>; | ||
| } | ||
|
|
||
| /// Wraps a concrete future with the tracer, handling type erasure and | ||
| /// restoration internally. | ||
| pub(crate) fn trace_future<T, F>( | ||
| tracer: &dyn RuntimeTracer, | ||
| future: F, | ||
| ) -> impl Future<Output = T> + Send + 'static | ||
| where | ||
| F: Future<Output = T> + Send + 'static, | ||
| T: Send + 'static, | ||
| { | ||
| let erased = async move { Box::new(future.await) as Box<dyn Any + Send> }.boxed(); | ||
|
|
||
| tracer.trace_future(erased).map(|any_box| { | ||
| *any_box | ||
| .downcast::<T>() | ||
| .expect("RuntimeTracer must preserve the future's output type") | ||
| }) | ||
| } | ||
|
|
||
| /// Wraps a concrete blocking closure with the tracer, handling type erasure and | ||
| /// restoration internally. | ||
| pub(crate) fn trace_block<T, F>( | ||
| tracer: &dyn RuntimeTracer, | ||
| f: F, | ||
| ) -> impl FnOnce() -> T + Send + 'static | ||
| where | ||
| F: FnOnce() -> T + Send + 'static, | ||
| T: Send + 'static, | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same as above why do we need |
||
| { | ||
| let erased: Box<dyn FnOnce() -> Box<dyn Any + Send> + Send> = | ||
| Box::new(|| Box::new(f()) as Box<dyn Any + Send>); | ||
|
|
||
| let traced = tracer.trace_block(erased); | ||
|
|
||
| move || { | ||
| *traced() | ||
| .downcast::<T>() | ||
| .expect("RuntimeTracer must preserve the closure's return type") | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why is the io.tracer cloned, but the cpu tracer isn't?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You need it twice so the first is a "move" but clone of
Arcis very cheapThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sounds good, I think I was over-complicating the question of why the CPU tracer takes direct ownership of the tracer memory while the io takes a copy. Thank you for responding!