From d63147a2abafc3c613c0c03b51c0f4de84f57af7 Mon Sep 17 00:00:00 2001 From: liugq Date: Fri, 13 May 2022 16:03:59 +0800 Subject: [PATCH] copy channel name and type to execution result --- model/alerting/alert.go | 6 +++-- model/alerting/rule.go | 1 + plugin/api/alerting/api.go | 2 +- plugin/api/alerting/rule.go | 29 +++++++++++++++++------- service/alerting/elasticsearch/engine.go | 17 +++++++++----- 5 files changed, 38 insertions(+), 17 deletions(-) diff --git a/model/alerting/alert.go b/model/alerting/alert.go index 7d9ac6d7..6d80afb8 100644 --- a/model/alerting/alert.go +++ b/model/alerting/alert.go @@ -32,10 +32,12 @@ type Alert struct { } type ActionExecutionResult struct { - LastExecutionTime int `json:"last_execution_time"` - Error string `json:"error"` + ExecutionTime int `json:"execution_time"` + Error string `json:"error"` Result string `json:"result"` Message string `json:"message"` + ChannelName string `json:"channel_name"` + ChannelType string `json:"channel_type"` } const ( diff --git a/model/alerting/rule.go b/model/alerting/rule.go index 6848ff5b..dafeb501 100644 --- a/model/alerting/rule.go +++ b/model/alerting/rule.go @@ -78,6 +78,7 @@ func (tr *TimeRange) Include( t time.Time) bool { type FilterParam struct { Start interface{} `json:"start"` End interface{} `json:"end"` + BucketSize string `json:"bucket_size"` } //ctx //rule expression, rule_id, resource_id, resource_name, event_id, condition_name, preset_value,[group_tags, check_values], diff --git a/plugin/api/alerting/api.go b/plugin/api/alerting/api.go index c8fb0cc6..0a29d4b1 100644 --- a/plugin/api/alerting/api.go +++ b/plugin/api/alerting/api.go @@ -25,7 +25,7 @@ func (alert *AlertAPI) Init() { api.HandleAPIMethod(api.GET, "/alerting/stats", alert.getAlertStats) api.HandleAPIMethod(api.POST, "/alerting/rule/info", alert.fetchAlertInfos) api.HandleAPIMethod(api.POST, "/alerting/rule/:rule_id/_enable", alert.enableRule) - api.HandleAPIMethod(api.POST, "/alerting/rule/metric", alert.getMetricData) + api.HandleAPIMethod(api.GET, "/alerting/rule/:rule_id/metric", alert.getMetricData) api.HandleAPIMethod(api.GET, "/alerting/channel/:channel_id", alert.getChannel) api.HandleAPIMethod(api.POST, "/alerting/channel", alert.createChannel) diff --git a/plugin/api/alerting/rule.go b/plugin/api/alerting/rule.go index 9c1171d2..7b3a5e1b 100644 --- a/plugin/api/alerting/rule.go +++ b/plugin/api/alerting/rule.go @@ -16,6 +16,7 @@ import ( "infini.sh/framework/core/orm" "infini.sh/framework/core/task" "infini.sh/framework/core/util" + "infini.sh/framework/modules/elastic/api" "net/http" "time" ) @@ -476,17 +477,29 @@ func (alertAPI *AlertAPI) getTemplateParams(w http.ResponseWriter, req *http.Req } func (alertAPI *AlertAPI) getMetricData(w http.ResponseWriter, req *http.Request, ps httprouter.Params) { - rule := alerting.Rule{} - err := alertAPI.DecodeJSON(req, &rule) - if err != nil { - log.Error(err) + rule := &alerting.Rule{ + ID: ps.ByName("rule_id"), + } + exists, err := orm.Get(rule) + if !exists || err != nil { alertAPI.WriteJSON(w, util.MapStr{ - "error": err.Error(), - }, http.StatusInternalServerError) + "_id": rule.ID, + "found": false, + }, http.StatusNotFound) return } + var ( + minStr = alertAPI.Get(req, "min", "") + maxStr = alertAPI.Get(req, "max", "") + ) + bucketSize, min, max, err := api.GetMetricRangeAndBucketSize(minStr, maxStr, 60, 15) + filterParam := &alerting.FilterParam{ + Start: min, + End: max, + BucketSize: fmt.Sprintf("%ds", bucketSize), + } eng := alerting2.GetEngine(rule.Resource.Type) - metricData, err := eng.GetTargetMetricData(&rule, true, nil) + metricData, err := eng.GetTargetMetricData(rule, true, filterParam) if err != nil { log.Error(err) alertAPI.WriteJSON(w, util.MapStr{ @@ -502,7 +515,7 @@ func (alertAPI *AlertAPI) getMetricData(w http.ResponseWriter, req *http.Request filteredMetricData = append(filteredMetricData, md) } alertAPI.WriteJSON(w, util.MapStr{ - "metric": filteredMetricData, + "metrics": filteredMetricData, }, http.StatusOK) } diff --git a/service/alerting/elasticsearch/engine.go b/service/alerting/elasticsearch/engine.go index 6e972093..205587f2 100644 --- a/service/alerting/elasticsearch/engine.go +++ b/service/alerting/elasticsearch/engine.go @@ -56,10 +56,14 @@ func (engine *Engine) GenerateQuery(rule *alerting.Rule, filterParam *alerting.F if err != nil { return nil, fmt.Errorf("get interval field error: %w", err) } + var periodInterval = rule.Metrics.PeriodInterval + if filterParam != nil && filterParam.BucketSize != "" { + periodInterval = filterParam.BucketSize + } timeAggs := util.MapStr{ "date_histogram": util.MapStr{ "field": rule.Resource.TimeField, - intervalField: rule.Metrics.PeriodInterval, + intervalField: periodInterval, }, "aggs": basicAggs, } @@ -263,7 +267,6 @@ func (engine *Engine) GenerateRawFilter(rule *alerting.Rule, filterParam *alerti var err error if rule.Resource.RawFilter != nil { query = util.DeepCopy(rule.Resource.RawFilter).(map[string]interface{}) - }else{ if !rule.Resource.Filter.IsEmpty(){ query, err = engine.ConvertFilterQueryToDsl(&rule.Resource.Filter) @@ -742,10 +745,12 @@ func performChannels(channels []alerting.Channel, ctx map[string]interface{}) ([ errStr = err.Error() } actionResults = append(actionResults, alerting.ActionExecutionResult{ - Result: string(resBytes), - Error: errStr, - Message: string(messageBytes), - LastExecutionTime: int(time.Now().UnixNano()/1e6), + Result: string(resBytes), + Error: errStr, + Message: string(messageBytes), + ExecutionTime: int(time.Now().UnixNano()/1e6), + ChannelType: channel.Type, + ChannelName: channel.Name, }) } return actionResults, errCount