Skip to content

Commit 0090b97

Browse files
authored
fix: make Prometheus namespace optional (GoogleCloudPlatform#87)
This is an adaptation of GoogleCloudPlatform/cloud-sql-proxy#1280.
1 parent e0b95b9 commit 0090b97

File tree

2 files changed

+8
-7
lines changed

2 files changed

+8
-7
lines changed

cmd/root.go

+7-4
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ type Command struct {
8888
disableMetrics bool
8989
telemetryProject string
9090
telemetryPrefix string
91+
prometheus bool
9192
prometheusNamespace string
9293
healthCheck bool
9394
httpPort string
@@ -184,8 +185,10 @@ the maximum time has passed. Defaults to 0s.`)
184185
"Disable Cloud Monitoring integration (used with telemetry-project)")
185186
cmd.PersistentFlags().StringVar(&c.telemetryPrefix, "telemetry-prefix", "",
186187
"Prefix to use for Cloud Monitoring metrics.")
188+
cmd.PersistentFlags().BoolVar(&c.prometheus, "prometheus", false,
189+
"Enable Prometheus HTTP endpoint /metrics")
187190
cmd.PersistentFlags().StringVar(&c.prometheusNamespace, "prometheus-namespace", "",
188-
"Enable Prometheus for metric collection using the provided namespace")
191+
"Use the provided Prometheus namespace for metrics")
189192
cmd.PersistentFlags().StringVar(&c.httpPort, "http-port", "9090",
190193
"Port for the Prometheus server to use")
191194
cmd.PersistentFlags().BoolVar(&c.healthCheck, "health-check", false,
@@ -247,8 +250,8 @@ func parseConfig(cmd *Command, conf *proxy.Config, args []string) error {
247250
cmd.logger.Infof("Using API Endpoint %v", conf.APIEndpointURL)
248251
}
249252

250-
if userHasSet("http-port") && !userHasSet("prometheus-namespace") && !userHasSet("health-check") {
251-
cmd.logger.Infof("Ignoring --http-port because --prometheus-namespace or --health-check was not set")
253+
if userHasSet("http-port") && !userHasSet("prometheus") && !userHasSet("health-check") {
254+
cmd.logger.Infof("Ignoring --http-port because --prometheus or --health-check was not set")
252255
}
253256

254257
if !userHasSet("telemetry-project") && userHasSet("telemetry-prefix") {
@@ -367,7 +370,7 @@ func runSignalWrapper(cmd *Command) error {
367370
needsHTTPServer bool
368371
mux = http.NewServeMux()
369372
)
370-
if cmd.prometheusNamespace != "" {
373+
if cmd.prometheus {
371374
needsHTTPServer = true
372375
e, err := prometheus.NewExporter(prometheus.Options{
373376
Namespace: cmd.prometheusNamespace,

cmd/root_test.go

+1-3
Original file line numberDiff line numberDiff line change
@@ -457,9 +457,7 @@ func TestPrometheusMetricsEndpoint(t *testing.T) {
457457
// Keep the test output quiet
458458
c.SilenceUsage = true
459459
c.SilenceErrors = true
460-
c.SetArgs([]string{
461-
"--prometheus-namespace", "prometheus",
462-
"my-project:my-region:my-instance"})
460+
c.SetArgs([]string{"--prometheus", "my-project:my-region:my-instance"})
463461

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

0 commit comments

Comments
 (0)