feat: adds configurable HTTP request path exclusion from logging
This commit is contained in:
@@ -10,6 +10,7 @@ import (
|
|||||||
"flag"
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
|
"regexp"
|
||||||
"runtime/debug"
|
"runtime/debug"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@@ -105,6 +106,12 @@ func prepareConfig(cfg *AppConfig) error {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Prepare user-provided expressions, panic up-front if invalid
|
||||||
|
cfg.HTTP.excludeRegexps = make([]*regexp.Regexp, len(cfg.HTTP.LogExcludePathRegexps))
|
||||||
|
for i, re := range cfg.HTTP.LogExcludePathRegexps {
|
||||||
|
cfg.HTTP.excludeRegexps[i] = regexp.MustCompile(re)
|
||||||
|
}
|
||||||
|
|
||||||
return errs
|
return errs
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -1,6 +1,10 @@
|
|||||||
package config
|
package config
|
||||||
|
|
||||||
import "time"
|
import (
|
||||||
|
"regexp"
|
||||||
|
"sync"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
var defaultHTTPConfig = &HTTPConfig{
|
var defaultHTTPConfig = &HTTPConfig{
|
||||||
Enabled: true,
|
Enabled: true,
|
||||||
@@ -14,13 +18,23 @@ var defaultHTTPConfig = &HTTPConfig{
|
|||||||
|
|
||||||
// HTTPConfig provides HTTP server Configuration
|
// HTTPConfig provides HTTP server Configuration
|
||||||
type HTTPConfig struct {
|
type HTTPConfig struct {
|
||||||
Enabled bool `yaml:"enabled" env:"APP_HTTP_ENABLED" json:"enabled,omitempty"`
|
Enabled bool `yaml:"enabled" env:"APP_HTTP_ENABLED" json:"enabled,omitempty"`
|
||||||
Listen string `yaml:"listen,omitempty" env:"APP_HTTP_LISTEN" json:"listen,omitempty"`
|
Listen string `yaml:"listen,omitempty" env:"APP_HTTP_LISTEN" json:"listen,omitempty"`
|
||||||
LogRequests bool `yaml:"logRequests" env:"APP_HTTP_LOG_REQUESTS" json:"logRequests,omitempty"`
|
LogRequests bool `yaml:"logRequests" env:"APP_HTTP_LOG_REQUESTS" json:"logRequests,omitempty"`
|
||||||
ReadTimeout string `yaml:"readTimeout" env:"APP_HTTP_READ_TIMEOUT" json:"readTimeout,omitempty"` // Go duration (e.g. 10s)
|
LogExcludePathRegexps []string `yaml:"logExcludePathRegexps" env:"APP_HTTP_LOG_EXCLUDE_PATH_REGEXPS" json:"logExcludePathRegexps,omitempty"`
|
||||||
WriteTimeout string `yaml:"writeTimeout" env:"APP_HTTP_WRITE_TIMEOUT" json:"writeTimeout,omitempty"` // Go duration (e.g. 10s)
|
ReadTimeout string `yaml:"readTimeout" env:"APP_HTTP_READ_TIMEOUT" json:"readTimeout,omitempty"` // Go duration (e.g. 10s)
|
||||||
IdleTimeout string `yaml:"idleTimeout" env:"APP_HTTP_IDLE_TIMEOUT" json:"idleTimeout,omitempty"` // Go duration (e.g. 10s)
|
WriteTimeout string `yaml:"writeTimeout" env:"APP_HTTP_WRITE_TIMEOUT" json:"writeTimeout,omitempty"` // Go duration (e.g. 10s)
|
||||||
rT *time.Duration
|
IdleTimeout string `yaml:"idleTimeout" env:"APP_HTTP_IDLE_TIMEOUT" json:"idleTimeout,omitempty"` // Go duration (e.g. 10s)
|
||||||
wT *time.Duration
|
excludeRegexps []*regexp.Regexp
|
||||||
iT *time.Duration
|
rT *time.Duration
|
||||||
|
wT *time.Duration
|
||||||
|
iT *time.Duration
|
||||||
|
lock sync.RWMutex
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *HTTPConfig) GetExcludeRegexps() []*regexp.Regexp {
|
||||||
|
h.lock.RLock()
|
||||||
|
defer h.lock.RUnlock()
|
||||||
|
|
||||||
|
return h.excludeRegexps
|
||||||
}
|
}
|
||||||
|
@@ -10,6 +10,8 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/rs/zerolog"
|
"github.com/rs/zerolog"
|
||||||
|
|
||||||
|
"gitea.libretechconsulting.com/rmcguire/go-app/pkg/config"
|
||||||
)
|
)
|
||||||
|
|
||||||
var ExcludeFromLogging = regexp.MustCompile(`\/(ready|live|metrics)$`)
|
var ExcludeFromLogging = regexp.MustCompile(`\/(ready|live|metrics)$`)
|
||||||
@@ -21,12 +23,22 @@ type LoggingResponseWriter struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func loggingMiddleware(appCtx context.Context, next http.Handler) http.Handler {
|
func loggingMiddleware(appCtx context.Context, next http.Handler) http.Handler {
|
||||||
|
appConfig := config.MustFromCtx(appCtx)
|
||||||
|
|
||||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
if ExcludeFromLogging.Match([]byte(r.URL.Path)) {
|
if ExcludeFromLogging.Match([]byte(r.URL.Path)) {
|
||||||
next.ServeHTTP(w, r)
|
next.ServeHTTP(w, r)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// User-configurable logging exclusions
|
||||||
|
for _, re := range appConfig.HTTP.GetExcludeRegexps() {
|
||||||
|
if re.MatchString(r.URL.Path) {
|
||||||
|
next.ServeHTTP(w, r)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
log := zerolog.Ctx(appCtx)
|
log := zerolog.Ctx(appCtx)
|
||||||
|
|
||||||
start := time.Now()
|
start := time.Now()
|
||||||
|
Reference in New Issue
Block a user