From d7aa62ed38df80444b77db2bd98d87b10d062461 Mon Sep 17 00:00:00 2001 From: Jonathan Wright Date: Thu, 15 Aug 2024 17:15:47 +0100 Subject: [PATCH] Disable metrics endpoint access logs by default Switch off the metrics endpoint access logs as it's unlikely they're going to be of significant value, but allow them to be re-enabled by the command-line or configuration file. --- config/serve.yaml | 1 + internal/cmd/serve.go | 4 ++++ internal/serve/metrics/main.go | 5 ++++- internal/serve/middleware/logger.go | 5 ----- schemas/serve.json | 11 ++++++++++- 5 files changed, 19 insertions(+), 7 deletions(-) diff --git a/config/serve.yaml b/config/serve.yaml index 4b1aa31..b30579a 100644 --- a/config/serve.yaml +++ b/config/serve.yaml @@ -11,4 +11,5 @@ endpoints: logging: json: true + metrics: false level: debug diff --git a/internal/cmd/serve.go b/internal/cmd/serve.go index d60d495..029bc0d 100644 --- a/internal/cmd/serve.go +++ b/internal/cmd/serve.go @@ -124,6 +124,10 @@ func init() { flags.Int("shutdown-timeout", shutdownTimeout, "Timeout (in seconds) to wait for requests to finish") _ = viper.BindPFlag("endpoints.timeouts.shutdown", flags.Lookup("shutdown-timeout")) + viper.SetDefault("logging.metrics", false) + flags.Bool("log-metrics", false, "Set whether to log metrics port requests") + _ = viper.BindPFlag("logging.metrics", flags.Lookup("log-metrics")) + rootCmd.AddCommand(serveCmd) } diff --git a/internal/serve/metrics/main.go b/internal/serve/metrics/main.go index deadeb8..c223f59 100644 --- a/internal/serve/metrics/main.go +++ b/internal/serve/metrics/main.go @@ -27,7 +27,10 @@ type Service struct { func NewService() *Service { router := gin.New() - router.Use(middleware.Logger()) + if viper.GetBool("logging.metrics") { + router.Use(middleware.Logger()) + } + router.Use(middleware.Prometheus("metrics")) router.Use(gin.Recovery()) diff --git a/internal/serve/middleware/logger.go b/internal/serve/middleware/logger.go index bc4e65a..56207f8 100644 --- a/internal/serve/middleware/logger.go +++ b/internal/serve/middleware/logger.go @@ -21,11 +21,6 @@ func Logger() gin.HandlerFunc { http.StatusUnauthorized, http.StatusNotFound, ), - // slogg.IgnorePath( - // "/alive", - // "/healthz", - // "/metrics", - // ), }, }, ) diff --git a/schemas/serve.json b/schemas/serve.json index 6eb88d8..4fdb12b 100644 --- a/schemas/serve.json +++ b/schemas/serve.json @@ -171,6 +171,9 @@ "level": { "$ref": "#/$defs/logging-level" }, + "metrics": { + "$ref": "#/$defs/logging-metrics" + }, "json": { "$ref": "#/$defs/logging-json" } @@ -181,9 +184,15 @@ "type": "string", "enum": ["debug", "info", "warning", "error"] }, + "logging-metrics": { + "description": "Set whether or not to log requests to the metrics port", + "type": "boolean", + "default": false + }, "logging-json": { "description": "Set whether or not to use JSON-based structured logging", - "type": "boolean" + "type": "boolean", + "default": false } }, "type": "object",