suggestquery.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. // Copyright 2019 Yunion
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package conditions
  15. import (
  16. "fmt"
  17. "yunion.io/x/onecloud/pkg/apis/monitor"
  18. "yunion.io/x/onecloud/pkg/monitor/alerting"
  19. "yunion.io/x/onecloud/pkg/monitor/tsdb"
  20. )
  21. func init() {
  22. alerting.RegisterCondition("suggest_query", func(model *monitor.AlertCondition, index int) (alerting.Condition,
  23. error) {
  24. return newSuggestQueryCondition(model, index)
  25. })
  26. }
  27. type SuggestQueryCondition struct {
  28. *QueryCondition
  29. }
  30. func newSuggestQueryCondition(model *monitor.AlertCondition, index int) (*SuggestQueryCondition, error) {
  31. queryCondition, err := newQueryCondition(model, index)
  32. if err != nil {
  33. return nil, err
  34. }
  35. condition := new(SuggestQueryCondition)
  36. condition.QueryCondition = queryCondition
  37. return condition, nil
  38. }
  39. func (c *SuggestQueryCondition) Eval(context *alerting.EvalContext) (*alerting.ConditionResult, error) {
  40. timeRange := tsdb.NewTimeRange(c.Query.From, c.Query.To)
  41. ret, err := c.executeQuery(context, timeRange)
  42. if err != nil {
  43. return nil, err
  44. }
  45. seriesList := ret.series
  46. emptySeriesCount := 0
  47. evalMatchCount := 0
  48. var matches []*monitor.EvalMatch
  49. for _, series := range seriesList {
  50. reducedValue, _ := c.Reducer.Reduce(series)
  51. evalMatch := c.Evaluator.Eval(reducedValue)
  52. if reducedValue == nil {
  53. emptySeriesCount++
  54. }
  55. if context.IsTestRun {
  56. context.Logs = append(context.Logs, &monitor.ResultLogEntry{
  57. Message: fmt.Sprintf("Condition[%d]: Eval: %v, Metric: %s, Value: %v", c.Index, evalMatch, series.Name, reducedValue),
  58. })
  59. }
  60. if evalMatch {
  61. evalMatchCount++
  62. }
  63. if evalMatch {
  64. matches = append(matches, &monitor.EvalMatch{
  65. Metric: series.Name,
  66. Value: reducedValue,
  67. Tags: series.Tags,
  68. })
  69. }
  70. }
  71. // handle no series special case
  72. if len(seriesList) == 0 {
  73. // eval condition for null value
  74. evalMatch := c.Evaluator.Eval(nil)
  75. if context.IsTestRun {
  76. context.Logs = append(context.Logs, &monitor.ResultLogEntry{
  77. Message: fmt.Sprintf("Condition: Eval: %v, Query returned No Series (reduced to null/no value)", evalMatch),
  78. })
  79. }
  80. if evalMatch {
  81. evalMatchCount++
  82. matches = append(matches, &monitor.EvalMatch{
  83. Metric: "NoData",
  84. Value: nil,
  85. })
  86. }
  87. }
  88. return &alerting.ConditionResult{
  89. Firing: evalMatchCount > 0,
  90. NoDataFound: emptySeriesCount == len(seriesList),
  91. Operator: c.Operator,
  92. EvalMatches: matches,
  93. }, nil
  94. }