Skip to content

Commit

Permalink
remove logger from logql_validator
Browse files Browse the repository at this point in the history
  • Loading branch information
BrunoTarijon committed Feb 19, 2024
1 parent c169f5c commit 0aac04d
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 11 deletions.
6 changes: 1 addition & 5 deletions pkg/controllers/logql_validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand All @@ -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
}
9 changes: 4 additions & 5 deletions pkg/controllers/logql_validator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -88,7 +87,7 @@ func TestValidateLogQLOnServerFuncInvalidRequest(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {

Check warning on line 87 in pkg/controllers/logql_validator_test.go

View workflow job for this annotation

GitHub Actions / go-lint

unused-parameter: parameter 'r' seems to be unused, consider removing or renaming it as _ (revive)
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)
Expand Down
4 changes: 3 additions & 1 deletion pkg/controllers/lokirule_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Expand Down

0 comments on commit 0aac04d

Please sign in to comment.