|
| 1 | +//! Define the ancestry of an event or span. |
| 2 | +//! |
| 3 | +//! See the documentation on the [`Ancestry`] enum for further details. |
| 4 | +
|
| 5 | +use tracing_core::{ |
| 6 | + span::{self, Attributes}, |
| 7 | + Event, |
| 8 | +}; |
| 9 | + |
| 10 | +/// The ancestry of an event or span. |
| 11 | +/// |
| 12 | +/// An event or span can have an explicitly assigned parent, or be an explicit root. Otherwise, |
| 13 | +/// an event or span may have a contextually assigned parent or in the final case will be a |
| 14 | +/// contextual root. |
| 15 | +#[derive(Debug, Eq, PartialEq)] |
| 16 | +pub enum Ancestry { |
| 17 | + /// The event or span has an explicitly assigned parent (created with `parent: span_id`) with |
| 18 | + /// the specified name. |
| 19 | + HasExplicitParent(String), |
| 20 | + /// The event or span is an explicitly defined root. It was created with `parent: None` and |
| 21 | + /// has no parent. |
| 22 | + IsExplicitRoot, |
| 23 | + /// The event or span has a contextually assigned parent with the specified name. It has no |
| 24 | + /// explicitly assigned parent, nor has it been explicitly defined as a root (it was created |
| 25 | + /// without the `parent:` directive). There was a span in context when this event or span was |
| 26 | + /// created. |
| 27 | + HasContextualParent(String), |
| 28 | + /// The event or span is a contextual root. It has no explicitly assigned parent, nor has it |
| 29 | + /// been explicitly defined as a root (it was created without the `parent:` directive). |
| 30 | + /// Additionally, no span was in context when this event or span was created. |
| 31 | + IsContextualRoot, |
| 32 | +} |
| 33 | + |
| 34 | +impl Ancestry { |
| 35 | + #[track_caller] |
| 36 | + pub(crate) fn check( |
| 37 | + &self, |
| 38 | + actual_ancestry: &Ancestry, |
| 39 | + ctx: impl std::fmt::Display, |
| 40 | + collector_name: &str, |
| 41 | + ) { |
| 42 | + let expected_description = |ancestry: &Ancestry| match ancestry { |
| 43 | + Self::IsExplicitRoot => "be an explicit root".to_string(), |
| 44 | + Self::HasExplicitParent(name) => format!("have an explicit parent with name='{name}'"), |
| 45 | + Self::IsContextualRoot => "be a contextual root".to_string(), |
| 46 | + Self::HasContextualParent(name) => { |
| 47 | + format!("have a contextual parent with name='{name}'") |
| 48 | + } |
| 49 | + }; |
| 50 | + |
| 51 | + let actual_description = |ancestry: &Ancestry| match ancestry { |
| 52 | + Self::IsExplicitRoot => "was actually an explicit root".to_string(), |
| 53 | + Self::HasExplicitParent(name) => { |
| 54 | + format!("actually has an explicit parent with name='{name}'") |
| 55 | + } |
| 56 | + Self::IsContextualRoot => "was actually a contextual root".to_string(), |
| 57 | + Self::HasContextualParent(name) => { |
| 58 | + format!("actually has a contextual parent with name='{name}'") |
| 59 | + } |
| 60 | + }; |
| 61 | + |
| 62 | + assert_eq!( |
| 63 | + self, |
| 64 | + actual_ancestry, |
| 65 | + "[{collector_name}] expected {ctx} to {expected_description}, but {actual_description}", |
| 66 | + expected_description = expected_description(self), |
| 67 | + actual_description = actual_description(actual_ancestry) |
| 68 | + ); |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +pub(crate) trait HasAncestry { |
| 73 | + fn is_contextual(&self) -> bool; |
| 74 | + |
| 75 | + fn is_root(&self) -> bool; |
| 76 | + |
| 77 | + fn parent(&self) -> Option<&span::Id>; |
| 78 | +} |
| 79 | + |
| 80 | +impl HasAncestry for &Event<'_> { |
| 81 | + fn is_contextual(&self) -> bool { |
| 82 | + (self as &Event<'_>).is_contextual() |
| 83 | + } |
| 84 | + |
| 85 | + fn is_root(&self) -> bool { |
| 86 | + (self as &Event<'_>).is_root() |
| 87 | + } |
| 88 | + |
| 89 | + fn parent(&self) -> Option<&span::Id> { |
| 90 | + (self as &Event<'_>).parent() |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | +impl HasAncestry for &Attributes<'_> { |
| 95 | + fn is_contextual(&self) -> bool { |
| 96 | + (self as &Attributes<'_>).is_contextual() |
| 97 | + } |
| 98 | + |
| 99 | + fn is_root(&self) -> bool { |
| 100 | + (self as &Attributes<'_>).is_root() |
| 101 | + } |
| 102 | + |
| 103 | + fn parent(&self) -> Option<&span::Id> { |
| 104 | + (self as &Attributes<'_>).parent() |
| 105 | + } |
| 106 | +} |
| 107 | + |
| 108 | +/// Determines the ancestry of an actual span or event. |
| 109 | +/// |
| 110 | +/// The rules for determining the ancestry are as follows: |
| 111 | +/// |
| 112 | +/// +------------+--------------+-----------------+---------------------+ |
| 113 | +/// | Contextual | Current Span | Explicit Parent | Ancestry | |
| 114 | +/// +------------+--------------+-----------------+---------------------+ |
| 115 | +/// | Yes | Yes | - | HasContextualParent | |
| 116 | +/// | Yes | No | - | IsContextualRoot | |
| 117 | +/// | No | - | Yes | HasExplicitParent | |
| 118 | +/// | No | - | No | IsExplicitRoot | |
| 119 | +/// +------------+--------------+-----------------+---------------------+ |
| 120 | +pub(crate) fn get_ancestry( |
| 121 | + item: impl HasAncestry, |
| 122 | + lookup_current: impl FnOnce() -> Option<span::Id>, |
| 123 | + span_name: impl FnOnce(&span::Id) -> Option<&str>, |
| 124 | +) -> Ancestry { |
| 125 | + if item.is_contextual() { |
| 126 | + if let Some(parent_id) = lookup_current() { |
| 127 | + let contextual_parent_name = span_name(&parent_id).expect( |
| 128 | + "tracing-mock: contextual parent cannot \ |
| 129 | + be looked up by ID. Was it recorded correctly?", |
| 130 | + ); |
| 131 | + Ancestry::HasContextualParent(contextual_parent_name.to_string()) |
| 132 | + } else { |
| 133 | + Ancestry::IsContextualRoot |
| 134 | + } |
| 135 | + } else if item.is_root() { |
| 136 | + Ancestry::IsExplicitRoot |
| 137 | + } else { |
| 138 | + let parent_id = item.parent().expect( |
| 139 | + "tracing-mock: is_contextual=false is_root=false \ |
| 140 | + but no explicit parent found. This is a bug!", |
| 141 | + ); |
| 142 | + let explicit_parent_name = span_name(parent_id).expect( |
| 143 | + "tracing-mock: explicit parent cannot be looked \ |
| 144 | + up by ID. Is the provided Span ID valid: {parent_id}", |
| 145 | + ); |
| 146 | + Ancestry::HasExplicitParent(explicit_parent_name.to_string()) |
| 147 | + } |
| 148 | +} |
0 commit comments