Skip to content
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

chore: record spans regardless if they're spawned, send only if they're spawned #15

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .envrc

This file was deleted.

1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ toolkit consists of multiple components:

## extremely cool and amazing demo


[![asciicast](https://asciinema.org/a/412139.svg)](https://asciinema.org/a/412139?t=44)

wow! whoa! it's like `top(1)` for tasks!
Expand Down
66 changes: 34 additions & 32 deletions console-subscriber/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ impl<F> TasksLayer<F> {
pub const DEFAULT_EVENT_BUFFER_CAPACITY: usize = 1024 * 10;
pub const DEFAULT_CLIENT_BUFFER_CAPACITY: usize = 1024 * 4;
pub const DEFAULT_FLUSH_INTERVAL: Duration = Duration::from_secs(1);

#[inline(always)]
fn is_spawn(&self, meta: &'static Metadata<'static>) -> bool {
ptr::eq(self.task_meta.load(Relaxed), meta as *const _ as *mut _)
Expand Down Expand Up @@ -163,48 +164,49 @@ where
F: for<'writer> FormatFields<'writer> + 'static,
{
fn register_callsite(&self, meta: &'static Metadata<'static>) -> subscriber::Interest {
if meta.target() == "tokio::task" && meta.name() == "task" {
if meta.fields().iter().any(|f| f.name() == "function") {
let _ = self.blocking_meta.compare_exchange(
ptr::null_mut(),
meta as *const _ as *mut _,
AcqRel,
Acquire,
);
} else {
let _ = self.task_meta.compare_exchange(
ptr::null_mut(),
meta as *const _ as *mut _,
AcqRel,
Acquire,
);
}
if meta.fields().iter().any(|f| f.name() == "function") {
let _ = self.blocking_meta.compare_exchange(
ptr::null_mut(),
meta as *const _ as *mut _,
AcqRel,
Acquire,
);
} else {
let _ = self.task_meta.compare_exchange(
ptr::null_mut(),
meta as *const _ as *mut _,
AcqRel,
Acquire,
);
}

self.try_send(Event::Metadata(meta));
if meta.target() == "tokio::task" && meta.name() == "task" {
self.try_send(Event::Metadata(meta));
}

subscriber::Interest::always()
}

fn new_span(&self, attrs: &span::Attributes<'_>, id: &span::Id, cx: Context<'_, S>) {
let metadata = attrs.metadata();
if self.is_spawn(metadata) {
let at = SystemTime::now();
let span = cx.span(id).expect("newly-created span should exist");
let mut exts = span.extensions_mut();
let fields = match exts.get_mut::<FormattedFields<F>>() {
Some(fields) => fields.fields.clone(),
None => {
let mut fields = String::new();
match self.format.format_fields(&mut fields, attrs) {
Ok(()) => exts.insert(FormattedFields::<F>::new(fields.clone())),
Err(_) => {
tracing::warn!(span.id = ?id, span.attrs = ?attrs, "error formatting fields for span")
}
let at = SystemTime::now();
let span = cx.span(id).expect("newly-created span should exist");
let mut exts = span.extensions_mut();
let fields = match exts.get_mut::<FormattedFields<F>>() {
Some(fields) => fields.fields.clone(),
None => {
let mut fields = String::new();
match self.format.format_fields(&mut fields, attrs) {
Ok(()) => exts.insert(FormattedFields::<F>::new(fields.clone())),
Err(_) => {
tracing::warn!(span.id = ?id, span.attrs = ?attrs, "error formatting fields for span")
}
fields
}
};
fields
}
};

if self.is_spawn(metadata) {
self.try_send(Event::Spawn {
id: id.clone(),
at,
Expand Down