Skip to content
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

use stat placeholders for normalize #1503

Merged
merged 1 commit into from
Dec 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import com.netflix.atlas.core.model.FilterExpr
import com.netflix.atlas.core.model.ModelExtractors
import com.netflix.atlas.core.model.Query
import com.netflix.atlas.core.model.StyleExpr
import com.netflix.atlas.core.model.TimeSeriesExpr
import com.netflix.atlas.core.stacklang.Context
import com.netflix.atlas.core.stacklang.Interpreter
import com.netflix.atlas.core.stacklang.Word
Expand Down Expand Up @@ -295,10 +296,31 @@ object ExprApi {
}
}

private def normalizeStat(expr: StyleExpr): StyleExpr = {
expr
.rewrite {
case FilterExpr.Filter(ts1, ts2) =>
val updated = ts2.rewrite {
case FilterExpr.Stat(ts, s, None) if ts == ts1 =>
s match {
case "avg" => FilterExpr.StatAvg
case "min" => FilterExpr.StatMin
case "max" => FilterExpr.StatMax
case "last" => FilterExpr.StatLast
case "total" => FilterExpr.StatTotal
case "count" => FilterExpr.StatCount
case _ => FilterExpr.Stat(ts, s, None)
}
}
FilterExpr.Filter(ts1, updated.asInstanceOf[TimeSeriesExpr])
}
.asInstanceOf[StyleExpr]
}

private def exprStrings(exprs: List[StyleExpr]): List[String] = {
// Rewrite the expressions and convert to a normalized strings
exprs.map { expr =>
val rewritten = expr.rewrite {
val rewritten = normalizeStat(expr).rewrite {
case q: Query => sort(q)
}
// Remove explicit :const, it can be determined from implicit conversion
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,24 @@ class ExprApiSuite extends MUnitRouteSuite {
assertEquals(normalize(avg), List(avg))
}

test("normalize :stat-aggr with no condition filters") {
val expr = "app,foo,:eq,name,cpuUser,:eq,:and,:sum,(,nf.cluster,),:by,:stat-max,:filter"
assertEquals(normalize(expr), List(expr))
}

test("normalize :stat to :stat-aggr if possible") {
val expr = "name,sps,:eq,(,nf.cluster,),:by,:dup,max,:stat,5,:gt,:filter"
val expected = "name,sps,:eq,:sum,(,nf.cluster,),:by,:stat-max,5.0,:gt,:filter"
assertEquals(normalize(expr), List(expected))
}

test("normalize :stat to :stat-aggr nested") {
val expr =
"name,sps,:eq,(,nf.cluster,),:by,:dup,:dup,max,:stat,:swap,avg,:stat,:sub,5,:gt,:filter"
val expected = "name,sps,:eq,:sum,(,nf.cluster,),:by,:stat-max,:stat-avg,:sub,5.0,:gt,:filter"
assertEquals(normalize(expr), List(expected))
}

test("normalize simplify query") {
val input = "app,foo,:eq,name,cpuUser,:eq,:and,:true,:and,:sum"
val expected = "app,foo,:eq,name,cpuUser,:eq,:and,:sum"
Expand Down