diff --git a/pkg/tsdb/azuremonitor/loganalytics/azure-log-analytics-datasource.go b/pkg/tsdb/azuremonitor/loganalytics/azure-log-analytics-datasource.go index 35640d45d82..69e0886333f 100644 --- a/pkg/tsdb/azuremonitor/loganalytics/azure-log-analytics-datasource.go +++ b/pkg/tsdb/azuremonitor/loganalytics/azure-log-analytics-datasource.go @@ -485,11 +485,19 @@ func (e *AzureLogAnalyticsDatasource) createRequest(ctx context.Context, queryUR } if len(query.Resources) > 1 && query.QueryType == dataquery.AzureQueryTypeAzureLogAnalytics && !query.AppInsightsQuery { - body["workspaces"] = query.Resources + str := strings.ToLower(query.Resources[0]) + + if strings.Contains(str, "microsoft.operationalinsights/workspaces") { + body["workspaces"] = query.Resources + } else { + body["resources"] = query.Resources + } } + if query.AppInsightsQuery { body["applications"] = query.Resources } + jsonValue, err := json.Marshal(body) if err != nil { return nil, fmt.Errorf("%v: %w", "failed to create request", err) @@ -499,6 +507,7 @@ func (e *AzureLogAnalyticsDatasource) createRequest(ctx context.Context, queryUR if err != nil { return nil, fmt.Errorf("%v: %w", "failed to create request", err) } + req.URL.Path = "/" req.Header.Set("Content-Type", "application/json") req.URL.Path = path.Join(req.URL.Path, query.URL) 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 69a7edbf4bc..56e56a545bb 100644 --- a/pkg/tsdb/azuremonitor/loganalytics/azure-log-analytics-datasource_test.go +++ b/pkg/tsdb/azuremonitor/loganalytics/azure-log-analytics-datasource_test.go @@ -1553,6 +1553,42 @@ func TestLogAnalyticsCreateRequest(t *testing.T) { t.Errorf("Unexpected Body: %v", cmp.Diff(string(body), expectedBody)) } }) + + t.Run("correctly classifies resources as workspaces when matching criteria", func(t *testing.T) { + ds := AzureLogAnalyticsDatasource{} + req, err := ds.createRequest(ctx, url, &AzureLogAnalyticsQuery{ + Resources: []string{"/subscriptions/test-sub/resourceGroups/test-rg/providers/microsoft.operationalInsights/workSpaces/ws1", "microsoft.operationalInsights/workspaces/ws2"}, // Note different casings and partial paths + Query: "Perf", + QueryType: dataquery.AzureQueryTypeAzureLogAnalytics, + AppInsightsQuery: false, + DashboardTime: false, + }) + require.NoError(t, err) + expectedBody := `{"query":"Perf","workspaces":["/subscriptions/test-sub/resourceGroups/test-rg/providers/microsoft.operationalInsights/workSpaces/ws1","microsoft.operationalInsights/workspaces/ws2"]}` // Expecting resources to be classified as workspaces + body, err := io.ReadAll(req.Body) + require.NoError(t, err) + if !cmp.Equal(string(body), expectedBody) { + t.Errorf("Unexpected Body: %v", cmp.Diff(string(body), expectedBody)) + } + }) + + t.Run("correctly passes multiple resources not classified as workspaces", func(t *testing.T) { + ds := AzureLogAnalyticsDatasource{} + req, err := ds.createRequest(ctx, url, &AzureLogAnalyticsQuery{ + Resources: []string{"/subscriptions/test-sub/resourceGroups/test-rg/providers/SomeOtherService/serviceInstances/r1", "/subscriptions/test-sub/resourceGroups/test-rg/providers/SomeOtherService/serviceInstances/r2"}, + Query: "Perf", + QueryType: dataquery.AzureQueryTypeAzureLogAnalytics, + AppInsightsQuery: false, + DashboardTime: false, + }) + require.NoError(t, err) + expectedBody := `{"query":"Perf","resources":["/subscriptions/test-sub/resourceGroups/test-rg/providers/SomeOtherService/serviceInstances/r1","/subscriptions/test-sub/resourceGroups/test-rg/providers/SomeOtherService/serviceInstances/r2"]}` + body, err := io.ReadAll(req.Body) + require.NoError(t, err) + if !cmp.Equal(string(body), expectedBody) { + t.Errorf("Unexpected Body: %v", cmp.Diff(string(body), expectedBody)) + } + }) } func Test_executeQueryErrorWithDifferentLogAnalyticsCreds(t *testing.T) {