Repository navigation
Add span event assertions to smoke tests #12934
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鈥檒l occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
gh-worker-dd-mergequeue-cf854d
merged 3 commits into
master
from
bbujon/span-events-assert
Oct 22, 2026
Merged
Changes from all commits
Commits
Show all changes
3 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 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
99 changes: 99 additions & 0 deletions
99
dd-smoke-tests/src/main/java/datadog/smoketest/trace/SpanEventMatcher.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,99 @@ | ||
| package datadog.smoketest.trace; | ||
|
|
||
| import static datadog.trace.test.junit.utils.assertions.Matchers.assertValue; | ||
| import static datadog.trace.test.junit.utils.assertions.Matchers.is; | ||
| import static datadog.trace.test.junit.utils.assertions.Matchers.validates; | ||
| import static java.util.Collections.emptyMap; | ||
|
|
||
| import datadog.trace.test.agent.decoder.DecodedSpan; | ||
| import datadog.trace.test.agent.decoder.DecodedSpanEvent; | ||
| import datadog.trace.test.junit.utils.assertions.Matcher; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.concurrent.TimeUnit; | ||
|
|
||
| /** | ||
| * Provides matchers for decoded span events based on their properties such as name, time, and | ||
| * attributes. | ||
| */ | ||
| public final class SpanEventMatcher { | ||
| private final Matcher<String> nameMatcher; | ||
| private Matcher<Long> timeMatcher; | ||
| private Matcher<Map<String, Object>> attributesMatcher; | ||
|
|
||
| private SpanEventMatcher(Matcher<String> nameMatcher) { | ||
| this.nameMatcher = nameMatcher; | ||
| this.attributesMatcher = is(emptyMap()); | ||
| } | ||
|
|
||
| /** | ||
| * Creates a {@code SpanEventMatcher} that matches a span event with the given name. By default, | ||
| * the event must have no attribute and its time must be within the span start and end times. | ||
| * | ||
| * @param name The span event name to match against. | ||
| * @return A {@code SpanEventMatcher} that matches a span event with the given name. | ||
| */ | ||
| public static SpanEventMatcher event(String name) { | ||
| return new SpanEventMatcher(is(name)); | ||
| } | ||
|
|
||
| /** | ||
| * Checks the event time matches the given value. | ||
| * | ||
| * @param timestamp The event time to match against. | ||
| * @param unit The event time unit. | ||
| * @return The current {@link SpanEventMatcher} instance with the time constraint applied. | ||
| */ | ||
| public SpanEventMatcher time(long timestamp, TimeUnit unit) { | ||
| this.timeMatcher = is(unit.toNanos(timestamp)); | ||
| return this; | ||
| } | ||
|
|
||
| /** | ||
| * Checks the event time matches the given matcher. | ||
| * | ||
| * @param matcher The matcher to check the event time, in nanoseconds since the Unix epoch. | ||
| * @return The current {@link SpanEventMatcher} instance with the time constraint applied. | ||
| */ | ||
| public SpanEventMatcher time(Matcher<Long> matcher) { | ||
| this.timeMatcher = matcher; | ||
| return this; | ||
| } | ||
|
|
||
| /** | ||
| * Checks the event attributes match the given values. | ||
| * | ||
| * @param attributes The event attributes to match against, with values being {@link String}, | ||
| * {@link Boolean}, {@link Long}, {@link Double}, or a {@link List} of them. | ||
| * @return The current {@link SpanEventMatcher} instance with the attributes constraint applied. | ||
| */ | ||
| public SpanEventMatcher attributes(Map<String, Object> attributes) { | ||
| this.attributesMatcher = is(attributes); | ||
| return this; | ||
| } | ||
|
|
||
| /** | ||
| * Checks the event attributes match the given matcher. | ||
| * | ||
| * @param matcher The matcher to check the event attributes. | ||
| * @return The current {@link SpanEventMatcher} instance with the attributes constraint applied. | ||
| */ | ||
| public SpanEventMatcher attributes(Matcher<Map<String, Object>> matcher) { | ||
| this.attributesMatcher = matcher; | ||
| return this; | ||
| } | ||
|
|
||
| void assertEvent(DecodedSpan span, DecodedSpanEvent event, int eventIndex) { | ||
| String at = " (event #" + eventIndex + ")"; | ||
| Matcher<Long> timeMatcher = this.timeMatcher; | ||
| if (timeMatcher == null) { | ||
| long startTime = span.getStart(); | ||
| long endTime = startTime + span.getDuration(); | ||
| timeMatcher = validates(time -> startTime <= time && time <= endTime); | ||
| } | ||
| assertValue(this.nameMatcher, event.getName(), "Unexpected span event name" + at); | ||
| assertValue(timeMatcher, event.getTimeUnixNano(), "Unexpected span event time" + at); | ||
| assertValue( | ||
| this.attributesMatcher, event.getAttributes(), "Unexpected span event attributes" + at); | ||
| } | ||
| } |
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
34 changes: 34 additions & 0 deletions
34
...-agent-utils/decoder/src/main/java/datadog/trace/test/agent/decoder/DecodedSpanEvent.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,34 @@ | ||
| package datadog.trace.test.agent.decoder; | ||
|
|
||
| import java.util.List; | ||
| import java.util.Map; | ||
|
|
||
| /** | ||
| * This interface describes an event a decoded span carries, as the tracer serialized it. It is the | ||
| * wire-side counterpart of the tracer's {@code DDSpanEvent}. | ||
| * | ||
| * <p>Every accessor is non-null: absent attributes decode to an empty map. | ||
| */ | ||
| public interface DecodedSpanEvent { | ||
| /** | ||
| * Returns the event name. | ||
| * | ||
| * @return The event name. | ||
| */ | ||
| String getName(); | ||
|
|
||
| /** | ||
| * Returns the event time. | ||
| * | ||
| * @return The event time, in nanoseconds since the Unix epoch. | ||
| */ | ||
| long getTimeUnixNano(); | ||
|
|
||
| /** | ||
| * Returns the attributes attached to this event. | ||
| * | ||
| * @return The event attributes, with values being {@link String}, {@link Boolean}, {@link Long}, | ||
| * {@link Double}, or a {@link List} of them; empty if the tracer sent none. | ||
| */ | ||
| Map<String, Object> getAttributes(); | ||
| } |
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.
Uh oh!
There was an error while loading. Please reload this page.