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

function: add support for more functions #142

Merged
merged 1 commit into from
Jan 6, 2023
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
166 changes: 166 additions & 0 deletions engine/engine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,165 @@ func TestQueriesAgainstOldEngine(t *testing.T) {
http_requests_total{pod="nginx-2"} 1+2x18`,
query: "deriv(http_requests_total[30s])",
},
{
name: "abs",
load: `load 30s
http_requests_total{pod="nginx-1"} -5+1x15
http_requests_total{pod="nginx-2"} -5+2x18`,
query: "abs(http_requests_total)",
},
{
name: "ceil",
load: `load 30s
http_requests_total{pod="nginx-1"} -5.5+1x15
http_requests_total{pod="nginx-2"} -5.5+2x18`,
query: "ceil(http_requests_total)",
},
{
name: "exp",
load: `load 30s
http_requests_total{pod="nginx-1"} -5.5+1x15
http_requests_total{pod="nginx-2"} -5.5+2x18`,
query: "exp(http_requests_total)",
},
{
name: "floor",
load: `load 30s
http_requests_total{pod="nginx-1"} -5.5+1x15
http_requests_total{pod="nginx-2"} -5.5+2x18`,
query: "floor(http_requests_total)",
},
{
name: "sqrt",
load: `load 30s
http_requests_total{pod="nginx-1"} 5.5+1x15
http_requests_total{pod="nginx-2"} 5.5+2x18`,
query: "sqrt(http_requests_total)",
},
{
name: "ln",
load: `load 30s
http_requests_total{pod="nginx-1"} 5.5+1x15
http_requests_total{pod="nginx-2"} 5.5+2x18`,
query: "ln(http_requests_total)",
},
{
name: "log2",
load: `load 30s
http_requests_total{pod="nginx-1"} 5.5+1x15
http_requests_total{pod="nginx-2"} 5.5+2x18`,
query: "log2(http_requests_total)",
},
{
name: "log10",
load: `load 30s
http_requests_total{pod="nginx-1"} 5.5+1x15
http_requests_total{pod="nginx-2"} 5.5+2x18`,
query: "log10(http_requests_total)",
},
{
name: "sin",
load: `load 30s
http_requests_total{pod="nginx-1"} 5.5+1x15
http_requests_total{pod="nginx-2"} 5.5+2x18`,
query: "sin(http_requests_total)",
},
{
name: "cos",
load: `load 30s
http_requests_total{pod="nginx-1"} 5.5+1x15
http_requests_total{pod="nginx-2"} 5.5+2x18`,
query: "cos(http_requests_total)",
},
{
name: "tan",
load: `load 30s
http_requests_total{pod="nginx-1"} 5.5+1x15
http_requests_total{pod="nginx-2"} 5.5+2x18`,
query: "tan(http_requests_total)",
},
{
name: "asin",
load: `load 30s
http_requests_total{pod="nginx-1"} 0
http_requests_total{pod="nginx-2"} 1`,
query: "asin(http_requests_total)",
},
{
name: "acos",
load: `load 30s
http_requests_total{pod="nginx-1"} 0
http_requests_total{pod="nginx-2"} 1`,
query: "acos(http_requests_total)",
},
{
name: "atan",
load: `load 30s
http_requests_total{pod="nginx-1"} 0
http_requests_total{pod="nginx-2"} 1`,
query: "atan(http_requests_total)",
},
{
name: "sinh",
load: `load 30s
http_requests_total{pod="nginx-1"} 0
http_requests_total{pod="nginx-2"} 1`,
query: "sinh(http_requests_total)",
},
{
name: "cosh",
load: `load 30s
http_requests_total{pod="nginx-1"} 0
http_requests_total{pod="nginx-2"} 1`,
query: "cosh(http_requests_total)",
},
{
name: "tanh",
load: `load 30s
http_requests_total{pod="nginx-1"} 5.5+1x15
http_requests_total{pod="nginx-2"} 5.5+2x18`,
query: "tanh(http_requests_total)",
},
{
name: "asinh",
load: `load 30s
http_requests_total{pod="nginx-1"} 5.5+1x15
http_requests_total{pod="nginx-2"} 5.5+2x18`,
query: "asinh(http_requests_total)",
},
{
name: "acosh",
load: `load 30s
http_requests_total{pod="nginx-1"} 5.5+1x15
http_requests_total{pod="nginx-2"} 5.5+2x18`,
query: "acosh(http_requests_total)",
},
{
name: "atanh",
load: `load 30s
http_requests_total{pod="nginx-1"} 0
http_requests_total{pod="nginx-2"} 1`,
query: "atanh(http_requests_total)",
},
{
name: "rad",
load: `load 30s
http_requests_total{pod="nginx-1"} 5.5+1x15
http_requests_total{pod="nginx-2"} 5.5+2x18`,
query: "rad(http_requests_total)",
},
{
name: "deg",
load: `load 30s
http_requests_total{pod="nginx-1"} 5.5+1x15
http_requests_total{pod="nginx-2"} 5.5+2x18`,
query: "deg(http_requests_total)",
},
{
name: "pi",
load: ``,
query: "pi()",
},
{
name: "sum",
load: `load 30s
Expand Down Expand Up @@ -183,6 +342,13 @@ func TestQueriesAgainstOldEngine(t *testing.T) {
http_requests_total{pod="nginx-2"} 1+2x18`,
query: "avg_over_time(http_requests_total[30s])",
},
{
name: "abs",
load: `load 30s
http_requests_total{pod="nginx-1"} -10+1x15
http_requests_total{pod="nginx-2"} -10+2x18`,
query: "abs(http_requests_total)",
},
{
name: "max",
load: `load 30s
Expand Down
2 changes: 1 addition & 1 deletion engine/enginefuzz_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func FuzzEngineQueryRangeMatrixFunctions(f *testing.F) {
}
for funcName := range function.Funcs {
// Skipping multi-arg functions in fuzz test for now.
if len(parser.Functions[funcName].ArgTypes) > 1 || funcName == "scalar" || funcName == "time" {
if len(parser.Functions[funcName].ArgTypes) != 1 || parser.Functions[funcName].ArgTypes[0] != parser.ValueTypeMatrix {
continue
}

Expand Down
50 changes: 50 additions & 0 deletions execution/function/functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,57 @@ type FunctionArgs struct {
// FunctionCall represents functions as defined in https://prometheus.io/docs/prometheus/latest/querying/functions/
type FunctionCall func(f FunctionArgs) promql.Sample

func simpleFunc(f func(float64) float64) FunctionCall {
return func(fa FunctionArgs) promql.Sample {
if len(fa.Points) == 0 {
return InvalidSample
}
return promql.Sample{
Metric: fa.Labels,
Point: promql.Point{
T: fa.StepTime,
V: f(fa.Points[0].V),
},
}
}

}

var Funcs = map[string]FunctionCall{
"abs": simpleFunc(math.Abs),
"ceil": simpleFunc(math.Ceil),
"exp": simpleFunc(math.Exp),
"floor": simpleFunc(math.Floor),
"sqrt": simpleFunc(math.Sqrt),
"ln": simpleFunc(math.Log),
"log2": simpleFunc(math.Log2),
"log10": simpleFunc(math.Log10),
"sin": simpleFunc(math.Sin),
"cos": simpleFunc(math.Cos),
"tan": simpleFunc(math.Tan),
"asin": simpleFunc(math.Asin),
"acos": simpleFunc(math.Acos),
"atan": simpleFunc(math.Atan),
"sinh": simpleFunc(math.Sinh),
"cosh": simpleFunc(math.Cosh),
"tanh": simpleFunc(math.Tanh),
"asinh": simpleFunc(math.Asinh),
"acosh": simpleFunc(math.Acosh),
"atanh": simpleFunc(math.Atanh),
"rad": simpleFunc(func(v float64) float64 {
return v * math.Pi / 180
}),
"deg": simpleFunc(func(v float64) float64 {
return v * 180 / math.Pi
}),
"pi": func(f FunctionArgs) promql.Sample {
return promql.Sample{
Point: promql.Point{
T: f.StepTime,
V: math.Pi,
},
}
},
"sum_over_time": func(f FunctionArgs) promql.Sample {
if len(f.Points) == 0 {
return InvalidSample
Expand Down