mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
Azure Monitor: Fix health check for empty default subscription (#60569)
This commit is contained in:
parent
b5834fd6d3
commit
7db8d031d3
@ -235,8 +235,8 @@ func checkAzureMonitorMetricsHealth(dsInfo types.DatasourceInfo) (*http.Response
|
|||||||
return res, nil
|
return res, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func checkAzureLogAnalyticsHealth(dsInfo types.DatasourceInfo) (*http.Response, error) {
|
func checkAzureLogAnalyticsHealth(dsInfo types.DatasourceInfo, subscription string) (*http.Response, error) {
|
||||||
workspacesUrl := fmt.Sprintf("%v/subscriptions/%v/providers/Microsoft.OperationalInsights/workspaces?api-version=2017-04-26-preview", dsInfo.Routes["Azure Monitor"].URL, dsInfo.Settings.SubscriptionId)
|
workspacesUrl := fmt.Sprintf("%v/subscriptions/%v/providers/Microsoft.OperationalInsights/workspaces?api-version=2017-04-26-preview", dsInfo.Routes["Azure Monitor"].URL, subscription)
|
||||||
workspacesReq, err := http.NewRequest(http.MethodGet, workspacesUrl, nil)
|
workspacesReq, err := http.NewRequest(http.MethodGet, workspacesUrl, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@ -280,10 +280,10 @@ func checkAzureLogAnalyticsHealth(dsInfo types.DatasourceInfo) (*http.Response,
|
|||||||
return res, nil
|
return res, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func checkAzureMonitorResourceGraphHealth(dsInfo types.DatasourceInfo) (*http.Response, error) {
|
func checkAzureMonitorResourceGraphHealth(dsInfo types.DatasourceInfo, subscription string) (*http.Response, error) {
|
||||||
body, err := json.Marshal(map[string]interface{}{
|
body, err := json.Marshal(map[string]interface{}{
|
||||||
"query": "Resources | project id | limit 1",
|
"query": "Resources | project id | limit 1",
|
||||||
"subscriptions": []string{dsInfo.Settings.SubscriptionId},
|
"subscriptions": []string{subscription},
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@ -303,6 +303,29 @@ func checkAzureMonitorResourceGraphHealth(dsInfo types.DatasourceInfo) (*http.Re
|
|||||||
return res, nil
|
return res, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func parseSubscriptions(res *http.Response) ([]string, error) {
|
||||||
|
var target struct {
|
||||||
|
Value []struct {
|
||||||
|
SubscriptionId string `json:"subscriptionId"`
|
||||||
|
}
|
||||||
|
}
|
||||||
|
err := json.NewDecoder(res.Body).Decode(&target)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
defer func() {
|
||||||
|
err := res.Body.Close()
|
||||||
|
backend.Logger.Error("Failed to close response body", "err", err)
|
||||||
|
}()
|
||||||
|
|
||||||
|
result := make([]string, len(target.Value))
|
||||||
|
for i, v := range target.Value {
|
||||||
|
result[i] = v.SubscriptionId
|
||||||
|
}
|
||||||
|
|
||||||
|
return result, nil
|
||||||
|
}
|
||||||
|
|
||||||
func (s *Service) CheckHealth(ctx context.Context, req *backend.CheckHealthRequest) (*backend.CheckHealthResult, error) {
|
func (s *Service) CheckHealth(ctx context.Context, req *backend.CheckHealthRequest) (*backend.CheckHealthResult, error) {
|
||||||
dsInfo, err := s.getDSInfo(req.PluginContext)
|
dsInfo, err := s.getDSInfo(req.PluginContext)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -316,6 +339,7 @@ func (s *Service) CheckHealth(ctx context.Context, req *backend.CheckHealthReque
|
|||||||
metricsLog := "Successfully connected to Azure Monitor endpoint."
|
metricsLog := "Successfully connected to Azure Monitor endpoint."
|
||||||
logAnalyticsLog := "Successfully connected to Azure Log Analytics endpoint."
|
logAnalyticsLog := "Successfully connected to Azure Log Analytics endpoint."
|
||||||
graphLog := "Successfully connected to Azure Resource Graph endpoint."
|
graphLog := "Successfully connected to Azure Resource Graph endpoint."
|
||||||
|
defaultSubscription := dsInfo.Settings.SubscriptionId
|
||||||
|
|
||||||
metricsRes, err := checkAzureMonitorMetricsHealth(dsInfo)
|
metricsRes, err := checkAzureMonitorMetricsHealth(dsInfo)
|
||||||
if err != nil || metricsRes.StatusCode != 200 {
|
if err != nil || metricsRes.StatusCode != 200 {
|
||||||
@ -333,9 +357,17 @@ func (s *Service) CheckHealth(ctx context.Context, req *backend.CheckHealthReque
|
|||||||
}
|
}
|
||||||
metricsLog = fmt.Sprintf("Error connecting to Azure Monitor endpoint: %s", string(body))
|
metricsLog = fmt.Sprintf("Error connecting to Azure Monitor endpoint: %s", string(body))
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
subscriptions, err := parseSubscriptions(metricsRes)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if defaultSubscription == "" && len(subscriptions) > 0 {
|
||||||
|
defaultSubscription = subscriptions[0]
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
logsRes, err := checkAzureLogAnalyticsHealth(dsInfo)
|
logsRes, err := checkAzureLogAnalyticsHealth(dsInfo, defaultSubscription)
|
||||||
if err != nil || logsRes.StatusCode != 200 {
|
if err != nil || logsRes.StatusCode != 200 {
|
||||||
status = backend.HealthStatusError
|
status = backend.HealthStatusError
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -356,7 +388,7 @@ func (s *Service) CheckHealth(ctx context.Context, req *backend.CheckHealthReque
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
resourceGraphRes, err := checkAzureMonitorResourceGraphHealth(dsInfo)
|
resourceGraphRes, err := checkAzureMonitorResourceGraphHealth(dsInfo, defaultSubscription)
|
||||||
if err != nil || resourceGraphRes.StatusCode != 200 {
|
if err != nil || resourceGraphRes.StatusCode != 200 {
|
||||||
status = backend.HealthStatusError
|
status = backend.HealthStatusError
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -265,7 +265,7 @@ func TestCheckHealth(t *testing.T) {
|
|||||||
if !fail {
|
if !fail {
|
||||||
return &http.Response{
|
return &http.Response{
|
||||||
StatusCode: 200,
|
StatusCode: 200,
|
||||||
Body: io.NopCloser(bytes.NewBufferString("OK")),
|
Body: io.NopCloser(bytes.NewBufferString("{\"value\": [{\"subscriptionId\": \"abcd-1234\"}]}")),
|
||||||
Header: make(http.Header),
|
Header: make(http.Header),
|
||||||
}, nil
|
}, nil
|
||||||
} else {
|
} else {
|
||||||
|
Loading…
Reference in New Issue
Block a user