mock: correct contextual/explicit parent assertions #3004
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.
Motivation
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 anexplicit 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.
Solution
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 an
Ancestry
enum so that theexpected and actual values can be compared in a more explicit way.
Additionally, the
Ancestry
struct has been moved into its own module andthe check behavior has been fixed. The error message has also been
unified across all cases.
Another problem with the previous API is that the two methods
with_contextual_parent
andwith_explicit_parent
are actuallymutually exclusive, a span or event cannot be both of them. It is also a
(small) mental leap for the user to go from
with_*_parent(None)
tounderstanding that this means that a span or event is a root (either
contextual or explicit).
As such, the API has been reworked into a single method
with_ancestry
,which takes an enum with the following four variants:
HasExplicitParent(String)
(parent span name)IsExplicitRoot
HasContextualParent(String)
(parent span name)IsContextualRoot
To make the interface as useable as possible, helper functions have been
defined in the
expect
module which can be used to create the enumvariants. Specifically, these take
Into<String>
parameter for the spanname.
Given the number of different cases involved in checking ancestry,
separate integration tests have been added to
tracing-mock
specifically for testing all the positive and negative cases when
asserting on the ancestry of events and spans.
There were two tests in
tracing-attributes
which specified both anexplicit 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
PR details
This PR attempts to address the comments made on #2812.
Specifically to address this comment #2812 (comment)
by replacing
.with_explicit_parent()
and.with_contextual_parent()
methodswith a single
.with_ancestry()
method.To address this comment #2812 (comment)
the logic to pick the ancestry of an actual span or event has been centralised. This
required factoring out the logic to get the current span and to look up a span by ID,
since these are different between the
Running
(MockCollector
) and theMockSubscriber
structs.