Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add runtime modification of log level #439

Merged
merged 10 commits into from
Oct 11, 2023
30 changes: 15 additions & 15 deletions cmd/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,21 @@ import (
var v = viper.GetViper()

type UpfConfig struct {
InterfaceName []string `mapstructure:"interface_name"`
XDPAttachMode string `mapstructure:"xdp_attach_mode" validate:"oneof=generic native offload"`
ApiAddress string `mapstructure:"api_address" validate:"hostname_port"`
PfcpAddress string `mapstructure:"pfcp_address" validate:"hostname_port"`
PfcpNodeId string `mapstructure:"pfcp_node_id" validate:"hostname|ip"`
MetricsAddress string `mapstructure:"metrics_address" validate:"hostname_port"`
N3Address string `mapstructure:"n3_address" validate:"ipv4"`
QerMapSize uint32 `mapstructure:"qer_map_size" validate:"min=1"`
FarMapSize uint32 `mapstructure:"far_map_size" validate:"min=1"`
PdrMapSize uint32 `mapstructure:"pdr_map_size" validate:"min=1"`
EbpfMapResize bool `mapstructure:"resize_ebpf_maps"`
HeartbeatRetries uint32 `mapstructure:"heartbeat_retries"`
HeartbeatInterval uint32 `mapstructure:"heartbeat_interval"`
HeartbeatTimeout uint32 `mapstructure:"heartbeat_timeout"`
LoggingLevel string `mapstructure:"logging_level"`
InterfaceName []string `mapstructure:"interface_name" json:"interface_name"`
XDPAttachMode string `mapstructure:"xdp_attach_mode" validate:"oneof=generic native offload" json:"xdp_attach_mode"`
ApiAddress string `mapstructure:"api_address" validate:"hostname_port" json:"api_address"`
PfcpAddress string `mapstructure:"pfcp_address" validate:"hostname_port" json:"pfcp_address"`
PfcpNodeId string `mapstructure:"pfcp_node_id" validate:"hostname|ip" json:"pfcp_node_id"`
MetricsAddress string `mapstructure:"metrics_address" validate:"hostname_port" json:"metrics_address"`
N3Address string `mapstructure:"n3_address" validate:"ipv4" json:"n3_address"`
QerMapSize uint32 `mapstructure:"qer_map_size" validate:"min=1" json:"qer_map_size"`
FarMapSize uint32 `mapstructure:"far_map_size" validate:"min=1" json:"far_map_size"`
PdrMapSize uint32 `mapstructure:"pdr_map_size" validate:"min=1" json:"pdr_map_size"`
EbpfMapResize bool `mapstructure:"resize_ebpf_maps" json:"resize_ebpf_maps"`
HeartbeatRetries uint32 `mapstructure:"heartbeat_retries" json:"heartbeat_retries"`
HeartbeatInterval uint32 `mapstructure:"heartbeat_interval" json:"heartbeat_interval"`
HeartbeatTimeout uint32 `mapstructure:"heartbeat_timeout" json:"heartbeat_timeout"`
LoggingLevel string `mapstructure:"logging_level" validate:"required" json:"logging_level"`
}

func init() {
Expand Down
28 changes: 26 additions & 2 deletions cmd/core/api.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
package core

import (
"fmt"
"net"
"net/http"
"strconv"
"unsafe"

"github.com/edgecomllc/eupf/cmd/config"
"github.com/edgecomllc/eupf/cmd/ebpf"

"github.com/edgecomllc/eupf/cmd/config"
eupfDocs "github.com/edgecomllc/eupf/cmd/docs"
"github.com/gin-contrib/cors"
"github.com/gin-gonic/gin"
Expand All @@ -33,10 +34,15 @@ func CreateApiServer(bpfObjects *ebpf.BpfObjects, pfcpSrv *PfcpConnection, forwa
v1 := router.Group("/api/v1")
{
v1.GET("upf_pipeline", ListUpfPipeline(bpfObjects))
v1.GET("config", DisplayConfig())
v1.GET("xdp_stats", DisplayXdpStatistics(forwardPlaneStats))
v1.GET("packet_stats", DisplayPacketStats(forwardPlaneStats))

config := v1.Group("config")
{
config.GET("", DisplayConfig())
config.POST("", EditConfig)
}

qerMap := v1.Group("qer_map")
{
qerMap.GET("", ListQerMapContent(bpfObjects))
Expand All @@ -60,6 +66,24 @@ func CreateApiServer(bpfObjects *ebpf.BpfObjects, pfcpSrv *PfcpConnection, forwa
return &ApiServer{router: router}
}

func EditConfig(c *gin.Context) {
var conf config.UpfConfig
if err := c.BindJSON(&conf); err != nil {
c.IndentedJSON(http.StatusBadRequest, gin.H{
"message": "Request body json has incorrect format. Use one or more fields from the following structure",
"correctFormat": config.UpfConfig{},
})
return
}
if err := SetConfig(conf); err != nil {
c.IndentedJSON(http.StatusBadRequest, gin.H{
"message": fmt.Sprintf("Error during editing config: %s", err.Error()),
})
} else {
c.Status(http.StatusOK)
}
}

type PacketStats struct {
RxArp uint64 `json:"rx_arp"`
RxIcmp uint64 `json:"rx_icmp"`
Expand Down
18 changes: 18 additions & 0 deletions cmd/core/config_edit.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package core

import (
"fmt"

"github.com/edgecomllc/eupf/cmd/config"

"github.com/rs/zerolog"
)

func SetConfig(conf config.UpfConfig) error {
// For now only logging_level parameter update in config.UpfConfig is supported.
if err := SetLoggerLevel(conf.LoggingLevel); err != nil {
return fmt.Errorf("Logger configuring error: %s. Using '%s' level",
err.Error(), zerolog.GlobalLevel().String())
}
return nil
}
28 changes: 28 additions & 0 deletions cmd/core/logger.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package core

import (
"fmt"
"os"

"github.com/edgecomllc/eupf/cmd/config"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
)

func InitLogger() {
output := zerolog.ConsoleWriter{Out: os.Stdout, TimeFormat: "2006/01/02 15:04:05"}
log.Logger = zerolog.New(output).With().Timestamp().Logger()
}

func SetLoggerLevel(loggingLevel string) error {
if loggingLevel == "" {
return fmt.Errorf("Logging level can't be empty")
}
if loglvl, err := zerolog.ParseLevel(loggingLevel); err == nil {
zerolog.SetGlobalLevel(loglvl)
config.Conf.LoggingLevel = zerolog.GlobalLevel().String()
} else {
return fmt.Errorf("Can't parse logging level: '%s'", loggingLevel)
}
return nil
}
16 changes: 4 additions & 12 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,10 @@ func main() {

// Warning: inefficient log writing.
// As zerolog docs says: "Pretty logging on the console is made possible using the provided (but inefficient) zerolog.ConsoleWriter."
ConfigureLogger()
core.InitLogger()
if err := core.SetLoggerLevel(config.Conf.LoggingLevel); err != nil {
log.Error().Msgf("Logger configuring error: %s. Using '%s' level", err.Error(), zerolog.GlobalLevel().String())
}

if err := ebpf.IncreaseResourceLimits(); err != nil {
log.Fatal().Msgf("Can't increase resource limits: %s", err.Error())
Expand Down Expand Up @@ -136,14 +139,3 @@ func StringToXDPAttachMode(Mode string) link.XDPAttachFlags {
return link.XDPGenericMode
}
}

func ConfigureLogger() {
output := zerolog.ConsoleWriter{Out: os.Stdout, TimeFormat: "2006/01/02 15:04:05"}
log.Logger = zerolog.New(output).With().Timestamp().Logger()

if loglvl, err := zerolog.ParseLevel(config.Conf.LoggingLevel); err == nil {
zerolog.SetGlobalLevel(loglvl)
} else {
log.Warn().Msgf("Can't parse logging level: %s. Using \"info\" level.", err.Error())
}
}