From cb177bc4db103b42ef1823d184984b33135f7852 Mon Sep 17 00:00:00 2001 From: zhaoxiangchun <1422928955@qq.com> Date: Tue, 30 Mar 2021 18:14:24 +0800 Subject: [PATCH] fix(monitor): alertQuery fix MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 1.兼容meter相关的监控数据查询 2.根据groupby 确定resType,从而确定云管中对应的资源,然后进行属性填充,解决资源属性修改的问题 --- pkg/apis/monitor/alertquery_const.go | 11 ++++++ pkg/apis/monitor/metric.go | 2 + pkg/apis/monitor/unifiedmonitor_const.go | 14 ++++--- .../alerting/conditions/metricquery.go | 13 ++++--- pkg/monitor/alerting/conditions/query.go | 39 +++++++++++++++++-- pkg/monitor/alerting/notifier.go | 5 ++- pkg/monitor/models/unifiedmonitor.go | 37 ++++++++++++------ 7 files changed, 94 insertions(+), 27 deletions(-) diff --git a/pkg/apis/monitor/alertquery_const.go b/pkg/apis/monitor/alertquery_const.go index 1dec844f1b..161d742cc9 100644 --- a/pkg/apis/monitor/alertquery_const.go +++ b/pkg/apis/monitor/alertquery_const.go @@ -133,4 +133,15 @@ var ( "domain_id": "domain_id", "project_domain": "project_domain", } + + TenantTags = map[string]string{ + "tenant_id": "id", + "tenant": "name", + "domain_id": "domain_id", + "project_domain": "project_domain", + } + DomainTags = map[string]string{ + "domain_id": "id", + "project_domain": "name", + } ) diff --git a/pkg/apis/monitor/metric.go b/pkg/apis/monitor/metric.go index c6de9f5583..0ae2d63f75 100644 --- a/pkg/apis/monitor/metric.go +++ b/pkg/apis/monitor/metric.go @@ -9,6 +9,8 @@ const ( METRIC_RES_TYPE_OSS = "oss" METRIC_RES_TYPE_RDS = "rds" METRIC_RES_TYPE_CLOUDACCOUNT = "cloudaccount" + METRIC_RES_TYPE_TENANT = "tenant" + METRIC_RES_TYPE_DOMAIN = "domain" METRIC_UNIT_PERCENT = "%" METRIC_UNIT_BPS = "bps" diff --git a/pkg/apis/monitor/unifiedmonitor_const.go b/pkg/apis/monitor/unifiedmonitor_const.go index 8615ffb271..eb166efec6 100644 --- a/pkg/apis/monitor/unifiedmonitor_const.go +++ b/pkg/apis/monitor/unifiedmonitor_const.go @@ -21,12 +21,14 @@ var ( "cloudaccount": "cloudaccount_name", } MEASUREMENT_TAG_ID = map[string]string{ - "host": "host_id", - "guest": "vm_id", - "redis": "redis_id", - "rds": "rds_id", - "oss": "oss_id", - "cloudaccount": "cloudaccount_id", + METRIC_RES_TYPE_HOST: "host_id", + METRIC_RES_TYPE_GUEST: "vm_id", + METRIC_RES_TYPE_REDIS: "redis_id", + METRIC_RES_TYPE_RDS: "rds_id", + METRIC_RES_TYPE_OSS: "oss_id", + METRIC_RES_TYPE_CLOUDACCOUNT: "cloudaccount_id", + METRIC_RES_TYPE_TENANT: "tenant_id", + METRIC_RES_TYPE_DOMAIN: "domain_id", } AlertReduceFunc = map[string]string{ "avg": "average value", diff --git a/pkg/monitor/alerting/conditions/metricquery.go b/pkg/monitor/alerting/conditions/metricquery.go index 2e153ca8f2..0b782e26c5 100644 --- a/pkg/monitor/alerting/conditions/metricquery.go +++ b/pkg/monitor/alerting/conditions/metricquery.go @@ -64,14 +64,18 @@ func (query *MetricQueryCondition) ExecuteQuery() (*mq.Metrics, error) { if err != nil { return nil, err } + metrics := mq.Metrics{ + Series: make(tsdb.TimeSeriesSlice, 0), + Metas: queryResult.metas, + } + if len(query.QueryCons[0].ResType) == 0 { + metrics.Series = queryResult.series + return &metrics, nil + } allResources, err := query.QueryCons[0].GetQueryResources() if err != nil { return nil, errors.Wrap(err, "MetricQueryCondition GetQueryResources err") } - metrics := mq.Metrics{ - Series: make(tsdb.TimeSeriesSlice, 0), - Metas: nil, - } for _, serie := range queryResult.series { isLatestOfSerie, resource := query.QueryCons[0].serieIsLatestResource(allResources, serie) if !isLatestOfSerie { @@ -80,7 +84,6 @@ func (query *MetricQueryCondition) ExecuteQuery() (*mq.Metrics, error) { query.QueryCons[0].FillSerieByResourceField(resource, serie) metrics.Series = append(metrics.Series, serie) } - metrics.Metas = queryResult.metas return &metrics, nil } diff --git a/pkg/monitor/alerting/conditions/query.go b/pkg/monitor/alerting/conditions/query.go index 3f3cab1901..8a6bfcda9f 100644 --- a/pkg/monitor/alerting/conditions/query.go +++ b/pkg/monitor/alerting/conditions/query.go @@ -161,11 +161,13 @@ func (c *QueryCondition) Eval(context *alerting.EvalContext) (*alerting.Conditio return nil, errors.Wrap(err, "GetQueryResources err") } for _, series := range seriesList { - isLatestOfSerie, resource := c.serieIsLatestResource(allResources, series) - if !isLatestOfSerie { - continue + if len(c.ResType) != 0 { + isLatestOfSerie, resource := c.serieIsLatestResource(allResources, series) + if !isLatestOfSerie { + continue + } + c.FillSerieByResourceField(resource, series) } - c.FillSerieByResourceField(resource, series) reducedValue, valStrArr := c.Reducer.Reduce(series) evalMatch := c.Evaluator.Eval(reducedValue) @@ -494,10 +496,28 @@ func newQueryCondition(model *monitor.AlertCondition, index int) (*QueryConditio func (c *QueryCondition) setResType() { var resType = "" + if len(c.Query.Model.GroupBy) == 0 { + return + } metricMeasurement, _ := models.MetricMeasurementManager.GetCache().Get(c.Query.Model.Measurement) if metricMeasurement != nil { resType = metricMeasurement.ResType } + if len(resType) != 0 && c.Query.Model.GroupBy[0].Params[0] != monitor. + MEASUREMENT_TAG_ID[resType] { + resType = "" + for _, groupBy := range c.Query.Model.GroupBy { + tag := groupBy.Params[0] + if tag == "tenant_id" { + resType = monitor.METRIC_RES_TYPE_TENANT + break + } + if tag == "domain_id" { + resType = monitor.METRIC_RES_TYPE_DOMAIN + break + } + } + } c.ResType = resType } @@ -535,6 +555,10 @@ func (c *QueryCondition) getOnecloudResources() ([]jsonutils.JSONObject, error) allResources, err = ListAllResources(&mc_mds.ElasticCache, query) case monitor.METRIC_RES_TYPE_OSS: allResources, err = ListAllResources(&mc_mds.Buckets, query) + case monitor.METRIC_RES_TYPE_TENANT: + allResources, err = ListAllResources(&mc_mds.Projects, query) + case monitor.METRIC_RES_TYPE_DOMAIN: + allResources, err = ListAllResources(&mc_mds.Domains, query) default: query := jsonutils.NewDict() query.Set("brand", jsonutils.NewString(hostconsts.TELEGRAF_TAG_ONECLOUD_BRAND)) @@ -635,6 +659,9 @@ func (c *QueryCondition) getFilterResources(start int, end int, tmp := resources for i := start; i <= end; i++ { tag := c.Query.Model.Tags[i] + if tag.Key == hostconsts.TELEGRAF_TAG_KEY_RES_TYPE { + continue + } relationKey := relationMap[tag.Key] filterObj := make([]jsonutils.JSONObject, 0) for _, res := range tmp { @@ -691,6 +718,10 @@ func (c *QueryCondition) getTagKeyRelationMap() map[string]string { relationMap = monitor.RedisTags case monitor.METRIC_RES_TYPE_OSS: relationMap = monitor.OssTags + case monitor.METRIC_RES_TYPE_TENANT: + relationMap = monitor.TenantTags + case monitor.METRIC_RES_TYPE_DOMAIN: + relationMap = monitor.DomainTags default: relationMap = monitor.HostTags } diff --git a/pkg/monitor/alerting/notifier.go b/pkg/monitor/alerting/notifier.go index 7f32e57b1a..c8dc76a049 100644 --- a/pkg/monitor/alerting/notifier.go +++ b/pkg/monitor/alerting/notifier.go @@ -249,7 +249,10 @@ func newAlertRecordRule(evalCtx *EvalContext) monitor.AlertRecordRule { alertRule.Period = fmt.Sprintf("%dm", evalCtx.Rule.Frequency/60) } - alertRule.AlertDuration = int64(evalCtx.Rule.For)/evalCtx.Rule.Frequency + 1 + alertRule.AlertDuration = int64(evalCtx.Rule.For) / evalCtx.Rule.Frequency + if alertRule.AlertDuration == 0 { + alertRule.AlertDuration = 1 + } if evalCtx.Rule.SilentPeriod != 0 { alertRule.SilentPeriod = fmt.Sprintf("%dm", evalCtx.Rule.SilentPeriod/60) diff --git a/pkg/monitor/models/unifiedmonitor.go b/pkg/monitor/models/unifiedmonitor.go index c369fe27ad..9c2e72ba4a 100644 --- a/pkg/monitor/models/unifiedmonitor.go +++ b/pkg/monitor/models/unifiedmonitor.go @@ -313,17 +313,7 @@ func setDefaultValue(query *monitor.AlertQuery, inputQuery *monitor.MetricInputQ metricMeasurement, _ := MetricMeasurementManager.GetCache().Get(query.Model.Measurement) - if true { - tagId := "host_id" - if metricMeasurement != nil { - tagId = monitor.MEASUREMENT_TAG_ID[metricMeasurement.ResType] - } - query.Model.GroupBy = append(query.Model.GroupBy, - monitor.MetricQueryPart{ - Type: "field", - Params: []string{tagId}, - }) - } + checkQueryGroupBy(query, inputQuery) if len(inputQuery.Interval) != 0 { query.Model.GroupBy = append(query.Model.GroupBy, @@ -413,6 +403,31 @@ func setDataSourceId(query *monitor.AlertQuery) { query.DataSourceId = datasource.Id } +func checkQueryGroupBy(query *monitor.AlertQuery, inputQuery *monitor.MetricInputQuery) { + if len(query.Model.GroupBy) != 0 { + return + } + if inputQuery.Unit { + return + } + if query.Model.Database == monitor.METRIC_DATABASE_METER && inputQuery.Unit { + return + } + metricMeasurement, _ := MetricMeasurementManager.GetCache().Get(query.Model.Measurement) + tagId := "" + if metricMeasurement != nil { + tagId = monitor.MEASUREMENT_TAG_ID[metricMeasurement.ResType] + } + if len(tagId) == 0 { + tagId = "*" + } + query.Model.GroupBy = append(query.Model.GroupBy, + monitor.MetricQueryPart{ + Type: "field", + Params: []string{tagId}, + }) +} + func setSerieRowName(series *tsdb.TimeSeriesSlice, groupTag []string) { //Add rowname,The front end displays the curve according to rowname var index, unknownIndex = 1, 1