-
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: correct contextual/explicit parent assertions
When recording the parent of an event or span, the `MockCollector` treats an explicit parent of `None` (i.e. an event or span that is an explicit root) in the same way as if there is no explicit root. This leads to it picking up the contextual parent or treating the event or span as a contextual root. This change refactors the recording of the parent to use `is_contextual` to distinguish whether or not an explicit parent has been specified. The actual parent is also written into a `Parent` enum so that the expected and actual values can be compared in a more explicit way. Additionally, the `Parent` struct has been moved into its own module and the check behavior has been fixed. The error message has also been unified across all cases. Given the number of different cases involved in checking parents, separate integration tests have been added to `tracing-mock` specifically for testing all the positive and negative cases when asserting on the parents of events and spans. There were two tests in `tracing-attributes` which specified both an explicit and a contextual parent. This behavior was never intended to work as all events and spans are either contextual or not. The tests have been corrected to only expect one of the two. Fixes: #2440
- Loading branch information
Showing
9 changed files
with
906 additions
and
156 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
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,50 @@ | ||
/// The parent of an event or span. | ||
/// | ||
/// This enum is used to represent the expected and the actual parent of an | ||
/// event or a span. | ||
#[derive(Debug, Eq, PartialEq)] | ||
pub(crate) enum Parent { | ||
/// The event or span is contextually a root - it has no parent. | ||
ContextualRoot, | ||
/// The event or span has a contextually assigned parent, with the specified name. | ||
Contextual(String), | ||
/// The event or span is explicitly a root, it was created with `parent: None`. | ||
ExplicitRoot, | ||
/// The event or span has an explicit parent with the specified name, it was created with | ||
/// `parent: span_id`. | ||
Explicit(String), | ||
} | ||
|
||
impl Parent { | ||
#[track_caller] | ||
pub(crate) fn check( | ||
&self, | ||
actual_parent: &Parent, | ||
ctx: impl std::fmt::Display, | ||
collector_name: &str, | ||
) { | ||
let expected_description = |parent: &Parent| match parent { | ||
Self::ExplicitRoot => "be an explicit root".to_string(), | ||
Self::Explicit(name) => format!("have an explicit parent with name='{name}'"), | ||
Self::ContextualRoot => "be a contextual root".to_string(), | ||
Self::Contextual(name) => format!("have a contextual parent with name='{name}'"), | ||
}; | ||
|
||
let actual_description = |parent: &Parent| match parent { | ||
Self::ExplicitRoot => "was actually an explicit root".to_string(), | ||
Self::Explicit(name) => format!("actually has an explicit parent with name='{name}'"), | ||
Self::ContextualRoot => "was actually a contextual root".to_string(), | ||
Self::Contextual(name) => { | ||
format!("actually has a contextual parent with name='{name}'") | ||
} | ||
}; | ||
|
||
assert_eq!( | ||
self, | ||
actual_parent, | ||
"[{collector_name}] expected {ctx} to {expected_description}, but {actual_description}", | ||
expected_description = expected_description(self), | ||
actual_description = actual_description(actual_parent) | ||
); | ||
} | ||
} |
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
Oops, something went wrong.