Adjusted the order of logging Profiler Report

This CL modified the order in which profiler report is logged. First,
the output of arbitrary shell commands is logged, then USEMetrics are
logged.

Change-Id: Ida90c473cb4d40fc8911dd176f2e0f418ec8605d
Reviewed-on: https://cos-review.googlesource.com/c/cos/tools/+/20811
Cloud-Build: GCB Service account <228075978874@cloudbuild.gserviceaccount.com>
Reviewed-by: Dexter Rivera <riverade@google.com>
Reviewed-by: Vaibhav Rustagi <vaibhavrustagi@google.com>
Tested-by: Dexter Rivera <riverade@google.com>
diff --git a/src/pkg/nodeprofiler/cloudlogger/cloudlogger.go b/src/pkg/nodeprofiler/cloudlogger/cloudlogger.go
index c0b7aea..953145f 100644
--- a/src/pkg/nodeprofiler/cloudlogger/cloudlogger.go
+++ b/src/pkg/nodeprofiler/cloudlogger/cloudlogger.go
@@ -221,6 +221,27 @@
 	log.Info("Running Profiler . . .")
 	// Ensure logging entries are written to the cloud logging backend.
 	defer g.Flush()
+	// Only log shell command if the user specified a command.
+	if len(opts.Command) == 0 {
+		emptyCmd = true
+	} else {
+		emptyCmd = false
+		log.Info("Running shell command . . .")
+		// Fetching command from user input and populating the cmdArray
+		// with the main command and its flags.
+		cmdArray := strings.Split(opts.Command, " ")
+		usrMainCmd := cmdArray[0]
+		usrMainCmdFlags := cmdArray[1:]
+		for i := 0; i < opts.CmdCount; i++ {
+			if err := logShellCommand(g, opts.CmdTimeOut, usrMainCmd, usrMainCmdFlags...); err != nil {
+				errArr = append(errArr, err)
+				continue
+			}
+			// Delaying execution by cmdInterval seconds.
+			time.Sleep(opts.CmdInterval)
+		}
+		log.Infof("Done running shell command.")
+	}
 	// Run the profiler profCount times. The default value is 1 time unless user
 	// set the counter to a different number.
 	for i := 0; i < opts.ProfilerCount; i++ {
@@ -237,26 +258,5 @@
 		time.Sleep(opts.ProfilerInterval)
 	}
 	log.Info("Done running profiler.")
-	// Only log shell command if the user specified a command.
-	if len(opts.Command) == 0 {
-		emptyCmd = true
-		return checkLogError(emptyCmd, errArr)
-	}
-	emptyCmd = false
-	log.Info("Running shell command . . .")
-	// Fetching command from user input and populating the cmdArray
-	// with the main command and its flags.
-	cmdArray := strings.Split(opts.Command, " ")
-	usrMainCmd := cmdArray[0]
-	usrMainCmdFlags := cmdArray[1:]
-	for i := 0; i < opts.CmdCount; i++ {
-		if err := logShellCommand(g, opts.CmdTimeOut, usrMainCmd, usrMainCmdFlags...); err != nil {
-			errArr = append(errArr, err)
-			continue
-		}
-		// Delaying execution by cmdInterval seconds.
-		time.Sleep(opts.CmdInterval)
-	}
-	log.Infof("Done running shell command.")
 	return checkLogError(emptyCmd, errArr)
 }
diff --git a/src/pkg/nodeprofiler/cloudlogger/cloudlogger_test.go b/src/pkg/nodeprofiler/cloudlogger/cloudlogger_test.go
index 613479e..3aac11a 100644
--- a/src/pkg/nodeprofiler/cloudlogger/cloudlogger_test.go
+++ b/src/pkg/nodeprofiler/cloudlogger/cloudlogger_test.go
@@ -304,15 +304,6 @@
 			wantOutput: []logging.Entry{
 				{
 					Payload: struct {
-						Components []componentInfo
-						Analysis   string
-					}{
-						Components: cInfos,
-						Analysis:   useReport.Analysis,
-					},
-					Severity: logging.Debug,
-				}, {
-					Payload: struct {
 						CommandName   string
 						CommandOutput string
 					}{
@@ -321,7 +312,17 @@
 					},
 					Severity: logging.Debug,
 				},
-			},
+
+				{
+					Payload: struct {
+						Components []componentInfo
+						Analysis   string
+					}{
+						Components: cInfos,
+						Analysis:   useReport.Analysis,
+					},
+					Severity: logging.Debug,
+				}},
 			wantErr: false,
 		},
 		{
@@ -340,24 +341,6 @@
 			wantOutput: []logging.Entry{
 				{
 					Payload: struct {
-						Components []componentInfo
-						Analysis   string
-					}{
-						Components: cInfos,
-						Analysis:   useReport.Analysis,
-					},
-					Severity: logging.Debug,
-				}, {
-					Payload: struct {
-						Components []componentInfo
-						Analysis   string
-					}{
-						Components: cInfos,
-						Analysis:   useReport.Analysis,
-					},
-					Severity: logging.Debug,
-				}, {
-					Payload: struct {
 						CommandName   string
 						CommandOutput string
 					}{
@@ -384,7 +367,26 @@
 					},
 					Severity: logging.Debug,
 				},
-			},
+
+				{
+					Payload: struct {
+						Components []componentInfo
+						Analysis   string
+					}{
+						Components: cInfos,
+						Analysis:   useReport.Analysis,
+					},
+					Severity: logging.Debug,
+				}, {
+					Payload: struct {
+						Components []componentInfo
+						Analysis   string
+					}{
+						Components: cInfos,
+						Analysis:   useReport.Analysis,
+					},
+					Severity: logging.Debug,
+				}},
 			wantErr: false,
 		},