From 357b5c1f43e148fa541979facd931caeaf3ade0d Mon Sep 17 00:00:00 2001 From: Zexi Li Date: Tue, 18 Mar 2025 11:52:25 +0800 Subject: [PATCH] feat(monitor): support listing monitor_resource by alert_state (#22287) --- pkg/apis/monitor/monitor_resource.go | 7 ++++--- pkg/mcclient/options/monitor/monitor_resource.go | 7 ++++--- pkg/monitor/alerting/conditions/query.go | 12 +++++++++--- pkg/monitor/models/monitor_resource.go | 3 +++ 4 files changed, 20 insertions(+), 9 deletions(-) diff --git a/pkg/apis/monitor/monitor_resource.go b/pkg/apis/monitor/monitor_resource.go index 65f1fc6d4d..a65af14b3b 100644 --- a/pkg/apis/monitor/monitor_resource.go +++ b/pkg/apis/monitor/monitor_resource.go @@ -39,9 +39,10 @@ type MonitorResourceListInput struct { apis.EnabledResourceBaseListInput compute.ManagedResourceListInput - ResId []string `json:"res_id"` - ResType string `json:"res_type"` - OnlyResId bool `json:"only_res_id"` + ResId []string `json:"res_id"` + ResType string `json:"res_type"` + OnlyResId bool `json:"only_res_id"` + AlertStates []string `json:"alert_states"` ResName string `json:"res_name"` } diff --git a/pkg/mcclient/options/monitor/monitor_resource.go b/pkg/mcclient/options/monitor/monitor_resource.go index 448ec233e8..484bd30997 100644 --- a/pkg/mcclient/options/monitor/monitor_resource.go +++ b/pkg/mcclient/options/monitor/monitor_resource.go @@ -33,9 +33,10 @@ func (o *MonitorResourceJointAlertOptions) Property() string { type MonitorResourceListOptions struct { options.BaseListOptions - ResType string `help:"filter by resource type" json:"res_type"` - ResId []string `help:"filter by resource id" json:"res_id"` - ResName string `help:"filter by resource name" json:"res_name"` + ResType string `help:"filter by resource type" json:"res_type"` + ResId []string `help:"filter by resource id" json:"res_id"` + ResName string `help:"filter by resource name" json:"res_name"` + AlertStates []string `help:"filter by alert state" json:"alert_states"` } func (o *MonitorResourceListOptions) Params() (jsonutils.JSONObject, error) { diff --git a/pkg/monitor/alerting/conditions/query.go b/pkg/monitor/alerting/conditions/query.go index 10fc2c30af..6f2e43695d 100644 --- a/pkg/monitor/alerting/conditions/query.go +++ b/pkg/monitor/alerting/conditions/query.go @@ -110,9 +110,15 @@ func (c FormatCond) String() string { return "no_data" } -func (c *QueryCondition) filterTags(tags map[string]string, details monitor.CommonAlertMetricDetails) map[string]string { +func (c *QueryCondition) filterTags(tags map[string]string, cloudTags map[string]string, details monitor.CommonAlertMetricDetails) map[string]string { ret := make(map[string]string) - for key, val := range tags { + allTags := make(map[string]string) + for _, ts := range []map[string]string{tags, cloudTags} { + for k, v := range ts { + allTags[k] = v + } + } + for key, val := range allTags { //if strings.HasSuffix(key, "_id") { // continue //} @@ -303,7 +309,7 @@ func (c *QueryCondition) NewEvalMatch( queryKeyInfo = evalMatch.Metric } evalMatch.Unit = alertDetails.FieldDescription.Unit - evalMatch.Tags = c.filterTags(series.Tags, *alertDetails) + evalMatch.Tags = c.filterTags(series.Tags, series.CloudTags, *alertDetails) evalMatch.Value = value evalMatch.ValueStr = models.RationalizeValueFromUnit(*value, alertDetails.FieldDescription.Unit, alertDetails.FieldOpt) diff --git a/pkg/monitor/models/monitor_resource.go b/pkg/monitor/models/monitor_resource.go index b73e8cf34a..bf06faec6d 100644 --- a/pkg/monitor/models/monitor_resource.go +++ b/pkg/monitor/models/monitor_resource.go @@ -203,6 +203,9 @@ func (manager *SMonitorResourceManager) FieldListFilter(q *sqlchemy.SQuery, quer if len(query.ResName) != 0 { q.Contains("name", query.ResName) } + if len(query.AlertStates) != 0 { + q.In("alert_state", query.AlertStates) + } return q }