Skip to content

Commit c05e819

Browse files
authored
fix: make Prometheus namespace optional (#1280)
1 parent bdc1139 commit c05e819

File tree

2 files changed

+8
-7
lines changed

2 files changed

+8
-7
lines changed

cmd/root.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ type Command struct {
7676
disableMetrics bool
7777
telemetryProject string
7878
telemetryPrefix string
79+
prometheus bool
7980
prometheusNamespace string
8081
healthCheck bool
8182
httpPort string
@@ -169,8 +170,10 @@ the maximum time has passed. Defaults to 0s.`)
169170
"Disable Cloud Monitoring integration (used with telemetry-project)")
170171
cmd.PersistentFlags().StringVar(&c.telemetryPrefix, "telemetry-prefix", "",
171172
"Prefix to use for Cloud Monitoring metrics.")
173+
cmd.PersistentFlags().BoolVar(&c.prometheus, "prometheus", false,
174+
"Enable Prometheus HTTP endpoint /metrics")
172175
cmd.PersistentFlags().StringVar(&c.prometheusNamespace, "prometheus-namespace", "",
173-
"Enable Prometheus for metric collection using the provided namespace")
176+
"Use the provided Prometheus namespace for metrics")
174177
cmd.PersistentFlags().StringVar(&c.httpPort, "http-port", "9090",
175178
"Port for the Prometheus server to use")
176179
cmd.PersistentFlags().BoolVar(&c.healthCheck, "health-check", false,
@@ -230,8 +233,8 @@ func parseConfig(cmd *Command, conf *proxy.Config, args []string) error {
230233
return newBadCommandError("cannot specify --credentials-file and --gcloud-auth flags at the same time")
231234
}
232235

233-
if userHasSet("http-port") && !userHasSet("prometheus-namespace") && !userHasSet("health-check") {
234-
cmd.logger.Infof("Ignoring --http-port because --prometheus-namespace or --health-check was not set")
236+
if userHasSet("http-port") && !userHasSet("prometheus") && !userHasSet("health-check") {
237+
cmd.logger.Infof("Ignoring --http-port because --prometheus or --health-check was not set")
235238
}
236239

237240
if !userHasSet("telemetry-project") && userHasSet("telemetry-prefix") {
@@ -402,7 +405,7 @@ func runSignalWrapper(cmd *Command) error {
402405
needsHTTPServer bool
403406
mux = http.NewServeMux()
404407
)
405-
if cmd.prometheusNamespace != "" {
408+
if cmd.prometheus {
406409
needsHTTPServer = true
407410
e, err := prometheus.NewExporter(prometheus.Options{
408411
Namespace: cmd.prometheusNamespace,

cmd/root_test.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -595,9 +595,7 @@ func TestPrometheusMetricsEndpoint(t *testing.T) {
595595
// Keep the test output quiet
596596
c.SilenceUsage = true
597597
c.SilenceErrors = true
598-
c.SetArgs([]string{
599-
"--prometheus-namespace", "prometheus",
600-
"my-project:my-region:my-instance"})
598+
c.SetArgs([]string{"--prometheus", "my-project:my-region:my-instance"})
601599

602600
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
603601
defer cancel()

0 commit comments

Comments
 (0)