-
Notifications
You must be signed in to change notification settings - Fork 847
mock: correct contextual/explicit parent assertions #3004
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
2885c46
mock: correct contextual/explicit parent assertions
hds 7896b85
Merge branch 'master' into hds/mock-refactor-with-ancestry
hds dc01dad
Merge branch 'master' into hds/mock-refactor-with-ancestry
hds 3cb2c16
Merge branch 'master' into hds/mock-refactor-with-ancestry
hds dbdf5f0
clarify `Ancestry` options in the enum
hds File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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,145 @@ | ||
| //! 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. Additionally, | ||
| /// it has no explicitly assigned parent. | ||
| HasContextualParent(String), | ||
| /// The event or span is a contextual root. It has no contextual parent and also has no | ||
| /// explicitly assigned parent. | ||
| 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 | | ||
| /// +------------+--------------+-----------------+---------------------+ | ||
|
Comment on lines
+112
to
+119
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. oh, this is very helpful! |
||
| 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()) | ||
| } | ||
| } | ||
This file contains hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not parsing this sentence right: are you trying to say it has no explicitly defined root..?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here I want to say that the span or event has no explicitly defined parent (which means the
parent:field in a span or event macro was not used). Instead, it has a parent that was contextually assigned.Does that make sense? I see how it's not super clear, I can have another go at rewriting it if it's still not making sense.