Skip to content

Commit a6b5f5f

Browse files
committed
Refactor Prometheus metrics configuration and validation
1 parent 82d7bdc commit a6b5f5f

File tree

2 files changed

+17
-15
lines changed

2 files changed

+17
-15
lines changed

cmd/root.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,8 +121,8 @@ func init() {
121121
rootCmd.Flags().StringVar(&config.SendAPIAuthFile, "send-api-auth-file", config.SendAPIAuthFile, "A password file for Send API authentication")
122122
rootCmd.Flags().BoolVar(&config.SendAPIAuthAcceptAny, "send-api-auth-accept-any", config.SendAPIAuthAcceptAny, "Accept any username and password for the Send API endpoint, including none")
123123

124-
// Prometheus Metrics
125-
rootCmd.Flags().StringVar(&config.PrometheusListen, "enable-prometheus", config.PrometheusListen, "Enable Prometheus metrics: false=disabled, 'true'=use web port, address=separate server (':9090')")
124+
// Prometheus metrics
125+
rootCmd.Flags().StringVar(&config.PrometheusListen, "enable-prometheus", config.PrometheusListen, "Enable Prometheus metrics: true|false|<bind interface & port> (eg:':9090')")
126126

127127
// SMTP server
128128
rootCmd.Flags().StringVarP(&config.SMTPListen, "smtp", "s", config.SMTPListen, "SMTP bind interface and port")

config/config.go

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ var (
192192
AllowUntrustedTLS bool
193193

194194
// PrometheusListen address for Prometheus metrics server
195-
// Empty = disabled, "true"= use existing web server, address = separate server
195+
// Empty = disabled, true= use existing web server, address = separate server
196196
PrometheusListen string
197197

198198
// Version is the default application version, updated on release
@@ -362,6 +362,20 @@ func VerifyConfig() error {
362362
logger.Log().Info("[send-api] disabling authentication")
363363
}
364364

365+
// Prometheus configuration validation
366+
if PrometheusListen != "" {
367+
mode := strings.ToLower(strings.TrimSpace(PrometheusListen))
368+
if mode != "true" && mode != "false" {
369+
// Validate as address for separate server mode
370+
_, err := net.ResolveTCPAddr("tcp", PrometheusListen)
371+
if err != nil {
372+
return fmt.Errorf("[prometheus] %s", err.Error())
373+
}
374+
} else if mode == "true" {
375+
logger.Log().Info("[prometheus] enabling metrics")
376+
}
377+
}
378+
365379
// SMTP server
366380
if SMTPTLSCert != "" && SMTPTLSKey == "" || SMTPTLSCert == "" && SMTPTLSKey != "" {
367381
return errors.New("[smtp] you must provide both an SMTP TLS certificate and a key")
@@ -571,17 +585,5 @@ func VerifyConfig() error {
571585
logger.Log().Info("demo mode enabled")
572586
}
573587

574-
// Prometheus configuration validation
575-
if PrometheusListen != "" {
576-
mode := strings.ToLower(strings.TrimSpace(PrometheusListen))
577-
if mode != "true" && mode != "false" {
578-
// Validate as address for separate server mode
579-
_, err := net.ResolveTCPAddr("tcp", PrometheusListen)
580-
if err != nil {
581-
return fmt.Errorf("[prometheus] %s", err.Error())
582-
}
583-
}
584-
}
585-
586588
return nil
587589
}

0 commit comments

Comments
 (0)