Commit 9bab551d by Kyle Brandt Committed by GitHub

AzureMonitor: change filterDimensions property to match what is stored (#27459)

Changes the dimensionsFilters property to dimensionsFilter in the Azure Monitor Datasource's AzureMonitor service to make what is sent to match the saved model.

Before this, the property that the backend was expecting was not available in the case of alerting, where the stored model is fetched.

This also fixes a panic when there is a dimension alias but no dimension
parent 5b6e1569
......@@ -135,12 +135,12 @@ func (e *AzureMonitorDatasource) buildQueries(queries []*tsdb.Query, timeRange *
dimSB := strings.Builder{}
if dimension != "" && dimensionFilter != "" && dimension != "None" && len(azJSONModel.DimensionsFilters) == 0 {
if dimension != "" && dimensionFilter != "" && dimension != "None" && len(azJSONModel.DimensionFilters) == 0 {
dimSB.WriteString(fmt.Sprintf("%s eq '%s'", dimension, dimensionFilter))
} else {
for i, filter := range azJSONModel.DimensionsFilters {
for i, filter := range azJSONModel.DimensionFilters {
dimSB.WriteString(filter.String())
if i != len(azJSONModel.DimensionsFilters)-1 {
if i != len(azJSONModel.DimensionFilters)-1 {
dimSB.WriteString(" and ")
}
}
......@@ -381,10 +381,16 @@ func formatAzureMonitorLegendKey(alias string, resourceName string, metricName s
}
if metaPartName == "dimensionname" {
if len(keys) == 0 {
return []byte{}
}
return []byte(keys[0])
}
if metaPartName == "dimensionvalue" {
if len(keys) == 0 {
return []byte{}
}
return []byte(lowerLabels[keys[0]])
}
......
......@@ -86,9 +86,9 @@ func TestAzureMonitorBuildQueries(t *testing.T) {
{
name: "has dimensionFilter*s* property with one dimension",
azureMonitorVariedProperties: map[string]interface{}{
"timeGrain": "PT1M",
"dimensionsFilters": []azureMonitorDimensionFilter{{"blob", "eq", "*"}},
"top": "30",
"timeGrain": "PT1M",
"dimensionFilters": []azureMonitorDimensionFilter{{"blob", "eq", "*"}},
"top": "30",
},
queryIntervalMS: 400000,
expectedInterval: "PT1M",
......@@ -97,9 +97,9 @@ func TestAzureMonitorBuildQueries(t *testing.T) {
{
name: "has dimensionFilter*s* property with two dimensions",
azureMonitorVariedProperties: map[string]interface{}{
"timeGrain": "PT1M",
"dimensionsFilters": []azureMonitorDimensionFilter{{"blob", "eq", "*"}, {"tier", "eq", "*"}},
"top": "30",
"timeGrain": "PT1M",
"dimensionFilters": []azureMonitorDimensionFilter{{"blob", "eq", "*"}, {"tier", "eq", "*"}},
"top": "30",
},
queryIntervalMS: 400000,
expectedInterval: "PT1M",
......
......@@ -98,7 +98,7 @@ type azureMonitorJSONQuery struct {
TimeGrain string `json:"timeGrain"`
Top string `json:"top"`
DimensionsFilters []azureMonitorDimensionFilter `json:"dimensionsFilters"` // new model
DimensionFilters []azureMonitorDimensionFilter `json:"dimensionFilters"` // new model
} `json:"azureMonitor"`
Subscription string `json:"subscription"`
}
......@@ -112,7 +112,11 @@ type azureMonitorDimensionFilter struct {
}
func (a azureMonitorDimensionFilter) String() string {
return fmt.Sprintf("%v %v '%v'", a.Dimension, a.Operator, a.Filter)
filter := "*"
if a.Filter != "" {
filter = a.Filter
}
return fmt.Sprintf("%v %v '%v'", a.Dimension, a.Operator, filter)
}
// insightsJSONQuery is the frontend JSON query model for an Azure Application Insights query.
......
......@@ -69,7 +69,7 @@ export default class AzureMonitorDatasource extends DataSourceWithBackend<AzureM
const aggregation = templateSrv.replace(item.aggregation, scopedVars);
const top = templateSrv.replace(item.top || '', scopedVars);
const dimensionsFilters = item.dimensionFilters
const dimensionFilters = item.dimensionFilters
.filter(f => f.dimension && f.dimension !== 'None')
.map(f => {
const filter = templateSrv.replace(f.filter ?? '', scopedVars);
......@@ -94,7 +94,7 @@ export default class AzureMonitorDatasource extends DataSourceWithBackend<AzureM
metricNamespace:
metricNamespace && metricNamespace !== defaultDropdownValue ? metricNamespace : metricDefinition,
aggregation: aggregation,
dimensionsFilters,
dimensionFilters,
top: top || '10',
alias: item.alias,
format: target.format,
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment