Skip to content

Commit

Permalink
Merge pull request #28 from quero-edu/add_logger_to_logql_validation
Browse files Browse the repository at this point in the history
Add logs when logql is rejected
  • Loading branch information
heitorrbarros authored Feb 21, 2024
2 parents 84c5293 + 143eb16 commit d27c088
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 12 deletions.
6 changes: 6 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -137,3 +137,9 @@ linters-settings:
- name: unused-parameter
severity: warning
disabled: false

issues:
exclude-rules:
- path: '(.+)_test\.go'
text: "dot-imports: should not use dot imports"

2 changes: 1 addition & 1 deletion internal/logger/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ func NewLogger(level string, onError func(err error, args ...interface{})) Logge
}

func NewNopLogger() Logger {
noopErrorCallback := func(err error, args ...interface{}) {}
noopErrorCallback := func(_ error, _ ...interface{}) {}

return &loggerImpl{logger: gokitlog.NewNopLogger(), errorCallback: noopErrorCallback}
}
4 changes: 2 additions & 2 deletions pkg/controllers/logql_validator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func TestValidateLogQLOnServerWithHeadersFunc(t *testing.T) {
}

func TestValidateLogQLOnServerFuncHTTP500IsAnInvalidResponse(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
w.WriteHeader(http.StatusInternalServerError)
}))

Expand All @@ -84,7 +84,7 @@ func TestValidateLogQLOnServerFuncHTTP500IsAnInvalidResponse(t *testing.T) {
}

func TestValidateLogQLOnServerFuncInvalidRequest(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
w.WriteHeader(500)
}))
isValid, err := ValidateLogQLOnServerFunc(http.DefaultClient, ts.URL, "{job=\"loki-test\"}")
Expand Down
12 changes: 6 additions & 6 deletions pkg/controllers/lokirule_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,20 +111,20 @@ func getLokiStatefulSet(
return statefulSet, nil
}

var handleValidateLogQLResult = func(client *http.Client, lokiURL string, queryStringArray []string) bool {

func (r *LokiRuleReconciler) handleValidateLogQLResult(queryStringArray []string) bool {
for _, queryString := range queryStringArray {
valid, err := ValidateLogQLOnServerFunc(client, lokiURL, 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
}
}

return true
}

Expand All @@ -145,11 +145,11 @@ func handleByEventType(r *LokiRuleReconciler) predicate.Predicate {
return predicate.Funcs{
CreateFunc: func(e event.CreateEvent) bool {
queryStringArray := getStringQueryFromLokiRule(e.Object.(*querocomv1alpha1.LokiRule))
return handleValidateLogQLResult(r.LokiClient, r.LokiURL, queryStringArray)
return r.handleValidateLogQLResult(queryStringArray)
},
UpdateFunc: func(e event.UpdateEvent) bool {
queryStringArray := getStringQueryFromLokiRule(e.ObjectNew.(*querocomv1alpha1.LokiRule))
return handleValidateLogQLResult(r.LokiClient, r.LokiURL, queryStringArray)
return r.handleValidateLogQLResult(queryStringArray)
},
DeleteFunc: func(e event.DeleteEvent) bool {
options := k8sutils.Options{Ctx: context.TODO(), Logger: r.Logger}
Expand Down
16 changes: 13 additions & 3 deletions pkg/controllers/lokirule_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"fmt"
"net/http"
"net/http/httptest"
"path/filepath"
"reflect"
"time"
Expand Down Expand Up @@ -43,6 +44,8 @@ const lokiRuleConfigMapName = "loki-rule-cfg"
var k8sClient client.Client
var testEnv *envtest.Environment

var httpServer *httptest.Server

var lokiStatefulSet *appsv1.StatefulSet
var lokiRuleReconcilerInstance *LokiRuleReconciler

Expand Down Expand Up @@ -76,6 +79,10 @@ var _ = BeforeSuite(func() {
})
Expect(err).ToNot(HaveOccurred())

httpServer = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
w.WriteHeader(http.StatusOK)
}))

selector := &metav1.LabelSelector{
MatchLabels: labels,
}
Expand All @@ -88,6 +95,8 @@ var _ = BeforeSuite(func() {
LokiLabelSelector: selector,
LokiNamespace: lokiSTSNamespaceName,
LokiRuleConfigMapName: lokiRuleConfigMapName,
LokiURL: httpServer.URL,
LokiClient: &http.Client{},
}

err = (lokiRuleReconcilerInstance).SetupWithManager(mgr)
Expand All @@ -104,16 +113,17 @@ var _ = BeforeSuite(func() {
}()
})

var _ = AfterSuite(func() {
defer httpServer.Close()
})

var _ = Describe("LokiRuleController", func() {
Describe("Reconcile", func() {
Context("When a LokiRule is created", func() {
var lokiRule *querocomv1alpha1.LokiRule
configMapName := "loki-rule-cfg"

BeforeEach(func() {
handleValidateLogQLResult = func(client *http.Client, lokiURL string, queryStringArray []string) bool {
return true
}
lokiRule = &querocomv1alpha1.LokiRule{
ObjectMeta: metav1.ObjectMeta{
Name: "test-lokirule",
Expand Down

0 comments on commit d27c088

Please sign in to comment.