diff --git a/cmd/rbe_configs_gen/rbe_configs_gen.go b/cmd/rbe_configs_gen/rbe_configs_gen.go index 96cebebb0..99b94c1fa 100644 --- a/cmd/rbe_configs_gen/rbe_configs_gen.go +++ b/cmd/rbe_configs_gen/rbe_configs_gen.go @@ -168,8 +168,7 @@ func main() { } else { log.Printf("Config generation was successful.") } - // Monitoring is optional for config generation and used for internal alerting by the owners - // of this repo only. + // Monitoring is optional and used for internal alerting by the owners of this repo only. if mc != nil { if err := mc.ReportToolchainConfigsGeneration(ctx, *monitoringDockerImage, result); err != nil { log.Fatalf("Failed to report config result to monitoring: %v", err) diff --git a/cmd/rbe_configs_upload/rbe_configs_upload.go b/cmd/rbe_configs_upload/rbe_configs_upload.go index bc171bf11..f1fe5949d 100644 --- a/cmd/rbe_configs_upload/rbe_configs_upload.go +++ b/cmd/rbe_configs_upload/rbe_configs_upload.go @@ -49,8 +49,9 @@ import ( var ( configsTarball = flag.String("configs_tarball", "", "Path to the configs tarball generated by rbe_configs_gen to be uploaded to GCS.") configsManifest = flag.String("configs_manifest", "", "Path to the JSON manifest generated by rbe_configs_gen.") - monitoringProjectID = flag.String("monitoring_project_id", "", "GCP Project ID where monitoring results will be reported.") - monitoringDockerImage = flag.String("monitoring_docker_image", "", "Name of the toolchain docker image to be reported as a string label to monitoring.") + enableMonitoring = flag.Bool("enable_monitoring", false, "(Optional) Enables reporting reporting results to Google Cloud Monitoring. Defaults to false.") + monitoringProjectID = flag.String("monitoring_project_id", "", "GCP Project ID where monitoring results will be reported. Required if --enable_monitoring is true.") + monitoringDockerImage = flag.String("monitoring_docker_image", "", "Name of the toolchain docker image to be reported as a string label to monitoring. Required if --enable_monitoring is true.") ) // manifest is the metadata about the configs that'll be uploaded to GCS. @@ -136,6 +137,7 @@ func printFlags() { log.Println("rbe_configs_upload.go \\") log.Printf("--configs_tarball=%q \\", *configsTarball) log.Printf("--configs_manifest=%q \\", *configsManifest) + log.Printf("--enable_monitoring=%v \\", *enableMonitoring) log.Printf("--monitoring_project_id=%q \\", *monitoringProjectID) log.Printf("--monitoring_docker_image=%q", *monitoringDockerImage) } @@ -172,6 +174,23 @@ func uploadConfigs(ctx context.Context, containerImage string) error { return nil } +func initMonitoringClient(ctx context.Context) (*monitoring.Client, error) { + if !(*enableMonitoring) { + return nil, nil + } + if len(*monitoringProjectID) == 0 { + return nil, fmt.Errorf("--monitoring_project_id is required because --enable_monitoring is true") + } + if len(*monitoringDockerImage) == 0 { + return nil, fmt.Errorf("--monitoring_docker_image is required because --enable_monitoring is true") + } + c, err := monitoring.NewClient(ctx, *monitoringProjectID) + if err != nil { + return nil, fmt.Errorf("unable to initialize the monitoring client: %w", err) + } + return c, nil +} + func main() { flag.Parse() printFlags() @@ -182,15 +201,9 @@ func main() { if len(*configsManifest) == 0 { log.Fatalf("--configs_manifest was not specified.") } - if len(*monitoringProjectID) == 0 { - log.Fatalf("--monitoring_project_id was not specified.") - } - if len(*monitoringDockerImage) == 0 { - log.Fatalf("--monitoring_docker_image was not specified.") - } ctx := context.Background() - mc, err := monitoring.NewClient(ctx, *monitoringProjectID) + mc, err := initMonitoringClient(ctx) if err != nil { log.Fatalf("Failed to initialize monitoring: %v", err) } @@ -203,8 +216,11 @@ func main() { log.Printf("Configs uploaded successfully.") } - if err := mc.ReportToolchainConfigsUpload(ctx, *monitoringDockerImage, result); err != nil { - log.Fatalf("Failed to report results to monitoring: %v", err) + // Monitoring is optional and used for internal alerting by the owners of this repo only. + if mc != nil { + if err := mc.ReportToolchainConfigsUpload(ctx, *monitoringDockerImage, result); err != nil { + log.Fatalf("Failed to report results to monitoring: %v", err) + } } if !result { os.Exit(1) diff --git a/tests/scripts/configs_e2e/configs_e2e.go b/tests/scripts/configs_e2e/configs_e2e.go index 936af8811..56f508fe6 100644 --- a/tests/scripts/configs_e2e/configs_e2e.go +++ b/tests/scripts/configs_e2e/configs_e2e.go @@ -54,8 +54,9 @@ var ( destRoot = flag.String("dest_root", "", "Path to an empty or non-existent output directory where the Bazel Hello world repo will be set up & a Bazel build will be executed.") rbeInstance = flag.String("rbe_instance", "", "Name of the RBE instance to test the configs on in the format projects//instances/.") timeoutSeconds = flag.Int("timeout_seconds", 0, "Number of seconds before the Bazel build run in the test is killed and a timeout failure is declared.") - monitoringProjectID = flag.String("monitoring_project_id", "", "GCP Project ID where monitoring results will be reported.") - monitoringDockerImage = flag.String("monitoring_docker_image", "", "Name of the toolchain docker image to be reported as a string label to monitoring.") + enableMonitoring = flag.Bool("enable_monitoring", false, "(Optional) Enables reporting reporting results to Google Cloud Monitoring. Defaults to false.") + monitoringProjectID = flag.String("monitoring_project_id", "", "GCP Project ID where monitoring results will be reported. Required if --enable_monitoring is true.") + monitoringDockerImage = flag.String("monitoring_docker_image", "", "Name of the toolchain docker image to be reported as a string label to monitoring. Required if --enable_monitoring is true.") // filesToCopy are the files that'll be copied from srcRoot to destRoot. filesToCopy = []string{ @@ -365,6 +366,7 @@ func printFlags() { log.Printf("--dest_root=%q \\", *destRoot) log.Printf("--rbe_instance=%q \\", *rbeInstance) log.Printf("--timeout_seconds=%d \\", *timeoutSeconds) + log.Printf("--enable_monitoring=%v \\", *enableMonitoring) log.Printf("--monitoring_project_id=%q \\", *monitoringProjectID) log.Printf("--monitoring_docker_image=%q", *monitoringDockerImage) } @@ -397,6 +399,23 @@ func runTest(ctx context.Context) error { return nil } +func initMonitoringClient(ctx context.Context) (*monitoring.Client, error) { + if !(*enableMonitoring) { + return nil, nil + } + if len(*monitoringProjectID) == 0 { + return nil, fmt.Errorf("--monitoring_project_id is required because --enable_monitoring is true") + } + if len(*monitoringDockerImage) == 0 { + return nil, fmt.Errorf("--monitoring_docker_image is required because --enable_monitoring is true") + } + c, err := monitoring.NewClient(ctx, *monitoringProjectID) + if err != nil { + return nil, fmt.Errorf("unable to initialize the monitoring client: %w", err) + } + return c, nil +} + func main() { flag.Parse() printFlags() @@ -422,15 +441,9 @@ func main() { if *timeoutSeconds <= 0 { log.Fatalf("--timeout_seconds was either not specified or negative.") } - if len(*monitoringProjectID) == 0 { - log.Fatalf("--monitoring_project_id was not specified.") - } - if len(*monitoringDockerImage) == 0 { - log.Fatalf("--monitoring_docker_image was not specified.") - } ctx := context.Background() - mc, err := monitoring.NewClient(ctx, *monitoringProjectID) + mc, err := initMonitoringClient(ctx) if err != nil { log.Fatalf("Failed to initialize monitoring: %v", err) } @@ -442,8 +455,12 @@ func main() { } else { log.Printf("Config E2E test passed.") } - if err := mc.ReportToolchainConfigsTest(ctx, *monitoringDockerImage, result); err != nil { - log.Fatalf("Failed to report results to monitoring: %v", err) + + // Monitoring is optional and used for internal alerting by the owners of this repo only. + if mc != nil { + if err := mc.ReportToolchainConfigsTest(ctx, *monitoringDockerImage, result); err != nil { + log.Fatalf("Failed to report results to monitoring: %v", err) + } } if !result { os.Exit(1)