@@ -17,6 +17,29 @@ import (
1717 network "github.com/cloudflare/cloudflared/diagnostic/network"
1818)
1919
20+ const (
21+ taskSuccess = "success"
22+ taskFailure = "failure"
23+ jobReportName = "job report"
24+ tunnelStateJobName = "tunnel state"
25+ systemInformationJobName = "system information"
26+ goroutineJobName = "goroutine profile"
27+ heapJobName = "heap profile"
28+ metricsJobName = "metrics"
29+ logInformationJobName = "log information"
30+ rawNetworkInformationJobName = "raw network information"
31+ networkInformationJobName = "network information"
32+ cliConfigurationJobName = "cli configuration"
33+ configurationJobName = "configuration"
34+ )
35+
36+ // Struct used to hold the results of different routines executing the network collection.
37+ type taskResult struct {
38+ Result string `json:"result,omitempty"`
39+ Err error `json:"error,omitempty"`
40+ path string
41+ }
42+
2043// Struct used to hold the results of different routines executing the network collection.
2144type networkCollectionResult struct {
2245 name string
@@ -335,54 +358,54 @@ func createJobs(
335358 rawNetworkCollectorFunc , jsonNetworkCollectorFunc := networkInformationCollectors ()
336359 jobs := []collectJob {
337360 {
338- jobName : "tunnel state" ,
361+ jobName : tunnelStateJobName ,
339362 fn : tunnelStateCollectEndpointAdapter (client , tunnel , tunnelStateBaseName ),
340363 bypass : false ,
341364 },
342365 {
343- jobName : "system information" ,
366+ jobName : systemInformationJobName ,
344367 fn : collectFromEndpointAdapter (client .GetSystemInformation , systemInformationBaseName ),
345368 bypass : noDiagSystem ,
346369 },
347370 {
348- jobName : "goroutine profile" ,
371+ jobName : goroutineJobName ,
349372 fn : collectFromEndpointAdapter (client .GetGoroutineDump , goroutinePprofBaseName ),
350373 bypass : noDiagRuntime ,
351374 },
352375 {
353- jobName : "heap profile" ,
376+ jobName : heapJobName ,
354377 fn : collectFromEndpointAdapter (client .GetMemoryDump , heapPprofBaseName ),
355378 bypass : noDiagRuntime ,
356379 },
357380 {
358- jobName : "metrics" ,
381+ jobName : metricsJobName ,
359382 fn : collectFromEndpointAdapter (client .GetMetrics , metricsBaseName ),
360383 bypass : noDiagMetrics ,
361384 },
362385 {
363- jobName : "log information" ,
386+ jobName : logInformationJobName ,
364387 fn : func (ctx context.Context ) (string , error ) {
365388 return collectLogs (ctx , client , diagContainer , diagPod )
366389 },
367390 bypass : noDiagLogs ,
368391 },
369392 {
370- jobName : "raw network information" ,
393+ jobName : rawNetworkInformationJobName ,
371394 fn : rawNetworkCollectorFunc ,
372395 bypass : noDiagNetwork ,
373396 },
374397 {
375- jobName : "network information" ,
398+ jobName : networkInformationJobName ,
376399 fn : jsonNetworkCollectorFunc ,
377400 bypass : noDiagNetwork ,
378401 },
379402 {
380- jobName : "cli configuration" ,
403+ jobName : cliConfigurationJobName ,
381404 fn : collectFromEndpointAdapter (client .GetCliConfiguration , cliConfigurationBaseName ),
382405 bypass : false ,
383406 },
384407 {
385- jobName : "configuration" ,
408+ jobName : configurationJobName ,
386409 fn : collectFromEndpointAdapter (client .GetTunnelConfiguration , configurationBaseName ),
387410 bypass : false ,
388411 },
@@ -391,6 +414,69 @@ func createJobs(
391414 return jobs
392415}
393416
417+ func createTaskReport (taskReport map [string ]taskResult ) (string , error ) {
418+ dumpHandle , err := os .Create (filepath .Join (os .TempDir (), taskResultBaseName ))
419+ if err != nil {
420+ return "" , ErrCreatingTemporaryFile
421+ }
422+ defer dumpHandle .Close ()
423+
424+ err = json .NewEncoder (dumpHandle ).Encode (taskReport )
425+ if err != nil {
426+ return "" , fmt .Errorf ("error encoding task results: %w" , err )
427+ }
428+
429+ return dumpHandle .Name (), nil
430+ }
431+
432+ func runJobs (ctx context.Context , jobs []collectJob , log * zerolog.Logger ) map [string ]taskResult {
433+ jobReport := make (map [string ]taskResult , len (jobs ))
434+
435+ for _ , job := range jobs {
436+ if job .bypass {
437+ continue
438+ }
439+
440+ log .Info ().Msgf ("Collecting %s..." , job .jobName )
441+ path , err := job .fn (ctx )
442+
443+ var result taskResult
444+ if err != nil {
445+ result = taskResult {Result : taskFailure , Err : err , path : path }
446+
447+ log .Error ().Err (err ).Msgf ("Job: %s finished with error." , job .jobName )
448+ } else {
449+ result = taskResult {Result : taskSuccess , Err : nil , path : path }
450+
451+ log .Info ().Msgf ("Collected %s." , job .jobName )
452+ }
453+
454+ jobReport [job .jobName ] = result
455+ }
456+
457+ taskReportName , err := createTaskReport (jobReport )
458+
459+ var result taskResult
460+
461+ if err != nil {
462+ result = taskResult {
463+ Result : taskFailure ,
464+ path : taskReportName ,
465+ Err : err ,
466+ }
467+ } else {
468+ result = taskResult {
469+ Result : taskSuccess ,
470+ path : taskReportName ,
471+ Err : nil ,
472+ }
473+ }
474+
475+ jobReport [jobReportName ] = result
476+
477+ return jobReport
478+ }
479+
394480func RunDiagnostic (
395481 log * zerolog.Logger ,
396482 options Options ,
@@ -410,7 +496,6 @@ func RunDiagnostic(
410496
411497 defer cancel ()
412498
413- paths := make ([]string , 0 )
414499 jobs := createJobs (
415500 client ,
416501 tunnel ,
@@ -423,27 +508,17 @@ func RunDiagnostic(
423508 options .Toggles .NoDiagNetwork ,
424509 )
425510
426- for _ , job := range jobs {
427- if job .bypass {
428- continue
429- }
511+ jobsReport := runJobs (ctx , jobs , log )
512+ paths := make ([]string , 0 )
430513
431- log . Info (). Msgf ( "Collecting %s..." , job . jobName )
432- path , err := job . fn ( ctx )
514+ for _ , v := range jobsReport {
515+ paths = append ( paths , v . path )
433516
434517 defer func () {
435- if ! errors .Is (err , ErrCreatingTemporaryFile ) {
436- os .Remove (path )
518+ if ! errors .Is (v . Err , ErrCreatingTemporaryFile ) {
519+ os .Remove (v . path )
437520 }
438521 }()
439-
440- if err != nil {
441- return nil , err
442- }
443-
444- log .Info ().Msgf ("Collected %s." , job .jobName )
445-
446- paths = append (paths , path )
447522 }
448523
449524 zipfile , err := CreateDiagnosticZipFile (zipName , paths )
0 commit comments