Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions restapi/admin_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ package restapi
import (
"context"
"encoding/json"
"errors"
"fmt"
"net/http"
"net/url"
"regexp"
"strings"
Expand Down Expand Up @@ -871,10 +873,38 @@ func unmarshalPrometheus(endpoint string, data interface{}) bool {
return false
}

func testPrometheusURL(url string) bool {
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
defer cancel()

req, err := http.NewRequestWithContext(ctx, http.MethodGet, url+"/-/healthy", nil)

if err != nil {
LogError("Error Building Request: (%v)", err)
return false
}

response, err := GetConsoleHTTPClient().Do(req)

if err != nil {
LogError("Non reachable Prometheus URL: %s (%v)", url, err)
return false

}

return response.StatusCode == http.StatusOK
}

func getAdminInfoWidgetResponse(params admin_api.DashboardWidgetDetailsParams) (*models.WidgetDetails, *models.Error) {
prometheusURL := getPrometheusURL()
prometheusJobID := getPrometheusJobID()

// We test if prometheus URL is reachable. this is meant to avoid unuseful calls and application hang.
if !testPrometheusURL(prometheusURL) {
error := errors.New("Prometheus URL is unreachable")
return nil, prepareError(error)
}

return getWidgetDetails(prometheusURL, prometheusJobID, params.WidgetID, params.Step, params.Start, params.End)
}

Expand Down