Commit 8b05af2f by bergquist

feat(alerting): add exeuction time to alertResult

parent f95be63c
...@@ -99,6 +99,7 @@ func (e *Engine) executeJob(job *AlertJob) { ...@@ -99,6 +99,7 @@ func (e *Engine) executeJob(job *AlertJob) {
Duration: float64(time.Since(now).Nanoseconds()) / float64(1000000), Duration: float64(time.Since(now).Nanoseconds()) / float64(1000000),
Error: fmt.Errorf("Timeout"), Error: fmt.Errorf("Timeout"),
AlertJob: job, AlertJob: job,
ExeuctionTime: time.Now(),
} }
e.log.Debug("Job Execution timeout", "alertRuleId", job.Rule.Id) e.log.Debug("Job Execution timeout", "alertRuleId", job.Rule.Id)
case result := <-resultChan: case result := <-resultChan:
......
...@@ -2,6 +2,7 @@ package alerting ...@@ -2,6 +2,7 @@ package alerting
import ( import (
"fmt" "fmt"
"time"
"github.com/grafana/grafana/pkg/bus" "github.com/grafana/grafana/pkg/bus"
"github.com/grafana/grafana/pkg/log" "github.com/grafana/grafana/pkg/log"
...@@ -31,6 +32,7 @@ func (e *HandlerImpl) Execute(job *AlertJob, resultQueue chan *AlertResult) { ...@@ -31,6 +32,7 @@ func (e *HandlerImpl) Execute(job *AlertJob, resultQueue chan *AlertResult) {
Error: err, Error: err,
State: alertstates.Pending, State: alertstates.Pending,
AlertJob: job, AlertJob: job,
ExeuctionTime: time.Now(),
} }
} }
...@@ -136,5 +138,5 @@ func (e *HandlerImpl) evaluateRule(rule *AlertRule, series tsdb.TimeSeriesSlice) ...@@ -136,5 +138,5 @@ func (e *HandlerImpl) evaluateRule(rule *AlertRule, series tsdb.TimeSeriesSlice)
} }
} }
return &AlertResult{State: executionState, Description: "Returned " + executionState, TriggeredAlerts: triggeredAlert} return &AlertResult{State: executionState, Description: "Returned " + executionState, TriggeredAlerts: triggeredAlert, ExeuctionTime: time.Now()}
} }
package alerting package alerting
import "time"
type AlertJob struct { type AlertJob struct {
Offset int64 Offset int64
Delay bool Delay bool
...@@ -28,6 +30,7 @@ type AlertResult struct { ...@@ -28,6 +30,7 @@ type AlertResult struct {
Description string Description string
Error error Error error
AlertJob *AlertJob AlertJob *AlertJob
ExeuctionTime time.Time
} }
type TriggeredAlert struct { type TriggeredAlert struct {
......
...@@ -55,13 +55,15 @@ func (handler *ResultHandlerImpl) shouldUpdateState(result *AlertResult) bool { ...@@ -55,13 +55,15 @@ func (handler *ResultHandlerImpl) shouldUpdateState(result *AlertResult) bool {
return false return false
} }
now := time.Now()
if query.Result == nil { if query.Result == nil {
return true return true
} }
olderThen15Min := query.Result.Created.Before(now.Add(time.Minute * -15)) //now := time.Now()
//olderThen15Min := query.Result.Created.Before(now.Add(time.Minute * -15))
lastExecution := query.Result.Created
asdf := result.ExeuctionTime.Add(time.Minute * -15)
olderThen15Min := lastExecution.Before(asdf)
changedState := query.Result.NewState != result.State changedState := query.Result.NewState != result.State
return changedState || olderThen15Min return changedState || olderThen15Min
......
...@@ -51,6 +51,7 @@ func TestAlertResultHandler(t *testing.T) { ...@@ -51,6 +51,7 @@ func TestAlertResultHandler(t *testing.T) {
Created: now.Add(time.Minute * -30), Created: now.Add(time.Minute * -30),
} }
mockResult.State = alertstates.Critical mockResult.State = alertstates.Critical
mockResult.ExeuctionTime = time.Now()
So(resultHandler.shouldUpdateState(mockResult), ShouldBeTrue) So(resultHandler.shouldUpdateState(mockResult), ShouldBeTrue)
}) })
}) })
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment