fixed alerting bug

This commit is contained in:
liugq 2022-06-02 18:39:43 +08:00
parent bc27ebce63
commit e13a06a31a
8 changed files with 37 additions and 12 deletions

View File

@ -60,6 +60,7 @@ type AlertMessage struct {
Created time.Time `json:"created,omitempty" elastic_mapping:"created: { type: date }"`
Updated time.Time `json:"updated,omitempty" elastic_mapping:"updated: { type: date }"`
RuleID string `json:"rule_id" elastic_mapping:"rule_id: { type: keyword,copy_to:search_text }"`
ResourceID string `json:"resource_id" elastic_mapping:"resource_id: { type: keyword,copy_to:search_text }"`
Title string `json:"title" elastic_mapping:"title: { type: keyword,copy_to:search_text }"`
Message string `json:"message" elastic_mapping:"content: { type: keyword,copy_to:search_text }"`
Status string `json:"status" elastic_mapping:"status: { type: keyword,copy_to:search_text }"`

View File

@ -153,6 +153,15 @@ func (h *AlertAPI) searchAlertMessage(w http.ResponseWriter, req *http.Request,
if ruleID != "" {
mustBuilder.WriteString(fmt.Sprintf(`,{"term":{"rule_id":{"value":"%s"}}}`, ruleID))
}
clusterFilter, hasAllPrivilege := h.GetClusterFilter(req, "resource_id")
if !hasAllPrivilege && clusterFilter == nil {
h.WriteJSON(w, elastic.SearchResponse{}, http.StatusOK)
return
}
if !hasAllPrivilege {
mustBuilder.WriteString(",")
mustBuilder.Write(util.MustToJSONBytes(clusterFilter))
}
if sort != "" {
sortParts := strings.Split(sort, ",")

View File

@ -81,7 +81,7 @@ func (alertAPI *AlertAPI) createRule(w http.ResponseWriter, req *http.Request, p
ID: rule.ID,
Interval: rule.Schedule.Interval,
Description: rule.Metrics.Expression,
Task: eng.GenerateTask(&rule),
Task: eng.GenerateTask(rule),
}
task.RegisterScheduleTask(ruleTask)
task.StartTask(ruleTask.ID)
@ -309,7 +309,7 @@ func (alertAPI *AlertAPI) updateRule(w http.ResponseWriter, req *http.Request, p
ID: rule.ID,
Interval: rule.Schedule.Interval,
Description: rule.Metrics.Expression,
Task: eng.GenerateTask(rule),
Task: eng.GenerateTask(*rule),
}
task.RegisterScheduleTask(ruleTask)
task.StartTask(ruleTask.ID)
@ -390,6 +390,15 @@ func (alertAPI *AlertAPI) searchRule(w http.ResponseWriter, req *http.Request, p
mustQuery := []util.MapStr{
}
clusterFilter, hasAllPrivilege := alertAPI.GetClusterFilter(req, "resource.resource_id")
if !hasAllPrivilege && clusterFilter == nil {
alertAPI.WriteJSON(w, elastic.SearchResponse{
}, http.StatusOK)
return
}
if !hasAllPrivilege {
mustQuery = append(mustQuery, clusterFilter)
}
if keyword != "" {
mustQuery = append(mustQuery, util.MapStr{
"match": util.MapStr{
@ -568,7 +577,7 @@ func (alertAPI *AlertAPI) enableRule(w http.ResponseWriter, req *http.Request, p
ID: obj.ID,
Interval: obj.Schedule.Interval,
Description: obj.Metrics.Expression,
Task: eng.GenerateTask(&obj),
Task: eng.GenerateTask(obj),
}
task.RegisterScheduleTask(ruleTask)
task.StartTask(ruleTask.ID)
@ -749,6 +758,10 @@ func getRuleMetricData( rule *alerting.Rule, filterParam *alerting.FilterParam)
if sampleData == nil {
sampleData = targetData
}
var label = strings.Join(md.GroupValues, "-")
if label == "" {
label, _ = rule.GetOrInitExpression()
}
metricItem.Lines = append(metricItem.Lines, &common.MetricLine{
Data: targetData,
BucketSize: filterParam.BucketSize,

View File

@ -117,6 +117,7 @@ func (handler APIHandler) HandleDeleteIndexAction(w http.ResponseWriter, req *ht
}
func (handler APIHandler) HandleCreateIndexAction(w http.ResponseWriter, req *http.Request, ps httprouter.Params) {
targetClusterID := ps.ByName("id")
client := elastic.GetClient(targetClusterID)
indexName := ps.ByName("index")

View File

@ -36,11 +36,11 @@ func Init(cfg *config.AppConfig) {
api.HandleAPIMethod(api.DELETE, path.Join(pathPrefix, "rebuild/:id"), handler.HandleDeleteRebuildAction)
api.HandleAPIMethod(api.GET, path.Join(esPrefix, "_cat/indices"), handler.RequireLogin(handler.HandleGetIndicesAction))
api.HandleAPIMethod(api.GET, path.Join(esPrefix, "index/:index/_mappings"), handler.HandleGetMappingsAction)
api.HandleAPIMethod(api.GET, path.Join(esPrefix, "index/:index/_settings"), handler.HandleGetSettingsAction)
api.HandleAPIMethod(api.PUT, path.Join(esPrefix, "index/:index/_settings"),handler.HandleUpdateSettingsAction)
api.HandleAPIMethod(api.DELETE, path.Join(esPrefix, "index/:index"), handler.HandleDeleteIndexAction)
api.HandleAPIMethod(api.POST, path.Join(esPrefix, "index/:index"), handler.HandleCreateIndexAction)
api.HandleAPIMethod(api.GET, path.Join(esPrefix, "index/:index/_mappings"), handler.IndexRequired(handler.HandleGetMappingsAction, "indices.get_mapping"))
api.HandleAPIMethod(api.GET, path.Join(esPrefix, "index/:index/_settings"), handler.IndexRequired(handler.HandleGetSettingsAction, "indices.get_settings"))
api.HandleAPIMethod(api.PUT, path.Join(esPrefix, "index/:index/_settings"), handler.IndexRequired(handler.HandleUpdateSettingsAction, "indices.put_settings"))
api.HandleAPIMethod(api.DELETE, path.Join(esPrefix, "index/:index"), handler.IndexRequired(handler.HandleDeleteIndexAction, "indices.delete"))
api.HandleAPIMethod(api.POST, path.Join(esPrefix, "index/:index"), handler.IndexRequired(handler.HandleCreateIndexAction, "indices.create"))
api.HandleAPIMethod(api.POST, path.Join(pathPrefix, "elasticsearch/command"), handler.RequirePermission(handler.HandleAddCommonCommandAction, enum.PermissionCommandWrite))
api.HandleAPIMethod(api.PUT, path.Join(pathPrefix, "elasticsearch/command/:cid"), handler.RequirePermission(handler.HandleSaveCommonCommandAction, enum.PermissionCommandWrite))

View File

@ -693,6 +693,7 @@ func (engine *Engine) Do(rule *alerting.Rule) error {
Created: time.Now(),
Updated: time.Now(),
ID: util.GetUUID(),
ResourceID: rule.Resource.ID,
Status: alerting.MessageStateAlerting,
Severity: severity,
Title: alertItem.Title,
@ -953,7 +954,7 @@ func performChannel(channel *alerting.Channel, ctx map[string]interface{}) ([]by
executeResult, err := act.Execute()
return executeResult, err, message
}
func (engine *Engine) GenerateTask(rule *alerting.Rule) func(ctx context.Context) {
func (engine *Engine) GenerateTask(rule alerting.Rule) func(ctx context.Context) {
return func(ctx context.Context) {
defer func() {
if err := recover(); err != nil {
@ -961,7 +962,7 @@ func (engine *Engine) GenerateTask(rule *alerting.Rule) func(ctx context.Context
debug.PrintStack()
}
}()
err := engine.Do(rule)
err := engine.Do(&rule)
if err != nil {
log.Error(err)
}

View File

@ -15,7 +15,7 @@ type Engine interface {
GenerateQuery(rule *alerting.Rule, filterParam *alerting.FilterParam) (interface{}, error)
ExecuteQuery(rule *alerting.Rule, filterParam *alerting.FilterParam)(*alerting.QueryResult, error)
CheckCondition(rule *alerting.Rule)(*alerting.ConditionResult, error)
GenerateTask(rule *alerting.Rule) func(ctx context.Context)
GenerateTask(rule alerting.Rule) func(ctx context.Context)
Test(rule *alerting.Rule) ([]alerting.ActionExecutionResult, error)
GetTargetMetricData(rule *alerting.Rule, isFilterNaN bool, filterParam *alerting.FilterParam)([]alerting.MetricData, *alerting.QueryResult, error)
}

View File

@ -39,7 +39,7 @@ func InitTasks() error {
ID: rule.ID,
Interval: rule.Schedule.Interval,
Description: rule.Metrics.Expression,
Task: eng.GenerateTask(rule),
Task: eng.GenerateTask(*rule),
})
task.StartTask(rule.ID)
}