Skip to content

Commit

Permalink
Clean up logging and update golangci file (#136)
Browse files Browse the repository at this point in the history
* Refactor logging

Signed-off-by: Arrobo, Gabriel <[email protected]>

* Remove unused file

Signed-off-by: Arrobo, Gabriel <[email protected]>

* Update golangci file

Signed-off-by: Arrobo, Gabriel <[email protected]>

---------

Signed-off-by: Arrobo, Gabriel <[email protected]>
  • Loading branch information
gab-arrobo authored Oct 22, 2024
1 parent 98139ee commit 3658baf
Show file tree
Hide file tree
Showing 10 changed files with 44 additions and 95 deletions.
20 changes: 0 additions & 20 deletions .fossa.yml

This file was deleted.

44 changes: 16 additions & 28 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -238,11 +224,11 @@ linters:
- predeclared
- noctx
- dogsled
# - bodyclose
- bodyclose
- asciicheck
#- stylecheck
# - unparam
#- wsl
# - stylecheck
# - unparam
# - wsl

#disable-all: false
fast: true
Expand All @@ -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,
Expand Down
1 change: 0 additions & 1 deletion nssaiavailability/api_nf_instance_id_document.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
1 change: 0 additions & 1 deletion nssaiavailability/api_subscription_id_document.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
1 change: 0 additions & 1 deletion nssaiavailability/api_subscriptions_collection.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
3 changes: 1 addition & 2 deletions nssaiavailability/routers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
1 change: 0 additions & 1 deletion nsselection/api_network_slice_information_document.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
3 changes: 1 addition & 2 deletions nsselection/routers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
11 changes: 2 additions & 9 deletions nssf.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

Expand Down
54 changes: 24 additions & 30 deletions service/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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)
}
}
Expand All @@ -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)

Expand All @@ -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
Expand All @@ -179,46 +173,46 @@ 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
wg.Add(goRoutines)
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()
}()
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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())
}
}

Expand All @@ -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 {
Expand All @@ -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())
}
}
}

0 comments on commit 3658baf

Please sign in to comment.