Skip to content

Commit

Permalink
feat(console): don't display fields with "" values
Browse files Browse the repository at this point in the history
Now that Tokio supports optional user-provided task names
(in tokio-rs/tokio#3881), the task spans it generates may contain empty
fields (if the user does not provide a name for a spawned task).
However, the console always displays the `task.name` field, even when
they are empty. This wastes space, and looks kind of ugly.

This branch updates the console to skip displaying fields whose value is
the empty string.

### Before:
![image](https://user-images.githubusercontent.com/2796466/128227118-7ee3b4d7-634f-410f-a1d0-4e5afd423566.png)

### After:
![image](https://user-images.githubusercontent.com/2796466/128227203-248bd059-839a-4254-8470-2761f726c885.png)

Signed-off-by: Eliza Weisman <eliza@buoyant.io>
  • Loading branch information
hawkw committed Aug 4, 2021
1 parent 66b0f00 commit 41ae55a
Showing 1 changed file with 31 additions and 10 deletions.
41 changes: 31 additions & 10 deletions console/src/tasks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,24 +161,37 @@ impl State {
.drain(..)
.filter_map(|f| {
let field_name = f.name.as_ref()?;
let name: Option<Arc<str>> = match field_name {
proto::field::Name::StrName(n) => Some(n.clone().into()),
let name: Arc<str> = match field_name {
proto::field::Name::StrName(n) => n.clone().into(),
proto::field::Name::NameIdx(idx) => {
debug_assert_eq!(
f.metadata_id.map(|m| m.id),
Some(meta_id),
"malformed field name: metadata ID mismatch!"
);
meta.field_names.get(*idx as usize).cloned()
let name = meta.field_names.get(*idx as usize).cloned();
if name.is_none() {
tracing::warn!(index = idx, "missing field name for");
};
name?
}
};
let mut value: FieldValue = f.value.as_ref().expect("no value").clone().into();
name.map(|name| {
if &*name == "spawn.location" {
value = value.truncate_registry_path();
}
Field { name, value }
})

let value = f.value;
debug_assert!(
value.is_some(),
"missing field value for field `{:?}`",
name
);
let mut value = FieldValue::from(value?)
// if the value is an empty string, just skip it.
.ensure_nonempty()?;

if &*name == "spawn.location" {
value = value.truncate_registry_path();
}

Some(Field { name, value })
})
.collect();

Expand Down Expand Up @@ -494,4 +507,12 @@ impl FieldValue {
};
FieldValue::Debug(s)
}

/// If `self` is an empty string, returns `None`. Otherwise, returns `Some(self)`.
fn ensure_nonempty(self) -> Option<Self> {
match self {
FieldValue::Debug(s) | FieldValue::Str(s) if s.is_empty() => None,
val => Some(val),
}
}
}

0 comments on commit 41ae55a

Please sign in to comment.