Skip to content

Commit c27f0fd

Browse files
authored
core: change metadata::Kind to a bitflag (#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]: #1821 (comment)
1 parent 7e3f64f commit c27f0fd

File tree

1 file changed

+55
-15
lines changed

1 file changed

+55
-15
lines changed

tracing-core/src/metadata.rs

Lines changed: 55 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,8 @@ pub struct Metadata<'a> {
8989
}
9090

9191
/// Indicates whether the callsite is a span or event.
92-
#[derive(Clone, Debug, Eq, PartialEq)]
93-
pub struct Kind(KindInner);
92+
#[derive(Clone, Eq, PartialEq)]
93+
pub struct Kind(u8);
9494

9595
/// Describes the level of verbosity of a span or event.
9696
///
@@ -369,38 +369,78 @@ impl<'a> fmt::Debug for Metadata<'a> {
369369
}
370370
}
371371

372-
#[derive(Clone, Debug, Eq, PartialEq)]
373-
enum KindInner {
374-
Event,
375-
Span,
376-
Hint,
377-
}
378-
379372
impl Kind {
373+
const EVENT_BIT: u8 = 1 << 0;
374+
const SPAN_BIT: u8 = 1 << 1;
375+
const HINT_BIT: u8 = 1 << 2;
376+
380377
/// `Event` callsite
381-
pub const EVENT: Kind = Kind(KindInner::Event);
378+
pub const EVENT: Kind = Kind(Self::EVENT_BIT);
382379

383380
/// `Span` callsite
384-
pub const SPAN: Kind = Kind(KindInner::Span);
381+
pub const SPAN: Kind = Kind(Self::SPAN_BIT);
385382

386383
/// `enabled!` callsite. [`Collect`][`crate::collect::Collect`]s can assume
387384
/// this `Kind` means they will never recieve a
388385
/// full event with this [`Metadata`].
389-
pub const HINT: Kind = Kind(KindInner::Hint);
386+
pub const HINT: Kind = Kind(Self::HINT_BIT);
390387

391388
/// Return true if the callsite kind is `Span`
392389
pub fn is_span(&self) -> bool {
393-
matches!(self, Kind(KindInner::Span))
390+
self.0 & Self::SPAN_BIT == Self::SPAN_BIT
394391
}
395392

396393
/// Return true if the callsite kind is `Event`
397394
pub fn is_event(&self) -> bool {
398-
matches!(self, Kind(KindInner::Event))
395+
self.0 & Self::EVENT_BIT == Self::EVENT_BIT
399396
}
400397

401398
/// Return true if the callsite kind is `Hint`
402399
pub fn is_hint(&self) -> bool {
403-
matches!(self, Kind(KindInner::Hint))
400+
self.0 & Self::HINT_BIT == Self::HINT_BIT
401+
}
402+
403+
/// Sets that this `Kind` is a [hint](Self::HINT).
404+
///
405+
/// This can be called on [`SPAN`](Self::SPAN) and [`EVENT`](Self::EVENT)
406+
/// kinds to construct a hint callsite that also counts as a span or event.
407+
pub const fn hint(self) -> Self {
408+
Self(self.0 | Self::HINT_BIT)
409+
}
410+
}
411+
412+
impl fmt::Debug for Kind {
413+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
414+
f.write_str("Kind(")?;
415+
let mut has_bits = false;
416+
let mut write_bit = |name: &str| {
417+
if has_bits {
418+
f.write_str(" | ")?;
419+
}
420+
f.write_str(name)?;
421+
has_bits = true;
422+
Ok(())
423+
};
424+
425+
if self.is_event() {
426+
write_bit("EVENT")?;
427+
}
428+
429+
if self.is_span() {
430+
write_bit("SPAN")?;
431+
}
432+
433+
if self.is_hint() {
434+
write_bit("HINT")?;
435+
}
436+
437+
// if none of the expected bits were set, something is messed up, so
438+
// just print the bits for debugging purposes
439+
if !has_bits {
440+
write!(f, "{:#b}", self.0)?;
441+
}
442+
443+
f.write_str(")")
404444
}
405445
}
406446

0 commit comments

Comments
 (0)