-
-
Notifications
You must be signed in to change notification settings - Fork 148
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(subscriber): resource instrumentation (#77)
This PR adds the pieces needed to get resource instrumentation data into the console. This includes: - changes to the proto definitions - changes to the subscriber The UI part will come as a follow up PR. This branch uses a patched `tokio` that emits these tracing spans and events for the `Sleep` resource. You can look at the raw data by running: ``` cargo run --example app cargo run --example dump ``` The information piped through includes: - data describing the resource lifecycle, namely when resources are created and dropped - data describing the async operations that take place on these events and their associationg with tasks - data reflecting the state updates that take place on resources (e.g. resetting timer's duration, adding permits to a semaphore, etc) Signed-off-by: Zahari Dichev <zaharidichev@gmail.com> Co-authored-by: Eliza Weisman <eliza@buoyant.io>
- Loading branch information
1 parent
5fe4437
commit f4a21ac
Showing
25 changed files
with
1,807 additions
and
508 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
tonic::include_proto!("rs.tokio.console.async_ops"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
tonic::include_proto!("rs.tokio.console.instrument"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,7 @@ | ||
pub mod async_ops; | ||
mod common; | ||
pub mod instrument; | ||
pub mod resources; | ||
pub mod tasks; | ||
pub mod trace; | ||
pub use common::*; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
tonic::include_proto!("rs.tokio.console.resources"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1 @@ | ||
tonic::include_proto!("rs.tokio.console.tasks"); | ||
|
||
// === IDs === | ||
|
||
impl From<u64> for TaskId { | ||
fn from(id: u64) -> Self { | ||
TaskId { id } | ||
} | ||
} | ||
|
||
impl From<TaskId> for u64 { | ||
fn from(id: TaskId) -> Self { | ||
id.id | ||
} | ||
} | ||
|
||
impl Copy for TaskId {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,150 @@ | ||
use super::{shrink::ShrinkMap, Closable, Id, Ids, ToProto}; | ||
use std::collections::{HashMap, HashSet}; | ||
use std::ops::{Deref, DerefMut}; | ||
use std::time::{Duration, SystemTime}; | ||
|
||
pub(crate) struct IdData<T> { | ||
data: ShrinkMap<Id, (T, bool)>, | ||
} | ||
|
||
pub(crate) struct Updating<'a, T>(&'a mut (T, bool)); | ||
|
||
pub(crate) enum Include { | ||
All, | ||
UpdatedOnly, | ||
} | ||
|
||
// === impl IdData === | ||
|
||
impl<T> Default for IdData<T> { | ||
fn default() -> Self { | ||
IdData { | ||
data: ShrinkMap::<Id, (T, bool)>::new(), | ||
} | ||
} | ||
} | ||
|
||
impl<T> IdData<T> { | ||
pub(crate) fn update_or_default(&mut self, id: Id) -> Updating<'_, T> | ||
where | ||
T: Default, | ||
{ | ||
Updating(self.data.entry(id).or_default()) | ||
} | ||
|
||
pub(crate) fn update(&mut self, id: &Id) -> Option<Updating<'_, T>> { | ||
self.data.get_mut(id).map(Updating) | ||
} | ||
|
||
pub(crate) fn insert(&mut self, id: Id, data: T) { | ||
self.data.insert(id, (data, true)); | ||
} | ||
|
||
pub(crate) fn since_last_update(&mut self) -> impl Iterator<Item = (&Id, &mut T)> { | ||
self.data.iter_mut().filter_map(|(id, (data, dirty))| { | ||
if *dirty { | ||
*dirty = false; | ||
Some((id, data)) | ||
} else { | ||
None | ||
} | ||
}) | ||
} | ||
|
||
pub(crate) fn all(&self) -> impl Iterator<Item = (&Id, &T)> { | ||
self.data.iter().map(|(id, (data, _))| (id, data)) | ||
} | ||
|
||
pub(crate) fn get(&self, id: &Id) -> Option<&T> { | ||
self.data.get(id).map(|(data, _)| data) | ||
} | ||
|
||
pub(crate) fn as_proto(&mut self, include: Include) -> HashMap<u64, T::Output> | ||
where | ||
T: ToProto, | ||
{ | ||
match include { | ||
Include::UpdatedOnly => self | ||
.since_last_update() | ||
.map(|(id, d)| (*id, d.to_proto())) | ||
.collect(), | ||
Include::All => self.all().map(|(id, d)| (*id, d.to_proto())).collect(), | ||
} | ||
} | ||
|
||
pub(crate) fn drop_closed<R: Closable>( | ||
&mut self, | ||
stats: &mut IdData<R>, | ||
now: SystemTime, | ||
retention: Duration, | ||
has_watchers: bool, | ||
ids: &mut Ids, | ||
) { | ||
let _span = tracing::debug_span!( | ||
"drop_closed", | ||
entity = %std::any::type_name::<T>(), | ||
stats = %std::any::type_name::<R>(), | ||
) | ||
.entered(); | ||
|
||
// drop closed entities | ||
tracing::trace!(?retention, has_watchers, "dropping closed"); | ||
|
||
let mut dropped_ids = HashSet::new(); | ||
stats.data.retain_and_shrink(|id, (stats, dirty)| { | ||
if let Some(closed) = stats.closed_at() { | ||
let closed_for = now.duration_since(closed).unwrap_or_default(); | ||
let should_drop = | ||
// if there are any clients watching, retain all dirty tasks regardless of age | ||
(*dirty && has_watchers) | ||
|| closed_for > retention; | ||
tracing::trace!( | ||
stats.id = ?id, | ||
stats.closed_at = ?closed, | ||
stats.closed_for = ?closed_for, | ||
stats.dirty = *dirty, | ||
should_drop, | ||
); | ||
|
||
if should_drop { | ||
dropped_ids.insert(*id); | ||
} | ||
return !should_drop; | ||
} | ||
|
||
true | ||
}); | ||
|
||
// drop closed entities which no longer have stats. | ||
self.data | ||
.retain_and_shrink(|id, (_, _)| stats.data.contains_key(id)); | ||
|
||
if !dropped_ids.is_empty() { | ||
// drop closed entities which no longer have stats. | ||
self.data | ||
.retain_and_shrink(|id, (_, _)| stats.data.contains_key(id)); | ||
ids.remove_all(&dropped_ids); | ||
} | ||
} | ||
} | ||
|
||
// === impl Updating === | ||
|
||
impl<'a, T> Deref for Updating<'a, T> { | ||
type Target = T; | ||
fn deref(&self) -> &Self::Target { | ||
&self.0 .0 | ||
} | ||
} | ||
|
||
impl<'a, T> DerefMut for Updating<'a, T> { | ||
fn deref_mut(&mut self) -> &mut Self::Target { | ||
&mut self.0 .0 | ||
} | ||
} | ||
|
||
impl<'a, T> Drop for Updating<'a, T> { | ||
fn drop(&mut self) { | ||
self.0 .1 = true; | ||
} | ||
} |
Oops, something went wrong.