query.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. package conditions
  2. import (
  3. "fmt"
  4. "github.com/grafana/grafana/pkg/bus"
  5. "github.com/grafana/grafana/pkg/components/simplejson"
  6. m "github.com/grafana/grafana/pkg/models"
  7. "github.com/grafana/grafana/pkg/services/alerting"
  8. "github.com/grafana/grafana/pkg/tsdb"
  9. )
  10. func init() {
  11. alerting.RegisterCondition("query", func(model *simplejson.Json, index int) (alerting.Condition, error) {
  12. return NewQueryCondition(model, index)
  13. })
  14. }
  15. type QueryCondition struct {
  16. Index int
  17. Query AlertQuery
  18. Reducer QueryReducer
  19. Evaluator AlertEvaluator
  20. HandleRequest tsdb.HandleRequestFunc
  21. }
  22. type AlertQuery struct {
  23. Model *simplejson.Json
  24. DatasourceId int64
  25. From string
  26. To string
  27. }
  28. func (c *QueryCondition) Eval(context *alerting.EvalContext) {
  29. seriesList, err := c.executeQuery(context)
  30. if err != nil {
  31. context.Error = err
  32. return
  33. }
  34. for _, series := range seriesList {
  35. reducedValue := c.Reducer.Reduce(series)
  36. evalMatch := c.Evaluator.Eval(series, reducedValue)
  37. if context.IsTestRun {
  38. context.Logs = append(context.Logs, &alerting.ResultLogEntry{
  39. Message: fmt.Sprintf("Condition[%d]: Eval: %v, Metric: %s, Value: %1.3f", c.Index, evalMatch, series.Name, reducedValue),
  40. })
  41. }
  42. if evalMatch {
  43. context.EvalMatches = append(context.EvalMatches, &alerting.EvalMatch{
  44. Metric: series.Name,
  45. Value: reducedValue,
  46. })
  47. context.Firing = true
  48. }
  49. }
  50. }
  51. func (c *QueryCondition) executeQuery(context *alerting.EvalContext) (tsdb.TimeSeriesSlice, error) {
  52. getDsInfo := &m.GetDataSourceByIdQuery{
  53. Id: c.Query.DatasourceId,
  54. OrgId: context.Rule.OrgId,
  55. }
  56. if err := bus.Dispatch(getDsInfo); err != nil {
  57. return nil, fmt.Errorf("Could not find datasource")
  58. }
  59. req := c.getRequestForAlertRule(getDsInfo.Result)
  60. result := make(tsdb.TimeSeriesSlice, 0)
  61. resp, err := c.HandleRequest(req)
  62. if err != nil {
  63. return nil, fmt.Errorf("tsdb.HandleRequest() error %v", err)
  64. }
  65. for _, v := range resp.Results {
  66. if v.Error != nil {
  67. return nil, fmt.Errorf("tsdb.HandleRequest() response error %v", v)
  68. }
  69. result = append(result, v.Series...)
  70. if context.IsTestRun {
  71. context.Logs = append(context.Logs, &alerting.ResultLogEntry{
  72. Message: fmt.Sprintf("Condition[%d]: Query Result", c.Index),
  73. Data: v.Series,
  74. })
  75. }
  76. }
  77. return result, nil
  78. }
  79. func (c *QueryCondition) getRequestForAlertRule(datasource *m.DataSource) *tsdb.Request {
  80. req := &tsdb.Request{
  81. TimeRange: tsdb.TimeRange{
  82. From: c.Query.From,
  83. To: c.Query.To,
  84. },
  85. Queries: []*tsdb.Query{
  86. {
  87. RefId: "A",
  88. Query: c.Query.Model.Get("target").MustString(),
  89. DataSource: &tsdb.DataSourceInfo{
  90. Id: datasource.Id,
  91. Name: datasource.Name,
  92. PluginId: datasource.Type,
  93. Url: datasource.Url,
  94. },
  95. },
  96. },
  97. }
  98. return req
  99. }
  100. func NewQueryCondition(model *simplejson.Json, index int) (*QueryCondition, error) {
  101. condition := QueryCondition{}
  102. condition.Index = index
  103. condition.HandleRequest = tsdb.HandleRequest
  104. queryJson := model.Get("query")
  105. condition.Query.Model = queryJson.Get("model")
  106. condition.Query.From = queryJson.Get("params").MustArray()[1].(string)
  107. condition.Query.To = queryJson.Get("params").MustArray()[2].(string)
  108. condition.Query.DatasourceId = queryJson.Get("datasourceId").MustInt64()
  109. reducerJson := model.Get("reducer")
  110. condition.Reducer = NewSimpleReducer(reducerJson.Get("type").MustString())
  111. evaluatorJson := model.Get("evaluator")
  112. evaluator, err := NewDefaultAlertEvaluator(evaluatorJson)
  113. if err != nil {
  114. return nil, err
  115. }
  116. condition.Evaluator = evaluator
  117. return &condition, nil
  118. }