Skip to content

Commit

Permalink
internal/log: Improve API for testing
Browse files Browse the repository at this point in the history
I just noticed that d9472a0 caused the profiler package to always emit
some log output, regardless of `go test -v` being used or not. I'm very
sorry about this. IMO tests should never print to stdout unless
requested.

This patch is my attempt to fix this by making it easy for individual
tests to capture the log output (see UseLogger, RecordLogger). I've also
added DiscardLogger since it can be very convenient for tests that want
to ignore log output without capturing it. Arguably RecordLogger could
be used for this as well, but that would do a poor job of capturing the
intend of such code.
  • Loading branch information
felixge committed May 3, 2021
1 parent b74a5df commit fd28bf2
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 2 deletions.
26 changes: 24 additions & 2 deletions internal/log/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,15 @@ var (
logger ddtrace.Logger = &defaultLogger{l: log.New(os.Stderr, "", log.LstdFlags)}
)

// UseLogger sets l as the active logger.
func UseLogger(l ddtrace.Logger) {
// UseLogger sets l as the active logger and returns the previously configured
// logger.
func UseLogger(l ddtrace.Logger) ddtrace.Logger {
Flush()
mu.Lock()
defer mu.Unlock()
old := logger
logger = l
return old
}

// SetLevel sets the given lvl for logging.
Expand Down Expand Up @@ -172,3 +175,22 @@ func printMsg(lvl, format string, a ...interface{}) {
type defaultLogger struct{ l *log.Logger }

func (p *defaultLogger) Log(msg string) { p.l.Print(msg) }

// DiscardLogger discards every call to Log().
type DiscardLogger struct{}

// Log implements ddtrace.Logger.
func (d DiscardLogger) Log(msg string) {}

// RecordLogger appends every call to Log() to Logs.
type RecordLogger struct {
m sync.Mutex
Logs []string
}

// Log implements ddtrace.Logger.
func (r *RecordLogger) Log(msg string) {
r.m.Lock()
defer r.m.Unlock()
r.Logs = append(r.Logs, msg)
}
13 changes: 13 additions & 0 deletions profiler/profiler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"gopkg.in/DataDog/dd-trace-go.v1/internal/log"
)

func TestStart(t *testing.T) {
Expand Down Expand Up @@ -59,20 +60,32 @@ func TestStart(t *testing.T) {
})

t.Run("options/GoodAPIKey/Agent", func(t *testing.T) {
rl := &log.RecordLogger{}
old := log.UseLogger(rl)
defer log.UseLogger(old)

err := Start(WithAPIKey("12345678901234567890123456789012"))
defer Stop()
assert.Nil(t, err)
assert.Equal(t, activeProfiler.cfg.agentURL, activeProfiler.cfg.targetURL)
assert.Equal(t, 1, len(rl.Logs))
assert.Contains(t, rl.Logs[0], "profiler.WithAPIKey")
})

t.Run("options/GoodAPIKey/Agentless", func(t *testing.T) {
rl := &log.RecordLogger{}
old := log.UseLogger(rl)
defer log.UseLogger(old)

err := Start(
WithAPIKey("12345678901234567890123456789012"),
WithAgentlessUpload(),
)
defer Stop()
assert.Nil(t, err)
assert.Equal(t, activeProfiler.cfg.apiURL, activeProfiler.cfg.targetURL)
assert.Equal(t, 1, len(rl.Logs))
assert.Contains(t, rl.Logs[0], "profiler.WithAgentlessUpload")
})

t.Run("options/BadAPIKey", func(t *testing.T) {
Expand Down

0 comments on commit fd28bf2

Please sign in to comment.