-
Notifications
You must be signed in to change notification settings - Fork 7
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
Add exclude and grep functions #11
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package expr | ||
|
||
import ( | ||
"regexp" | ||
|
||
"github.com/grafana/metrictank/api/models" | ||
) | ||
|
||
type FuncGrep struct { | ||
in GraphiteFunc | ||
pattern *regexp.Regexp | ||
excludeMatches bool | ||
} | ||
|
||
func NewGrep() GraphiteFunc { | ||
return &FuncGrep{excludeMatches: false} | ||
} | ||
|
||
func NewExclude() GraphiteFunc { | ||
return &FuncGrep{excludeMatches: true} | ||
} | ||
|
||
func (s *FuncGrep) Signature() ([]Arg, []Arg) { | ||
return []Arg{ | ||
ArgSeriesList{val: &s.in}, | ||
ArgRegex{key: "pattern", val: &s.pattern}, | ||
}, []Arg{ | ||
ArgSeriesList{}, | ||
} | ||
} | ||
|
||
func (s *FuncGrep) Context(context Context) Context { | ||
return context | ||
} | ||
|
||
func (s *FuncGrep) Exec(cache map[Req][]models.Series) ([]models.Series, error) { | ||
series, err := s.in.Exec(cache) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
var outputs []models.Series | ||
for _, serie := range series { | ||
if s.pattern.MatchString(serie.Target) != s.excludeMatches { | ||
outputs = append(outputs, serie) | ||
} | ||
} | ||
return outputs, nil | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
package expr | ||
|
||
import ( | ||
"fmt" | ||
"regexp" | ||
"testing" | ||
|
||
"github.com/grafana/metrictank/api/models" | ||
) | ||
|
||
func TestGrep(t *testing.T) { | ||
cases := []struct { | ||
pattern string | ||
in []string | ||
matches []string | ||
nonmatches []string | ||
}{ | ||
{ | ||
"this", | ||
[]string{"series.name.this.ok"}, | ||
[]string{"series.name.this.ok"}, | ||
[]string{}, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. On the same idea, do we need a test to specify what happens when grep finds nothing? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This test has an "exclude" that excludes everything. It's the same code at that point. |
||
}, | ||
{ | ||
`cpu\d`, | ||
[]string{"series.cpu1.ok", "series.cpu2.ok", "series.cpu.notok", "series.cpu3.ok"}, | ||
[]string{"series.cpu1.ok", "series.cpu2.ok", "series.cpu3.ok"}, | ||
[]string{"series.cpu.notok"}, | ||
}, | ||
{ | ||
`cpu[02468]`, | ||
[]string{"series.cpu1.ok", "series.cpu2.ok", "series.cpu.notok", "series.cpu3.ok"}, | ||
[]string{"series.cpu2.ok"}, | ||
[]string{"series.cpu1.ok", "series.cpu.notok", "series.cpu3.ok"}, | ||
}, | ||
} | ||
for i, c := range cases { | ||
var in []models.Series | ||
for _, name := range c.in { | ||
in = append(in, models.Series{ | ||
Target: name, | ||
}) | ||
} | ||
|
||
{ | ||
f := NewGrep() | ||
grep := f.(*FuncGrep) | ||
grep.pattern = regexp.MustCompile(c.pattern) | ||
grep.in = NewMock(in) | ||
checkGrepOutput(t, f, i, c.matches) | ||
} | ||
|
||
{ | ||
f := NewExclude() | ||
grep := f.(*FuncGrep) | ||
grep.pattern = regexp.MustCompile(c.pattern) | ||
grep.in = NewMock(in) | ||
checkGrepOutput(t, f, i, c.nonmatches) | ||
} | ||
} | ||
} | ||
|
||
func checkGrepOutput(t *testing.T, f GraphiteFunc, i int, expected []string) { | ||
got, err := f.Exec(make(map[Req][]models.Series)) | ||
if err != nil { | ||
t.Fatalf("case %d: err should be nil. got %q", i, err) | ||
} | ||
if len(got) != len(expected) { | ||
t.Fatalf("case %d: expected %d output series, got %d", i, len(expected), len(got)) | ||
} | ||
for i, o := range expected { | ||
g := got[i] | ||
if o != g.Target { | ||
t.Fatalf("case %d: expected target %q, got %q", i, o, g.Target) | ||
} | ||
} | ||
} | ||
|
||
func BenchmarkGrep_1(b *testing.B) { | ||
benchmarkGrep(b, 1) | ||
} | ||
func BenchmarkGrep_10(b *testing.B) { | ||
benchmarkGrep(b, 10) | ||
} | ||
func BenchmarkGrep_100(b *testing.B) { | ||
benchmarkGrep(b, 100) | ||
} | ||
func BenchmarkGrep_1000(b *testing.B) { | ||
benchmarkGrep(b, 1000) | ||
} | ||
func BenchmarkGrep_100000(b *testing.B) { | ||
benchmarkGrep(b, 100000) | ||
} | ||
|
||
func benchmarkGrep(b *testing.B, numSeries int) { | ||
var input []models.Series | ||
for i := 0; i < numSeries; i++ { | ||
series := models.Series{ | ||
Target: fmt.Sprintf("metrictank.stats.env.instance.input.plugin%d.metrics_received.counter32", i), | ||
} | ||
input = append(input, series) | ||
} | ||
b.ResetTimer() | ||
for i := 0; i < b.N; i++ { | ||
f := NewGrep() | ||
grep := f.(*FuncGrep) | ||
grep.pattern = regexp.MustCompile("input.plugin[0246].metrics") | ||
grep.in = NewMock(input) | ||
got, err := f.Exec(make(map[Req][]models.Series)) | ||
if err != nil { | ||
b.Fatalf("%s", err) | ||
} | ||
results = got | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is it okay to return an empty slice? Should we provide a warning if it's empty?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, an empty slice is fine. No need to warn.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I looked around guess convention is to send back empty. (y)