Skip to content

Commit

Permalink
Replace logger logrus with zap (#82)
Browse files Browse the repository at this point in the history
* Replace logger `logrus` with `zap`

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

---------

Signed-off-by: Arrobo, Gabriel <[email protected]>
  • Loading branch information
gab-arrobo authored Sep 30, 2024
1 parent f4d2641 commit 66520bc
Show file tree
Hide file tree
Showing 9 changed files with 117 additions and 106 deletions.
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.4.4-dev
1.5.0
36 changes: 18 additions & 18 deletions backend/grpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@ import (
func (b *GrpcServer) ConnectToServer(port int) {
target := fmt.Sprintf("%s:%d", b.address, port)

fmt.Println("Connecting to target ", target)
logger.AppLog.Infoln("connecting to target", target)

var err error
b.conn, err = grpc.NewClient(target, grpc.WithTransportCredentials(insecure.NewCredentials()))

if err != nil {
fmt.Println("did not connect: ", err)
logger.AppLog.Errorln("did not connect:", err)
deleteBackendNF(b)
return
}
Expand All @@ -35,7 +35,7 @@ func (b *GrpcServer) ConnectToServer(port int) {

stream, err := b.gc.HandleMessage(ctxt.Background())
if err != nil {
logger.AppLog.Println("openn stream error ", err)
logger.AppLog.Errorw("open stream error", err)
deleteBackendNF(b)
return
}
Expand All @@ -53,19 +53,19 @@ func (b *GrpcServer) ConnectToServer(port int) {
if candidate.RanId != nil {
req.GnbId = *candidate.RanId
} else {
logger.AppLog.Printf("ran connection %v is exist without GnbId, so not sending this ran details to NF\n",
logger.AppLog.Infof("ran connection %v is exist without GnbId, so not sending this ran details to NF",
candidate.GnbIp)
}
if err := stream.Send(&req); err != nil {
logger.AppLog.Println("can not send: ", err)
logger.AppLog.Warnln("can not send:", err)
}
logger.AppLog.Println("send Request message")
logger.AppLog.Infoln("send Request message")
response, err := stream.Recv()
if err != nil {
logger.AppLog.Println("response from server: error ", err)
logger.AppLog.Errorln("response from server: error", err)
b.state = false
} else {
logger.AppLog.Printf("init Response from Server %s server: %s\n", response.AmfId, response.VerboseMsg)
logger.AppLog.Infof("init Response from Server %s server: %s", response.AmfId, response.VerboseMsg)
b.state = true
}
return true
Expand All @@ -82,20 +82,20 @@ func (b *GrpcServer) readFromServer() {
for {
response, err := b.stream.Recv()
if err != nil {
logger.GrpcLog.Printf("error in Recv %v, Stop listening for this server %v", err, b.address)
logger.GrpcLog.Errorf("error in Recv %v, Stop listening for this server %v", err, b.address)
deleteBackendNF(b)
return
} else {
if response.Msgtype == gClient.MsgType_INIT_MSG {
logger.GrpcLog.Printf("init Response from Server %s server: %s", response.AmfId, response.VerboseMsg)
logger.GrpcLog.Infof("init Response from Server %s server: %s", response.AmfId, response.VerboseMsg)
} else if response.Msgtype == gClient.MsgType_REDIRECT_MSG {
var found bool
ctx := context.Sctplb_Self()
for _, instance := range ctx.Backends {
b1 := instance.(*GrpcServer)
if b1.address == response.RedirectId {
if !b1.state {
logger.GrpcLog.Printf("backend state is not in READY state, so not forwarding redirected Msg")
logger.GrpcLog.Infoln("backend state is not in READY state, so not forwarding redirected Msg")
} else {
t := gClient.SctplbMessage{}
t.VerboseMsg = "Hello From gNB Message !"
Expand All @@ -105,39 +105,39 @@ func (b *GrpcServer) readFromServer() {
t.GnbId = response.GnbId
err := b1.stream.Send(&t)
if err != nil {
logger.GrpcLog.Printf("error forwarding msg")
logger.GrpcLog.Infoln("error forwarding msg")
}
logger.GrpcLog.Printf("successfully forwarded msg to correct AMF")
logger.GrpcLog.Infoln("successfully forwarded msg to correct AMF")
found = true
}
break
}
}
if !found {
logger.GrpcLog.Printf("dropping redirected message as backend ip [%v] is not exist", response.RedirectId)
logger.GrpcLog.Infof("dropping redirected message as backend ip [%v] is not exist", response.RedirectId)
}
} else {
var ran *context.Ran
// fetch ran connection based on GnbId
if response.GnbId == "" {
logger.RanLog.Printf("received null GnbId from backend NF")
logger.RanLog.Infoln("received null GnbId from backend NF")
} else if response.GnbIpAddr != "" {
// GnbId may present NGSetupreponse/failure receives from NF
ran, _ = context.Sctplb_Self().RanFindByGnbIp(response.GnbIpAddr)
if ran != nil && response.GnbId != "" {
ran.SetRanId(response.GnbId)
logger.RanLog.Printf("received GnbId: %v for GNbIpAddress: %v from NF", response.GnbId, response.GnbIpAddr)
logger.RanLog.Infof("received GnbId: %v for GNbIpAddress: %v from NF", response.GnbId, response.GnbIpAddr)
}
} else if response.GnbId != "" {
ran, _ = context.Sctplb_Self().RanFindByGnbId(response.GnbId)
}
if ran != nil {
_, err := ran.Conn.Write(response.Msg)
if err != nil {
logger.RanLog.Printf("err %+v", err)
logger.RanLog.Infof("err %+v", err)
}
} else {
logger.RanLog.Printf("couldn't fetch sctp connection with GnbId: %v", response.GnbId)
logger.RanLog.Infof("couldn't fetch sctp connection with GnbId: %v", response.GnbId)
}
}
}
Expand Down
33 changes: 16 additions & 17 deletions backend/sched.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
// SPDX-FileCopyrightText: 2022 Open Networking Foundation <[email protected]>
// SPDX-FileCopyrightText: 2024 Intel Corporation
//
// SPDX-License-Identifier: Apache-2.0

package backend

import (
"fmt"
"net"
"time"

Expand All @@ -26,7 +26,7 @@ func RoundRobin() Backend {
length := ctx.NFLength()

if length <= 0 {
logger.DispatchLog.Errorln("There are no backend NFs running")
logger.DispatchLog.Errorln("there are no backend NFs running")
return nil
}
if next >= length {
Expand All @@ -49,15 +49,15 @@ func (b BackendSvc) DispatchAddServer() {
svcList := b.Cfg.Configuration.Services
for _, svc := range svcList {
for {
logger.DiscoveryLog.Traceln("Discover Service ", svc.Uri)
logger.DiscoveryLog.Debugln("discover Service", svc.Uri)
ips, err := net.LookupIP(svc.Uri)
if err != nil {
logger.DiscoveryLog.Errorln("Discover Service ", svc.Uri, " Error ", err)
logger.DiscoveryLog.Warnf("discover Service %s error %+v", svc.Uri, err)
time.Sleep(2 * time.Second)
continue
}
for _, ip := range ips {
logger.DiscoveryLog.Traceln("Discover Service ", svc.Uri, ", ip ", ip)
logger.DiscoveryLog.Debugln("discover Service %s, ip %s", svc.Uri, ", ip", ip.String())
found := false
if ipv4 := ip.To4(); ipv4 != nil {
for _, instance := range ctx.Backends {
Expand All @@ -70,16 +70,15 @@ func (b BackendSvc) DispatchAddServer() {
if found {
continue
}
logger.DiscoveryLog.Infoln("New Server found IPv4: ", ipv4.String())
logger.DiscoveryLog.Infoln("new server found IPv4:", ipv4.String())
var backend context.NF
switch b.Cfg.Configuration.Type {
case "grpc":
backend = &GrpcServer{
address: ipv4.String(),
}
default:
logger.DiscoveryLog.Warnln("unsupported backend type: " +
b.Cfg.Configuration.Type)
logger.DiscoveryLog.Warnln("unsupported backend type:", b.Cfg.Configuration.Type)
}
ctx.Lock()
ctx.AddNF(backend)
Expand All @@ -99,7 +98,7 @@ func deleteBackendNF(b context.NF) {
defer ctx.Unlock()
ctx.DeleteNF(b)
for _, b1 := range ctx.Backends {
fmt.Printf("Available backend %v \n", b1)
logger.AppLog.Infof("available backend %v", b1)
}
}

Expand All @@ -114,40 +113,40 @@ func dispatchMessage(conn net.Conn, msg []byte) { //*gClient.Message) {
var peer *SctpConnections
p, ok := connections.Load(conn)
if !ok {
logger.SctpLog.Infof("Notification for unknown connection")
logger.SctpLog.Infoln("notification for unknown connection")
return
} else {
peer = p.(*SctpConnections)
logger.SctpLog.Warnf("Handle SCTP Notification[addr: %+v], peer %v ", conn.RemoteAddr(), peer)
logger.SctpLog.Infoln("handle SCTP Notification from peer", peer.address)
}
ctx := context.Sctplb_Self()
ctx.Lock()
defer ctx.Unlock()
ran, _ := ctx.RanFindByConn(conn)
if len(msg) == 0 {
logger.SctpLog.Infof("send Gnb connection [%v] close message to all AMF Instances", peer)
logger.SctpLog.Infof("send Gnb connection [%v] close message to all AMF Instances", peer.address)
if ctx.Backends != nil && ctx.NFLength() > 0 {
var i int
for ; i < ctx.NFLength(); i++ {
backend := ctx.Backends[i]
if backend.State() {
if err := backend.Send(msg, true, ran); err != nil {
logger.SctpLog.Errorln("can not send ", err)
logger.SctpLog.Errorln("can not send", err)
}
}
}
} else {
logger.SctpLog.Errorln("No AMF Connections")
logger.SctpLog.Errorln("no AMF Connections")
}
context.Sctplb_Self().DeleteRan(conn)
return
}
if ran == nil {
ran = context.Sctplb_Self().NewRan(conn)
}
logger.SctpLog.Println("Message received from remoteAddr ", conn.RemoteAddr().String())
logger.SctpLog.Infoln("message received from remoteAddr", conn.RemoteAddr().String())
if ctx.NFLength() == 0 {
fmt.Println("NO backend available")
logger.AppLog.Errorln("no backend available")
return
}
var i int
Expand All @@ -156,7 +155,7 @@ func dispatchMessage(conn net.Conn, msg []byte) { //*gClient.Message) {
backend := RoundRobin()
if backend.State() {
if err := backend.Send(msg, false, ran); err != nil {
logger.SctpLog.Errorln("can not send: ", err)
logger.SctpLog.Errorln("can not send:", err)
}
break
}
Expand Down
24 changes: 12 additions & 12 deletions backend/service.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
// SPDX-FileCopyrightText: 2022 Open Networking Foundation <[email protected]>
// SPDX-FileCopyrightText: 2024 Intel Corporation
//
// SPDX-License-Identifier: Apache-2.0

package backend

import (
"encoding/hex"
"fmt"
"io"
"net"
"sync"
Expand Down Expand Up @@ -39,7 +39,7 @@ var sctpConfig sctp.SocketConfig = sctp.SocketConfig{
}

func ServiceRun(addresses []string, port int) {
fmt.Println("service Run is called")
logger.AppLog.Infoln("service Run is called")
handler := SCTPHandler{
HandleMessage: dispatchMessage,
}
Expand All @@ -48,9 +48,9 @@ func ServiceRun(addresses []string, port int) {

for _, addr := range addresses {
if netAddr, err := net.ResolveIPAddr("ip", addr); err != nil {
logger.SctpLog.Errorf("error resolving address '%s': %v\n", addr, err)
logger.SctpLog.Errorf("error resolving address '%s': %v", addr, err)
} else {
logger.SctpLog.Debugf("resolved address '%s' to %s\n", addr, netAddr)
logger.SctpLog.Debugf("resolved address '%s' to %s", addr, netAddr)
ips = append(ips, *netAddr)
}
}
Expand All @@ -71,7 +71,7 @@ func listenAndServe(addr *sctp.SCTPAddr, handler SCTPHandler) {
sctpListener = listener
}

logger.SctpLog.Infof("listen on %s", sctpListener.Addr())
logger.SctpLog.Infoln("listen on", sctpListener.Addr())

for {
newConn, err := sctpListener.AcceptSCTP()
Expand Down Expand Up @@ -116,7 +116,7 @@ func listenAndServe(addr *sctp.SCTPAddr, handler SCTPHandler) {
}
continue
} else {
logger.SctpLog.Debugln("Subscribe SCTP event[DATA_IO, SHUTDOWN_EVENT, ASSOCIATION_CHANGE]")
logger.SctpLog.Debugln("subscribe SCTP event[DATA_IO, SHUTDOWN_EVENT, ASSOCIATION_CHANGE]")
}

if err := newConn.SetReadBuffer(int(readBufSize)); err != nil {
Expand Down Expand Up @@ -178,7 +178,7 @@ func handleConnection(conn *sctp.SCTPConn, bufsize uint32, handler SCTPHandler)
logger.SctpLog.Debugf("SCTPRead: %+v", err)
continue
default:
logger.SctpLog.Errorf("handle connection[addr: %+v] error: %+v", conn.RemoteAddr(), err)
logger.SctpLog.Errorf("handle connection [addr: %+v] error: %+v", conn.RemoteAddr(), err)
GnbConnChan <- false
return
}
Expand All @@ -187,10 +187,10 @@ func handleConnection(conn *sctp.SCTPConn, bufsize uint32, handler SCTPHandler)
if notification != nil {
p, ok := connections.Load(conn)
if !ok {
logger.SctpLog.Infof("notification for unknown connection")
logger.SctpLog.Warnln("notification for unknown connection")
} else {
peer := p.(*SctpConnections)
logger.SctpLog.Warnf("handle SCTP Notification[addr: %+v], peer %v ", conn.RemoteAddr(), peer)
logger.SctpLog.Infof("handle SCTP Notification peer %v ", peer.address)
GnbConnChan <- false
}
} else {
Expand All @@ -199,16 +199,16 @@ func handleConnection(conn *sctp.SCTPConn, bufsize uint32, handler SCTPHandler)
continue
}

logger.SctpLog.Tracef("read %d bytes", n)
logger.SctpLog.Tracef("packet content:\n%+v", hex.Dump(buf[:n]))
logger.SctpLog.Debugf("read %d bytes", n)
logger.SctpLog.Debugf("packet content: %+v", hex.Dump(buf[:n]))

handler.HandleMessage(conn, buf[:n])
}
}
}()

for x := range GnbConnChan {
logger.SctpLog.Errorln("closing gnb Connection ", x)
logger.SctpLog.Infoln("closing gnb Connection:", x)
buf := make([]byte, bufsize)
handler.HandleMessage(conn, buf[:0])
return
Expand Down
14 changes: 9 additions & 5 deletions context/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@
package context

import (
"fmt"
"net"
"strings"
"sync"

"github.com/omec-project/sctplb/logger"
"github.com/sirupsen/logrus"
"go.uber.org/zap"
)

var sctplbContext = SctplbContext{}
Expand All @@ -35,7 +35,7 @@ type Ran struct {
/* socket Connect*/
Conn net.Conn `json:"-"`

Log *logrus.Entry `json:"-"`
Log *zap.SugaredLogger `json:"-"`
}

func (ran *Ran) Remove() {
Expand All @@ -49,7 +49,11 @@ func (ran *Ran) SetRanId(gnbId string) {

func (ran *Ran) RanID() string {
if ran.RanId != nil {
return fmt.Sprintf("<Mcc:Mnc:GNbID %s>", *ran.RanId)
var builder strings.Builder
builder.WriteString("<Mcc:Mnc:GNbID ")
builder.WriteString(*ran.RanId)
builder.WriteString(">")
return builder.String()
}
return ""
}
Expand All @@ -58,7 +62,7 @@ func (context *SctplbContext) NewRan(conn net.Conn) *Ran {
ran := Ran{}
ran.Conn = conn
ran.GnbIp = conn.RemoteAddr().String()
ran.Log = logger.RanLog.WithField(logger.FieldRanAddr, conn.RemoteAddr().String())
ran.Log = logger.RanLog.Desugar().Sugar().With(logger.FieldRanAddr, conn.RemoteAddr().String())
context.RanPool.Store(conn, &ran)
return &ran
}
Expand Down
Loading

0 comments on commit 66520bc

Please sign in to comment.