-
Notifications
You must be signed in to change notification settings - Fork 731
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
mock: add
ExpectedId
to link span expectations (#3007)
It currently isn't possible to differentiate spans with the same name, target, and level when setting expectations on `enter`, `exit`, and `drop_span`. This is not an issue for `tracing-mock`'s original (and still primary) use case, which is to test `tracing` itself. However, when testing the tracing instrumentation in library or application code, this can be a limitation. For example, when testing the instrumentation in tokio (tokio-rs/tokio#6112), it isn't possible to set an expectation on which task span is entered first, because the name, target, and level of those spans are always identical - in fact, the spans have the same metadata and only the field values are different. To make differentiating different spans possible, `ExpectId` has been introduced. It is an opaque struct which represents a `span::Id` and can be used to match spans from a `new_span` expectation (where a `NewSpan` is accepted and all fields and values can be expected) through to subsequent `enter`, `exit`, and `drop_span` expectations. An `ExpectedId` is passed to an `ExpectedSpan` which then needs to be expected with `MockCollector::new_span`. A clone of the `ExpectedId` (or a clone of the `ExpectedSpan` with the `ExpectedId` already on it) will then match the ID assigned to the span to the other span lifecycle expectations. The `ExpectedId` uses an `Arc<AtomicU64>` which has the ID for the new span assigned to it, and then its clones will be matched against that same ID. In future changes it will also be possible to use this `ExpectedId` to match parent spans, currently a parent is only matched by name.
- Loading branch information
Showing
14 changed files
with
1,353 additions
and
356 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,148 @@ | ||
//! Define the ancestry of an event or span. | ||
//! | ||
//! See the documentation on the [`Ancestry`] enum for further details. | ||
use tracing_core::{ | ||
span::{self, Attributes}, | ||
Event, | ||
}; | ||
|
||
/// The ancestry of an event or span. | ||
/// | ||
/// An event or span can have an explicitly assigned parent, or be an explicit root. Otherwise, | ||
/// an event or span may have a contextually assigned parent or in the final case will be a | ||
/// contextual root. | ||
#[derive(Debug, Eq, PartialEq)] | ||
pub enum Ancestry { | ||
/// The event or span has an explicitly assigned parent (created with `parent: span_id`) with | ||
/// the specified name. | ||
HasExplicitParent(String), | ||
/// The event or span is an explicitly defined root. It was created with `parent: None` and | ||
/// has no parent. | ||
IsExplicitRoot, | ||
/// The event or span has a contextually assigned parent with the specified name. It has no | ||
/// explicitly assigned parent, nor has it been explicitly defined as a root (it was created | ||
/// without the `parent:` directive). There was a span in context when this event or span was | ||
/// created. | ||
HasContextualParent(String), | ||
/// The event or span is a contextual root. It has no explicitly assigned parent, nor has it | ||
/// been explicitly defined as a root (it was created without the `parent:` directive). | ||
/// Additionally, no span was in context when this event or span was created. | ||
IsContextualRoot, | ||
} | ||
|
||
impl Ancestry { | ||
#[track_caller] | ||
pub(crate) fn check( | ||
&self, | ||
actual_ancestry: &Ancestry, | ||
ctx: impl std::fmt::Display, | ||
collector_name: &str, | ||
) { | ||
let expected_description = |ancestry: &Ancestry| match ancestry { | ||
Self::IsExplicitRoot => "be an explicit root".to_string(), | ||
Self::HasExplicitParent(name) => format!("have an explicit parent with name='{name}'"), | ||
Self::IsContextualRoot => "be a contextual root".to_string(), | ||
Self::HasContextualParent(name) => { | ||
format!("have a contextual parent with name='{name}'") | ||
} | ||
}; | ||
|
||
let actual_description = |ancestry: &Ancestry| match ancestry { | ||
Self::IsExplicitRoot => "was actually an explicit root".to_string(), | ||
Self::HasExplicitParent(name) => { | ||
format!("actually has an explicit parent with name='{name}'") | ||
} | ||
Self::IsContextualRoot => "was actually a contextual root".to_string(), | ||
Self::HasContextualParent(name) => { | ||
format!("actually has a contextual parent with name='{name}'") | ||
} | ||
}; | ||
|
||
assert_eq!( | ||
self, | ||
actual_ancestry, | ||
"[{collector_name}] expected {ctx} to {expected_description}, but {actual_description}", | ||
expected_description = expected_description(self), | ||
actual_description = actual_description(actual_ancestry) | ||
); | ||
} | ||
} | ||
|
||
pub(crate) trait HasAncestry { | ||
fn is_contextual(&self) -> bool; | ||
|
||
fn is_root(&self) -> bool; | ||
|
||
fn parent(&self) -> Option<&span::Id>; | ||
} | ||
|
||
impl HasAncestry for &Event<'_> { | ||
fn is_contextual(&self) -> bool { | ||
(self as &Event<'_>).is_contextual() | ||
} | ||
|
||
fn is_root(&self) -> bool { | ||
(self as &Event<'_>).is_root() | ||
} | ||
|
||
fn parent(&self) -> Option<&span::Id> { | ||
(self as &Event<'_>).parent() | ||
} | ||
} | ||
|
||
impl HasAncestry for &Attributes<'_> { | ||
fn is_contextual(&self) -> bool { | ||
(self as &Attributes<'_>).is_contextual() | ||
} | ||
|
||
fn is_root(&self) -> bool { | ||
(self as &Attributes<'_>).is_root() | ||
} | ||
|
||
fn parent(&self) -> Option<&span::Id> { | ||
(self as &Attributes<'_>).parent() | ||
} | ||
} | ||
|
||
/// Determines the ancestry of an actual span or event. | ||
/// | ||
/// The rules for determining the ancestry are as follows: | ||
/// | ||
/// +------------+--------------+-----------------+---------------------+ | ||
/// | Contextual | Current Span | Explicit Parent | Ancestry | | ||
/// +------------+--------------+-----------------+---------------------+ | ||
/// | Yes | Yes | - | HasContextualParent | | ||
/// | Yes | No | - | IsContextualRoot | | ||
/// | No | - | Yes | HasExplicitParent | | ||
/// | No | - | No | IsExplicitRoot | | ||
/// +------------+--------------+-----------------+---------------------+ | ||
pub(crate) fn get_ancestry( | ||
item: impl HasAncestry, | ||
lookup_current: impl FnOnce() -> Option<span::Id>, | ||
span_name: impl FnOnce(&span::Id) -> Option<&str>, | ||
) -> Ancestry { | ||
if item.is_contextual() { | ||
if let Some(parent_id) = lookup_current() { | ||
let contextual_parent_name = span_name(&parent_id).expect( | ||
"tracing-mock: contextual parent cannot \ | ||
be looked up by ID. Was it recorded correctly?", | ||
); | ||
Ancestry::HasContextualParent(contextual_parent_name.to_string()) | ||
} else { | ||
Ancestry::IsContextualRoot | ||
} | ||
} else if item.is_root() { | ||
Ancestry::IsExplicitRoot | ||
} else { | ||
let parent_id = item.parent().expect( | ||
"tracing-mock: is_contextual=false is_root=false \ | ||
but no explicit parent found. This is a bug!", | ||
); | ||
let explicit_parent_name = span_name(parent_id).expect( | ||
"tracing-mock: explicit parent cannot be looked \ | ||
up by ID. Is the provided Span ID valid: {parent_id}", | ||
); | ||
Ancestry::HasExplicitParent(explicit_parent_name.to_string()) | ||
} | ||
} |
Oops, something went wrong.