Commit 3d66ec81 by bergquist

chore(alerting): minor refactoring

parent 8ac635b6
...@@ -50,7 +50,7 @@ func NewScheduler() *Scheduler { ...@@ -50,7 +50,7 @@ func NewScheduler() *Scheduler {
func (this *Scheduler) heartBeat() { func (this *Scheduler) heartBeat() {
//Lets cheat on this until we focus on clustering //Lets cheat on this until we focus on clustering
log.Info("Heartbeat: Sending heartbeat from " + this.serverId) //log.Info("Heartbeat: Sending heartbeat from " + this.serverId)
this.clusterSize = 1 this.clusterSize = 1
this.serverPosition = 1 this.serverPosition = 1
...@@ -119,7 +119,7 @@ func (this *Scheduler) queueJobs() { ...@@ -119,7 +119,7 @@ func (this *Scheduler) queueJobs() {
func (this *Scheduler) Executor(executor Executor) { func (this *Scheduler) Executor(executor Executor) {
for job := range this.runQueue { for job := range this.runQueue {
log.Info("Executor: queue length %d", len(this.runQueue)) //log.Info("Executor: queue length %d", len(this.runQueue))
log.Info("Executor: executing %s", job.rule.Title) log.Info("Executor: executing %s", job.rule.Title)
this.jobs[job.rule.Id].running = true this.jobs[job.rule.Id].running = true
this.MeasureAndExecute(executor, job) this.MeasureAndExecute(executor, job)
......
package alerting package alerting
import ( import (
"encoding/json"
"fmt" "fmt"
"github.com/franela/goreq" "github.com/franela/goreq"
"github.com/grafana/grafana/pkg/bus" "github.com/grafana/grafana/pkg/bus"
...@@ -14,13 +13,12 @@ import ( ...@@ -14,13 +13,12 @@ import (
type GraphiteExecutor struct{} type GraphiteExecutor struct{}
type Series struct { type GraphiteSerie struct {
Datapoints []DataPoint Datapoints [][2]float64
Target string Target string
} }
type Response []Series type GraphiteResponse []GraphiteSerie
type DataPoint []json.Number
func (this *GraphiteExecutor) Execute(rule m.AlertRule, responseQueue chan *AlertResult) { func (this *GraphiteExecutor) Execute(rule m.AlertRule, responseQueue chan *AlertResult) {
response, err := this.getSeries(rule) response, err := this.getSeries(rule)
...@@ -32,38 +30,7 @@ func (this *GraphiteExecutor) Execute(rule m.AlertRule, responseQueue chan *Aler ...@@ -32,38 +30,7 @@ func (this *GraphiteExecutor) Execute(rule m.AlertRule, responseQueue chan *Aler
responseQueue <- this.executeRules(response, rule) responseQueue <- this.executeRules(response, rule)
} }
func (this *GraphiteExecutor) executeRules(series []Series, rule m.AlertRule) *AlertResult { func (this *GraphiteExecutor) getSeries(rule m.AlertRule) (GraphiteResponse, error) {
for _, v := range series {
var avg float64
var sum float64
for _, dp := range v.Datapoints {
i, _ := dp[0].Float64()
sum += i
}
avg = sum / float64(len(v.Datapoints))
if float64(rule.CritLevel) < avg {
return &AlertResult{State: m.AlertStateCritical, Id: rule.Id, ActualValue: avg}
}
if float64(rule.WarnLevel) < avg {
return &AlertResult{State: m.AlertStateWarn, Id: rule.Id, ActualValue: avg}
}
if float64(rule.CritLevel) < sum {
return &AlertResult{State: m.AlertStateCritical, Id: rule.Id, ActualValue: sum}
}
if float64(rule.WarnLevel) < sum {
return &AlertResult{State: m.AlertStateWarn, Id: rule.Id, ActualValue: sum}
}
}
return &AlertResult{State: m.AlertStateOk, Id: rule.Id}
}
func (this *GraphiteExecutor) getSeries(rule m.AlertRule) (Response, error) {
query := &m.GetDataSourceByIdQuery{Id: rule.DatasourceId, OrgId: rule.OrgId} query := &m.GetDataSourceByIdQuery{Id: rule.DatasourceId, OrgId: rule.OrgId}
if err := bus.Dispatch(query); err != nil { if err := bus.Dispatch(query); err != nil {
return nil, err return nil, err
...@@ -71,22 +38,19 @@ func (this *GraphiteExecutor) getSeries(rule m.AlertRule) (Response, error) { ...@@ -71,22 +38,19 @@ func (this *GraphiteExecutor) getSeries(rule m.AlertRule) (Response, error) {
v := url.Values{ v := url.Values{
"format": []string{"json"}, "format": []string{"json"},
"target": []string{getTargetFromQuery(rule)}, "target": []string{getTargetFromRule(rule)},
"until": []string{"now"},
"from": []string{"-" + rule.QueryRange},
} }
v.Add("from", "-"+rule.QueryRange) res, err := goreq.Request{
v.Add("until", "now")
req := goreq.Request{
Method: "POST", Method: "POST",
Uri: query.Result.Url + "/render", Uri: query.Result.Url + "/render",
Body: v.Encode(), Body: v.Encode(),
Timeout: 500 * time.Millisecond, Timeout: 500 * time.Millisecond,
} }.Do()
res, err := req.Do()
response := Response{} response := GraphiteResponse{}
res.Body.FromJsonTo(&response) res.Body.FromJsonTo(&response)
if err != nil { if err != nil {
...@@ -100,7 +64,7 @@ func (this *GraphiteExecutor) getSeries(rule m.AlertRule) (Response, error) { ...@@ -100,7 +64,7 @@ func (this *GraphiteExecutor) getSeries(rule m.AlertRule) (Response, error) {
return response, nil return response, nil
} }
func getTargetFromQuery(rule m.AlertRule) string { func getTargetFromRule(rule m.AlertRule) string {
json, _ := simplejson.NewJson([]byte(rule.Query)) json, _ := simplejson.NewJson([]byte(rule.Query))
return json.Get("target").MustString() return json.Get("target").MustString()
......
package alerting
import (
m "github.com/grafana/grafana/pkg/models"
)
func (this *GraphiteExecutor) executeRules(series []GraphiteSerie, rule m.AlertRule) *AlertResult {
for _, v := range series {
var avg float64
var sum float64
for _, dp := range v.Datapoints {
sum += dp[0]
}
avg = sum / float64(len(v.Datapoints))
if float64(rule.CritLevel) < avg {
return &AlertResult{State: m.AlertStateCritical, Id: rule.Id, ActualValue: avg}
}
if float64(rule.WarnLevel) < avg {
return &AlertResult{State: m.AlertStateWarn, Id: rule.Id, ActualValue: avg}
}
if float64(rule.CritLevel) < sum {
return &AlertResult{State: m.AlertStateCritical, Id: rule.Id, ActualValue: sum}
}
if float64(rule.WarnLevel) < sum {
return &AlertResult{State: m.AlertStateWarn, Id: rule.Id, ActualValue: sum}
}
}
return &AlertResult{State: m.AlertStateOk, Id: rule.Id}
}
package alerting
type TimeSeries struct {
Name string `json:"name"`
Points [][2]float64 `json:"points"`
}
type TimeSeriesSlice []*TimeSeries
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