-
Notifications
You must be signed in to change notification settings - Fork 754
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
mock: correct contextual/explicit parent assertions #2812
Closed
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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 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.
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.
it seems like the fact that both of these methods can be called on an
ExpectedSpan
creates some ambiguity in thetracing-mock
API surface. what do you think about getting rid ofwith_contextual_parent()
andwith_explicit_parent()
, and replacing them with a singlewith_parent()
method taking aParent
enum? something like this:this way, the user can no longer construct a span that expects both a contextual parent and an explicit parent at the same time.
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 started doing this, but the change is huge. It also has some unfortunate side-efffects (at lesat the way that I implemented it). Because then we'll need:
Which starts to get a bit long. Also, exposing the
Parent
enum makes everything longer too.I think that there is room for improvement here, but I would like to look at it in a separate PR so as not to make this one too large. What do you think?