-
-
Notifications
You must be signed in to change notification settings - Fork 148
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
feat(subscriber): resource instrumentation #77
Merged
Merged
Changes from 13 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
18af311
resource instrumentation
zaharidichev 9a2a952
fmt
zaharidichev 38bed5e
feedback
zaharidichev d3dd8a2
lint
zaharidichev c76dfc8
feedback
zaharidichev 806d01f
Merge branch 'main' into zd/res-schema
zaharidichev be39664
Merge branch 'main' into zd/res-schema
zaharidichev 768958a
task_data -> id_data
zaharidichev 9e1d1fa
patch tokio
zaharidichev 1b83bb4
do not use shrink vec for new_poll_ops
zaharidichev 5ebaf40
add some more comments
zaharidichev 67ba3f6
Merge branch 'main' into zd/res-schema
hawkw b29f5e3
simplify updates handling + feedback
zaharidichev 74c50a4
Merge branch 'main' into zd/res-schema
zaharidichev 5f0396c
Merge branch 'main' into zd/res-schema
zaharidichev e1b1579
newline
zaharidichev 1fe75c9
ensure value fields do not end with unit or up suffixes
zaharidichev File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Ah, this is required because
prost
generates a struct with aPartialEq
impl, but we can't addderive(Hash)
to that struct. It might be worth adding a comment explaining why we allow this lint: