diff --git a/plugins/outputs/pagerduty/pagerduty.go b/plugins/outputs/pagerduty/pagerduty.go index 15b3623b5..7b4396337 100644 --- a/plugins/outputs/pagerduty/pagerduty.go +++ b/plugins/outputs/pagerduty/pagerduty.go @@ -82,17 +82,20 @@ func (p *PD) Write(metrics []telegraf.Metric) error { 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] 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) - trigger, err := evalBoolExpr(expr) + return evalBoolExpr(expr) +} + +func (p *PD) processForEvent(metric telegraf.Metric) error { + trigger, err := p.isTripped(metric) if err != nil { return err } - event := Event{ ServiceKey: p.ServiceKey, Client: "telegraf", diff --git a/plugins/outputs/pagerduty/pagerduty_test.go b/plugins/outputs/pagerduty/pagerduty_test.go index 9162520a0..24ae4db7e 100644 --- a/plugins/outputs/pagerduty/pagerduty_test.go +++ b/plugins/outputs/pagerduty/pagerduty_test.go @@ -5,22 +5,31 @@ import ( "testing" ) -func TestMetricMatch(t *testing.T) { - metric := testutil.TestMetric(1.0, "foo") +func TestPDAlert(t *testing.T) { + metric := testutil.TestMetric(2.0, "foo") + var err error + var tripped bool p := PD{ Metric: "foo", Field: "value", - Expression: "> 0", + Expression: "> 5", } - if !p.Match(metric) { - t.Error("Metric did not match for greater than expression") + if !p.isMatch(metric) { + t.Error("Metric should match when name is same") } - p.Expression = "== 1" - if !p.Match(metric) { - t.Error("Metric did not match for equality expression") + tripped, err = p.isTripped(metric) + if err != nil { + t.Error(err) } - p.Expression = "< 0" - if p.Match(metric) { - t.Error("Metric did not match for less than expression") + if tripped { + t.Error("Metric should not trigger alert when its expression evaluates to false") + } + 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") } }