Skip to content

Commit

Permalink
[query] Implemented the Graphite grep function (#2655)
Browse files Browse the repository at this point in the history
  • Loading branch information
teddywahle authored Sep 30, 2020
1 parent 43ff200 commit 77455be
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 0 deletions.
21 changes: 21 additions & 0 deletions src/query/graphite/native/builtin_functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,26 @@ func limit(_ *common.Context, series singlePathSpec, n int) (ts.SeriesList, erro
return r, nil
}

// grep takes a metric or a wildcard seriesList, followed by a regular
// expression in double quotes. Excludes metrics that don’t match the regular expression.
func grep(_ *common.Context, seriesList singlePathSpec, regex string) (ts.SeriesList, error) {
re, err := regexp.Compile(regex)
if err != nil {
return ts.NewSeriesList(), err
}

filtered := seriesList.Values[:0]
for _, series := range seriesList.Values {
if re.MatchString(series.Name()) {
filtered = append(filtered, series)
}
}

r := ts.NewSeriesList()
r.Values = filtered
return r, nil
}

// timeShift draws the selected metrics shifted in time. If no sign is given, a minus sign ( - ) is
// implied which will shift the metric back in time. If a plus sign ( + ) is given, the metric will
// be shifted forward in time
Expand Down Expand Up @@ -2205,6 +2225,7 @@ func init() {
MustRegisterFunction(exclude)
MustRegisterFunction(exponentialMovingAverage)
MustRegisterFunction(fallbackSeries)
MustRegisterFunction(grep)
MustRegisterFunction(group)
MustRegisterFunction(groupByNode)
MustRegisterFunction(groupByNodes)
Expand Down
39 changes: 39 additions & 0 deletions src/query/graphite/native/builtin_functions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,44 @@ func TestExcludeErr(t *testing.T) {
require.Nil(t, results.Values)
}

func TestGrep(t *testing.T) {
ctx := common.NewTestContext()
defer ctx.Close()

now := time.Now()
values := ts.NewConstantValues(ctx, 10.0, 5, 10)

series1 := ts.NewSeries(ctx, "collectd.test-db1.load.value", now, values)
series2 := ts.NewSeries(ctx, "collectd.test-db2.load.value", now, values)
series3 := ts.NewSeries(ctx, "collectd.test-db3.load.value", now, values)
series4 := ts.NewSeries(ctx, "collectd.test-db4.load.value", now, values)

testInputs := []*ts.Series{series1, series2, series3, series4}
expectedOutput := []common.TestSeries{
{
Name: "collectd.test-db1.load.value",
Data: []float64{10.0, 10.0, 10.0, 10.0, 10.0},
},
{
Name: "collectd.test-db2.load.value",
Data: []float64{10.0, 10.0, 10.0, 10.0, 10.0},
},
}

results, err := grep(nil, singlePathSpec{
Values: testInputs,
}, ".*db[12]")
require.Nil(t, err)
require.NotNil(t, results)
common.CompareOutputsAndExpected(t, 10, now, expectedOutput, results.Values)

// error case
_, err = grep(nil, singlePathSpec{
Values: testInputs,
}, "+++++")
require.NotNil(t, err)
}

func TestSortByName(t *testing.T) {
ctx := common.NewTestContext()
defer ctx.Close()
Expand Down Expand Up @@ -3312,6 +3350,7 @@ func TestFunctionsRegistered(t *testing.T) {
"exclude",
"exponentialMovingAverage",
"fallbackSeries",
"grep",
"group",
"groupByNode",
"groupByNodes",
Expand Down

0 comments on commit 77455be

Please sign in to comment.