-
Notifications
You must be signed in to change notification settings - Fork 347
Add decoded span assert rules #12088
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
Open
PerfectSlayer
wants to merge
2
commits into
bbujon/smoke-tests-decoded-spans
Choose a base branch
from
bbujon/smoke-tests-decoded-span-assertions
base: bbujon/smoke-tests-decoded-spans
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
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
173 changes: 173 additions & 0 deletions
173
dd-smoke-tests/src/main/java/datadog/smoketest/trace/SmokeTraceAssertions.java
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,173 @@ | ||
| package datadog.smoketest.trace; | ||
|
|
||
| import static java.lang.Long.MAX_VALUE; | ||
| import static java.lang.Math.min; | ||
| import static java.util.function.UnaryOperator.identity; | ||
| import static org.junit.jupiter.api.AssertionFailureBuilder.assertionFailure; | ||
|
|
||
| import datadog.trace.test.agent.decoder.DecodedSpan; | ||
| import datadog.trace.test.agent.decoder.DecodedTrace; | ||
| import java.util.ArrayList; | ||
| import java.util.BitSet; | ||
| import java.util.Comparator; | ||
| import java.util.List; | ||
| import java.util.function.UnaryOperator; | ||
|
|
||
| /** | ||
| * This class is a helper class to verify trace structure. | ||
| * | ||
| * <p>To check for trace structure, use the static factory methods: {@link #assertTraces(List, | ||
| * TraceMatcher...)} with the expected {@link TraceMatcher}s (one per trace), or {@link | ||
| * #assertTraces(List, UnaryOperator, TraceMatcher...)} to configure the checks with a {@link | ||
| * Options} object. | ||
| * | ||
| * <p>The following predefined configurations: | ||
| * | ||
| * <ul> | ||
| * <li>{@link #IGNORE_ADDITIONAL_TRACES} ignores additional traces if there are more than | ||
| * expected, | ||
| * <li>{@link #SORT_BY_START_TIME} sorts traces by their start time, | ||
| * <li>{@link #UNORDERED} allows matchers to match any distinct trace rather than the one at its | ||
| * position. | ||
| * </ul> | ||
| */ | ||
| public final class SmokeTraceAssertions { | ||
| /* | ||
| * Trace comparators. | ||
| */ | ||
| /** Trace comparator to sort by start time. */ | ||
| public static final Comparator<DecodedTrace> TRACE_START_TIME_COMPARATOR = | ||
| Comparator.comparingLong(SmokeTraceAssertions::earliestStart); | ||
|
|
||
| /* | ||
| * Trace assertion options. | ||
| */ | ||
| /** Ignores additional traces. If there are more traces than expected, do not fail. */ | ||
| public static final UnaryOperator<Options> IGNORE_ADDITIONAL_TRACES = | ||
| Options::ignoreAdditionalTraces; | ||
|
|
||
| /** Allows matchers to match any distinct trace rather than the one at its position. */ | ||
| public static final UnaryOperator<Options> UNORDERED = Options::unorder; | ||
|
|
||
| /** Sorts traces by start time. */ | ||
| public static final UnaryOperator<Options> SORT_BY_START_TIME = | ||
| options -> options.sort(TRACE_START_TIME_COMPARATOR); | ||
|
|
||
| private SmokeTraceAssertions() {} | ||
|
|
||
| /** | ||
| * Checks the structure of a trace collection. | ||
| * | ||
| * @param traces The trace collection to check. | ||
| * @param matchers The matchers to verify the trace collection, one matcher by expected trace. | ||
| */ | ||
| public static void assertTraces(List<DecodedTrace> traces, TraceMatcher... matchers) { | ||
| assertTraces(traces, identity(), matchers); | ||
| } | ||
|
|
||
| /** | ||
| * Checks the structure of a trace collection. | ||
| * | ||
| * @param traces The trace collection to check. | ||
| * @param options The {@link Options} to configure the checks. | ||
| * @param matchers The matchers to verify the trace collection, one matcher by expected trace. | ||
| */ | ||
| public static void assertTraces( | ||
| List<DecodedTrace> traces, UnaryOperator<Options> options, TraceMatcher... matchers) { | ||
| Options opts = options.apply(new Options()); | ||
| // Check trace count first | ||
| int traceCount = traces.size(); | ||
| if (opts.ignoreAdditionalTraces) { | ||
| if (traceCount < matchers.length) { | ||
| assertionFailure() | ||
| .message("Not enough of traces") | ||
| .expected(matchers.length) | ||
| .actual(traceCount) | ||
| .buildAndThrow(); | ||
| } | ||
| } else { | ||
| if (traceCount != matchers.length) { | ||
| assertionFailure() | ||
| .message("Invalid number of traces") | ||
| .expected(matchers.length) | ||
| .actual(traceCount) | ||
| .buildAndThrow(); | ||
| } | ||
| } | ||
| // Apply sorter | ||
| if (opts.sorter != null) { | ||
| traces = new ArrayList<>(traces); | ||
| traces.sort(opts.sorter); | ||
| } | ||
| // Assert traces | ||
| boolean strictPositional = !opts.unordered && !opts.ignoreAdditionalTraces; | ||
| BitSet skippedTraces = new BitSet(traceCount); | ||
| for (int matcherIndex = 0; matcherIndex < matchers.length; matcherIndex++) { | ||
| TraceMatcher matcher = matchers[matcherIndex]; | ||
| if (strictPositional) { | ||
| matcher.assertTrace(traces.get(matcherIndex).getSpans(), matcherIndex); | ||
| continue; | ||
| } | ||
| boolean matched = false; | ||
| for (int traceIndex = skippedTraces.nextClearBit(0); traceIndex < traceCount; traceIndex++) { | ||
| if (skippedTraces.get(traceIndex)) { | ||
| continue; | ||
| } | ||
| try { | ||
| matcher.assertTrace(traces.get(traceIndex).getSpans(), matcherIndex); | ||
| matched = true; | ||
| if (opts.unordered) { | ||
| skippedTraces.set(traceIndex); | ||
| } else { | ||
| skippedTraces.set(0, traceIndex + 1); | ||
| } | ||
| break; | ||
| } catch (AssertionError ignored) { | ||
| // Swallow assertion errors, keep looking for a match | ||
| } | ||
| } | ||
| if (!matched) { | ||
| assertionFailure().message("No trace matches matcher # " + matcherIndex).buildAndThrow(); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private static long earliestStart(DecodedTrace trace) { | ||
| long start = MAX_VALUE; | ||
| for (DecodedSpan span : trace.getSpans()) { | ||
| start = min(start, span.getStart()); | ||
| } | ||
| return start == MAX_VALUE ? 0L : start; | ||
| } | ||
|
|
||
| public static final class Options { | ||
| boolean ignoreAdditionalTraces = false; | ||
| boolean unordered = false; | ||
| Comparator<DecodedTrace> sorter = null; | ||
|
|
||
| public Options ignoreAdditionalTraces() { | ||
| this.ignoreAdditionalTraces = true; | ||
| return this; | ||
| } | ||
|
|
||
| /** | ||
| * Matches each matcher against any distinct trace rather than the one at its position. | ||
| * | ||
| * <p>Assignment is greedy: each matcher takes the first still-unmatched trace it accepts. This | ||
| * can spuriously fail when matchers accept overlapping sets of traces and a valid distinct | ||
| * assignment exists only under a different pairing. Keep matchers specific enough to be | ||
| * unambiguous. | ||
| */ | ||
| public Options unorder() { | ||
| this.unordered = true; | ||
| this.sorter = null; | ||
| return this; | ||
| } | ||
|
|
||
| public Options sort(Comparator<DecodedTrace> sorter) { | ||
| this.unordered = false; | ||
| this.sorter = sorter; | ||
| return this; | ||
| } | ||
| } | ||
| } | ||
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.
Uh oh!
There was an error while loading. Please reload this page.