Skip to content

Commit

Permalink
summarize: add support for 'count' and validate aggregation function (#…
Browse files Browse the repository at this point in the history
…321)

summarize: 

* add support for 'count' and validate aggregation function
name

* add `average` as alias for `avg`

* add `median`

* summarize percentiles: 
  improve error message if aggregation starts with
`p`
   support only percentiles of this form: <int of max two digits>
  • Loading branch information
avereha authored Sep 28, 2020
1 parent 667b270 commit d91b492
Show file tree
Hide file tree
Showing 6 changed files with 272 additions and 205 deletions.
185 changes: 0 additions & 185 deletions expr/expr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -978,191 +978,6 @@ func TestEvalExpression(t *testing.T) {
}
}

func TestEvalSummarize(t *testing.T) {
tenThirtyTwo, _, tenThirty := th.InitTestSummarize()
now32 := tenThirty

tests := []th.SummarizeEvalTestItem{
{
"summarize(metric1,'5s')",
map[parser.MetricRequest][]*types.MetricData{
{"metric1", 0, 1}: {types.MakeMetricData("metric1", []float64{
1, 1, 1, 1, 1,
2, 2, 2, 2, 2,
3, 3, 3, 3, 3,
4, 4, 4, 4, 4,
5, 5, 5, 5, 5,
math.NaN(), 2, 3, 4, 5,
math.NaN(), math.NaN(), math.NaN(), math.NaN(), math.NaN(),
}, 1, now32)},
},
[]float64{5, 10, 15, 20, 25, 14, math.NaN()},
"summarize(metric1,'5s')",
5,
now32,
now32 + 35,
},
{
"summarize(metric1,'5s')",
map[parser.MetricRequest][]*types.MetricData{
{"metric1", 0, 1}: {types.MakeMetricData("metric1", []float64{
1, 2, 3, 4, 5,
}, 10, now32)},
},
[]float64{1, 2, 3, 4, 5},
"summarize(metric1,'5s')",
10,
now32,
now32 + 50,
},
{
"summarize(metric1,'5s','avg')",
map[parser.MetricRequest][]*types.MetricData{
{"metric1", 0, 1}: {types.MakeMetricData("metric1", []float64{1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 1, 2, 3, math.NaN(), math.NaN(), math.NaN(), math.NaN(), math.NaN(), math.NaN(), math.NaN()}, 1, now32)},
},
[]float64{1, 2, 3, 4, 5, 2, math.NaN()},
"summarize(metric1,'5s','avg')",
5,
now32,
now32 + 35,
},
{
"summarize(metric1,'5s','max')",
map[parser.MetricRequest][]*types.MetricData{
{"metric1", 0, 1}: {types.MakeMetricData("metric1", []float64{1, 0, 0, 0.5, 1, 2, 1, 1, 1.5, 2, 3, 2, 2, 1.5, 3, 4, 3, 2, 3, 4.5, 5, 5, 5, 5, 5}, 1, now32)},
},
[]float64{1, 2, 3, 4.5, 5},
"summarize(metric1,'5s','max')",
5,
now32,
now32 + 25*1,
},
{
"summarize(metric1,'5s','min')",
map[parser.MetricRequest][]*types.MetricData{
{"metric1", 0, 1}: {types.MakeMetricData("metric1", []float64{1, 0, 0, 0.5, 1, 2, 1, 1, 1.5, 2, 3, 2, 2, 1.5, 3, 4, 3, 2, 3, 4.5, 5, 5, 5, 5, 5}, 1, now32)},
},
[]float64{0, 1, 1.5, 2, 5},
"summarize(metric1,'5s','min')",
5,
now32,
now32 + 25*1,
},
{
"summarize(metric1,'5s','last')",
map[parser.MetricRequest][]*types.MetricData{
{"metric1", 0, 1}: {types.MakeMetricData("metric1", []float64{1, 0, 0, 0.5, 1, 2, 1, 1, 1.5, 2, 3, 2, 2, 1.5, 3, 4, 3, 2, 3, 4.5, 5, 5, 5, 5, 5}, 1, now32)},
},
[]float64{1, 2, 3, 4.5, 5},
"summarize(metric1,'5s','last')",
5,
now32,
now32 + 25*1,
},
{
"summarize(metric1,'5s','p50')",
map[parser.MetricRequest][]*types.MetricData{
{"metric1", 0, 1}: {types.MakeMetricData("metric1", []float64{1, 0, 0, 0.5, 1, 2, 1, 1, 1.5, 2, 3, 2, 2, 1.5, 3, 4, 3, 2, 3, 4.5, 5, 5, 5, 5, 5}, 1, now32)},
},
[]float64{0.5, 1.5, 2, 3, 5},
"summarize(metric1,'5s','p50')",
5,
now32,
now32 + 25*1,
},
{
"summarize(metric1,'5s','p25')",
map[parser.MetricRequest][]*types.MetricData{
{"metric1", 0, 1}: {types.MakeMetricData("metric1", []float64{1, 0, 0, 0.5, 1, 2, 1, 1, 1.5, 2, 3, 2, 2, 1.5, 3, 4, 3, 2, 3, 4.5, 5, 5, 5, 5, 5}, 1, now32)},
},
[]float64{0, 1, 2, 3, 5},
"summarize(metric1,'5s','p25')",
5,
now32,
now32 + 25*1,
},
{
"summarize(metric1,'5s','p99.9')",
map[parser.MetricRequest][]*types.MetricData{
{"metric1", 0, 1}: {types.MakeMetricData("metric1", []float64{1, 0, 0, 0.5, 1, 2, 1, 1, 1.5, 2, 3, 2, 2, 1.5, 3, 4, 3, 2, 3, 4.5, 5, 5, 5, 5, 5}, 1, now32)},
},
[]float64{1, 2, 3, 4.498, 5},
"summarize(metric1,'5s','p99.9')",
5,
now32,
now32 + 25*1,
},
{
"summarize(metric1,'5s','p100.1')",
map[parser.MetricRequest][]*types.MetricData{
{"metric1", 0, 1}: {types.MakeMetricData("metric1", []float64{1, 0, 0, 0.5, 1, 2, 1, 1, 1.5, 2, 3, 2, 2, 1.5, 3, 4, 3, 2, 3, 4.5, 5, 5, 5, 5, 5}, 1, now32)},
},
[]float64{math.NaN(), math.NaN(), math.NaN(), math.NaN(), math.NaN()},
"summarize(metric1,'5s','p100.1')",
5,
now32,
now32 + 25*1,
},
{
"summarize(metric1,'1s','p50')",
map[parser.MetricRequest][]*types.MetricData{
{"metric1", 0, 1}: {types.MakeMetricData("metric1", []float64{1, 0, 0, 0.5, 1, 2, 1, 1, 1.5, 2, 3, 2, 2, 1.5, 3, 4, 3, 2, 3, 4.5, 5, 5, 5, 5, 5}, 1, now32)},
},
[]float64{1, 0, 0, 0.5, 1, 2, 1, 1, 1.5, 2, 3, 2, 2, 1.5, 3, 4, 3, 2, 3, 4.5, 5, 5, 5, 5, 5},
"summarize(metric1,'1s','p50')",
1,
now32,
now32 + 25*1,
},
{
"summarize(metric1,'10min')",
map[parser.MetricRequest][]*types.MetricData{
{"metric1", 0, 1}: {types.MakeMetricData("metric1", []float64{
1, 1, 1, 1, 1, 2, 2, 2, 2, 2,
3, 3, 3, 3, 3, 4, 4, 4, 4, 4,
5, 5, 5, 5, 5}, 60, tenThirtyTwo)},
},
[]float64{11, 31, 33},
"summarize(metric1,'10min')",
600,
tenThirty,
tenThirty + 30*60,
},
{
"summarize(metric1,'10min','sum',true)",
map[parser.MetricRequest][]*types.MetricData{
{"metric1", 0, 1}: {types.MakeMetricData("metric1", []float64{
1, 1, 1, 1, 1, 2, 2, 2, 2, 2,
3, 3, 3, 3, 3, 4, 4, 4, 4, 4,
5, 5, 5, 5, 5}, 60, tenThirtyTwo)},
},
[]float64{15, 35, 25},
"summarize(metric1,'10min','sum',true)",
600,
tenThirtyTwo,
tenThirtyTwo + 25*60,
},
{
"summarize(metric1,'10min','sum',true)",
map[parser.MetricRequest][]*types.MetricData{
{"metric1", 0, 1}: {types.MakeMetricData("metric1", []float64{
1, 1, 1, 1, 1, 2, 2, 2, 2, 2,
3, 3, 3, 3, 3, 4, 4, 4, 4, 4,
5, 5, 5, 5, 5}, 60, tenThirtyTwo)},
},
[]float64{15, 35, 25},
"summarize(metric1,'10min','sum',true)",
600,
tenThirtyTwo,
tenThirtyTwo + 25*60,
},
}

for _, tt := range tests {
th.TestSummarizeEvalExpr(t, &tt)
}
}

