Skip to content

Commit 0f8f1e8

Browse files
committed
Add cpu profile to debug bundle
1 parent 6654e2d commit 0f8f1e8

File tree

8 files changed

+412
-51
lines changed

8 files changed

+412
-51
lines changed

client/cmd/debug.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,20 @@ func runForDuration(cmd *cobra.Command, args []string) error {
220220

221221
time.Sleep(3 * time.Second)
222222

223+
cpuProfilingStarted := false
224+
if _, err := client.StartCPUProfile(cmd.Context(), &proto.StartCPUProfileRequest{}); err != nil {
225+
cmd.PrintErrf("Failed to start CPU profiling: %v\n", err)
226+
} else {
227+
cpuProfilingStarted = true
228+
defer func() {
229+
if cpuProfilingStarted {
230+
if _, err := client.StopCPUProfile(cmd.Context(), &proto.StopCPUProfileRequest{}); err != nil {
231+
cmd.PrintErrf("Failed to stop CPU profiling: %v\n", err)
232+
}
233+
}
234+
}()
235+
}
236+
223237
headerPostUp := fmt.Sprintf("----- NetBird post-up - Timestamp: %s", time.Now().Format(time.RFC3339))
224238
statusOutput := fmt.Sprintf("%s\n%s", headerPostUp, getStatusOutput(cmd, anonymizeFlag))
225239

@@ -228,6 +242,13 @@ func runForDuration(cmd *cobra.Command, args []string) error {
228242
}
229243
cmd.Println("\nDuration completed")
230244

245+
if cpuProfilingStarted {
246+
if _, err := client.StopCPUProfile(cmd.Context(), &proto.StopCPUProfileRequest{}); err != nil {
247+
cmd.PrintErrf("Failed to stop CPU profiling: %v\n", err)
248+
}
249+
cpuProfilingStarted = false
250+
}
251+
231252
cmd.Println("Creating debug bundle...")
232253

233254
headerPreDown := fmt.Sprintf("----- NetBird pre-down - Timestamp: %s - Duration: %s", time.Now().Format(time.RFC3339), duration)
@@ -379,6 +400,7 @@ func generateDebugBundle(config *profilemanager.Config, recorder *peer.Status, c
379400
StatusRecorder: recorder,
380401
SyncResponse: syncResponse,
381402
LogFile: logFilePath,
403+
CPUProfile: nil,
382404
},
383405
debug.BundleConfig{
384406
IncludeSystemInfo: true,

client/internal/debug/debug.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,7 @@ type BundleGenerator struct {
203203
statusRecorder *peer.Status
204204
syncResponse *mgmProto.SyncResponse
205205
logFile string
206+
cpuProfile []byte
206207

207208
anonymize bool
208209
clientStatus string
@@ -224,6 +225,7 @@ type GeneratorDependencies struct {
224225
StatusRecorder *peer.Status
225226
SyncResponse *mgmProto.SyncResponse
226227
LogFile string
228+
CPUProfile []byte
227229
}
228230

229231
func NewBundleGenerator(deps GeneratorDependencies, cfg BundleConfig) *BundleGenerator {
@@ -240,6 +242,7 @@ func NewBundleGenerator(deps GeneratorDependencies, cfg BundleConfig) *BundleGen
240242
statusRecorder: deps.StatusRecorder,
241243
syncResponse: deps.SyncResponse,
242244
logFile: deps.LogFile,
245+
cpuProfile: deps.CPUProfile,
243246

244247
anonymize: cfg.Anonymize,
245248
clientStatus: cfg.ClientStatus,
@@ -311,6 +314,10 @@ func (g *BundleGenerator) createArchive() error {
311314
log.Errorf("failed to add profiles to debug bundle: %v", err)
312315
}
313316

317+
if err := g.addCPUProfile(); err != nil {
318+
log.Errorf("failed to add CPU profile to debug bundle: %v", err)
319+
}
320+
314321
if err := g.addSyncResponse(); err != nil {
315322
return fmt.Errorf("add sync response: %w", err)
316323
}
@@ -490,6 +497,19 @@ func (g *BundleGenerator) addProf() (err error) {
490497
return nil
491498
}
492499

500+
func (g *BundleGenerator) addCPUProfile() error {
501+
if g.cpuProfile == nil || len(g.cpuProfile) == 0 {
502+
return nil
503+
}
504+
505+
reader := bytes.NewReader(g.cpuProfile)
506+
if err := g.addFileToZip(reader, "cpu.prof"); err != nil {
507+
return fmt.Errorf("add CPU profile to zip: %w", err)
508+
}
509+
510+
return nil
511+
}
512+
493513
func (g *BundleGenerator) addInterfaces() error {
494514
interfaces, err := net.Interfaces()
495515
if err != nil {

0 commit comments

Comments
 (0)