Skip to content

Commit

Permalink
feat(profiler): support configurable debug logging destination (#8104)
Browse files Browse the repository at this point in the history
* feat(profiler): support configurable debug logging destination

* address feedback
  • Loading branch information
noahdietz authored Jun 15, 2023
1 parent 3382ef8 commit fc3d840
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 7 deletions.
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

// DebugLoggingOutput is where the logger will write debug logs to, if enabled.
// It defaults to os.Stderr.
DebugLoggingOutput io.Writer

// 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.DebugLoggingOutput == nil {
cfg.DebugLoggingOutput = os.Stderr
}
logger = log.New(cfg.DebugLoggingOutput, "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,
DebugLoggingOutput: &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

0 comments on commit fc3d840

Please sign in to comment.