Commit 8b05af2f by bergquist

feat(alerting): add exeuction time to alertResult

parent f95be63c
......@@ -95,10 +95,11 @@ func (e *Engine) executeJob(job *AlertJob) {
select {
case <-time.After(time.Second * 5):
e.resultQueue <- &AlertResult{
State: alertstates.Pending,
Duration: float64(time.Since(now).Nanoseconds()) / float64(1000000),
Error: fmt.Errorf("Timeout"),
AlertJob: job,
State: alertstates.Pending,
Duration: float64(time.Since(now).Nanoseconds()) / float64(1000000),
Error: fmt.Errorf("Timeout"),
AlertJob: job,
ExeuctionTime: time.Now(),
}
e.log.Debug("Job Execution timeout", "alertRuleId", job.Rule.Id)
case result := <-resultChan:
......
......@@ -2,6 +2,7 @@ package alerting
import (
"fmt"
"time"
"github.com/grafana/grafana/pkg/bus"
"github.com/grafana/grafana/pkg/log"
......@@ -28,9 +29,10 @@ func (e *HandlerImpl) Execute(job *AlertJob, resultQueue chan *AlertResult) {
timeSeries, err := e.executeQuery(job)
if err != nil {
resultQueue <- &AlertResult{
Error: err,
State: alertstates.Pending,
AlertJob: job,
Error: err,
State: alertstates.Pending,
AlertJob: job,
ExeuctionTime: time.Now(),
}
}
......@@ -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
import "time"
type AlertJob struct {
Offset int64
Delay bool
......@@ -28,6 +30,7 @@ type AlertResult struct {
Description string
Error error
AlertJob *AlertJob
ExeuctionTime time.Time
}
type TriggeredAlert struct {
......
......@@ -55,13 +55,15 @@ func (handler *ResultHandlerImpl) shouldUpdateState(result *AlertResult) bool {
return false
}
now := time.Now()
if query.Result == nil {
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
return changedState || olderThen15Min
......
......@@ -51,6 +51,7 @@ func TestAlertResultHandler(t *testing.T) {
Created: now.Add(time.Minute * -30),
}
mockResult.State = alertstates.Critical
mockResult.ExeuctionTime = time.Now()
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