Skip to content

Commit 3e4be77

Browse files
committed
Serve metrics optionally with TLS via HTTPS
1 parent 9d52ed9 commit 3e4be77

File tree

3 files changed

+12
-3
lines changed

3 files changed

+12
-3
lines changed

internal/cmd/run.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ func newRunCommand() *runCommand {
2828
runCommand.cmd.Flags().IntVar(&globalConfig.HttpPort, "http-port", getEnvInt("HTTP_PORT", server.DefaultHttpPort), "Port to serve HTTP traffic on")
2929
runCommand.cmd.Flags().IntVar(&globalConfig.HttpsPort, "https-port", getEnvInt("HTTPS_PORT", server.DefaultHttpsPort), "Port to serve HTTPS traffic on")
3030
runCommand.cmd.Flags().IntVar(&globalConfig.MetricsPort, "metrics-port", getEnvInt("METRICS_PORT", 0), "Publish metrics on the specified port (default zero to disable)")
31+
runCommand.cmd.Flags().BoolVar(&globalConfig.MetricsTls, "metrics-tls", getEnvBool("METRICS_TLS", false), "Enable TLS for the metrics port (default false for HTTP)")
3132
runCommand.cmd.Flags().BoolVar(&globalConfig.HTTP3Enabled, "http3", false, "Enable HTTP/3")
3233

3334
return runCommand

internal/server/config.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ type Config struct {
1717
HttpPort int
1818
HttpsPort int
1919
MetricsPort int
20+
MetricsTls bool
2021
HTTP3Enabled bool
2122

2223
AlternateConfigDir string

internal/server/server.go

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -196,10 +196,17 @@ func (s *Server) startMetricsServer() error {
196196
Addr: addr,
197197
Handler: handler,
198198
}
199+
if s.config.MetricsTls {
200+
s.metricsServer.TLSConfig = &tls.Config{
201+
MinVersion: tls.VersionTLS13,
202+
GetCertificate: s.router.GetCertificate,
203+
}
204+
go s.metricsServer.ServeTLS(s.metricsListener, "", "")
205+
} else {
206+
go s.metricsServer.Serve(s.metricsListener)
207+
}
199208

200-
go s.metricsServer.Serve(s.metricsListener)
201-
202-
slog.Info("Metrics enabled", "address", addr)
209+
slog.Info("Metrics enabled", "port", s.config.MetricsPort, "TLS", s.config.MetricsTls)
203210

204211
return nil
205212
}

0 commit comments

Comments
 (0)