diff --git a/pkg/controllers/logql_validator.go b/pkg/controllers/logql_validator.go index 8bb97c8..beb46d1 100644 --- a/pkg/controllers/logql_validator.go +++ b/pkg/controllers/logql_validator.go @@ -3,18 +3,15 @@ package controllers import ( "net/http" "net/url" - - "github.com/quero-edu/loki-rule-operator/internal/logger" ) -func ValidateLogQLOnServerFunc(client *http.Client, lokiURL string, logger logger.Logger, logQLExpr string) (bool, error) { +func ValidateLogQLOnServerFunc(client *http.Client, lokiURL string, logQLExpr string) (bool, error) { logQLExprEscaped := url.QueryEscape(logQLExpr) lokiQueryEndpoint := "/loki/api/v1/query?query=" + logQLExprEscaped logQLURIWithQuery := lokiURL + lokiQueryEndpoint response, err := client.Get(logQLURIWithQuery) if err != nil { - logger.Error(err, "Failed to send request to Loki server") return false, err } @@ -24,6 +21,5 @@ func ValidateLogQLOnServerFunc(client *http.Client, lokiURL string, logger logge return true, nil } - logger.Warn("The query string:", logQLExpr, "is not a valid LogQL query") return false, nil } diff --git a/pkg/controllers/logql_validator_test.go b/pkg/controllers/logql_validator_test.go index 42a34bc..b3f20a2 100644 --- a/pkg/controllers/logql_validator_test.go +++ b/pkg/controllers/logql_validator_test.go @@ -3,7 +3,6 @@ package controllers import ( "github.com/quero-edu/loki-rule-operator/internal/flags" httputil "github.com/quero-edu/loki-rule-operator/internal/http" - "github.com/quero-edu/loki-rule-operator/internal/logger" "net/http" "net/http/httptest" "testing" @@ -25,7 +24,7 @@ func TestValidateLogQLOnServerFunc(t *testing.T) { defer ts.Close() - isValid, err := ValidateLogQLOnServerFunc(http.DefaultClient, ts.URL, logger.NewNopLogger(), "{job=\"loki-test\"}") + isValid, err := ValidateLogQLOnServerFunc(http.DefaultClient, ts.URL, "{job=\"loki-test\"}") if err != nil { t.Errorf("Error: %v", err) @@ -55,7 +54,7 @@ func TestValidateLogQLOnServerWithHeadersFunc(t *testing.T) { "X-Scope-Orgid=1", "Authorization=something", }) - isValid, err := ValidateLogQLOnServerFunc(client, ts.URL, logger.NewNopLogger(), "{job=\"loki-test\"}") + isValid, err := ValidateLogQLOnServerFunc(client, ts.URL, "{job=\"loki-test\"}") if err != nil { t.Errorf("Error: %v", err) @@ -73,7 +72,7 @@ func TestValidateLogQLOnServerFuncHTTP500IsAnInvalidResponse(t *testing.T) { defer ts.Close() - isValid, err := ValidateLogQLOnServerFunc(http.DefaultClient, ts.URL, logger.NewNopLogger(), "{job=\"loki-test\"}") + isValid, err := ValidateLogQLOnServerFunc(http.DefaultClient, ts.URL, "{job=\"loki-test\"}") if err != nil { t.Errorf("Error: %v", err) @@ -88,7 +87,7 @@ func TestValidateLogQLOnServerFuncInvalidRequest(t *testing.T) { ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(500) })) - isValid, err := ValidateLogQLOnServerFunc(http.DefaultClient, ts.URL, logger.NewNopLogger(), "{job=\"loki-test\"}") + isValid, err := ValidateLogQLOnServerFunc(http.DefaultClient, ts.URL, "{job=\"loki-test\"}") if err != nil { t.Errorf("Error: %v", err) diff --git a/pkg/controllers/lokirule_controller.go b/pkg/controllers/lokirule_controller.go index c178fd3..030a3e6 100644 --- a/pkg/controllers/lokirule_controller.go +++ b/pkg/controllers/lokirule_controller.go @@ -113,13 +113,15 @@ func getLokiStatefulSet( func (r *LokiRuleReconciler) handleValidateLogQLResult(queryStringArray []string) bool { for _, queryString := range queryStringArray { - valid, err := ValidateLogQLOnServerFunc(r.LokiClient, r.LokiURL, r.Logger, queryString) + valid, err := ValidateLogQLOnServerFunc(r.LokiClient, r.LokiURL, queryString) if err != nil { + r.Logger.Error(err, "Failed to send request to Loki server") return false } if !valid { + r.Logger.Warn("The query string:", queryString, "is not a valid LogQL query") return false } }