diff --git a/pkg/tsdb/azuremonitor/loganalytics/azure-log-analytics-datasource.go b/pkg/tsdb/azuremonitor/loganalytics/azure-log-analytics-datasource.go index d3c79793772..ead41f16ed7 100644 --- a/pkg/tsdb/azuremonitor/loganalytics/azure-log-analytics-datasource.go +++ b/pkg/tsdb/azuremonitor/loganalytics/azure-log-analytics-datasource.go @@ -226,7 +226,7 @@ func buildLogAnalyticsQuery(query backend.DataQuery, dsInfo types.DatasourceInfo func (e *AzureLogAnalyticsDatasource) buildQueries(ctx context.Context, queries []backend.DataQuery, dsInfo types.DatasourceInfo, fromAlert bool) ([]*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") } diff --git a/pkg/tsdb/azuremonitor/loganalytics/azure-log-analytics-datasource_test.go b/pkg/tsdb/azuremonitor/loganalytics/azure-log-analytics-datasource_test.go index 5e00c197670..29fb137f608 100644 --- a/pkg/tsdb/azuremonitor/loganalytics/azure-log-analytics-datasource_test.go +++ b/pkg/tsdb/azuremonitor/loganalytics/azure-log-analytics-datasource_test.go @@ -92,7 +92,7 @@ func TestBuildLogAnalyticsQuery(t *testing.T) { }, } - appInsightsRegExp, err := regexp.Compile("providers/Microsoft.Insights/components") + appInsightsRegExp, err := regexp.Compile("(?i)providers/microsoft.insights/components") if err != nil { t.Error("failed to compile reg: %w", err) } @@ -442,6 +442,84 @@ func TestBuildLogAnalyticsQuery(t *testing.T) { azureLogAnalyticsQuery: nil, Err: require.Error, }, + { + name: "Detects App Insights resource queries", + fromAlert: false, + 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), + }, + azureLogAnalyticsQuery: makeQueryPointer(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)", + fromAlert: false, + 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), + }, + azureLogAnalyticsQuery: makeQueryPointer(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 {