From 3658baf2ac000172445dc1015d09ac46c39aab88 Mon Sep 17 00:00:00 2001 From: gab-arrobo Date: Tue, 22 Oct 2024 15:22:44 -0700 Subject: [PATCH] Clean up logging and update golangci file (#136) * Refactor logging Signed-off-by: Arrobo, Gabriel * Remove unused file Signed-off-by: Arrobo, Gabriel * Update golangci file Signed-off-by: Arrobo, Gabriel --------- Signed-off-by: Arrobo, Gabriel --- .fossa.yml | 20 ------- .golangci.yml | 44 ++++++--------- .../api_nf_instance_id_document.go | 1 - .../api_subscription_id_document.go | 1 - .../api_subscriptions_collection.go | 1 - nssaiavailability/routers.go | 3 +- .../api_network_slice_information_document.go | 1 - nsselection/routers.go | 3 +- nssf.go | 11 +--- service/init.go | 54 +++++++++---------- 10 files changed, 44 insertions(+), 95 deletions(-) delete mode 100644 .fossa.yml diff --git a/.fossa.yml b/.fossa.yml deleted file mode 100644 index 1287f3c..0000000 --- a/.fossa.yml +++ /dev/null @@ -1,20 +0,0 @@ -# Copyright 2020-present Open Networking Foundation -# -# SPDX-License-Identifier: Apache-2.0 -# -# Generated by FOSSA CLI (https://github.com/fossas/fossa-cli) -# Visit https://fossa.com to learn more -# -# Usage: FOSSA_API_KEY=<> fossa analyze -# -T is not supported at this moment from fossa side. -version: 2 -cli: - server: https://app.fossa.com - fetcher: custom - project: nssf -analyze: - modules: - - name: nssf - type: raw - target: ../nssf - path: ../nssf diff --git a/.golangci.yml b/.golangci.yml index 01584d8..895696f 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -1,7 +1,6 @@ +# SPDX-FileCopyrightText: 2024 Intel Corporation # Copyright 2019 free5GC.org -# # SPDX-License-Identifier: Apache-2.0 -# # This file contains all available configuration options # with their default values. @@ -27,19 +26,6 @@ run: # default is true. Enables skipping of directories: # vendor$, third_party$, testdata$, examples$, Godeps$, builtin$ skip-dirs-use-default: true - # which files to skip: they will be analyzed, but issues from them - # won't be reported. Default value is empty list, but there is - # no need to include all autogenerated files, we confidently recognize - # autogenerated files. If it's not please let us know. - # "/" will be replaced by current OS file path separator to properly work - # on Windows. - skip-files: - - "api_.*\\.go$" - - "model_.*\\.go$" - - "routers.go" - - "client.go" - - "configuration.go" - - "nas.go" # by default isn't set. If set we pass it to "go list -mod={option}". From "go help modules": # If invoked with -mod=readonly, the go command is disallowed from the implicit # automatic updating of go.mod described above. Instead, it fails when any changes @@ -55,7 +41,7 @@ run: # output configuration options output: # colored-line-number|line-number|json|tab|checkstyle|code-climate, default is "colored-line-number" - format: colored-line-number + formats: colored-line-number # print lines of code with issue, default is true print-issued-lines: true # print linter name in the end of issue text, default is true @@ -138,10 +124,14 @@ linters-settings: # minimal confidence for issues, default is 0.8 min-confidence: 0.8 gomnd: - settings: - mnd: - # the list of enabled checks, see https://github.com/tommy-muehle/go-mnd/#checks for description. - checks: argument,case,condition,operation,return,assign + # the list of enabled checks, see https://github.com/tommy-muehle/go-mnd/#checks for description. + checks: + - argument + - case + - condition + - operation + - return + - assign gomodguard: allowed: modules: # List of allowed modules @@ -159,8 +149,6 @@ linters-settings: # version: "< 1.1.0" # Version constraint, see https://github.com/Masterminds/semver#basic-comparisons # reason: "testing if blocked version constraint works." # Reason why the version constraint exists. (Optional) govet: - # report about shadowed variables - check-shadowing: true # settings per analyzer settings: printf: # analyzer name, run `go tool vet help` to see all analyzers @@ -203,8 +191,6 @@ linters-settings: whitespace: multi-if: false # Enforces newlines (or comments) after every multi-line if statement multi-func: false # Enforces newlines (or comments) after every multi-line function signature - gci: - local-prefixes: "bitbucket.org" misspell: #locale: US ignore-words: @@ -238,11 +224,11 @@ linters: - predeclared - noctx - dogsled - # - bodyclose + - bodyclose - asciicheck - #- stylecheck - # - unparam - #- wsl + # - stylecheck + # - unparam + # - wsl #disable-all: false fast: true @@ -253,6 +239,8 @@ issues: # excluded by default patterns execute `golangci-lint run --help` exclude: # Excluding configuration per-path, per-linter, per-text and per-source + exclude-files: + - "routers.go" exclude-rules: # Exclude some linters from running on tests files. # Independently from option `exclude` we use default exclude patterns, diff --git a/nssaiavailability/api_nf_instance_id_document.go b/nssaiavailability/api_nf_instance_id_document.go index 8b4b531..72f2947 100644 --- a/nssaiavailability/api_nf_instance_id_document.go +++ b/nssaiavailability/api_nf_instance_id_document.go @@ -18,7 +18,6 @@ import ( "net/http" "github.com/gin-gonic/gin" - "github.com/omec-project/nssf/logger" "github.com/omec-project/nssf/plugin" "github.com/omec-project/nssf/producer" diff --git a/nssaiavailability/api_subscription_id_document.go b/nssaiavailability/api_subscription_id_document.go index 758a3c0..bf40e0e 100644 --- a/nssaiavailability/api_subscription_id_document.go +++ b/nssaiavailability/api_subscription_id_document.go @@ -18,7 +18,6 @@ import ( "net/http" "github.com/gin-gonic/gin" - "github.com/omec-project/nssf/logger" "github.com/omec-project/nssf/producer" "github.com/omec-project/openapi" diff --git a/nssaiavailability/api_subscriptions_collection.go b/nssaiavailability/api_subscriptions_collection.go index 27becf1..3743af7 100644 --- a/nssaiavailability/api_subscriptions_collection.go +++ b/nssaiavailability/api_subscriptions_collection.go @@ -18,7 +18,6 @@ import ( "net/http" "github.com/gin-gonic/gin" - "github.com/omec-project/nssf/logger" "github.com/omec-project/nssf/producer" "github.com/omec-project/openapi" diff --git a/nssaiavailability/routers.go b/nssaiavailability/routers.go index 24bf833..e3787b2 100644 --- a/nssaiavailability/routers.go +++ b/nssaiavailability/routers.go @@ -19,9 +19,8 @@ import ( "strings" "github.com/gin-gonic/gin" - - utilLogger "github.com/omec-project/util/logger" "github.com/omec-project/nssf/logger" + utilLogger "github.com/omec-project/util/logger" ) // Route is the information for every URI. diff --git a/nsselection/api_network_slice_information_document.go b/nsselection/api_network_slice_information_document.go index b3383fb..9175331 100644 --- a/nsselection/api_network_slice_information_document.go +++ b/nsselection/api_network_slice_information_document.go @@ -18,7 +18,6 @@ import ( "net/http" "github.com/gin-gonic/gin" - "github.com/omec-project/nssf/logger" "github.com/omec-project/nssf/producer" "github.com/omec-project/openapi" diff --git a/nsselection/routers.go b/nsselection/routers.go index 2227b12..d801ec7 100644 --- a/nsselection/routers.go +++ b/nsselection/routers.go @@ -19,9 +19,8 @@ import ( "strings" "github.com/gin-gonic/gin" - - utilLogger "github.com/omec-project/util/logger" "github.com/omec-project/nssf/logger" + utilLogger "github.com/omec-project/util/logger" ) // Route is the information for every URI. diff --git a/nssf.go b/nssf.go index 7316abb..5aafb77 100644 --- a/nssf.go +++ b/nssf.go @@ -21,27 +21,20 @@ import ( "github.com/omec-project/nssf/logger" "github.com/omec-project/nssf/service" "github.com/urfave/cli" - "go.uber.org/zap" ) var NSSF = &service.NSSF{} -var appLog *zap.SugaredLogger - -func init() { - appLog = logger.AppLog -} - func main() { app := cli.NewApp() app.Name = "nssf" - appLog.Infoln(app.Name) + logger.AppLog.Infoln(app.Name) app.Usage = "-free5gccfg common configuration file -nssfcfg nssf configuration file" app.Action = action app.Flags = NSSF.GetCliCmd() if err := app.Run(os.Args); err != nil { - appLog.Errorf("NSSF Run Error: %v", err) + logger.AppLog.Errorf("NSSF Run Error: %v", err) } } diff --git a/service/init.go b/service/init.go index 77685cd..390a41c 100644 --- a/service/init.go +++ b/service/init.go @@ -59,17 +59,11 @@ var nssfCLi = []cli.Flag{ }, } -var initLog *zap.SugaredLogger - var ( KeepAliveTimer *time.Timer KeepAliveTimerMutex sync.Mutex ) -func init() { - initLog = logger.InitLog -} - func (*NSSF) GetCliCmd() (flags []cli.Flag) { return nssfCLi } @@ -103,22 +97,22 @@ func (nssf *NSSF) Initialize(c *cli.Context) error { func (nssf *NSSF) setLogLevel() { if factory.NssfConfig.Logger == nil { - initLog.Warnln("NSSF config without log level setting") + logger.InitLog.Warnln("NSSF config without log level setting") return } if factory.NssfConfig.Logger.NSSF != nil { if factory.NssfConfig.Logger.NSSF.DebugLevel != "" { if level, err := zapcore.ParseLevel(factory.NssfConfig.Logger.NSSF.DebugLevel); err != nil { - initLog.Warnf("NSSF Log level [%s] is invalid, set to [info] level", + logger.InitLog.Warnf("NSSF Log level [%s] is invalid, set to [info] level", factory.NssfConfig.Logger.NSSF.DebugLevel) logger.SetLogLevel(zap.InfoLevel) } else { - initLog.Infof("NSSF Log level is set to [%s] level", level) + logger.InitLog.Infof("NSSF Log level is set to [%s] level", level) logger.SetLogLevel(level) } } else { - initLog.Infoln("NSSF Log level not set. Default set to [info] level") + logger.InitLog.Infoln("NSSF Log level not set. Default set to [info] level") logger.SetLogLevel(zap.InfoLevel) } } @@ -138,7 +132,7 @@ func (nssf *NSSF) FilterCli(c *cli.Context) (args []string) { } func (nssf *NSSF) Start() { - initLog.Infoln("server started") + logger.InitLog.Infoln("server started") router := utilLogger.NewGinWithZap(logger.GinLog) @@ -163,12 +157,12 @@ func (nssf *NSSF) Start() { server, err := http2_util.NewServer(addr, util.NSSF_LOG_PATH, router) if server == nil { - initLog.Errorf("initialize HTTP server failed: %+v", err) + logger.InitLog.Errorf("initialize HTTP server failed: %+v", err) return } if err != nil { - initLog.Warnf("initialize HTTP server: +%v", err) + logger.InitLog.Warnf("initialize HTTP server: +%v", err) } serverScheme := factory.NssfConfig.Configuration.Sbi.Scheme @@ -179,19 +173,19 @@ func (nssf *NSSF) Start() { } if err != nil { - initLog.Fatalf("HTTP server setup failed: %+v", err) + logger.InitLog.Fatalf("HTTP server setup failed: %+v", err) } } func (nssf *NSSF) Exec(c *cli.Context) error { - initLog.Debugln("args:", c.String("nssfcfg")) + logger.InitLog.Debugln("args:", c.String("nssfcfg")) args := nssf.FilterCli(c) - initLog.Debugln("filter:", args) + logger.InitLog.Debugln("filter:", args) command := exec.Command("./nssf", args...) stdout, err := command.StdoutPipe() if err != nil { - initLog.Fatalln(err) + logger.InitLog.Fatalln(err) } wg := sync.WaitGroup{} goRoutines := 3 @@ -199,26 +193,26 @@ func (nssf *NSSF) Exec(c *cli.Context) error { go func() { in := bufio.NewScanner(stdout) for in.Scan() { - initLog.Infoln(in.Text()) + logger.InitLog.Infoln(in.Text()) } wg.Done() }() stderr, err := command.StderrPipe() if err != nil { - initLog.Fatalln(err) + logger.InitLog.Fatalln(err) } go func() { in := bufio.NewScanner(stderr) for in.Scan() { - initLog.Infoln(in.Text()) + logger.InitLog.Infoln(in.Text()) } wg.Done() }() go func() { if err = command.Start(); err != nil { - initLog.Errorf("NSSF Start error: %v", err) + logger.InitLog.Errorf("NSSF start error: %v", err) } wg.Done() }() @@ -267,10 +261,10 @@ func (nssf *NSSF) BuildAndSendRegisterNFInstance() (models.NfProfile, error) { self := context.NSSF_Self() profile, err := consumer.BuildNFProfile(self) if err != nil { - initLog.Errorf("build NSSF Profile Error: %v", err) + logger.InitLog.Errorf("build NSSF Profile Error: %v", err) return profile, err } - initLog.Infof("Pcf Profile Registering to NRF: %v", profile) + logger.InitLog.Infof("NSSF Profile Registering to NRF: %v", profile) // Indefinite attempt to register until success profile, _, self.NfId, err = consumer.SendRegisterNFInstance(self.NrfUri, self.NfId, profile) return profile, err @@ -281,7 +275,7 @@ func (nssf *NSSF) UpdateNF() { KeepAliveTimerMutex.Lock() defer KeepAliveTimerMutex.Unlock() if KeepAliveTimer == nil { - initLog.Warnln("keepAlive timer has been stopped") + logger.InitLog.Warnln("keepAlive timer has been stopped") return } // setting default value 60 sec @@ -295,21 +289,21 @@ func (nssf *NSSF) UpdateNF() { patchItem = append(patchItem, pitem) nfProfile, problemDetails, err := consumer.SendUpdateNFInstance(patchItem) if problemDetails != nil { - initLog.Errorf("NSSF update to NRF ProblemDetails[%v]", problemDetails) + logger.InitLog.Errorf("NSSF update to NRF ProblemDetails[%v]", problemDetails) // 5xx response from NRF, 404 Not Found, 400 Bad Request if (problemDetails.Status >= 500 && problemDetails.Status <= 599) || problemDetails.Status == 404 || problemDetails.Status == 400 { // register with NRF full profile nfProfile, err = nssf.BuildAndSendRegisterNFInstance() if err != nil { - initLog.Errorf("NSSF update to NRF Error[%s]", err.Error()) + logger.InitLog.Errorf("NSSF update to NRF Error[%s]", err.Error()) } } } else if err != nil { - initLog.Errorf("NSSF update to NRF Error[%s]", err.Error()) + logger.InitLog.Errorf("NSSF update to NRF Error[%s]", err.Error()) nfProfile, err = nssf.BuildAndSendRegisterNFInstance() if err != nil { - initLog.Errorf("NSSF update to NRF Error[%s]", err.Error()) + logger.InitLog.Errorf("NSSF update to NRF Error[%s]", err.Error()) } } @@ -324,7 +318,7 @@ func (nssf *NSSF) UpdateNF() { func (nssf *NSSF) registerNF() { for msg := range factory.ConfigPodTrigger { - initLog.Infof("Minimum configuration from config pod available %v", msg) + logger.InitLog.Infof("minimum configuration from config pod available %v", msg) self := context.NSSF_Self() profile, err := consumer.BuildNFProfile(self) if err != nil { @@ -340,7 +334,7 @@ func (nssf *NSSF) registerNF() { logger.CfgLog.Infoln("sent register NFInstance with updated profile") self.NrfUri = newNrfUri } else { - initLog.Errorf("send register NFInstance Error[%s]", err.Error()) + logger.CfgLog.Errorf("send register NFInstance Error[%s]", err.Error()) } } }