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

internal/log: Improve API for testing #916

Merged
merged 4 commits into from
May 4, 2021
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
37 changes: 35 additions & 2 deletions internal/log/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,17 @@ 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 a function to restore the
// previous logger. The return value is mostly useful when testing.
func UseLogger(l ddtrace.Logger) (undo func()) {
Flush()
mu.Lock()
defer mu.Unlock()
old := logger
logger = l
return func() {
logger = old
}
}

// SetLevel sets the given lvl for logging.
Expand Down Expand Up @@ -172,3 +177,31 @@ 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 records every call to Log() and makes it available via 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)
}

// Logs returns the ordered list of logs recorded by the logger.
func (r *RecordLogger) Logs() []string {
r.m.Lock()
defer r.m.Unlock()
copied := make([]string, len(r.logs))
copy(copied, r.logs)
return copied
}
12 changes: 12 additions & 0 deletions profiler/profiler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import (
"testing"
"time"

"gopkg.in/DataDog/dd-trace-go.v1/internal/log"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
Expand Down Expand Up @@ -59,20 +61,30 @@ func TestStart(t *testing.T) {
})

t.Run("options/GoodAPIKey/Agent", func(t *testing.T) {
rl := &log.RecordLogger{}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There isn't really a need for a pointer. This could well be

Suggested change
rl := &log.RecordLogger{}
var rl log.RecordLogger

defer log.UseLogger(rl)()

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{}
defer log.UseLogger(rl)()

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