From 6ae42c699cfd4a477a22d4b02d071dde2beb2fea 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 | 42 +++++++++++++++++---------------- 4 files changed, 61 insertions(+), 24 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 a097ea7a..03e4abab 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 00000000..175800db --- /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 c1716e96..3b6d2b1a 100644 --- a/cmd/s3-gw/app.go +++ b/cmd/s3-gw/app.go @@ -220,7 +220,7 @@ func (s *appSettings) update(v *viper.Viper, log *zap.Logger) { s.setRetryMaxAttempts(fetchRetryMaxAttempts(v)) s.setRetryMaxBackoff(fetchRetryMaxBackoff(v)) s.setRetryStrategy(fetchRetryStrategy(v)) - s.updateHTTPLoggingSettings(v) + s.updateHTTPLoggingSettings(v, log) } func (s *appSettings) updateNamespacesSettings(v *viper.Viper, log *zap.Logger) { @@ -564,7 +564,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() @@ -573,6 +573,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 aee2f73e..594ebbe4 100644 --- a/internal/logs/logs.go +++ b/internal/logs/logs.go @@ -100,26 +100,28 @@ 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" - 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 - CouldntGenerateRandomKey = "couldn't generate random key" // Fatal in ../../cmd/s3-gw/app.go - FailedToCreateResolver = "failed to create resolver" // Fatal in ../../cmd/s3-gw/app.go - CouldNotLoadFrostFSPrivateKey = "could not load FrostFS private key" // Fatal in ../../cmd/s3-gw/app.go - FailedToCreateConnectionPool = "failed to create connection pool" // Fatal in ../../cmd/s3-gw/app.go - FailedToDialConnectionPool = "failed to dial connection pool" // Fatal in ../../cmd/s3-gw/app.go - FailedToCreateTreePool = "failed to create tree pool" // Fatal in ../../cmd/s3-gw/app.go - FailedToDialTreePool = "failed to dial tree pool" // Fatal in ../../cmd/s3-gw/app.go - ListenAndServe = "listen and serve" // Fatal in ../../cmd/s3-gw/app.go - NoHealthyServers = "no healthy servers" // Fatal in ../../cmd/s3-gw/app.go - CouldNotInitializeAPIHandler = "could not initialize API handler" // Fatal in ../../cmd/s3-gw/app.go - RuntimeSoftMemoryDefinedWithGOMEMLIMIT = "soft runtime memory defined with GOMEMLIMIT environment variable, config value skipped" // Warn in ../../cmd/s3-gw/app.go - RuntimeSoftMemoryLimitUpdated = "soft runtime memory limit value updated" // Info in ../../cmd/s3-gw/app.go - AnonRequestSkipFrostfsIDValidation = "anon request, skip FrostfsID validation" // Debug in ../../api/middleware/auth.go - FrostfsIDValidationFailed = "FrostfsID validation failed" // Error in ../../api/middleware/auth.go - InitFrostfsIDContractFailed = "init frostfsid contract failed" // Fatal in ../../cmd/s3-gw/app.go - InitPolicyContractFailed = "init policy contract failed" // Fatal in ../../cmd/s3-gw/app.go + 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 + CouldntGenerateRandomKey = "couldn't generate random key" // Fatal in ../../cmd/s3-gw/app.go + FailedToCreateResolver = "failed to create resolver" // Fatal in ../../cmd/s3-gw/app.go + CouldNotLoadFrostFSPrivateKey = "could not load FrostFS private key" // Fatal in ../../cmd/s3-gw/app.go + FailedToCreateConnectionPool = "failed to create connection pool" // Fatal in ../../cmd/s3-gw/app.go + FailedToDialConnectionPool = "failed to dial connection pool" // Fatal in ../../cmd/s3-gw/app.go + FailedToCreateTreePool = "failed to create tree pool" // Fatal in ../../cmd/s3-gw/app.go + FailedToDialTreePool = "failed to dial tree pool" // Fatal in ../../cmd/s3-gw/app.go + ListenAndServe = "listen and serve" // Fatal in ../../cmd/s3-gw/app.go + NoHealthyServers = "no healthy servers" // Fatal in ../../cmd/s3-gw/app.go + CouldNotInitializeAPIHandler = "could not initialize API handler" // Fatal in ../../cmd/s3-gw/app.go + RuntimeSoftMemoryDefinedWithGOMEMLIMIT = "soft runtime memory defined with GOMEMLIMIT environment variable, config value skipped" // Warn in ../../cmd/s3-gw/app.go + RuntimeSoftMemoryLimitUpdated = "soft runtime memory limit value updated" // Info in ../../cmd/s3-gw/app.go + AnonRequestSkipFrostfsIDValidation = "anon request, skip FrostfsID validation" // Debug in ../../api/middleware/auth.go + FrostfsIDValidationFailed = "FrostfsID validation failed" // Error in ../../api/middleware/auth.go + InitFrostfsIDContractFailed = "init frostfsid contract failed" // Fatal in ../../cmd/s3-gw/app.go + InitPolicyContractFailed = "init policy contract failed" // Fatal in ../../cmd/s3-gw/app.go PolicyValidationFailed = "policy validation failed" ServerReconnecting = "reconnecting server..." ServerReconnectedSuccessfully = "server reconnected successfully"