This commit is contained in:
Ranjib Dey 2016-08-21 01:37:56 -07:00
parent 2a5c92710b
commit 2b67d3b886
2 changed files with 27 additions and 15 deletions

View File

@ -82,17 +82,20 @@ func (p *PD) Write(metrics []telegraf.Metric) error {
return nil return nil
} }
func (p *PD) processForEvent(metric telegraf.Metric) error { func (p *PD) isTripped(metric telegraf.Metric) (bool, error) {
value, ok := metric.Fields()[p.Field] value, ok := metric.Fields()[p.Field]
if !ok { if !ok {
return fmt.Errorf("Filed '%s' absent", p.Field) return false, fmt.Errorf("Filed '%s' absent", p.Field)
} }
expr := fmt.Sprintf("%v %s", value, p.Expression) expr := fmt.Sprintf("%v %s", value, p.Expression)
trigger, err := evalBoolExpr(expr) return evalBoolExpr(expr)
}
func (p *PD) processForEvent(metric telegraf.Metric) error {
trigger, err := p.isTripped(metric)
if err != nil { if err != nil {
return err return err
} }
event := Event{ event := Event{
ServiceKey: p.ServiceKey, ServiceKey: p.ServiceKey,
Client: "telegraf", Client: "telegraf",

View File

@ -5,22 +5,31 @@ import (
"testing" "testing"
) )
func TestMetricMatch(t *testing.T) { func TestPDAlert(t *testing.T) {
metric := testutil.TestMetric(1.0, "foo") metric := testutil.TestMetric(2.0, "foo")
var err error
var tripped bool
p := PD{ p := PD{
Metric: "foo", Metric: "foo",
Field: "value", Field: "value",
Expression: "> 0", Expression: "> 5",
} }
if !p.Match(metric) { if !p.isMatch(metric) {
t.Error("Metric did not match for greater than expression") t.Error("Metric should match when name is same")
} }
p.Expression = "== 1" tripped, err = p.isTripped(metric)
if !p.Match(metric) { if err != nil {
t.Error("Metric did not match for equality expression") t.Error(err)
} }
p.Expression = "< 0" if tripped {
if p.Match(metric) { t.Error("Metric should not trigger alert when its expression evaluates to false")
t.Error("Metric did not match for less than expression") }
p.Expression = "> 1"
tripped, err = p.isTripped(metric)
if err != nil {
t.Error(err)
}
if !tripped {
t.Error("Metric should trigger alert when expression evaluates to true")
} }
} }