From 585f907a3c2eebdc60c56274ec54f99afe0f22c2 Mon Sep 17 00:00:00 2001 From: Eno Compton Date: Wed, 27 Jul 2022 21:09:30 -0600 Subject: [PATCH] fix: make Prometheus namespace optional --- cmd/root.go | 11 +++++++---- cmd/root_test.go | 4 +--- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/cmd/root.go b/cmd/root.go index ed7299357..923d092a4 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -76,6 +76,7 @@ type Command struct { disableMetrics bool telemetryProject string telemetryPrefix string + prometheus bool prometheusNamespace string healthCheck bool httpPort string @@ -169,8 +170,10 @@ the maximum time has passed. Defaults to 0s.`) "Disable Cloud Monitoring integration (used with telemetry-project)") cmd.PersistentFlags().StringVar(&c.telemetryPrefix, "telemetry-prefix", "", "Prefix to use for Cloud Monitoring metrics.") + cmd.PersistentFlags().BoolVar(&c.prometheus, "prometheus", false, + "Enable Prometheus HTTP endpoint /metrics") cmd.PersistentFlags().StringVar(&c.prometheusNamespace, "prometheus-namespace", "", - "Enable Prometheus for metric collection using the provided namespace") + "Use the provided Prometheus namespace for metrics") cmd.PersistentFlags().StringVar(&c.httpPort, "http-port", "9090", "Port for the Prometheus server to use") cmd.PersistentFlags().BoolVar(&c.healthCheck, "health-check", false, @@ -230,8 +233,8 @@ func parseConfig(cmd *Command, conf *proxy.Config, args []string) error { return newBadCommandError("cannot specify --credentials-file and --gcloud-auth flags at the same time") } - if userHasSet("http-port") && !userHasSet("prometheus-namespace") && !userHasSet("health-check") { - cmd.logger.Infof("Ignoring --http-port because --prometheus-namespace or --health-check was not set") + if userHasSet("http-port") && !userHasSet("prometheus") && !userHasSet("health-check") { + cmd.logger.Infof("Ignoring --http-port because --prometheus or --health-check was not set") } if !userHasSet("telemetry-project") && userHasSet("telemetry-prefix") { @@ -402,7 +405,7 @@ func runSignalWrapper(cmd *Command) error { needsHTTPServer bool mux = http.NewServeMux() ) - if cmd.prometheusNamespace != "" { + if cmd.prometheus { needsHTTPServer = true e, err := prometheus.NewExporter(prometheus.Options{ Namespace: cmd.prometheusNamespace, diff --git a/cmd/root_test.go b/cmd/root_test.go index 880acc879..7faeb4fbe 100644 --- a/cmd/root_test.go +++ b/cmd/root_test.go @@ -595,9 +595,7 @@ func TestPrometheusMetricsEndpoint(t *testing.T) { // Keep the test output quiet c.SilenceUsage = true c.SilenceErrors = true - c.SetArgs([]string{ - "--prometheus-namespace", "prometheus", - "my-project:my-region:my-instance"}) + c.SetArgs([]string{"--prometheus", "my-project:my-region:my-instance"}) ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) defer cancel()