From 6c69ecebd268b112dd9f70014f71c5efa2547f60 Mon Sep 17 00:00:00 2001 From: Nikita Zinkevich Date: Thu, 18 Jul 2024 08:54:27 +0300 Subject: [PATCH] [#369] Add build tag for http logging Build tag 'loghttp' for log_http.go. Created log_http_stub.go for default response without tag. Signed-off-by: Nikita Zinkevich --- api/middleware/log_http.go | 5 +++-- api/middleware/log_http_stub.go | 31 +++++++++++++++++++++++++++++++ cmd/s3-gw/app.go | 7 +++++-- internal/logs/logs.go | 5 +++-- 4 files changed, 42 insertions(+), 6 deletions(-) create mode 100644 api/middleware/log_http_stub.go diff --git a/api/middleware/log_http.go b/api/middleware/log_http.go index a097ea7..03e4aba 100644 --- a/api/middleware/log_http.go +++ b/api/middleware/log_http.go @@ -1,3 +1,5 @@ +//go:build loghttp + package middleware import ( @@ -88,10 +90,9 @@ func LogHTTP(l *zap.Logger, config *LogHTTPConfig) Func { httplog = withFieldIfExist(httplog, "query", r.URL.Query()) httplog = withFieldIfExist(httplog, "headers", r.Header) if r.ContentLength > 0 && r.ContentLength <= config.MaxBody { - var err error httplog, err = withBody(httplog, r) if err != nil { - l.Error("read body error", zap.Error(err)) + l.Warn(logs.FailedToGetRequestBody, zap.Error(err)) } } httplog.Info(logs.RequestHTTP) diff --git a/api/middleware/log_http_stub.go b/api/middleware/log_http_stub.go new file mode 100644 index 0000000..175800d --- /dev/null +++ b/api/middleware/log_http_stub.go @@ -0,0 +1,31 @@ +//go:build !loghttp + +package middleware + +import ( + "net/http" + + "git.frostfs.info/TrueCloudLab/frostfs-s3-gw/internal/logs" + "go.uber.org/zap" +) + +type LogHTTPConfig struct { + Enabled bool + MaxBody int64 + MaxLogSize int + OutputPath string + UseGzip bool +} + +func LogHTTP(l *zap.Logger, _ *LogHTTPConfig) Func { + l.Warn(logs.LogHTTPDisabledInThisBuild) + return func(h http.Handler) http.Handler { + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + h.ServeHTTP(w, r) + }) + } +} + +func ReloadFileLogger(conf *LogHTTPConfig) error { + return nil +} diff --git a/cmd/s3-gw/app.go b/cmd/s3-gw/app.go index 9a81bf0..74f5582 100644 --- a/cmd/s3-gw/app.go +++ b/cmd/s3-gw/app.go @@ -248,7 +248,7 @@ func (s *appSettings) update(v *viper.Viper, log *zap.Logger) { s.setRetryMaxBackoff(fetchRetryMaxBackoff(v)) s.setRetryStrategy(fetchRetryStrategy(v)) s.setVHSSettings(v, log) - s.updateHTTPLoggingSettings(v) + s.updateHTTPLoggingSettings(v, log) } func (s *appSettings) updateNamespacesSettings(v *viper.Viper, log *zap.Logger) { @@ -644,7 +644,7 @@ func newMaxClients(cfg *viper.Viper) maxClientsConfig { return config } -func (s *appSettings) updateHTTPLoggingSettings(cfg *viper.Viper) { +func (s *appSettings) updateHTTPLoggingSettings(cfg *viper.Viper, log *zap.Logger) { s.mu.Lock() defer s.mu.Unlock() @@ -653,6 +653,9 @@ func (s *appSettings) updateHTTPLoggingSettings(cfg *viper.Viper) { s.httpLogging.MaxLogSize = cfg.GetInt(cfgHTTPLoggingMaxLogSize) s.httpLogging.OutputPath = cfg.GetString(cfgHTTPLoggingDestination) s.httpLogging.UseGzip = cfg.GetBool(cfgHTTPLoggingGzip) + if err := s3middleware.ReloadFileLogger(s.httpLogging); err != nil { + log.Error(logs.FailedToReloadHTTPFileLogger, zap.Error(err)) + } } func getPools(ctx context.Context, logger *zap.Logger, cfg *viper.Viper) (*pool.Pool, *treepool.Pool, *keys.PrivateKey) { diff --git a/internal/logs/logs.go b/internal/logs/logs.go index f9c3505..f72f356 100644 --- a/internal/logs/logs.go +++ b/internal/logs/logs.go @@ -99,8 +99,9 @@ const ( FailedToPassAuthentication = "failed to pass authentication" // Error in ../../api/middleware/auth.go FailedToResolveCID = "failed to resolve CID" // Debug in ../../api/middleware/metrics.go RequestStart = "request start" // Info in ../../api/middleware/reqinfo.go - - RequestHTTP = "http request" + RequestHTTP = "http request" // Info in ../../api/middleware/log_http.go + LogHTTPDisabledInThisBuild = "http logging disabled in this build" // Warn in ../../api/middleware/log_http_stub.go + FailedToGetRequestBody = "failed to get request body" // Warn in ../../api/middleware/log_http_stub.go FailedToUnescapeObjectName = "failed to unescape object name" // Warn in ../../api/middleware/reqinfo.go InvalidDefaultMaxAge = "invalid defaultMaxAge" // Fatal in ../../cmd/s3-gw/app_settings.go CantShutDownService = "can't shut down service" // Panic in ../../cmd/s3-gw/service.go