Skip to content

Commit

Permalink
fix: make Prometheus namespace optional (#1280)
Browse files Browse the repository at this point in the history
  • Loading branch information
enocom committed Aug 23, 2022
1 parent fdc6e06 commit 22777fd
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 7 deletions.
11 changes: 7 additions & 4 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ type Command struct {
disableMetrics bool
telemetryProject string
telemetryPrefix string
prometheus bool
prometheusNamespace string
healthCheck bool
httpPort string
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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") {
Expand Down Expand Up @@ -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,
Expand Down
4 changes: 1 addition & 3 deletions cmd/root_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit 22777fd

Please sign in to comment.