1717import java .util .regex .Pattern ;
1818
1919/**
20- * Thin span matcher for smoke tests, operating on the <em>serialized</em> {@link DecodedSpan} model
21- * produced by both smoke backends (the in-process mock agent and the dd-apm-test-agent).
20+ * The class is a helper class to verify span attributes on the {@link DecodedSpan} model produced
21+ * by both smoke backends (the in-process mock agent and the dd-apm-test-agent).
2222 *
23- * <p>This is deliberately separate from the in-process instrumentation DSL ({@code
24- * datadog.trace.agent.test.assertions}, which matches on {@code DDSpan}); the two share only the
25- * generic value matchers in {@code datadog.trace.junit.utils.assertions}. Only what a span carries
26- * once serialized is matchable here: name/service/resource/type, error status, {@code meta} (string
27- * tags), {@code metrics} (numeric tags), {@code meta_struct} (nested structured data), and parent
28- * linkage.
23+ * <p>To get a {@code SpanMatcher}, use the static factory method {@link #span()} and use it as a
24+ * fluent builder to define the span matching constraints.
2925 *
30- * <p>Parent linkage can be expressed by {@link #root()}, {@link #childOf(long) explicit id}, or —
31- * for structural assertions within a sorted trace — by position with {@link #childOfIndex(int)} /
32- * {@link #childOfPrevious()} (mirroring the instrumentation DSL). Use the {@link #span()} factory
33- * as a fluent builder; unset constraints are not checked.
26+ * <p>Span matching constraints cover only what a span carries once serialized:
27+ *
28+ * <ul>
29+ * <li>span service name with {@link #service(String)}
30+ * <li>span operation name with {@link #operationName(String)} and {@link #operationName(Pattern)}
31+ * <li>span resource name with {@link #resourceName(String)}, {@link #resourceName(Pattern)}, and
32+ * {@link #resourceName(Predicate)}
33+ * <li>span type with {@link #type(String)}
34+ * <li>span error status with {@link #error(boolean)}
35+ * <li>span parent linkage with {@link #root()}, {@link #childOf(long)}, {@link
36+ * #childOfIndex(int)}, and {@link #childOfPrevious()}
37+ * <li>span meta (string) tags with {@link #tag(String, Matcher)} and {@link #tag(String, String)}
38+ * <li>span metrics (numeric) tags with {@link #metric(String, Matcher)}
39+ * <li>span meta_struct (nested structured data) with {@link #metaStruct(String, Matcher)}
40+ * </ul>
3441 */
3542public final class SpanMatcher {
3643 private Matcher <String > serviceMatcher ;
@@ -53,98 +60,203 @@ private SpanMatcher() {
5360 this .metaStructMatchers = new HashMap <>();
5461 }
5562
56- /** Creates a new span matcher; configure it with the fluent methods below. */
63+ /**
64+ * Checks a span and its attributes.
65+ *
66+ * @return A new {@link SpanMatcher} instance to configure span matching constraints.
67+ */
5768 public static SpanMatcher span () {
5869 return new SpanMatcher ();
5970 }
6071
72+ /**
73+ * Checks the span service name matches the given value.
74+ *
75+ * @param service The service name to match against.
76+ * @return The current {@link SpanMatcher} instance updated with the specified service name
77+ * constraint.
78+ */
6179 public SpanMatcher service (String service ) {
6280 this .serviceMatcher = is (service );
6381 return this ;
6482 }
6583
84+ /**
85+ * Checks the span operation name matches the given value.
86+ *
87+ * @param operationName The operation name to match against.
88+ * @return The current {@link SpanMatcher} instance updated with the specified operation name
89+ * constraint.
90+ */
6691 public SpanMatcher operationName (String operationName ) {
6792 this .operationNameMatcher = is (operationName );
6893 return this ;
6994 }
7095
96+ /**
97+ * Checks the span operation name matches the provided regular expression pattern.
98+ *
99+ * @param pattern The {@link Pattern} to match the operation name against.
100+ * @return The current {@link SpanMatcher} instance updated with the specified operation name
101+ * constraint.
102+ */
71103 public SpanMatcher operationName (Pattern pattern ) {
72104 this .operationNameMatcher = matches (pattern );
73105 return this ;
74106 }
75107
108+ /**
109+ * Checks the span resource name matches the given value.
110+ *
111+ * @param resourceName The resource name to match against.
112+ * @return The current {@link SpanMatcher} instance updated with the specified resource name
113+ * constraint.
114+ */
76115 public SpanMatcher resourceName (String resourceName ) {
77116 this .resourceNameMatcher = is (resourceName );
78117 return this ;
79118 }
80119
120+ /**
121+ * Checks the span resource name matches the provided regular expression pattern.
122+ *
123+ * @param pattern The {@link Pattern} used to match the resource name against.
124+ * @return The current {@link SpanMatcher} instance updated with the specified resource name
125+ * constraint.
126+ */
81127 public SpanMatcher resourceName (Pattern pattern ) {
82128 this .resourceNameMatcher = matches (pattern );
83129 return this ;
84130 }
85131
132+ /**
133+ * Checks the span resource name matches the provided validator.
134+ *
135+ * @param validator The {@link Predicate} used to validate the resource name.
136+ * @return The current {@link SpanMatcher} instance updated with the specified resource name
137+ * constraint.
138+ */
86139 public SpanMatcher resourceName (Predicate <CharSequence > validator ) {
87140 this .resourceNameMatcher = validates (validator );
88141 return this ;
89142 }
90143
144+ /**
145+ * Checks the span type matches the given value.
146+ *
147+ * @param type The span type to match against.
148+ * @return The current {@link SpanMatcher} instance updated with the specified span type
149+ * constraint.
150+ */
91151 public SpanMatcher type (String type ) {
92152 this .typeMatcher = is (type );
93153 return this ;
94154 }
95155
96- /** Matches the span error status. */
156+ /**
157+ * Checks the span error status matches the given value.
158+ *
159+ * @param errored The expected error status.
160+ * @return The current {@link SpanMatcher} instance updated with the specified error constraint.
161+ */
97162 public SpanMatcher error (boolean errored ) {
98163 this .errorMatcher = errored ? isTrue () : isFalse ();
99164 return this ;
100165 }
101166
102- /** Matches a root span (no parent). */
167+ /**
168+ * Checks the span is a root span (i.e., a span with no parent).
169+ *
170+ * @return The current {@link SpanMatcher} instance with the root constraint applied.
171+ */
103172 public SpanMatcher root () {
104173 this .parentIdMatcher = is (0L );
105174 this .parentSpanIndex = -1 ;
106175 return this ;
107176 }
108177
109- /** Matches a span whose parent is the given span id. */
178+ /**
179+ * Checks the span is a direct child of the specified parent span.
180+ *
181+ * @param parentSpanId The identifier of the parent span to match against.
182+ * @return The current {@link SpanMatcher} instance with the child-of constraint applied.
183+ */
110184 public SpanMatcher childOf (long parentSpanId ) {
111185 this .parentIdMatcher = is (parentSpanId );
112186 this .parentSpanIndex = -1 ;
113187 return this ;
114188 }
115189
116- /** Matches a span whose parent is the span at {@code parentSpanIndex} in the (sorted) trace. */
190+ /**
191+ * Checks the span is a direct child of the span at the specified index in the trace.
192+ *
193+ * @param parentSpanIndex The index of the parent span in the trace.
194+ * @return The current {@link SpanMatcher} instance with the child-of constraint applied.
195+ */
117196 public SpanMatcher childOfIndex (int parentSpanIndex ) {
118197 this .parentIdMatcher = null ;
119198 this .parentSpanIndex = parentSpanIndex ;
120199 return this ;
121200 }
122201
123- /** Matches a span whose parent is the immediately preceding span in the (sorted) trace. */
202+ /**
203+ * Checks the span is a direct child of the immediately preceding span in the trace.
204+ *
205+ * @return The current {@link SpanMatcher} instance with the child-of constraint applied.
206+ */
124207 public SpanMatcher childOfPrevious () {
125208 this .parentIdMatcher = CHILD_OF_PREVIOUS_MATCHER ;
126209 this .parentSpanIndex = -1 ;
127210 return this ;
128211 }
129212
130- /** Matches a {@code meta} (string) tag against the given matcher. */
213+ /**
214+ * Checks the span meta (string) tag matches the given matcher.
215+ *
216+ * @param name The name of the meta tag to match against.
217+ * @param matcher The matcher to check the meta tag value.
218+ * @return The current {@link SpanMatcher} instance updated with the specified meta tag
219+ * constraint.
220+ */
131221 public SpanMatcher tag (String name , Matcher <String > matcher ) {
132222 this .metaMatchers .put (name , matcher );
133223 return this ;
134224 }
135225
136- /** Matches a {@code meta} (string) tag against the given value. */
226+ /**
227+ * Checks the span meta (string) tag matches the given value.
228+ *
229+ * @param name The name of the meta tag to match against.
230+ * @param value The expected meta tag value.
231+ * @return The current {@link SpanMatcher} instance updated with the specified meta tag
232+ * constraint.
233+ */
137234 public SpanMatcher tag (String name , String value ) {
138235 this .metaMatchers .put (name , is (value ));
139236 return this ;
140237 }
141238
142- /** Matches a {@code metrics} (numeric) tag against the given matcher. */
239+ /**
240+ * Checks the span metric (numeric) tag matches the given matcher.
241+ *
242+ * @param name The name of the metric tag to match against.
243+ * @param matcher The matcher to check the metric tag value.
244+ * @return The current {@link SpanMatcher} instance updated with the specified metric tag
245+ * constraint.
246+ */
143247 public SpanMatcher metric (String name , Matcher <Number > matcher ) {
144248 this .metricMatchers .put (name , matcher );
145249 return this ;
146250 }
147251
252+ /**
253+ * Checks the span meta_struct entry matches the given matcher.
254+ *
255+ * @param name The name of the meta_struct entry to match against.
256+ * @param matcher The matcher to check the meta_struct entry value.
257+ * @return The current {@link SpanMatcher} instance updated with the specified meta_struct
258+ * constraint.
259+ */
148260 public SpanMatcher metaStruct (String name , Matcher <?> matcher ) {
149261 this .metaStructMatchers .put (name , matcher );
150262 return this ;
@@ -196,7 +308,9 @@ private void assertSpanMetaStruct(Map<String, Object> metaStruct) {
196308 for (Entry <String , Matcher <?>> entry : this .metaStructMatchers .entrySet ()) {
197309 String key = entry .getKey ();
198310 assertValue (
199- (Matcher <Object >) entry .getValue (), metaStruct .get (key ), "Unexpected meta_struct '" + key + "'" );
311+ (Matcher <Object >) entry .getValue (),
312+ metaStruct .get (key ),
313+ "Unexpected meta_struct '" + key + "'" );
200314 }
201315 }
202316}
0 commit comments