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
This commit is contained in:
Timothy Messier committed 2022-06-21 14:28:02 +00:00
1 parent f3c90df300
commit 21c0509a82
1 file changed
+11 -2
+11 -2
View File
@@ -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
}
}