update metric data struct
This commit is contained in:
parent
49e6e27d3d
commit
7864a482b7
|
@ -6,24 +6,18 @@ package alerting
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"infini.sh/framework/core/insight"
|
||||||
"regexp"
|
"regexp"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Metric struct {
|
type Metric struct {
|
||||||
PeriodInterval string `json:"period_interval"`
|
insight.Metric
|
||||||
Items []MetricItem `json:"items"`
|
|
||||||
Formula string `json:"formula,omitempty"`
|
|
||||||
Expression string `json:"expression" elastic_mapping:"expression:{type:keyword,copy_to:search_text}"` //告警表达式,自动生成 eg: avg(cpu) > 80
|
Expression string `json:"expression" elastic_mapping:"expression:{type:keyword,copy_to:search_text}"` //告警表达式,自动生成 eg: avg(cpu) > 80
|
||||||
Title string `json:"title"` //text template
|
Title string `json:"title"` //text template
|
||||||
Message string `json:"message"` // text template
|
Message string `json:"message"` // text template
|
||||||
FormatType string `json:"format_type,omitempty"`
|
|
||||||
Groups []MetricGroupItem `json:"groups"` //bucket group
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type MetricGroupItem struct {
|
|
||||||
Field string `json:"field"`
|
|
||||||
Limit int `json:"limit"`
|
|
||||||
}
|
|
||||||
func (m *Metric) GenerateExpression() (string, error){
|
func (m *Metric) GenerateExpression() (string, error){
|
||||||
if len(m.Items) == 1 {
|
if len(m.Items) == 1 {
|
||||||
return fmt.Sprintf("%s(%s)", m.Items[0].Statistic, m.Items[0].Field), nil
|
return fmt.Sprintf("%s(%s)", m.Items[0].Statistic, m.Items[0].Field), nil
|
||||||
|
|
|
@ -185,7 +185,7 @@ func (alertAPI *AlertAPI) getRuleDetail(w http.ResponseWriter, req *http.Request
|
||||||
"rule_name": obj.Name,
|
"rule_name": obj.Name,
|
||||||
"resource_name": obj.Resource.Name,
|
"resource_name": obj.Resource.Name,
|
||||||
"resource_objects": obj.Resource.Objects,
|
"resource_objects": obj.Resource.Objects,
|
||||||
"period_interval": obj.Metrics.PeriodInterval, //统计周期
|
"bucket_size": obj.Metrics.BucketSize, //统计周期
|
||||||
"updated": obj.Updated,
|
"updated": obj.Updated,
|
||||||
"conditions": obj.Conditions,
|
"conditions": obj.Conditions,
|
||||||
"message_count": alertNumbers[obj.ID], //所有关联告警消息数(包括已恢复的)
|
"message_count": alertNumbers[obj.ID], //所有关联告警消息数(包括已恢复的)
|
||||||
|
@ -677,8 +677,8 @@ func (alertAPI *AlertAPI) getPreviewMetricData(w http.ResponseWriter, req *http.
|
||||||
maxStr = alertAPI.Get(req, "max", "")
|
maxStr = alertAPI.Get(req, "max", "")
|
||||||
)
|
)
|
||||||
var bkSize float64 = 60
|
var bkSize float64 = 60
|
||||||
if rule.Metrics.PeriodInterval != "" {
|
if rule.Metrics.BucketSize != "" {
|
||||||
duration, err := time.ParseDuration(rule.Metrics.PeriodInterval)
|
duration, err := time.ParseDuration(rule.Metrics.BucketSize)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error(err)
|
log.Error(err)
|
||||||
alertAPI.WriteJSON(w, util.MapStr{
|
alertAPI.WriteJSON(w, util.MapStr{
|
||||||
|
|
|
@ -15,6 +15,7 @@ import (
|
||||||
"infini.sh/console/service/alerting/action"
|
"infini.sh/console/service/alerting/action"
|
||||||
"infini.sh/console/service/alerting/funcs"
|
"infini.sh/console/service/alerting/funcs"
|
||||||
"infini.sh/framework/core/elastic"
|
"infini.sh/framework/core/elastic"
|
||||||
|
"infini.sh/framework/core/insight"
|
||||||
"infini.sh/framework/core/kv"
|
"infini.sh/framework/core/kv"
|
||||||
"infini.sh/framework/core/orm"
|
"infini.sh/framework/core/orm"
|
||||||
"infini.sh/framework/core/util"
|
"infini.sh/framework/core/util"
|
||||||
|
@ -61,7 +62,7 @@ func (engine *Engine) GenerateQuery(rule *alerting.Rule, filterParam *alerting.F
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("get interval field error: %w", err)
|
return nil, fmt.Errorf("get interval field error: %w", err)
|
||||||
}
|
}
|
||||||
var periodInterval = rule.Metrics.PeriodInterval
|
var periodInterval = rule.Metrics.BucketSize
|
||||||
if filterParam != nil && filterParam.BucketSize != "" {
|
if filterParam != nil && filterParam.BucketSize != "" {
|
||||||
periodInterval = filterParam.BucketSize
|
periodInterval = filterParam.BucketSize
|
||||||
}
|
}
|
||||||
|
@ -124,7 +125,7 @@ func (engine *Engine) GenerateQuery(rule *alerting.Rule, filterParam *alerting.F
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
//generateAgg convert statistic of metric item to elasticsearch aggregation
|
//generateAgg convert statistic of metric item to elasticsearch aggregation
|
||||||
func (engine *Engine) generateAgg(metricItem *alerting.MetricItem) map[string]interface{}{
|
func (engine *Engine) generateAgg(metricItem *insight.MetricItem) map[string]interface{}{
|
||||||
var (
|
var (
|
||||||
aggType = "value_count"
|
aggType = "value_count"
|
||||||
field = metricItem.Field
|
field = metricItem.Field
|
||||||
|
@ -285,7 +286,7 @@ func (engine *Engine) generateTimeFilter(rule *alerting.Rule, filterParam *alert
|
||||||
units string
|
units string
|
||||||
value int
|
value int
|
||||||
)
|
)
|
||||||
intervalDuration, err := time.ParseDuration(rule.Metrics.PeriodInterval)
|
intervalDuration, err := time.ParseDuration(rule.Metrics.BucketSize)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
@ -299,7 +300,7 @@ func (engine *Engine) generateTimeFilter(rule *alerting.Rule, filterParam *alert
|
||||||
units = "s"
|
units = "s"
|
||||||
value = int(intervalDuration / time.Second)
|
value = int(intervalDuration / time.Second)
|
||||||
}else{
|
}else{
|
||||||
return nil, fmt.Errorf("period interval: %s is too small", rule.Metrics.PeriodInterval)
|
return nil, fmt.Errorf("period interval: %s is too small", rule.Metrics.BucketSize)
|
||||||
}
|
}
|
||||||
bucketCount := rule.Conditions.GetMinimumPeriodMatch() + 1
|
bucketCount := rule.Conditions.GetMinimumPeriodMatch() + 1
|
||||||
if bucketCount <= 0 {
|
if bucketCount <= 0 {
|
||||||
|
|
Loading…
Reference in New Issue