Skip to content

Commit

Permalink
lwcapi: add test for splitting trace query (#1639)
Browse files Browse the repository at this point in the history
Ensure trace time series query will get split up into
representative data expressions correctly.
  • Loading branch information
brharrington authored Mar 29, 2024
1 parent be64e22 commit 14689d1
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,30 @@ class LwcToAggrDatapointSuite extends FunSuite {
assertEquals(countData.map(_.value).toSet, Set(4.0))
}

test("eval trace time series") {
val styleExpr = "name,cpu,:eq,:avg"
val tsExpr = s"app,foo,:eq,$styleExpr,:span-time-series"
def subExpr(n: String, e: String): String = {
s"""{"id":"$n","expression":"$e","frequency":$step}"""
}
val expr1 = subExpr("sum", "name,cpu,:eq,:sum")
val expr2 = subExpr("count", "name,cpu,:eq,:count")
val subv2 =
s"""{"type":"subscription-v2","expression":"$tsExpr","exprType":"TRACE_TIME_SERIES","metrics":[$expr1,$expr2]}"""
val results = eval(subv2 :: input.tail)
assertEquals(results.size, 8)

val groups = results.groupBy(_.expr)
assertEquals(groups.size, 2)

val sumData = groups(DataExpr.Sum(Query.Equal("name", "cpu")))
assertEquals(sumData.map(_.value).toSet, Set(1.0, 2.0, 3.0, 4.0))

val countData = groups(DataExpr.Count(Query.Equal("name", "cpu")))
assertEquals(countData.size, 4)
assertEquals(countData.map(_.value).toSet, Set(4.0))
}

test("diagnostic messages are logged") {
logMessages.clear()
eval(input)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import com.netflix.atlas.core.model.EventExpr
import com.netflix.atlas.core.model.Query
import com.netflix.atlas.core.model.Query.KeyQuery
import com.netflix.atlas.core.model.StyleExpr
import com.netflix.atlas.core.model.TraceQuery
import com.netflix.atlas.eval.model.ExprType
import com.netflix.atlas.eval.stream.ExprInterpreter
import com.netflix.spectator.ipc.ServerGroup
Expand Down Expand Up @@ -134,11 +135,23 @@ class ExpressionSplitter(config: Config) {
val q = intern(compress(e.query))
DataExprMeta(e.toString, q)
}
case ExprType.TRACE_EVENTS | ExprType.TRACE_TIME_SERIES =>
case ExprType.TRACE_EVENTS =>
parsedExpressions.map { e =>
// Tracing cannot be scoped to specific infrastructure, always use True
DataExprMeta(e.toString, Query.True)
}
case ExprType.TRACE_TIME_SERIES =>
parsedExpressions
.collect {
case tq: TraceQuery.SpanTimeSeries =>
tq.expr.expr.dataExprs.map(e => tq.copy(expr = StyleExpr(e, Map.empty)))
}
.flatten
.distinct
.map { e =>
// Tracing cannot be scoped to specific infrastructure, always use True
DataExprMeta(e.toString, Query.True)
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,25 @@ class ExpressionSplitterSuite extends FunSuite {
assertEquals(actual, expected)
}

test("splits compound trace time series expression into data expressions") {
def childExpr(e: String): String = {
s"nf.app,api,:eq,nf.cluster,skan-test,:eq,:child,$e,:span-time-series"
}
val expr = s"${childExpr(query1)},${childExpr(query1)}"
val actual = splitter.split(expr, ExprType.TRACE_TIME_SERIES, frequency1)
val expected = List(
Subscription(
Query.True,
ExpressionMetadata(childExpr(ds1a), ExprType.TRACE_TIME_SERIES, frequency1)
),
Subscription(
Query.True,
ExpressionMetadata(childExpr(ds1b), ExprType.TRACE_TIME_SERIES, frequency1)
)
).reverse
assertEquals(actual, expected)
}

test("throws IAE for invalid expressions") {
val msg = intercept[IllegalArgumentException] {
splitter.split("foo", ExprType.TIME_SERIES, frequency1)
Expand Down

0 comments on commit 14689d1

Please sign in to comment.