[v10.4.x] AzureMonitor: Fix bug detecting app insights queries (#88786)

AzureMonitor: Fix bug detecting app insights queries (#88572)

Make regexp case insensitive

(cherry picked from commit f787418e4b)

# Conflicts:
#	pkg/tsdb/azuremonitor/loganalytics/azure-log-analytics-datasource_test.go
This commit is contained in:
Andreas Christou
2024-06-06 15:03:57 -04:00
committed by GitHub
parent 54a3ccf2a2
commit 58481df86e
2 changed files with 77 additions and 1 deletions
@@ -97,7 +97,7 @@ func getApiURL(resourceOrWorkspace string, isAppInsightsQuery bool) string {
func (e *AzureLogAnalyticsDatasource) buildQueries(ctx context.Context, queries []backend.DataQuery, dsInfo types.DatasourceInfo) ([]*AzureLogAnalyticsQuery, error) {
azureLogAnalyticsQueries := []*AzureLogAnalyticsQuery{}
appInsightsRegExp, err := regexp.Compile("providers/Microsoft.Insights/components")
appInsightsRegExp, err := regexp.Compile("(?i)providers/microsoft.insights/components")
if err != nil {
return nil, fmt.Errorf("failed to compile Application Insights regex")
}
@@ -1446,6 +1446,82 @@ func TestBuildingAzureLogAnalyticsQueries(t *testing.T) {
},
Err: require.NoError,
},
{
name: "Detects App Insights resource queries",
queryModel: []backend.DataQuery{{
JSON: []byte(fmt.Sprintf(`{
"queryType": "Azure Log Analytics",
"azureLogAnalytics": {
"resources": ["/subscriptions/aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee/resourceGroups/cloud-datasources/providers/Microsoft.Insights/components/AppInsightsTestDataWorkspace"],
"query": "Perf | where $__timeFilter() | where $__contains(Computer, 'comp1','comp2') | summarize avg(CounterValue) by bin(TimeGenerated, $__interval), Computer",
"resultFormat": "%s",
"dashboardTime": false
}
}`, dataquery.ResultFormatTimeSeries)),
RefID: "A",
TimeRange: timeRange,
QueryType: string(dataquery.AzureQueryTypeAzureLogAnalytics),
}},
azureLogAnalyticsQueries: []*AzureLogAnalyticsQuery{{
RefID: "A",
ResultFormat: dataquery.ResultFormatTimeSeries,
URL: "v1/apps/AppInsightsTestDataWorkspace/query",
JSON: []byte(fmt.Sprintf(`{
"queryType": "Azure Log Analytics",
"azureLogAnalytics": {
"resources": ["/subscriptions/aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee/resourceGroups/cloud-datasources/providers/Microsoft.Insights/components/AppInsightsTestDataWorkspace"],
"query": "Perf | where $__timeFilter() | where $__contains(Computer, 'comp1','comp2') | summarize avg(CounterValue) by bin(TimeGenerated, $__interval), Computer",
"resultFormat": "%s",
"dashboardTime": false
}
}`, dataquery.ResultFormatTimeSeries)),
Query: "Perf | where ['TimeGenerated'] >= datetime('2018-03-15T13:00:00Z') and ['TimeGenerated'] <= datetime('2018-03-15T13:34:00Z') | where ['Computer'] in ('comp1','comp2') | summarize avg(CounterValue) by bin(TimeGenerated, 34000ms), Computer",
Resources: []string{"/subscriptions/aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee/resourceGroups/cloud-datasources/providers/Microsoft.Insights/components/AppInsightsTestDataWorkspace"},
TimeRange: timeRange,
QueryType: dataquery.AzureQueryTypeAzureLogAnalytics,
AppInsightsQuery: true,
DashboardTime: false,
}},
Err: require.NoError,
},
{
name: "Detects App Insights resource queries (case insensitive)",
queryModel: []backend.DataQuery{{
JSON: []byte(fmt.Sprintf(`{
"queryType": "Azure Log Analytics",
"azureLogAnalytics": {
"resources": ["/subscriptions/aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee/resourceGroups/cloud-datasources/providers/microsoft.insights/components/AppInsightsTestDataWorkspace"],
"query": "Perf | where $__timeFilter() | where $__contains(Computer, 'comp1','comp2') | summarize avg(CounterValue) by bin(TimeGenerated, $__interval), Computer",
"resultFormat": "%s",
"dashboardTime": false
}
}`, dataquery.ResultFormatTimeSeries)),
RefID: "A",
TimeRange: timeRange,
QueryType: string(dataquery.AzureQueryTypeAzureLogAnalytics),
}},
azureLogAnalyticsQueries: []*AzureLogAnalyticsQuery{{
RefID: "A",
ResultFormat: dataquery.ResultFormatTimeSeries,
URL: "v1/apps/AppInsightsTestDataWorkspace/query",
JSON: []byte(fmt.Sprintf(`{
"queryType": "Azure Log Analytics",
"azureLogAnalytics": {
"resources": ["/subscriptions/aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee/resourceGroups/cloud-datasources/providers/microsoft.insights/components/AppInsightsTestDataWorkspace"],
"query": "Perf | where $__timeFilter() | where $__contains(Computer, 'comp1','comp2') | summarize avg(CounterValue) by bin(TimeGenerated, $__interval), Computer",
"resultFormat": "%s",
"dashboardTime": false
}
}`, dataquery.ResultFormatTimeSeries)),
Query: "Perf | where ['TimeGenerated'] >= datetime('2018-03-15T13:00:00Z') and ['TimeGenerated'] <= datetime('2018-03-15T13:34:00Z') | where ['Computer'] in ('comp1','comp2') | summarize avg(CounterValue) by bin(TimeGenerated, 34000ms), Computer",
Resources: []string{"/subscriptions/aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee/resourceGroups/cloud-datasources/providers/microsoft.insights/components/AppInsightsTestDataWorkspace"},
TimeRange: timeRange,
QueryType: dataquery.AzureQueryTypeAzureLogAnalytics,
AppInsightsQuery: true,
DashboardTime: false,
}},
Err: require.NoError,
},
}
for _, tt := range tests {