Skip to content

Commit

Permalink
feat: alert rule query add unit (#2299)
Browse files Browse the repository at this point in the history
Co-authored-by: Xu Bin <[email protected]>
  • Loading branch information
710leo and Reditiny authored Nov 15, 2024
1 parent 643c6c9 commit 890c12f
Show file tree
Hide file tree
Showing 7 changed files with 693 additions and 9 deletions.
20 changes: 11 additions & 9 deletions alert/common/conv.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,21 @@ import (
"strings"

"github.com/ccfos/nightingale/v6/models"
"github.com/ccfos/nightingale/v6/pkg/unit"
"github.com/prometheus/common/model"
)

type AnomalyPoint struct {
Key string `json:"key"`
Labels model.Metric `json:"labels"`
Timestamp int64 `json:"timestamp"`
Value float64 `json:"value"`
Severity int `json:"severity"`
Triggered bool `json:"triggered"`
Query string `json:"query"`
Values string `json:"values"`
RecoverConfig models.RecoverConfig `json:"recover_config"`
Key string `json:"key"`
Labels model.Metric `json:"labels"`
Timestamp int64 `json:"timestamp"`
Value float64 `json:"value"`
Severity int `json:"severity"`
Triggered bool `json:"triggered"`
Query string `json:"query"`
Values string `json:"values"`
ValuesUnit map[string]unit.FormattedValue `json:"values_unit"`
RecoverConfig models.RecoverConfig `json:"recover_config"`
}

func NewAnomalyPoint(key string, labels map[string]string, ts int64, value float64, severity int) AnomalyPoint {
Expand Down
36 changes: 36 additions & 0 deletions alert/eval/eval.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"github.com/ccfos/nightingale/v6/pkg/hash"
"github.com/ccfos/nightingale/v6/pkg/parser"
promsdk "github.com/ccfos/nightingale/v6/pkg/prom"
"github.com/ccfos/nightingale/v6/pkg/unit"
"github.com/ccfos/nightingale/v6/prom"
"github.com/ccfos/nightingale/v6/tdengine"

Expand Down Expand Up @@ -247,7 +248,11 @@ func (arw *AlertRuleWorker) GetPromAnomalyPoint(ruleConfig string) ([]common.Ano
for i := 0; i < len(points); i++ {
points[i].Severity = query.Severity
points[i].Query = promql
points[i].ValuesUnit = map[string]unit.FormattedValue{
"v": unit.ValueFormatter(query.Unit, 2, points[i].Value),
}
}

lst = append(lst, points...)
}
return lst, nil
Expand Down Expand Up @@ -471,11 +476,22 @@ func GetAnomalyPoint(ruleId int64, ruleQuery models.RuleQuery, seriesTagIndexes
return points, recoverPoints
}

unitMap := make(map[string]string)
for _, query := range ruleQuery.Queries {
ref, unit, err := GetQueryRefAndUnit(query)
if err != nil {
continue
}
unitMap[ref] = unit
}

for _, trigger := range ruleQuery.Triggers {
// seriesTagIndex 的 key 仅做分组使用,value 为每组 series 的 hash
seriesTagIndex := ProcessJoins(ruleId, trigger, seriesTagIndexes, seriesStore)

for _, seriesHash := range seriesTagIndex {
valuesUnitMap := make(map[string]unit.FormattedValue)

sort.Slice(seriesHash, func(i, j int) bool {
return seriesHash[i] < seriesHash[j]
})
Expand All @@ -501,6 +517,10 @@ func GetAnomalyPoint(ruleId int64, ruleQuery models.RuleQuery, seriesTagIndexes
continue
}

if u, exists := unitMap[series.Ref]; exists {
valuesUnitMap[series.Ref] = unit.ValueFormatter(u, 2, v)
}

m["$"+series.Ref] = v
m["$"+series.Ref+"."+series.MetricName()] = v
ts = int64(t)
Expand Down Expand Up @@ -529,6 +549,7 @@ func GetAnomalyPoint(ruleId int64, ruleQuery models.RuleQuery, seriesTagIndexes
Triggered: isTriggered,
Query: fmt.Sprintf("query:%+v trigger:%+v", ruleQuery.Queries, trigger),
RecoverConfig: trigger.RecoverConfig,
ValuesUnit: valuesUnitMap,
}

if sample.Query != "" {
Expand Down Expand Up @@ -815,3 +836,18 @@ func GetQueryRef(query interface{}) (string, error) {

return refField.String(), nil
}

func GetQueryRefAndUnit(query interface{}) (string, string, error) {
type Query struct {
Ref string `json:"ref"`
Unit string `json:"unit"`
}

queryMap := Query{}
queryBytes, err := json.Marshal(query)
if err != nil {
return "", "", err
}
json.Unmarshal(queryBytes, &queryMap)
return queryMap.Ref, queryMap.Unit, nil
}
1 change: 1 addition & 0 deletions alert/process/process.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,7 @@ func (p *Processor) BuildEvent(anomalyPoint common.AnomalyPoint, from string, no
event.TargetNote = p.targetNote
event.TriggerValue = anomalyPoint.ReadableValue()
event.TriggerValues = anomalyPoint.Values
event.TriggerValuesJson = models.EventTriggerValues{ValuesWithUnit: anomalyPoint.ValuesUnit}
event.TagsJSON = p.tagsArr
event.Tags = strings.Join(p.tagsArr, ",,")
event.IsRecovered = false
Expand Down
6 changes: 6 additions & 0 deletions models/alert_cur_event.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/ccfos/nightingale/v6/pkg/ctx"
"github.com/ccfos/nightingale/v6/pkg/poster"
"github.com/ccfos/nightingale/v6/pkg/tplx"
"github.com/ccfos/nightingale/v6/pkg/unit"

"github.com/toolkits/pkg/logger"
)
Expand Down Expand Up @@ -49,6 +50,7 @@ type AlertCurEvent struct {
TriggerTime int64 `json:"trigger_time"`
TriggerValue string `json:"trigger_value"`
TriggerValues string `json:"trigger_values" gorm:"-"`
TriggerValuesJson EventTriggerValues `json:"trigger_values_json" gorm:"-"`
Tags string `json:"-"` // for db
TagsJSON []string `json:"tags" gorm:"-"` // for fe
TagsMap map[string]string `json:"tags_map" gorm:"-"` // for internal usage
Expand All @@ -73,6 +75,10 @@ type AlertCurEvent struct {
ExtraInfoMap []map[string]string `json:"extra_info_map" gorm:"-"`
}

type EventTriggerValues struct {
ValuesWithUnit map[string]unit.FormattedValue `json:"values_with_unit"`
}

func (e *AlertCurEvent) TableName() string {
return "alert_cur_event"
}
Expand Down
1 change: 1 addition & 0 deletions models/alert_rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ type PromQuery struct {
PromQl string `json:"prom_ql"`
Severity int `json:"severity"`
RecoverConfig RecoverConfig `json:"recover_config"`
Unit string `json:"unit"`
}

type HostTrigger struct {
Expand Down
Loading

0 comments on commit 890c12f

Please sign in to comment.