From 9e64304499ffa3b28694d0ef62d4e87158886154 Mon Sep 17 00:00:00 2001 From: Alex Vanin Date: Mon, 21 Oct 2024 15:51:48 +0300 Subject: [PATCH] [#521] Use handler to register dial events While frostfs-node uses dial handler to udpate metric value, gateway starts with simple event logging. Signed-off-by: Alex Vanin --- cmd/s3-gw/app.go | 4 ++-- cmd/s3-gw/app_settings.go | 3 ++- internal/logs/logs.go | 2 ++ internal/net/config.go | 2 ++ internal/net/event_handler.go | 28 ++++++++++++++++++++++++++++ 5 files changed, 36 insertions(+), 3 deletions(-) create mode 100644 internal/net/event_handler.go diff --git a/cmd/s3-gw/app.go b/cmd/s3-gw/app.go index 382431b..3e24acf 100644 --- a/cmd/s3-gw/app.go +++ b/cmd/s3-gw/app.go @@ -658,7 +658,7 @@ func newMaxClients(cfg *viper.Viper) maxClientsConfig { } func getDialerSource(logger *zap.Logger, cfg *viper.Viper) *internalnet.DialerSource { - source, err := internalnet.NewDialerSource(fetchMultinetConfig(cfg)) + source, err := internalnet.NewDialerSource(fetchMultinetConfig(cfg, logger)) if err != nil { logger.Fatal(logs.FailedToLoadMultinetConfig, zap.Error(err)) } @@ -892,7 +892,7 @@ func (a *App) updateSettings() { a.settings.logLevel.SetLevel(lvl) } - if err := a.settings.dialerSource.Update(fetchMultinetConfig(a.cfg)); err != nil { + if err := a.settings.dialerSource.Update(fetchMultinetConfig(a.cfg, a.log)); err != nil { a.log.Warn(logs.MultinetConfigWontBeUpdated, zap.Error(err)) } diff --git a/cmd/s3-gw/app_settings.go b/cmd/s3-gw/app_settings.go index 24ceb43..bd7c35c 100644 --- a/cmd/s3-gw/app_settings.go +++ b/cmd/s3-gw/app_settings.go @@ -756,12 +756,13 @@ func fetchVHSNamespaces(v *viper.Viper, log *zap.Logger) map[string]bool { return vhsNamespacesEnabled } -func fetchMultinetConfig(v *viper.Viper) (cfg internalnet.Config) { +func fetchMultinetConfig(v *viper.Viper, logger *zap.Logger) (cfg internalnet.Config) { cfg.Enabled = v.GetBool(cfgMultinetEnabled) cfg.Balancer = v.GetString(cfgMultinetBalancer) cfg.Restrict = v.GetBool(cfgMultinetRestrict) cfg.FallbackDelay = v.GetDuration(cfgMultinetFallbackDelay) cfg.Subnets = make([]internalnet.Subnet, 0, 5) + cfg.EventHandler = internalnet.NewLogEventHandler(logger) for i := 0; ; i++ { key := cfgMultinetSubnets + "." + strconv.Itoa(i) + "." diff --git a/internal/logs/logs.go b/internal/logs/logs.go index a24065a..3da1255 100644 --- a/internal/logs/logs.go +++ b/internal/logs/logs.go @@ -175,4 +175,6 @@ const ( CheckCustomAccessKeyIDUniqueness = "check custom access key id uniqueness" FailedToLoadMultinetConfig = "failed to load multinet config" MultinetConfigWontBeUpdated = "multinet config won't be updated" + MultinetDialSuccess = "multinet dial successful" + MultinetDialFail = "multinet dial failed" ) diff --git a/internal/net/config.go b/internal/net/config.go index 6a04798..b40e003 100644 --- a/internal/net/config.go +++ b/internal/net/config.go @@ -23,6 +23,7 @@ type Config struct { Balancer string Restrict bool FallbackDelay time.Duration + EventHandler multinet.EventHandler } func (c Config) toMultinetConfig() (multinet.Config, error) { @@ -52,6 +53,7 @@ func (c Config) toMultinetConfig() (multinet.Config, error) { Restrict: c.Restrict, FallbackDelay: c.FallbackDelay, Dialer: newDefaultDialer(), + EventHandler: c.EventHandler, }, nil } diff --git a/internal/net/event_handler.go b/internal/net/event_handler.go new file mode 100644 index 0000000..866fdd0 --- /dev/null +++ b/internal/net/event_handler.go @@ -0,0 +1,28 @@ +package net + +import ( + "net" + + "git.frostfs.info/TrueCloudLab/frostfs-s3-gw/internal/logs" + "go.uber.org/zap" +) + +type LogEventHandler struct { + logger *zap.Logger +} + +func (l LogEventHandler) DialPerformed(sourceIP net.Addr, _, address string, err error) { + sourceIPString := "undefined" + if sourceIP != nil { + sourceIPString = sourceIP.Network() + "://" + sourceIP.String() + } + if err == nil { + l.logger.Debug(logs.MultinetDialSuccess, zap.String("source", sourceIPString), zap.String("destination", address)) + } else { + l.logger.Debug(logs.MultinetDialFail, zap.String("source", sourceIPString), zap.String("destination", address), zap.Error(err)) + } +} + +func NewLogEventHandler(logger *zap.Logger) LogEventHandler { + return LogEventHandler{logger: logger} +}