Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add logs when logql is rejected #28

Merged
merged 3 commits into from
Feb 21, 2024
Merged
Show file tree
Hide file tree
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
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
Loading