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

feat(profiler): support configurable debug logging destination #8104

Merged
merged 4 commits into from
Jun 15, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
10 changes: 9 additions & 1 deletion profiler/profiler.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import (
"context"
"errors"
"fmt"
"io"
"log"
"os"
"regexp"
Expand Down Expand Up @@ -130,6 +131,10 @@ type Config struct {
// defaults to false.
DebugLogging bool

// DebugLoggingOut is where the logger will write debug logs to, if enabled.
// It defaults to os.Stderr.
DebugLoggingOut io.Writer
noahdietz marked this conversation as resolved.
Show resolved Hide resolved

// MutexProfiling enables mutex profiling. It defaults to false.
// Note that mutex profiling is not supported by Go versions older
// than Go 1.8.
Expand Down Expand Up @@ -225,7 +230,10 @@ func Start(cfg Config, options ...option.ClientOption) error {
}

func start(cfg Config, options ...option.ClientOption) error {
logger = log.New(os.Stderr, "Cloud Profiler: ", log.LstdFlags)
if cfg.DebugLoggingOut == nil {
cfg.DebugLoggingOut = os.Stderr
}
logger = log.New(cfg.DebugLoggingOut, "Cloud Profiler: ", log.LstdFlags)
if err := initializeConfig(cfg); err != nil {
debugLog("failed to initialize config: %v", err)
return err
Expand Down
19 changes: 13 additions & 6 deletions profiler/profiler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -972,13 +972,16 @@ func TestAgentWithServer(t *testing.T) {
quitProfilee := make(chan bool)
go profileeLoop(quitProfilee)

var logs bytes.Buffer
if err := Start(Config{
Service: testService,
ProjectID: testProjectID,
APIAddr: srv.Addr,
Instance: testInstance,
Zone: testZone,
numProfiles: 2,
Service: testService,
ProjectID: testProjectID,
APIAddr: srv.Addr,
Instance: testInstance,
Zone: testZone,
numProfiles: 2,
DebugLogging: true,
DebugLoggingOut: &logs,
}); err != nil {
t.Fatalf("Start(): %v", err)
}
Expand All @@ -997,6 +1000,10 @@ func TestAgentWithServer(t *testing.T) {
t.Errorf("validateProfile(%s) got error: %v", pType, err)
}
}

if logs.Len() == 0 {
t.Error("expected some debug logging output, but got none")
}
}

// testConnPool is a gtransport.ConnPool used for testing.
Expand Down