Skip to content

Commit 4597072

Browse files
author
Benjamin Perez
committed
Added pre validation of prometheus URL
Signed-off-by: Benjamin Perez <[email protected]>
1 parent 4e87639 commit 4597072

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

restapi/admin_info.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@ package restapi
1919
import (
2020
"context"
2121
"encoding/json"
22+
"errors"
2223
"fmt"
24+
"net/http"
2325
"net/url"
2426
"regexp"
2527
"strings"
@@ -871,10 +873,38 @@ func unmarshalPrometheus(endpoint string, data interface{}) bool {
871873
return false
872874
}
873875

876+
func testPrometheusURL(url string) bool {
877+
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
878+
defer cancel()
879+
880+
req, err := http.NewRequestWithContext(ctx, http.MethodGet, url+"/-/healthy", nil)
881+
882+
if err != nil {
883+
LogError("Error Building Request: (%v)", err)
884+
return false
885+
}
886+
887+
response, err := GetConsoleHTTPClient().Do(req)
888+
889+
if err != nil {
890+
LogError("Non reachable Prometheus URL: %s (%v)", url, err)
891+
return false
892+
893+
}
894+
895+
return response.StatusCode == http.StatusOK
896+
}
897+
874898
func getAdminInfoWidgetResponse(params admin_api.DashboardWidgetDetailsParams) (*models.WidgetDetails, *models.Error) {
875899
prometheusURL := getPrometheusURL()
876900
prometheusJobID := getPrometheusJobID()
877901

902+
// We test if prometheus URL is reachable. this is meant to avoid unuseful calls and application hang.
903+
if !testPrometheusURL(prometheusURL) {
904+
error := errors.New("Prometheus URL is unreachable")
905+
return nil, prepareError(error)
906+
}
907+
878908
return getWidgetDetails(prometheusURL, prometheusJobID, params.WidgetID, params.Step, params.Start, params.End)
879909
}
880910

0 commit comments

Comments
 (0)