fix(monitor): set default query select and group by for alerting and panel (#21672)

This commit is contained in:
Zexi Li
2024-11-22 19:20:31 +08:00
committed by GitHub
parent da7e2a010a
commit c4324e25e6
6 changed files with 33 additions and 18 deletions
+2 -2
View File
@@ -125,7 +125,7 @@ func (man *SAlertPanelManager) ValidateCreateData(
}
}
}
err := CommonAlertManager.ValidateMetricQuery(&data.CommonMetricInputQuery, data.Scope, ownerId)
err := CommonAlertManager.ValidateMetricQuery(&data.CommonMetricInputQuery, data.Scope, ownerId, false)
if err != nil {
return data, errors.Wrap(err, "metric query error")
}
@@ -294,7 +294,7 @@ func (dash *SAlertPanel) ValidateUpdateData(
ownerId = userCred
}
scope, _ := data.GetString("scope")
err = CommonAlertManager.ValidateMetricQuery(metricQuery, scope, ownerId)
err = CommonAlertManager.ValidateMetricQuery(metricQuery, scope, ownerId, false)
if err != nil {
return data, errors.Wrap(err, "metric query error")
}
+4 -4
View File
@@ -242,7 +242,7 @@ func (man *SCommonAlertManager) ValidateCreateData(
return data, httperrors.NewInputParameterError("Invalid AlertType: %s", data.AlertType)
}
}
var err = man.ValidateMetricQuery(&data.CommonMetricInputQuery, data.Scope, ownerId)
var err = man.ValidateMetricQuery(&data.CommonMetricInputQuery, data.Scope, ownerId, true)
if err != nil {
return data, errors.Wrap(err, "metric query error")
}
@@ -293,14 +293,14 @@ func (man *SCommonAlertManager) genName(ctx context.Context, ownerId mcclient.II
return name, nil
}
func (man *SCommonAlertManager) ValidateMetricQuery(metricRequest *monitor.CommonMetricInputQuery, scope string, ownerId mcclient.IIdentityProvider) error {
func (man *SCommonAlertManager) ValidateMetricQuery(metricRequest *monitor.CommonMetricInputQuery, scope string, ownerId mcclient.IIdentityProvider, isAlert bool) error {
for _, q := range metricRequest.MetricQuery {
metriInputQuery := monitor.MetricQueryInput{
From: metricRequest.From,
To: metricRequest.To,
Interval: metricRequest.Interval,
}
setDefaultValue(q.AlertQuery, &metriInputQuery, scope, ownerId)
setDefaultValue(q.AlertQuery, &metriInputQuery, scope, ownerId, isAlert)
err := UnifiedMonitorManager.ValidateInputQuery(q.AlertQuery, &metriInputQuery)
if err != nil {
return err
@@ -1049,7 +1049,7 @@ func (alert *SCommonAlert) ValidateUpdateData(
}
scope, _ := data.GetString("scope")
ownerId := CommonAlertManager.GetOwnerId(ctx, userCred, data)
err = CommonAlertManager.ValidateMetricQuery(metricQuery, scope, ownerId)
err = CommonAlertManager.ValidateMetricQuery(metricQuery, scope, ownerId, true)
if err != nil {
return data, errors.Wrap(err, "metric query error")
}
+7 -6
View File
@@ -222,7 +222,7 @@ func (self *SUnifiedMonitorManager) PerformQuery(ctx context.Context, userCred m
if ownId == nil {
ownId = userCred
}
setDefaultValue(q, inputQuery, scope, ownId)
setDefaultValue(q, inputQuery, scope, ownId, false)
if err := self.ValidateInputQuery(q, inputQuery); err != nil {
return nil, errors.Wrapf(err, "ValidateInputQuery")
}
@@ -386,14 +386,15 @@ func (self *SUnifiedMonitorManager) ValidateInputQuery(query *monitor.AlertQuery
func setDefaultValue(
query *monitor.AlertQuery,
inputQuery *monitor.MetricQueryInput,
scope string, ownerId mcclient.IIdentityProvider) {
scope string, ownerId mcclient.IIdentityProvider,
isAlert bool) {
query.From = inputQuery.From
query.To = inputQuery.To
query.Model.Interval = inputQuery.Interval
metricMeasurement, _ := MetricMeasurementManager.GetCache().Get(query.Model.Measurement)
checkQueryGroupBy(query, inputQuery)
checkQueryGroupBy(query, inputQuery, isAlert)
if len(inputQuery.Interval) != 0 {
query.Model.GroupBy = append(query.Model.GroupBy,
@@ -430,7 +431,7 @@ func setDefaultValue(
}
drv, _ := DataSourceManager.GetTSDBDriver()
query = drv.FillSelect(query)
query = drv.FillSelect(query, isAlert)
var projectId, domainId string
switch rbacscope.TRbacScope(scope) {
@@ -479,7 +480,7 @@ func setDefaultValue(
}
}
func checkQueryGroupBy(query *monitor.AlertQuery, inputQuery *monitor.MetricQueryInput) {
func checkQueryGroupBy(query *monitor.AlertQuery, inputQuery *monitor.MetricQueryInput, isAlert bool) {
if len(query.Model.GroupBy) != 0 {
return
}
@@ -492,7 +493,7 @@ func checkQueryGroupBy(query *monitor.AlertQuery, inputQuery *monitor.MetricQuer
tagId = monitor.GetMeasurementTagIdKeyByResType(metricMeasurement.ResType)
}
drv, _ := DataSourceManager.GetTSDBDriver()
query = drv.FillGroupBy(query, inputQuery, tagId)
query = drv.FillGroupBy(query, inputQuery, tagId, isAlert)
}
func fillSerieTags(series *monitor.TimeSeriesSlice) {
+10 -2
View File
@@ -246,7 +246,7 @@ func (e *InfluxdbExecutor) FilterMeasurement(
return retMs, nil
}
func (e *InfluxdbExecutor) FillSelect(query *monitor.AlertQuery) *monitor.AlertQuery {
func FillSelectWithMean(query *monitor.AlertQuery) *monitor.AlertQuery {
for i, sel := range query.Model.Selects {
if len(sel) > 1 {
continue
@@ -260,7 +260,11 @@ func (e *InfluxdbExecutor) FillSelect(query *monitor.AlertQuery) *monitor.AlertQ
return query
}
func (e *InfluxdbExecutor) FillGroupBy(query *monitor.AlertQuery, inputQuery *monitor.MetricQueryInput, tagId string) *monitor.AlertQuery {
func (e *InfluxdbExecutor) FillSelect(query *monitor.AlertQuery, isAlert bool) *monitor.AlertQuery {
return FillSelectWithMean(query)
}
func FillGroupByWithWildChar(query *monitor.AlertQuery, inputQuery *monitor.MetricQueryInput, tagId string) *monitor.AlertQuery {
if len(tagId) == 0 || (len(inputQuery.Slimit) != 0 && len(inputQuery.Soffset) != 0) {
tagId = "*"
}
@@ -273,3 +277,7 @@ func (e *InfluxdbExecutor) FillGroupBy(query *monitor.AlertQuery, inputQuery *mo
}
return query
}
func (e *InfluxdbExecutor) FillGroupBy(query *monitor.AlertQuery, inputQuery *monitor.MetricQueryInput, tagId string, isAlert bool) *monitor.AlertQuery {
return FillGroupByWithWildChar(query, inputQuery, tagId)
}
@@ -305,10 +305,16 @@ func (vm *vmAdapter) FilterMeasurement(ctx context.Context, ds *tsdb.DataSource,
return retMs, nil
}
func (vm *vmAdapter) FillSelect(query *monitor.AlertQuery) *monitor.AlertQuery {
func (vm *vmAdapter) FillSelect(query *monitor.AlertQuery, isAlert bool) *monitor.AlertQuery {
if isAlert {
query = influxdb.FillSelectWithMean(query)
}
return query
}
func (vm *vmAdapter) FillGroupBy(query *monitor.AlertQuery, inputQuery *monitor.MetricQueryInput, tagId string) *monitor.AlertQuery {
func (vm *vmAdapter) FillGroupBy(query *monitor.AlertQuery, inputQuery *monitor.MetricQueryInput, tagId string, isAlert bool) *monitor.AlertQuery {
if isAlert {
query = influxdb.FillGroupByWithWildChar(query, inputQuery, tagId)
}
return query
}
+2 -2
View File
@@ -25,8 +25,8 @@ import (
type TsdbQueryEndpoint interface {
Query(ctx context.Context, ds *DataSource, query *TsdbQuery) (*Response, error)
FilterMeasurement(ctx context.Context, ds *DataSource, from, to string, ms *monitor.InfluxMeasurement, tagFilter *monitor.MetricQueryTag) (*monitor.InfluxMeasurement, error)
FillSelect(query *monitor.AlertQuery) *monitor.AlertQuery
FillGroupBy(query *monitor.AlertQuery, inputQuery *monitor.MetricQueryInput, tagId string) *monitor.AlertQuery
FillSelect(query *monitor.AlertQuery, isAlert bool) *monitor.AlertQuery
FillGroupBy(query *monitor.AlertQuery, inputQuery *monitor.MetricQueryInput, tagId string, isAlert bool) *monitor.AlertQuery
}
var registry map[string]GetTsdbQueryEndpointFn