mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
feat(alerting): abstract graphite from executor
This commit is contained in:
parent
3d66ec816d
commit
422234d03a
@ -1,4 +1,4 @@
|
||||
package alerting
|
||||
package models
|
||||
|
||||
type TimeSeries struct {
|
||||
Name string `json:"name"`
|
@ -21,7 +21,7 @@ func Init() {
|
||||
|
||||
scheduler := NewScheduler()
|
||||
go scheduler.Dispatch(&AlertRuleReader{})
|
||||
go scheduler.Executor(&GraphiteExecutor{})
|
||||
go scheduler.Executor(&ExecutorImpl{})
|
||||
go scheduler.HandleResponses()
|
||||
}
|
||||
|
||||
|
@ -6,10 +6,6 @@ import (
|
||||
"time"
|
||||
)
|
||||
|
||||
type Executor interface {
|
||||
Execute(rule m.AlertRule, responseQueue chan *AlertResult)
|
||||
}
|
||||
|
||||
type DummieExecutor struct{}
|
||||
|
||||
func (this *DummieExecutor) Execute(rule m.AlertRule, responseQueue chan *AlertResult) {
|
||||
|
@ -2,17 +2,34 @@ package alerting
|
||||
|
||||
import (
|
||||
m "github.com/grafana/grafana/pkg/models"
|
||||
"github.com/grafana/grafana/pkg/services/alerting/graphite"
|
||||
)
|
||||
|
||||
func (this *GraphiteExecutor) executeRules(series []GraphiteSerie, rule m.AlertRule) *AlertResult {
|
||||
type Executor interface {
|
||||
Execute(rule m.AlertRule, responseQueue chan *AlertResult)
|
||||
}
|
||||
|
||||
type ExecutorImpl struct{}
|
||||
|
||||
func (this *ExecutorImpl) Execute(rule m.AlertRule, responseQueue chan *AlertResult) {
|
||||
response, err := graphite.GraphiteClient{}.GetSeries(rule)
|
||||
|
||||
if err != nil {
|
||||
responseQueue <- &AlertResult{State: "CRITICAL", Id: rule.Id}
|
||||
}
|
||||
|
||||
responseQueue <- this.executeRules(response, rule)
|
||||
}
|
||||
|
||||
func (this *ExecutorImpl) executeRules(series m.TimeSeriesSlice, rule m.AlertRule) *AlertResult {
|
||||
for _, v := range series {
|
||||
var avg float64
|
||||
var sum float64
|
||||
for _, dp := range v.Datapoints {
|
||||
for _, dp := range v.Points {
|
||||
sum += dp[0]
|
||||
}
|
||||
|
||||
avg = sum / float64(len(v.Datapoints))
|
||||
avg = sum / float64(len(v.Points))
|
||||
|
||||
if float64(rule.CritLevel) < avg {
|
||||
return &AlertResult{State: m.AlertStateCritical, Id: rule.Id, ActualValue: avg}
|
@ -1,4 +1,4 @@
|
||||
package alerting
|
||||
package graphite
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
@ -11,7 +11,7 @@ import (
|
||||
"time"
|
||||
)
|
||||
|
||||
type GraphiteExecutor struct{}
|
||||
type GraphiteClient struct{}
|
||||
|
||||
type GraphiteSerie struct {
|
||||
Datapoints [][2]float64
|
||||
@ -20,17 +20,7 @@ type GraphiteSerie struct {
|
||||
|
||||
type GraphiteResponse []GraphiteSerie
|
||||
|
||||
func (this *GraphiteExecutor) Execute(rule m.AlertRule, responseQueue chan *AlertResult) {
|
||||
response, err := this.getSeries(rule)
|
||||
|
||||
if err != nil {
|
||||
responseQueue <- &AlertResult{State: "CRITICAL", Id: rule.Id}
|
||||
}
|
||||
|
||||
responseQueue <- this.executeRules(response, rule)
|
||||
}
|
||||
|
||||
func (this *GraphiteExecutor) getSeries(rule m.AlertRule) (GraphiteResponse, error) {
|
||||
func (this GraphiteClient) GetSeries(rule m.AlertRule) (m.TimeSeriesSlice, error) {
|
||||
query := &m.GetDataSourceByIdQuery{Id: rule.DatasourceId, OrgId: rule.OrgId}
|
||||
if err := bus.Dispatch(query); err != nil {
|
||||
return nil, err
|
||||
@ -61,7 +51,16 @@ func (this *GraphiteExecutor) getSeries(rule m.AlertRule) (GraphiteResponse, err
|
||||
return nil, fmt.Errorf("error!")
|
||||
}
|
||||
|
||||
return response, nil
|
||||
timeSeries := make([]*m.TimeSeries, 0)
|
||||
|
||||
for _, v := range response {
|
||||
timeSeries = append(timeSeries, &m.TimeSeries{
|
||||
Name: v.Target,
|
||||
Points: v.Datapoints,
|
||||
})
|
||||
}
|
||||
|
||||
return timeSeries, nil
|
||||
}
|
||||
|
||||
func getTargetFromRule(rule m.AlertRule) string {
|
Loading…
Reference in New Issue
Block a user