diff --git a/pkg/query-service/rules/resultTypes.go b/pkg/query-service/rules/resultTypes.go index e7e67bc7bd..de2095eea9 100644 --- a/pkg/query-service/rules/resultTypes.go +++ b/pkg/query-service/rules/resultTypes.go @@ -34,8 +34,9 @@ func (s Sample) MarshalJSON() ([]byte, error) { } type Point struct { - T int64 - V float64 + T int64 + V float64 + Vs []float64 } func (p Point) String() string { diff --git a/pkg/query-service/rules/thresholdRule.go b/pkg/query-service/rules/thresholdRule.go index f6e79a1643..fbd65d9948 100644 --- a/pkg/query-service/rules/thresholdRule.go +++ b/pkg/query-service/rules/thresholdRule.go @@ -550,7 +550,40 @@ func (r *ThresholdRule) runChQuery(ctx context.Context, db clickhouse.Conn, quer } } + + if s, ok := resultMap[labelHash]; ok { + s.Point.Vs = append(s.Point.Vs, s.Point.V) + } } + + for _, s := range resultMap { + if r.matchType() == AllTheTimes && r.compareOp() == ValueIsEq { + for _, v := range s.Point.Vs { + if v != r.targetVal() { // if any of the values is not equal to target, alert shouldn't be sent + s.Point.V = v + } + } + } else if r.matchType() == AllTheTimes && r.compareOp() == ValueIsNotEq { + for _, v := range s.Point.Vs { + if v == r.targetVal() { // if any of the values is equal to target, alert shouldn't be sent + s.Point.V = v + } + } + } else if r.matchType() == AtleastOnce && r.compareOp() == ValueIsEq { + for _, v := range s.Point.Vs { + if v == r.targetVal() { // if any of the values is equal to target, alert should be sent + s.Point.V = v + } + } + } else if r.matchType() == AtleastOnce && r.compareOp() == ValueIsNotEq { + for _, v := range s.Point.Vs { + if v != r.targetVal() { // if any of the values is not equal to target, alert should be sent + s.Point.V = v + } + } + } + } + zap.S().Debugf("ruleid:", r.ID(), "\t resultmap(potential alerts):", len(resultMap)) for _, sample := range resultMap {