Skip to content

Commit

Permalink
core: support chaining child queries
Browse files Browse the repository at this point in the history
Update matcher to make it more convenient to chain together
child queries to indicate a path.
  • Loading branch information
brharrington committed Mar 26, 2024
1 parent e4fefc1 commit dab4d29
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -86,12 +86,18 @@ object TraceVocabulary extends Vocabulary {
override def name: String = "child"

override protected def matcher: PartialFunction[List[Any], Boolean] = {
case (_: Query) :: (_: Query) :: _ => true
case (_: Query) :: (_: Query) :: _ => true
case (_: Query) :: (_: TraceQuery.Child) :: _ => true
case (_: Query) :: TraceQuery.SpanAnd(_, _: TraceQuery.Child) :: _ => true
}

override protected def executor: PartialFunction[List[Any], List[Any]] = {
case (q2: Query) :: (q1: Query) :: stack =>
TraceQuery.Child(q1, q2) :: stack
case (q2: Query) :: (tq: TraceQuery.Child) :: stack =>
TraceQuery.SpanAnd(tq, TraceQuery.Child(tq.q2, q2)) :: stack
case (q2: Query) :: (s @ TraceQuery.SpanAnd(_, tq: TraceQuery.Child)) :: stack =>
TraceQuery.SpanAnd(s, TraceQuery.Child(tq.q2, q2)) :: stack
}

override def signature: String = "q1:TraceQuery q2:TraceQuery -- TraceQuery"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,20 @@ class TraceMatcherSuite extends FunSuite {
)
}

/**
* a - b
* - c - e - g
* - f
* - d
*/
private val sampleTrace = List(
mkSpan("a", "1", null, 0L, 100L),
mkSpan("b", "2", "1", 1L, 25L),
mkSpan("c", "3", "1", 5L, 90L),
mkSpan("d", "4", "1", 15L, 50L),
mkSpan("e", "5", "3", 8L, 50L),
mkSpan("f", "6", "3", 55L, 20L)
mkSpan("f", "6", "3", 55L, 20L),
mkSpan("g", "7", "5", 4L, 5L)
)

private def checkMatch(query: TraceQuery, shouldMatch: Boolean): Unit = {
Expand Down Expand Up @@ -91,6 +98,17 @@ class TraceMatcherSuite extends FunSuite {
val q3 = ExprUtils.parseTraceEventsQuery("app,a,:eq,app,e,:eq,:child").q
checkMatch(q3, false)
}

test("transitive child") {
val q1 = ExprUtils.parseTraceEventsQuery("app,a,:eq,app,c,:eq,:child,app,e,:eq,:child").q
checkMatch(q1, true)

val q2 = ExprUtils.parseTraceEventsQuery("app,a,:eq,app,b,:eq,:child,app,e,:eq,:child").q
checkMatch(q2, false)

val q3 = ExprUtils.parseTraceEventsQuery("app,a,:eq,app,c,:eq,:child,app,e,:eq,:child,app,g,:eq,:child").q
checkMatch(q1, true)
}
}

object TraceMatcherSuite {
Expand Down

0 comments on commit dab4d29

Please sign in to comment.