Skip to content

Commit cf57a42

Browse files
authored
Serve metrics optionally via HTTPS
1 parent 950b252 commit cf57a42

File tree

3 files changed

+16
-8
lines changed

3 files changed

+16
-8
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.MetricsHttps, "metrics-https", getEnvBool("METRICS_HTTPS", false), "Enable HTTPS for the metrics port (default false for HTTP)")
3132

3233
return runCommand
3334
}

internal/server/config.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@ const (
1313
)
1414

1515
type Config struct {
16-
Bind string
17-
HttpPort int
18-
HttpsPort int
19-
MetricsPort int
20-
16+
Bind string
17+
HttpPort int
18+
HttpsPort int
19+
MetricsPort int
20+
MetricsHttps bool
2121
AlternateConfigDir string
2222
}
2323

internal/server/server.go

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -173,10 +173,17 @@ func (s *Server) startMetricsServer() error {
173173
Addr: addr,
174174
Handler: handler,
175175
}
176+
if s.config.MetricsHttps {
177+
s.metricsServer.TLSConfig = &tls.Config{
178+
MinVersion: tls.VersionTLS13,
179+
GetCertificate: s.router.GetCertificate,
180+
}
181+
go s.metricsServer.ServeTLS(s.metricsListener, "", "")
182+
} else {
183+
go s.metricsServer.Serve(s.metricsListener)
184+
}
176185

177-
go s.metricsServer.Serve(s.metricsListener)
178-
179-
slog.Info("Metrics enabled", "address", addr)
186+
slog.Info("Metrics enabled", "port", s.config.MetricsPort, "https", s.config.MetricsHttps)
180187

181188
return nil
182189
}

0 commit comments

Comments
 (0)