mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
chore(alerting): minor refactoring
This commit is contained in:
parent
8ac635b631
commit
3d66ec816d
@ -50,7 +50,7 @@ func NewScheduler() *Scheduler {
|
||||
func (this *Scheduler) heartBeat() {
|
||||
|
||||
//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.serverPosition = 1
|
||||
|
||||
@ -119,7 +119,7 @@ func (this *Scheduler) queueJobs() {
|
||||
|
||||
func (this *Scheduler) Executor(executor Executor) {
|
||||
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)
|
||||
this.jobs[job.rule.Id].running = true
|
||||
this.MeasureAndExecute(executor, job)
|
||||
|
@ -1,7 +1,6 @@
|
||||
package alerting
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"github.com/franela/goreq"
|
||||
"github.com/grafana/grafana/pkg/bus"
|
||||
@ -14,13 +13,12 @@ import (
|
||||
|
||||
type GraphiteExecutor struct{}
|
||||
|
||||
type Series struct {
|
||||
Datapoints []DataPoint
|
||||
type GraphiteSerie struct {
|
||||
Datapoints [][2]float64
|
||||
Target string
|
||||
}
|
||||
|
||||
type Response []Series
|
||||
type DataPoint []json.Number
|
||||
type GraphiteResponse []GraphiteSerie
|
||||
|
||||
func (this *GraphiteExecutor) Execute(rule m.AlertRule, responseQueue chan *AlertResult) {
|
||||
response, err := this.getSeries(rule)
|
||||
@ -32,38 +30,7 @@ func (this *GraphiteExecutor) Execute(rule m.AlertRule, responseQueue chan *Aler
|
||||
responseQueue <- this.executeRules(response, rule)
|
||||
}
|
||||
|
||||
func (this *GraphiteExecutor) executeRules(series []Series, rule m.AlertRule) *AlertResult {
|
||||
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) {
|
||||
func (this *GraphiteExecutor) getSeries(rule m.AlertRule) (GraphiteResponse, error) {
|
||||
query := &m.GetDataSourceByIdQuery{Id: rule.DatasourceId, OrgId: rule.OrgId}
|
||||
if err := bus.Dispatch(query); err != nil {
|
||||
return nil, err
|
||||
@ -71,22 +38,19 @@ func (this *GraphiteExecutor) getSeries(rule m.AlertRule) (Response, error) {
|
||||
|
||||
v := url.Values{
|
||||
"format": []string{"json"},
|
||||
"target": []string{getTargetFromQuery(rule)},
|
||||
"target": []string{getTargetFromRule(rule)},
|
||||
"until": []string{"now"},
|
||||
"from": []string{"-" + rule.QueryRange},
|
||||
}
|
||||
|
||||
v.Add("from", "-"+rule.QueryRange)
|
||||
v.Add("until", "now")
|
||||
|
||||
req := goreq.Request{
|
||||
res, err := goreq.Request{
|
||||
Method: "POST",
|
||||
Uri: query.Result.Url + "/render",
|
||||
Body: v.Encode(),
|
||||
Timeout: 500 * time.Millisecond,
|
||||
}
|
||||
}.Do()
|
||||
|
||||
res, err := req.Do()
|
||||
|
||||
response := Response{}
|
||||
response := GraphiteResponse{}
|
||||
res.Body.FromJsonTo(&response)
|
||||
|
||||
if err != nil {
|
||||
@ -100,7 +64,7 @@ func (this *GraphiteExecutor) getSeries(rule m.AlertRule) (Response, error) {
|
||||
return response, nil
|
||||
}
|
||||
|
||||
func getTargetFromQuery(rule m.AlertRule) string {
|
||||
func getTargetFromRule(rule m.AlertRule) string {
|
||||
json, _ := simplejson.NewJson([]byte(rule.Query))
|
||||
|
||||
return json.Get("target").MustString()
|
||||
|
35
pkg/services/alerting/rule_executor.go
Normal file
35
pkg/services/alerting/rule_executor.go
Normal file
@ -0,0 +1,35 @@
|
||||
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}
|
||||
}
|
8
pkg/services/alerting/types.go
Normal file
8
pkg/services/alerting/types.go
Normal file
@ -0,0 +1,8 @@
|
||||
package alerting
|
||||
|
||||
type TimeSeries struct {
|
||||
Name string `json:"name"`
|
||||
Points [][2]float64 `json:"points"`
|
||||
}
|
||||
|
||||
type TimeSeriesSlice []*TimeSeries
|
Loading…
Reference in New Issue
Block a user