fixed alerting bug
This commit is contained in:
parent
bc27ebce63
commit
e13a06a31a
|
@ -60,6 +60,7 @@ type AlertMessage struct {
|
||||||
Created time.Time `json:"created,omitempty" elastic_mapping:"created: { type: date }"`
|
Created time.Time `json:"created,omitempty" elastic_mapping:"created: { type: date }"`
|
||||||
Updated time.Time `json:"updated,omitempty" elastic_mapping:"updated: { 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 }"`
|
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 }"`
|
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 }"`
|
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 }"`
|
Status string `json:"status" elastic_mapping:"status: { type: keyword,copy_to:search_text }"`
|
||||||
|
|
|
@ -153,6 +153,15 @@ func (h *AlertAPI) searchAlertMessage(w http.ResponseWriter, req *http.Request,
|
||||||
if ruleID != "" {
|
if ruleID != "" {
|
||||||
mustBuilder.WriteString(fmt.Sprintf(`,{"term":{"rule_id":{"value":"%s"}}}`, 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 != "" {
|
if sort != "" {
|
||||||
sortParts := strings.Split(sort, ",")
|
sortParts := strings.Split(sort, ",")
|
||||||
|
|
|
@ -81,7 +81,7 @@ func (alertAPI *AlertAPI) createRule(w http.ResponseWriter, req *http.Request, p
|
||||||
ID: rule.ID,
|
ID: rule.ID,
|
||||||
Interval: rule.Schedule.Interval,
|
Interval: rule.Schedule.Interval,
|
||||||
Description: rule.Metrics.Expression,
|
Description: rule.Metrics.Expression,
|
||||||
Task: eng.GenerateTask(&rule),
|
Task: eng.GenerateTask(rule),
|
||||||
}
|
}
|
||||||
task.RegisterScheduleTask(ruleTask)
|
task.RegisterScheduleTask(ruleTask)
|
||||||
task.StartTask(ruleTask.ID)
|
task.StartTask(ruleTask.ID)
|
||||||
|
@ -309,7 +309,7 @@ func (alertAPI *AlertAPI) updateRule(w http.ResponseWriter, req *http.Request, p
|
||||||
ID: rule.ID,
|
ID: rule.ID,
|
||||||
Interval: rule.Schedule.Interval,
|
Interval: rule.Schedule.Interval,
|
||||||
Description: rule.Metrics.Expression,
|
Description: rule.Metrics.Expression,
|
||||||
Task: eng.GenerateTask(rule),
|
Task: eng.GenerateTask(*rule),
|
||||||
}
|
}
|
||||||
task.RegisterScheduleTask(ruleTask)
|
task.RegisterScheduleTask(ruleTask)
|
||||||
task.StartTask(ruleTask.ID)
|
task.StartTask(ruleTask.ID)
|
||||||
|
@ -390,6 +390,15 @@ func (alertAPI *AlertAPI) searchRule(w http.ResponseWriter, req *http.Request, p
|
||||||
|
|
||||||
mustQuery := []util.MapStr{
|
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 != "" {
|
if keyword != "" {
|
||||||
mustQuery = append(mustQuery, util.MapStr{
|
mustQuery = append(mustQuery, util.MapStr{
|
||||||
"match": util.MapStr{
|
"match": util.MapStr{
|
||||||
|
@ -568,7 +577,7 @@ func (alertAPI *AlertAPI) enableRule(w http.ResponseWriter, req *http.Request, p
|
||||||
ID: obj.ID,
|
ID: obj.ID,
|
||||||
Interval: obj.Schedule.Interval,
|
Interval: obj.Schedule.Interval,
|
||||||
Description: obj.Metrics.Expression,
|
Description: obj.Metrics.Expression,
|
||||||
Task: eng.GenerateTask(&obj),
|
Task: eng.GenerateTask(obj),
|
||||||
}
|
}
|
||||||
task.RegisterScheduleTask(ruleTask)
|
task.RegisterScheduleTask(ruleTask)
|
||||||
task.StartTask(ruleTask.ID)
|
task.StartTask(ruleTask.ID)
|
||||||
|
@ -749,6 +758,10 @@ func getRuleMetricData( rule *alerting.Rule, filterParam *alerting.FilterParam)
|
||||||
if sampleData == nil {
|
if sampleData == nil {
|
||||||
sampleData = targetData
|
sampleData = targetData
|
||||||
}
|
}
|
||||||
|
var label = strings.Join(md.GroupValues, "-")
|
||||||
|
if label == "" {
|
||||||
|
label, _ = rule.GetOrInitExpression()
|
||||||
|
}
|
||||||
metricItem.Lines = append(metricItem.Lines, &common.MetricLine{
|
metricItem.Lines = append(metricItem.Lines, &common.MetricLine{
|
||||||
Data: targetData,
|
Data: targetData,
|
||||||
BucketSize: filterParam.BucketSize,
|
BucketSize: filterParam.BucketSize,
|
||||||
|
|
|
@ -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) {
|
func (handler APIHandler) HandleCreateIndexAction(w http.ResponseWriter, req *http.Request, ps httprouter.Params) {
|
||||||
|
|
||||||
targetClusterID := ps.ByName("id")
|
targetClusterID := ps.ByName("id")
|
||||||
client := elastic.GetClient(targetClusterID)
|
client := elastic.GetClient(targetClusterID)
|
||||||
indexName := ps.ByName("index")
|
indexName := ps.ByName("index")
|
||||||
|
|
|
@ -36,11 +36,11 @@ func Init(cfg *config.AppConfig) {
|
||||||
api.HandleAPIMethod(api.DELETE, path.Join(pathPrefix, "rebuild/:id"), handler.HandleDeleteRebuildAction)
|
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, "_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/_mappings"), handler.IndexRequired(handler.HandleGetMappingsAction, "indices.get_mapping"))
|
||||||
api.HandleAPIMethod(api.GET, path.Join(esPrefix, "index/:index/_settings"), handler.HandleGetSettingsAction)
|
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.HandleUpdateSettingsAction)
|
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.HandleDeleteIndexAction)
|
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.HandleCreateIndexAction)
|
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.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))
|
api.HandleAPIMethod(api.PUT, path.Join(pathPrefix, "elasticsearch/command/:cid"), handler.RequirePermission(handler.HandleSaveCommonCommandAction, enum.PermissionCommandWrite))
|
||||||
|
|
|
@ -693,6 +693,7 @@ func (engine *Engine) Do(rule *alerting.Rule) error {
|
||||||
Created: time.Now(),
|
Created: time.Now(),
|
||||||
Updated: time.Now(),
|
Updated: time.Now(),
|
||||||
ID: util.GetUUID(),
|
ID: util.GetUUID(),
|
||||||
|
ResourceID: rule.Resource.ID,
|
||||||
Status: alerting.MessageStateAlerting,
|
Status: alerting.MessageStateAlerting,
|
||||||
Severity: severity,
|
Severity: severity,
|
||||||
Title: alertItem.Title,
|
Title: alertItem.Title,
|
||||||
|
@ -953,7 +954,7 @@ func performChannel(channel *alerting.Channel, ctx map[string]interface{}) ([]by
|
||||||
executeResult, err := act.Execute()
|
executeResult, err := act.Execute()
|
||||||
return executeResult, err, message
|
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) {
|
return func(ctx context.Context) {
|
||||||
defer func() {
|
defer func() {
|
||||||
if err := recover(); err != nil {
|
if err := recover(); err != nil {
|
||||||
|
@ -961,7 +962,7 @@ func (engine *Engine) GenerateTask(rule *alerting.Rule) func(ctx context.Context
|
||||||
debug.PrintStack()
|
debug.PrintStack()
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
err := engine.Do(rule)
|
err := engine.Do(&rule)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error(err)
|
log.Error(err)
|
||||||
}
|
}
|
||||||
|
|
|
@ -15,7 +15,7 @@ type Engine interface {
|
||||||
GenerateQuery(rule *alerting.Rule, filterParam *alerting.FilterParam) (interface{}, error)
|
GenerateQuery(rule *alerting.Rule, filterParam *alerting.FilterParam) (interface{}, error)
|
||||||
ExecuteQuery(rule *alerting.Rule, filterParam *alerting.FilterParam)(*alerting.QueryResult, error)
|
ExecuteQuery(rule *alerting.Rule, filterParam *alerting.FilterParam)(*alerting.QueryResult, error)
|
||||||
CheckCondition(rule *alerting.Rule)(*alerting.ConditionResult, 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)
|
Test(rule *alerting.Rule) ([]alerting.ActionExecutionResult, error)
|
||||||
GetTargetMetricData(rule *alerting.Rule, isFilterNaN bool, filterParam *alerting.FilterParam)([]alerting.MetricData, *alerting.QueryResult, error)
|
GetTargetMetricData(rule *alerting.Rule, isFilterNaN bool, filterParam *alerting.FilterParam)([]alerting.MetricData, *alerting.QueryResult, error)
|
||||||
}
|
}
|
||||||
|
|
|
@ -39,7 +39,7 @@ func InitTasks() error {
|
||||||
ID: rule.ID,
|
ID: rule.ID,
|
||||||
Interval: rule.Schedule.Interval,
|
Interval: rule.Schedule.Interval,
|
||||||
Description: rule.Metrics.Expression,
|
Description: rule.Metrics.Expression,
|
||||||
Task: eng.GenerateTask(rule),
|
Task: eng.GenerateTask(*rule),
|
||||||
})
|
})
|
||||||
task.StartTask(rule.ID)
|
task.StartTask(rule.ID)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue