Skip to content

Commit 594a32e

Browse files
hawkwkaffarell
authored andcommitted
core: change metadata::Kind to a bitflag (tokio-rs#1891)
This changes the `Kind` type to a bitflag, in order to represent callsites that are hints but also count as spans or events. The goal here is to allow variants of the `enabled!` macro that specifically check if a span would be enabled, or if an event would be enabled. See [this comment][1] for details. This does not actually implement the `enabled!` variants, just the `Kind` representation change. This way, we can add to the `enabled!` macro in a subsequent `tracing` release without having to change `tracing-core` again. I went with the bitflag representation rather than adding a bool to the `KindInner::Span` and `KindInner::Event` enum variants because it felt a bit simpler and maybe more performant, although I don't think it's particularly important to micro-optimize here. I'd consider changing this to an enum-based representation if people think it's significantly easier to understand. [1]: tokio-rs#1821 (comment)
1 parent 4d22437 commit 594a32e

File tree

1 file changed

+45
-2
lines changed

1 file changed

+45
-2
lines changed

tracing-core/src/metadata.rs

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -385,7 +385,7 @@ impl Kind {
385385
/// `enabled!` callsite. [`Subscriber`][`crate::subscriber::Subscriber`]s can assume
386386
/// this `Kind` means they will never recieve a
387387
/// full event with this [`Metadata`].
388-
pub const HINT: Kind = Kind(KindInner::Hint);
388+
pub const HINT: Kind = Kind(Self::HINT_BIT);
389389

390390
/// Return true if the callsite kind is `Span`
391391
pub fn is_span(&self) -> bool {
@@ -503,7 +503,50 @@ impl<'a> PartialEq for Metadata<'a> {
503503

504504
/// Return true if the callsite kind is `Hint`
505505
pub fn is_hint(&self) -> bool {
506-
matches!(self, Kind(KindInner::Hint))
506+
self.0 & Self::HINT_BIT == Self::HINT_BIT
507+
}
508+
509+
/// Sets that this `Kind` is a [hint](Self::HINT).
510+
///
511+
/// This can be called on [`SPAN`](Self::SPAN) and [`EVENT`](Self::EVENT)
512+
/// kinds to construct a hint callsite that also counts as a span or event.
513+
pub const fn hint(self) -> Self {
514+
Self(self.0 | Self::HINT_BIT)
515+
}
516+
}
517+
518+
impl fmt::Debug for Kind {
519+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
520+
f.write_str("Kind(")?;
521+
let mut has_bits = false;
522+
let mut write_bit = |name: &str| {
523+
if has_bits {
524+
f.write_str(" | ")?;
525+
}
526+
f.write_str(name)?;
527+
has_bits = true;
528+
Ok(())
529+
};
530+
531+
if self.is_event() {
532+
write_bit("EVENT")?;
533+
}
534+
535+
if self.is_span() {
536+
write_bit("SPAN")?;
537+
}
538+
539+
if self.is_hint() {
540+
write_bit("HINT")?;
541+
}
542+
543+
// if none of the expected bits were set, something is messed up, so
544+
// just print the bits for debugging purposes
545+
if !has_bits {
546+
write!(f, "{:#b}", self.0)?;
547+
}
548+
549+
f.write_str(")")
507550
}
508551
}
509552

0 commit comments

Comments
 (0)