Skip to content
This repository has been archived by the owner on Oct 14, 2024. It is now read-only.

Commit

Permalink
fix(e2e): default kics log-level to error to avoid excessive log mess…
Browse files Browse the repository at this point in the history
…ages (#1955)

* fix: use log-level: error for kics e2e tests

Signed-off-by: Bence Csati <[email protected]>

* fix(plugin): set default log level in python plugin sdk

* fix: create new context logger

Signed-off-by: Bence Csati <[email protected]>

fix: create new ctx logger

Signed-off-by: Bence Csati <[email protected]>

fix: create new ctx logger

Signed-off-by: Bence Csati <[email protected]>

fix: create new ctx logger

Signed-off-by: Bence Csati <[email protected]>

fix: create new ctx logger

Signed-off-by: Bence Csati <[email protected]>

fix: create new ctx logger

Signed-off-by: Bence Csati <[email protected]>

---------

Signed-off-by: Bence Csati <[email protected]>
Co-authored-by: Zsolt Kacsándi <[email protected]>
  • Loading branch information
csatib02 and zsoltkacsandi authored Jul 26, 2024
1 parent 9c352dd commit 3fd770c
Show file tree
Hide file tree
Showing 8 changed files with 28 additions and 10 deletions.
2 changes: 1 addition & 1 deletion core/log/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func GetLoggerFromContextOrDiscard(ctx context.Context) *log.Entry {
if !ok {
logger = log.NewEntry(&log.Logger{
Out: io.Discard,
Level: 0,
Level: log.PanicLevel,
}).WithContext(ctx)
}
return logger
Expand Down
14 changes: 12 additions & 2 deletions e2e/kics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ import (

"github.com/onsi/ginkgo/v2"
"github.com/onsi/gomega"
"github.com/sirupsen/logrus"

"github.com/openclarity/vmclarity/core/log"
"github.com/openclarity/vmclarity/scanner"
scannercommon "github.com/openclarity/vmclarity/scanner/common"
"github.com/openclarity/vmclarity/scanner/families"
Expand Down Expand Up @@ -51,6 +53,10 @@ var _ = ginkgo.Describe("Running KICS scan", func() {
ginkgo.Skip("KICS plugin image not provided")
}

log.InitLogger(logrus.ErrorLevel.String(), ginkgo.GinkgoWriter)
logger := logrus.WithContext(ctx)
specCtx := log.SetLoggerForContext(ctx, logger)

input, err := filepath.Abs("./testdata")
gomega.Expect(err).NotTo(gomega.HaveOccurred())
notifier := &Notifier{}
Expand All @@ -74,7 +80,7 @@ var _ = ginkgo.Describe("Running KICS scan", func() {
},
},
},
}).Run(ctx, notifier)
}).Run(specCtx, notifier)
gomega.Expect(errs).To(gomega.BeEmpty())

gomega.Eventually(func() bool {
Expand Down Expand Up @@ -106,6 +112,10 @@ var _ = ginkgo.Describe("Running a KICS scan", func() {
ginkgo.Skip("KICS plugin image not provided")
}

log.InitLogger(logrus.ErrorLevel.String(), ginkgo.GinkgoWriter)
logger := logrus.WithContext(ctx)
specCtx := log.SetLoggerForContext(ctx, logger)

input, err := filepath.Abs("./testdata")
gomega.Expect(err).NotTo(gomega.HaveOccurred())
notifier := &Notifier{}
Expand All @@ -129,7 +139,7 @@ var _ = ginkgo.Describe("Running a KICS scan", func() {
},
},
},
}).Run(ctx, notifier)
}).Run(specCtx, notifier)
gomega.Expect(errs).To(gomega.BeEmpty())

gomega.Eventually(func() bool {
Expand Down
4 changes: 3 additions & 1 deletion e2e/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ import (
uibackendclient "github.com/openclarity/vmclarity/uibackend/client"
)

const DefaultLogLevel = logrus.DebugLevel

var (
testEnv types.Environment
cfg *config.Config
Expand All @@ -52,7 +54,7 @@ func beforeSuite(ctx context.Context) {
var err error

ginkgo.By("initializing test environment")
log.InitLogger(logrus.DebugLevel.String(), ginkgo.GinkgoWriter)
log.InitLogger(DefaultLogLevel.String(), ginkgo.GinkgoWriter)
logger := logrus.WithContext(ctx)
ctx = log.SetLoggerForContext(ctx, logger)

Expand Down
7 changes: 4 additions & 3 deletions plugins/sdk-go/plugin/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,24 @@
package plugin

import (
"log/slog"
"os"
)

const (
EnvLogLevel = "PLUGIN_SERVER_LOG_LEVEL"
DefaultLogLevel = "info"
DefaultLogLevel = slog.LevelError

EnvListenAddress = "PLUGIN_SERVER_LISTEN_ADDRESS"
DefaultListenAddress = "http://0.0.0.0:8080"
)

func getLogLevel() string {
func GetLogLevel() string {
if logLevel := os.Getenv(EnvLogLevel); logLevel != "" {
return logLevel
}

return DefaultLogLevel
return DefaultLogLevel.String()
}

func getListenAddress() string {
Expand Down
4 changes: 2 additions & 2 deletions plugins/sdk-go/plugin/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ var logger *slog.Logger

func init() {
var logLevel slog.Level
if err := logLevel.UnmarshalText([]byte(getLogLevel())); err != nil {
logLevel = slog.LevelInfo
if err := logLevel.UnmarshalText([]byte(GetLogLevel())); err != nil {
logLevel = DefaultLogLevel
}

logger = slog.New(slog.NewJSONHandler(os.Stdout, &slog.HandlerOptions{
Expand Down
2 changes: 1 addition & 1 deletion plugins/sdk-python/plugin/server/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from urllib.parse import urlparse

ENV_LOG_LEVEL = "PLUGIN_SERVER_LOG_LEVEL"
DEFAULT_LOG_LEVEL = "info"
DEFAULT_LOG_LEVEL = logging.ERROR

ENV_LISTEN_ADDRESS = "PLUGIN_SERVER_LISTEN_ADDRESS"
DEFAULT_LISTEN_ADDRESS = "http://0.0.0.0:8080"
Expand Down
1 change: 1 addition & 0 deletions plugins/sdk-python/plugin/server/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

# Init logger
_logger = logging.getLogger('plugin.scanner')
_logger.setLevel(_config.get_log_level())
_logger.addHandler(logging.StreamHandler(sys.stdout))


Expand Down
4 changes: 4 additions & 0 deletions plugins/store/kics/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@ func (s *Scanner) SetStatus(newStatus *types.Status) {
func (s *Scanner) Start(config types.Config) {
go func() {
logger := plugin.GetLogger()
err := printer.LogLevel(plugin.GetLogLevel(), false) //nolint:forbidigo
if err != nil {
logger.Error("Failed to set log level", slog.Any("error", err))
}

ctx, cancel := context.WithTimeout(context.Background(), time.Duration(config.TimeoutSeconds)*time.Second)
s.cancel = cancel
Expand Down

0 comments on commit 3fd770c

Please sign in to comment.