Skip to content

Commit

Permalink
add logger
Browse files Browse the repository at this point in the history
Signed-off-by: Pavel Okhlopkov <[email protected]>
  • Loading branch information
Pavel Okhlopkov committed Oct 18, 2024
1 parent 054615d commit d99477b
Show file tree
Hide file tree
Showing 68 changed files with 1,133 additions and 359 deletions.
6 changes: 5 additions & 1 deletion cmd/shell-operator/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/flant/shell-operator/pkg/debug"
"github.com/flant/shell-operator/pkg/jq"
shell_operator "github.com/flant/shell-operator/pkg/shell-operator"
"github.com/flant/shell-operator/pkg/unilogger"
utils_signal "github.com/flant/shell-operator/pkg/utils/signal"
)

Expand All @@ -35,6 +36,9 @@ func main() {
return nil
})

logger := unilogger.NewLogger(unilogger.Options{})
unilogger.SetDefault(logger)

// start main loop
startCmd := kpApp.Command("start", "Start shell-operator.").
Default().
Expand All @@ -45,7 +49,7 @@ func main() {
rand.Seed(time.Now().UnixNano())

// Init logging and initialize a ShellOperator instance.
operator, err := shell_operator.Init()
operator, err := shell_operator.Init(logger.Named("shell-operator"))
if err != nil {
os.Exit(1)
}
Expand Down
5 changes: 3 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ require (
github.com/onsi/gomega v1.34.1
github.com/pkg/errors v0.9.1
github.com/prometheus/client_golang v1.19.1
github.com/sirupsen/logrus v1.9.3
github.com/stretchr/testify v1.9.0
golang.org/x/time v0.7.0
gopkg.in/alecthomas/kingpin.v2 v2.2.6
Expand All @@ -33,6 +32,8 @@ require (
// Remove 'in body' from errors, fix for Go 1.16 (https://github.com/go-openapi/validate/pull/138).
replace github.com/go-openapi/validate => github.com/flant/go-openapi-validate v0.19.12-flant.0

require github.com/gojuno/minimock/v3 v3.4.0

require (
github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751 // indirect
github.com/alecthomas/units v0.0.0-20211218093645-b94a6e3cc137 // indirect
Expand All @@ -53,7 +54,6 @@ require (
github.com/go-stack/stack v1.8.0 // indirect
github.com/go-task/slim-sprig v0.0.0-20230315185526-52ccab3ef572 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/gojuno/minimock/v3 v3.4.0 // indirect
github.com/golang/protobuf v1.5.4 // indirect
github.com/google/btree v1.0.1 // indirect
github.com/google/gnostic-models v0.6.8 // indirect
Expand All @@ -76,6 +76,7 @@ require (
github.com/prometheus/client_model v0.5.0 // indirect
github.com/prometheus/common v0.48.0 // indirect
github.com/prometheus/procfs v0.12.0 // indirect
github.com/sirupsen/logrus v1.9.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/tidwall/pretty v1.2.0 // indirect
go.mongodb.org/mongo-driver v1.5.4 // indirect
Expand Down
9 changes: 7 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ github.com/go-task/slim-sprig v0.0.0-20210107165309-348f09dbbbc0/go.mod h1:fyg78
github.com/go-task/slim-sprig v0.0.0-20230315185526-52ccab3ef572 h1:tfuBGBXKqDEevZMzYi5KSi8KkcZtzBcTgAUUtapy0OI=
github.com/go-task/slim-sprig v0.0.0-20230315185526-52ccab3ef572/go.mod h1:9Pwr4B2jHnOSGXyyzV8ROjYa2ojvAY6HCGYYfMoC3Ls=
github.com/go-task/slim-sprig/v3 v3.0.0 h1:sUs3vkvUymDpBKi3qH1YSqBQk9+9D/8M2mN1vB6EwHI=
github.com/go-task/slim-sprig/v3 v3.0.0/go.mod h1:W848ghGpv3Qj3dhTPRyJypKRiqCdHZiAzKg9hl15HA8=
github.com/gobuffalo/attrs v0.0.0-20190224210810-a9411de4debd/go.mod h1:4duuawTqi2wkkpB4ePgWMaai6/Kc6WEz83bhFwpHzj0=
github.com/gobuffalo/depgen v0.0.0-20190329151759-d478694a28d3/go.mod h1:3STtPUQYuzV0gBVOY3vy6CfMm/ljR4pABfrTeHNLHUY=
github.com/gobuffalo/depgen v0.1.0/go.mod h1:+ifsuy7fhi15RWncXQQKjWS9JPkdah5sZvtHc2RXGlg=
Expand Down Expand Up @@ -147,6 +148,7 @@ github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/
github.com/google/gofuzz v1.2.0 h1:xRy4A+RhZaiKjJ1bPfwQ8sedCA+YS2YcCHW6ec7JMi0=
github.com/google/gofuzz v1.2.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
github.com/google/pprof v0.0.0-20240424215950-a892ee059fd6 h1:k7nVchz72niMH6YLQNvHSdIE7iqsQxK1P41mySCvssg=
github.com/google/pprof v0.0.0-20240424215950-a892ee059fd6/go.mod h1:kf6iHlnVGwgKolg33glAes7Yg/8iWP8ukqeldJSO7jw=
github.com/google/uuid v1.0.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/google/uuid v1.1.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=
Expand Down Expand Up @@ -182,6 +184,7 @@ github.com/konsorten/go-windows-terminal-sequences v1.0.2/go.mod h1:T0+1ngSBFLxv
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI=
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/pty v1.1.5/go.mod h1:9r2w37qlBe7rQ6e1fg1S/9xpWHSnaqNdHD3WcMdbPDA=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
Expand Down Expand Up @@ -216,6 +219,7 @@ github.com/onsi/ginkgo v1.12.1/go.mod h1:zj2OWP4+oCPe1qIXoGWkgMRwljMUYCdkwsT2108
github.com/onsi/ginkgo v1.16.5 h1:8xi0RTUf59SOSfEtZMvwTvXYMzG4gV23XVHOZiXNtnE=
github.com/onsi/ginkgo v1.16.5/go.mod h1:+E8gABHa3K6zRBolWtd+ROzc/U5bkGt0FwiG042wbpU=
github.com/onsi/ginkgo/v2 v2.19.0 h1:9Cnnf7UHo57Hy3k6/m5k3dRfGTMXGvxhHFvkDTCTpvA=
github.com/onsi/ginkgo/v2 v2.19.0/go.mod h1:rlwLi9PilAFJ8jCg9UE1QP6VBpd6/xj3SRC0d6TU0To=
github.com/onsi/gomega v1.7.1/go.mod h1:XdKZgCCFLUoM/7CFJVPcG8C1xQ1AJ0vpAezJrB7JYyY=
github.com/onsi/gomega v1.10.1/go.mod h1:iN09h71vgCQne3DLsj+A5owkum+a2tYe+TOCB1ybHNo=
github.com/onsi/gomega v1.34.1 h1:EUMJIKUjM8sKjYbtxQI9A4z2o+rruxnzNvpknOXie6k=
Expand Down Expand Up @@ -243,12 +247,13 @@ github.com/rogpeppe/go-internal v1.1.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFR
github.com/rogpeppe/go-internal v1.2.2/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
github.com/rogpeppe/go-internal v1.10.0 h1:TMyTOH3F/DB16zRVcYyreMH6GnZZrwQVAoYjRBZyWFQ=
github.com/rogpeppe/go-internal v1.10.0/go.mod h1:UQnix2H7Ngw/k4C5ijL5+65zddjncjaFoBhdsK/akog=
github.com/sergi/go-diff v1.0.0/go.mod h1:0CfEIISq7TuYL3j771MWULgwwjU+GofnZX9QAmXWZgo=
github.com/sirupsen/logrus v1.4.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo=
github.com/sirupsen/logrus v1.4.1/go.mod h1:ni0Sbl8bgC9z8RoU9G6nDWqqs/fq4eDPysMBDgk/93Q=
github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6MwdIuYE2rE=
github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ=
github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
github.com/sirupsen/logrus v1.9.0 h1:trlNQbNUG3OdDrDil03MCb1H2o9nJ1x4/5LYw7byDE0=
github.com/sirupsen/logrus v1.9.0/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
github.com/spf13/cobra v0.0.3/go.mod h1:1l0Ry5zgKvJasoi3XT1TypsSe7PqH0Sj9dhYf7v3XqQ=
github.com/spf13/pflag v1.0.3/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnInEg4=
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
Expand Down
88 changes: 39 additions & 49 deletions pkg/app/log.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
package app

import (
"bytes"
"fmt"
"strings"
"time"

log "github.com/sirupsen/logrus"
log "github.com/flant/shell-operator/pkg/unilogger"
"gopkg.in/alecthomas/kingpin.v2"

"github.com/flant/shell-operator/pkg/config"
"github.com/flant/shell-operator/pkg/unilogger"
)

// Use info level with timestamps and a text output by default
Expand Down Expand Up @@ -45,33 +45,36 @@ func DefineLoggingFlags(cmd *kingpin.CmdClause) {
}

// SetupLogging sets logger formatter and level.
func SetupLogging(runtimeConfig *config.Config) {
jsonFormatter := log.JSONFormatter{DisableTimestamp: LogNoTime}
textFormatter := log.TextFormatter{DisableTimestamp: LogNoTime, DisableColors: true}
colorFormatter := log.TextFormatter{DisableTimestamp: LogNoTime, ForceColors: true, FullTimestamp: true}
switch strings.ToLower(LogType) {
case "json":
log.SetFormatter(&jsonFormatter)
case "text":
log.SetFormatter(&textFormatter)
case "color":
log.SetFormatter(&colorFormatter)
default:
log.SetFormatter(&jsonFormatter)
}
if LogProxyHookJSON {
formatter := log.StandardLogger().Formatter
log.SetFormatter(&ProxyJsonWrapperFormatter{WrappedFormatter: formatter})
}
func SetupLogging(runtimeConfig *config.Config, logger *unilogger.Logger) {
// TODO: if we need formatters - add to logger
// jsonFormatter := log.JSONFormatter{DisableTimestamp: LogNoTime}
// textFormatter := log.TextFormatter{DisableTimestamp: LogNoTime, DisableColors: true}
// colorFormatter := log.TextFormatter{DisableTimestamp: LogNoTime, ForceColors: true, FullTimestamp: true}
// switch strings.ToLower(LogType) {
// case "json":
// log.SetFormatter(&jsonFormatter)
// case "text":
// log.SetFormatter(&textFormatter)
// case "color":
// log.SetFormatter(&colorFormatter)
// default:
// log.SetFormatter(&jsonFormatter)
// }
// if LogProxyHookJSON {
// formatter := log.StandardLogger().Formatter
// log.SetFormatter(&ProxyJsonWrapperFormatter{WrappedFormatter: formatter})
// }

setLogLevel(LogLevel)
logger.SetLevel(unilogger.LogLevelFromStr(LogLevel))
unilogger.SetDefaultLevel(unilogger.LogLevelFromStr(LogLevel))

runtimeConfig.Register("log.level",
fmt.Sprintf("Global log level. Default duration for debug level is %s", ForcedDurationForDebugLevel),
strings.ToLower(LogLevel),
func(oldValue string, newValue string) error {
log.Infof("Set log level to '%s'", newValue)
setLogLevel(newValue)
logger.SetLevel(unilogger.LogLevelFromStr(newValue))
unilogger.SetDefaultLevel(unilogger.LogLevelFromStr(LogLevel))
return nil
}, func(oldValue string, newValue string) time.Duration {
if strings.ToLower(newValue) == "debug" {
Expand All @@ -81,32 +84,19 @@ func SetupLogging(runtimeConfig *config.Config) {
})
}

func setLogLevel(logLevel string) {
switch strings.ToLower(logLevel) {
case "debug":
log.SetLevel(log.DebugLevel)
case "error":
log.SetLevel(log.ErrorLevel)
case "info":
log.SetLevel(log.InfoLevel)
default:
log.SetLevel(log.InfoLevel)
}
}

type ProxyJsonWrapperFormatter struct {
WrappedFormatter log.Formatter
}
// type ProxyJsonWrapperFormatter struct {
// WrappedFormatter log.Formatter
// }

func (f *ProxyJsonWrapperFormatter) Format(entry *log.Entry) ([]byte, error) {
// if proxying the json message is intended, just return the bytes
// TODO: Find a more elegant way to carry this info
if entry.Data[ProxyJsonLogKey] == true {
b := bytes.NewBufferString(entry.Message)
b.WriteString("\n")
return b.Bytes(), nil
}
// func (f *ProxyJsonWrapperFormatter) Format(entry *log.Entry) ([]byte, error) {
// // if proxying the json message is intended, just return the bytes
// // TODO: Find a more elegant way to carry this info
// if entry.Data[ProxyJsonLogKey] == true {
// b := bytes.NewBufferString(entry.Message)
// b.WriteString("\n")
// return b.Bytes(), nil
// }

// otherwise, use the wrapped formatter
return f.WrappedFormatter.Format(entry)
}
// // otherwise, use the wrapped formatter
// return f.WrappedFormatter.Format(entry)
// }
14 changes: 8 additions & 6 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@ package config

import (
"fmt"
"log/slog"
"sort"
"strings"
"sync"
"time"

log "github.com/sirupsen/logrus"
"github.com/flant/shell-operator/pkg/unilogger"
log "github.com/flant/shell-operator/pkg/unilogger"
)

/**
Expand Down Expand Up @@ -42,7 +44,7 @@ type Config struct {
temporalValues map[string]*TemporalValue
expireTicker *time.Ticker

logEntry *log.Entry
logEntry *unilogger.Logger
}

func NewConfig() *Config {
Expand All @@ -51,7 +53,7 @@ func NewConfig() *Config {
values: make(map[string]string),
temporalValues: make(map[string]*TemporalValue),
errors: make(map[string]error),
logEntry: log.WithField("component", "runtimeConfig"),
logEntry: unilogger.NewLogger(unilogger.Options{}).With(slog.String("component", "runtimeConfig")),
}
}

Expand Down Expand Up @@ -254,7 +256,7 @@ func (c *Config) expireOverrides() {

for _, expire := range expires {
name, oldValue, newValue := expire[0], expire[1], expire[2]
c.logEntry.Debugf("Parameter '%s' expired", name)
c.logEntry.Debug("Parameter is expired", slog.String("parameter", name))
c.callOnChange(name, oldValue, newValue)
}
}
Expand All @@ -266,8 +268,8 @@ func (c *Config) callOnChange(name string, oldValue string, newValue string) {
}
err := c.params[name].onChange(oldValue, newValue)
if err != nil {
c.logEntry.Errorf("OnChange handler failed for '%s' during value change from '%s' to '%s': %v",
name, oldValue, newValue, err)
c.logEntry.Error("OnChange handler failed for parameter during value change values",
slog.String("parameter", name), slog.String("old_value", oldValue), slog.String("new_value", newValue), slog.String("error", err.Error()))
}
c.m.Lock()
delete(c.errors, name)
Expand Down
28 changes: 16 additions & 12 deletions pkg/debug/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ import (

"github.com/go-chi/chi/v5"
"github.com/go-chi/chi/v5/middleware"
log "github.com/sirupsen/logrus"
"gopkg.in/yaml.v3"

"github.com/flant/shell-operator/pkg/unilogger"
utils "github.com/flant/shell-operator/pkg/utils/file"
structured_logger "github.com/flant/shell-operator/pkg/utils/structured-logger"
structuredLogger "github.com/flant/shell-operator/pkg/utils/structured-logger"
)

type Server struct {
Expand All @@ -24,18 +24,22 @@ type Server struct {
HttpAddr string

Router chi.Router

logger *unilogger.Logger
}

func NewServer(prefix, socketPath, httpAddr string) *Server {
func NewServer(prefix, socketPath, httpAddr string, logger *unilogger.Logger) *Server {
router := chi.NewRouter()
router.Use(structured_logger.NewStructuredLogger(log.StandardLogger(), "debugEndpoint"))

router.Use(structuredLogger.NewStructuredLogger(logger.Named("debugEndpoint"), "debugEndpoint"))
router.Use(middleware.Recoverer)

return &Server{
Prefix: prefix,
SocketPath: socketPath,
HttpAddr: httpAddr,
Router: router,
logger: logger,
}
}

Expand All @@ -44,43 +48,43 @@ func (s *Server) Init() (err error) {

err = os.MkdirAll(path.Dir(address), 0o700)
if err != nil {
log.Errorf("Debug HTTP server fail to create socket '%s': %v", address, err)
s.logger.Errorf("Debug HTTP server fail to create socket '%s': %v", address, err)
return err
}

exists, err := utils.FileExists(address)
if err != nil {
log.Errorf("Debug HTTP server fail to check socket '%s': %v", address, err)
s.logger.Errorf("Debug HTTP server fail to check socket '%s': %v", address, err)
return err
}
if exists {
err = os.Remove(address)
if err != nil {
log.Errorf("Debug HTTP server fail to remove existing socket '%s': %v", address, err)
s.logger.Errorf("Debug HTTP server fail to remove existing socket '%s': %v", address, err)
return err
}
}

// Check if socket is available
listener, err := net.Listen("unix", address)
if err != nil {
log.Errorf("Debug HTTP server fail to listen on '%s': %v", address, err)
s.logger.Errorf("Debug HTTP server fail to listen on '%s': %v", address, err)
return err
}

log.Infof("Debug endpoint listen on %s", address)
s.logger.Infof("Debug endpoint listen on %s", address)

go func() {
if err := http.Serve(listener, s.Router); err != nil {
log.Errorf("Error starting Debug socket server: %s", err)
s.logger.Errorf("Error starting Debug socket server: %s", err)
os.Exit(1)
}
}()

if s.HttpAddr != "" {
go func() {
if err := http.ListenAndServe(s.HttpAddr, s.Router); err != nil {
log.Errorf("Error starting Debug HTTP server: %s", err)
s.logger.Errorf("Error starting Debug HTTP server: %s", err)
os.Exit(1)
}
}()
Expand Down Expand Up @@ -131,7 +135,7 @@ func handleFormattedOutput(writer http.ResponseWriter, request *http.Request, ha
}

format := FormatFromRequest(request)
structured_logger.GetLogEntry(request).Debugf("use format '%s'", format)
structuredLogger.GetLogEntry(request).Debugf("use format '%s'", format)

switch format {
case "text":
Expand Down
Loading

0 comments on commit d99477b

Please sign in to comment.