func TestEvalMultipleReturns(t *testing.T) {
now32 := int32(time.Now().Unix())

Expand Down
5 changes: 4 additions & 1 deletion expr/functions/legendValue/function.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,10 @@ func (f *legendValue) Do(ctx context.Context, e parser.Expr, from, until int32,
for _, a := range arg {
r := *a
for _, method := range methods {
summary, _ := helper.SummarizeValues(method, a.Values)
summary, _, err := helper.SummarizeValues(method, a.Values)
if err != nil {
return []*types.MetricData{}, err
}
r.Name = fmt.Sprintf("%s (%s: %f)", r.Name, method, summary)
}

Expand Down
6 changes: 3 additions & 3 deletions expr/functions/sortBy/function.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,11 @@ func (f *sortBy) Do(ctx context.Context, e parser.Expr, from, until int32, value
for i, a := range arg {
switch e.Target() {
case "sortByTotal":
vals[i], _ = helper.SummarizeValues("sum", a.Values)
vals[i], _, _ = helper.SummarizeValues("sum", a.Values)
case "sortByMaxima":
vals[i], _ = helper.SummarizeValues("max", a.Values)
vals[i], _, _ = helper.SummarizeValues("max", a.Values)
case "sortByMinima":
min, _ := helper.SummarizeValues("min", a.Values)
min, _, _ := helper.SummarizeValues("min", a.Values)
vals[i] = 1 / min
}
}
Expand Down
11 changes: 8 additions & 3 deletions expr/functions/summarize/function.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,10 @@ func (f *summarize) Do(ctx context.Context, e parser.Expr, from, until int32, va
}

if t >= bucketEnd {
r.Values[ridx], r.IsAbsent[ridx] = helper.SummarizeValues(summarizeFunction, values)

r.Values[ridx], r.IsAbsent[ridx], err = helper.SummarizeValues(summarizeFunction, values)
if err != nil {
return []*types.MetricData{}, err
}
ridx++
bucketEnd += bucketSize
bucketItems = 0
Expand All @@ -143,7 +145,10 @@ func (f *summarize) Do(ctx context.Context, e parser.Expr, from, until int32, va

// last partial bucket
if bucketItems > 0 {
r.Values[ridx], r.IsAbsent[ridx] = helper.SummarizeValues(summarizeFunction, values)
r.Values[ridx], r.IsAbsent[ridx], err = helper.SummarizeValues(summarizeFunction, values)
if err != nil {
return []*types.MetricData{}, err
}
}

results = append(results, &r)
Expand Down
Loading

0 comments on commit d91b492

Please sign in to comment.