Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 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
22 changes: 22 additions & 0 deletions client/cmd/debug.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@
return nil
}

func runForDuration(cmd *cobra.Command, args []string) error {

Check warning on line 153 in client/cmd/debug.go

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

This function has 118 lines of code, which is greater than the 100 authorized. Split it into smaller functions.

See more on https://sonarcloud.io/project/issues?id=netbirdio_netbird&issues=AZrBPcx3jYxA46X0CL0z&open=AZrBPcx3jYxA46X0CL0z&pullRequest=4700
duration, err := time.ParseDuration(args[0])
if err != nil {
return fmt.Errorf("invalid duration format: %v", err)
Expand Down Expand Up @@ -220,6 +220,20 @@

time.Sleep(3 * time.Second)

cpuProfilingStarted := false
if _, err := client.StartCPUProfile(cmd.Context(), &proto.StartCPUProfileRequest{}); err != nil {
cmd.PrintErrf("Failed to start CPU profiling: %v\n", err)
} else {
cpuProfilingStarted = true
defer func() {
if cpuProfilingStarted {
if _, err := client.StopCPUProfile(cmd.Context(), &proto.StopCPUProfileRequest{}); err != nil {
cmd.PrintErrf("Failed to stop CPU profiling: %v\n", err)
}
}
}()
}

headerPostUp := fmt.Sprintf("----- NetBird post-up - Timestamp: %s", time.Now().Format(time.RFC3339))
statusOutput := fmt.Sprintf("%s\n%s", headerPostUp, getStatusOutput(cmd, anonymizeFlag))

Expand All @@ -228,6 +242,13 @@
}
cmd.Println("\nDuration completed")

if cpuProfilingStarted {
if _, err := client.StopCPUProfile(cmd.Context(), &proto.StopCPUProfileRequest{}); err != nil {
cmd.PrintErrf("Failed to stop CPU profiling: %v\n", err)
}
cpuProfilingStarted = false
}
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Outdated

Comment thread
lixmal marked this conversation as resolved.
cmd.Println("Creating debug bundle...")

headerPreDown := fmt.Sprintf("----- NetBird pre-down - Timestamp: %s - Duration: %s", time.Now().Format(time.RFC3339), duration)
Expand Down Expand Up @@ -379,6 +400,7 @@
StatusRecorder: recorder,
SyncResponse: syncResponse,
LogFile: logFilePath,
CPUProfile: nil,
},
debug.BundleConfig{
IncludeSystemInfo: true,
Expand Down
21 changes: 21 additions & 0 deletions client/internal/debug/debug.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ block.prof: Block profiling information.
heap.prof: Heap profiling information (snapshot of memory allocations).
allocs.prof: Allocations profiling information.
threadcreate.prof: Thread creation profiling information.
cpu.prof: CPU profiling information.


Anonymization Process
Expand Down Expand Up @@ -219,6 +220,7 @@ type BundleGenerator struct {
statusRecorder *peer.Status
syncResponse *mgmProto.SyncResponse
logFile string
cpuProfile []byte

anonymize bool
clientStatus string
Expand All @@ -240,6 +242,7 @@ type GeneratorDependencies struct {
StatusRecorder *peer.Status
SyncResponse *mgmProto.SyncResponse
LogFile string
CPUProfile []byte
}

func NewBundleGenerator(deps GeneratorDependencies, cfg BundleConfig) *BundleGenerator {
Expand All @@ -256,6 +259,7 @@ func NewBundleGenerator(deps GeneratorDependencies, cfg BundleConfig) *BundleGen
statusRecorder: deps.StatusRecorder,
syncResponse: deps.SyncResponse,
logFile: deps.LogFile,
cpuProfile: deps.CPUProfile,

anonymize: cfg.Anonymize,
clientStatus: cfg.ClientStatus,
Expand Down Expand Up @@ -327,6 +331,10 @@ func (g *BundleGenerator) createArchive() error {
log.Errorf("failed to add profiles to debug bundle: %v", err)
}

if err := g.addCPUProfile(); err != nil {
log.Errorf("failed to add CPU profile to debug bundle: %v", err)
}

if err := g.addSyncResponse(); err != nil {
return fmt.Errorf("add sync response: %w", err)
}
Expand Down Expand Up @@ -522,6 +530,19 @@ func (g *BundleGenerator) addProf() (err error) {
return nil
}

func (g *BundleGenerator) addCPUProfile() error {
if len(g.cpuProfile) == 0 {
return nil
}

reader := bytes.NewReader(g.cpuProfile)
if err := g.addFileToZip(reader, "cpu.prof"); err != nil {
return fmt.Errorf("add CPU profile to zip: %w", err)
}

return nil
}

func (g *BundleGenerator) addInterfaces() error {
interfaces, err := net.Interfaces()
if err != nil {
Expand Down
Loading
Loading