From f3c90df3007a5ca7be758a059ce8283791dd4e78 Mon Sep 17 00:00:00 2001 From: Timothy Messier Date: Thu, 7 Apr 2022 10:53:37 -0400 Subject: [PATCH 1/2] chore(db): Silence db logs by default for tests This removes a lot of log clutter from normal test execution with `go test -v`. There are a number of test that expect no results from the database or other error cases from the database. The previous default log level of dbw.Error would cause these cases to generate logs in the success case, creating noise in the test output. The new default for tests is dbw.Silent. This can be overriden in two ways. A test can be updated to pass in the db.WithTestLogLevel option, which allows for targeting specific tests to include additional output. This may make sense for tests that normally should not cause errors, but would report an error if it fails. Alternatively a new environment variable can be set, DEFAULT_TEST_LOG_LEVEL. This allows for adjusting the log level without changing any test file. The environment variable should be set to the string value of the log level, and is treated as case insensitive, ie: export DEFAULT_TEST_LOG_LEVEL="error" --- internal/db/testing.go | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/internal/db/testing.go b/internal/db/testing.go index b97c58ff3..4b058a10d 100644 --- a/internal/db/testing.go +++ b/internal/db/testing.go @@ -5,6 +5,7 @@ import ( "crypto/rand" "encoding/base64" "fmt" + "os" "strings" "testing" "time" @@ -65,7 +66,20 @@ func TestSetup(t testing.TB, dialect string, opt ...TestOption) (*DB, string) { case opts.withLogLevel != DefaultTestLogLevel: db.wrapped.LogLevel(dbw.LogLevel(opts.withLogLevel)) default: - db.wrapped.LogLevel(dbw.Error) + var defaultLevel dbw.LogLevel + switch strings.ToLower(os.Getenv("DEFAULT_TEST_LOG_LEVEL")) { + case "silent": + defaultLevel = dbw.Silent + case "error": + defaultLevel = dbw.Error + case "warn": + defaultLevel = dbw.Warn + case "info": + defaultLevel = dbw.Info + default: + defaultLevel = dbw.Silent + } + db.wrapped.LogLevel(defaultLevel) } t.Cleanup(func() { sqlDB, err := db.SqlDB(ctx) From 21c0509a82cddb6c78a1d2fc60ad8dd8a0b24d24 Mon Sep 17 00:00:00 2001 From: Timothy Messier Date: Tue, 14 Jun 2022 17:10:32 +0000 Subject: [PATCH 2/2] chore(event): Reduce log noise for tests This silences error messages when: - There is no eventer in the context.Context - There is no SysEventer - And a new env var is set Previously when the first two conditions where met, the error was logged to a default hclog.Logger with the error being wrapped in a message to note the lack of an eventer. In production this is a very important message to help operators fix a likely misconfiguration. However, in tests it is common to not have an eventer setup, meaning this generates a lot of noise. This uses the CI environment variable which is an environment variable set by most common CI environments. Developers can set this variable locally to reduce the log noise: export CI=true See: https://docs.github.com/en/actions/learn-github-actions/environment-variables#default-environment-variables https://circleci.com/docs/2.0/env-vars/#built-in-environment-variables https://docs.travis-ci.com/user/environment-variables/#default-environment-variables --- internal/observability/event/context.go | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/internal/observability/event/context.go b/internal/observability/event/context.go index 0016cfde3..ac9763165 100644 --- a/internal/observability/event/context.go +++ b/internal/observability/event/context.go @@ -3,12 +3,15 @@ package event import ( "context" "fmt" + "os" "runtime" "time" "github.com/hashicorp/go-hclog" ) +var ci bool + type key int const cancelledSendTimeout = 3 * time.Second @@ -18,6 +21,10 @@ const ( requestInfoKey ) +func init() { + ci = os.Getenv("CI") != "" +} + // NewEventerContext will return a context containing a value of the provided Eventer func NewEventerContext(ctx context.Context, eventer *Eventer) (context.Context, error) { const op = "event.NewEventerContext" @@ -128,8 +135,10 @@ func WriteError(ctx context.Context, caller Op, e error, opt ...Option) { if !ok { eventer = SysEventer() if eventer == nil { - logger := hclog.New(nil) - logger.Error(fmt.Sprintf("%s: no eventer available to write error: %v", op, e)) + if !ci { + logger := hclog.New(nil) + logger.Error(fmt.Sprintf("%s: no eventer available to write error: %v", op, e)) + } return } }