mirror of
https://github.com/yunionio/cloudpods.git
synced 2026-09-24 16:03:43 +08:00
fix(monitor): alertQuery fix
1.兼容meter相关的监控数据查询 2.根据groupby 确定resType,从而确定云管中对应的资源,然后进行属性填充,解决资源属性修改的问题
This commit is contained in:
@@ -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",
|
||||
}
|
||||
)
|
||||
|
||||
@@ -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"
|
||||
|
||||
@@ -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",
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user