From b7ff07657159485eae929177d1339d986a33ccdc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=2E=20Mert=20Y=C4=B1ld=C4=B1ran?= Date: Tue, 19 Oct 2021 14:22:20 +0300 Subject: [PATCH] Set the default log level for Agent to `INFO` and raise it to `DEBUG` if `dump-logs=true` is provided (#373) * Set the default log level for Agent to `INFO` and change it to `DEBUG` if `dump-logs=true` is provided * Remove `Trace` method and replace its calls with `Debug` * Export logging levels from `logger` by defining functions * Revert "Export logging levels from `logger` by defining functions" This reverts commit e554e40f4a16f07bf7b1f2ee4a7d2544a5a5a19d. * Run `go mod tidy` on agent * Define a method named `determineLogLevel` --- agent/go.mod | 2 +- agent/go.sum | 1 - agent/main.go | 12 +++++++++++- cli/kubernetes/provider.go | 16 ++++++++++++++++ shared/consts.go | 1 + shared/logger/logger.go | 13 +++++-------- tap/passive_tapper.go | 3 --- tap/tcp_stream.go | 8 ++++---- 8 files changed, 38 insertions(+), 18 deletions(-) diff --git a/agent/go.mod b/agent/go.mod index 15884fac8..dc35d88b3 100644 --- a/agent/go.mod +++ b/agent/go.mod @@ -12,9 +12,9 @@ require ( github.com/go-playground/validator/v10 v10.5.0 github.com/google/martian v2.1.0+incompatible github.com/gorilla/websocket v1.4.2 + github.com/op/go-logging v0.0.0-20160315200505-970db520ece7 github.com/orcaman/concurrent-map v0.0.0-20210106121528-16402b402231 github.com/patrickmn/go-cache v2.1.0+incompatible - github.com/romana/rlog v0.0.0-20171115192701-f018bc92e7d7 github.com/up9inc/mizu/shared v0.0.0 github.com/up9inc/mizu/tap v0.0.0 github.com/up9inc/mizu/tap/api v0.0.0 diff --git a/agent/go.sum b/agent/go.sum index 454a8fc95..001c34f41 100644 --- a/agent/go.sum +++ b/agent/go.sum @@ -258,7 +258,6 @@ github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1: github.com/rogpeppe/go-internal v1.1.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4= 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/romana/rlog v0.0.0-20171115192701-f018bc92e7d7 h1:jkvpcEatpwuMF5O5LVxTnehj6YZ/aEZN4NWD/Xml4pI= github.com/romana/rlog v0.0.0-20171115192701-f018bc92e7d7/go.mod h1:KTrHyWpO1sevuXPZwyeZc72ddWRFqNSKDFl7uVWKpg0= 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= diff --git a/agent/main.go b/agent/main.go index acb29507a..6ca1e76d2 100644 --- a/agent/main.go +++ b/agent/main.go @@ -22,6 +22,7 @@ import ( "github.com/gin-contrib/static" "github.com/gin-gonic/gin" "github.com/gorilla/websocket" + "github.com/op/go-logging" "github.com/up9inc/mizu/shared" "github.com/up9inc/mizu/shared/logger" "github.com/up9inc/mizu/tap" @@ -40,7 +41,8 @@ var extensions []*tapApi.Extension // global var extensionsMap map[string]*tapApi.Extension // global func main() { - logger.InitLoggerStderrOnly() + logLevel := determineLogLevel() + logger.InitLoggerStderrOnly(logLevel) flag.Parse() loadExtensions() @@ -303,3 +305,11 @@ func getSyncEntriesConfig() *shared.SyncEntriesConfig { return syncEntriesConfig } + +func determineLogLevel() (logLevel logging.Level) { + logLevel = logging.INFO + if os.Getenv(shared.DebugModeEnvVar) == "1" { + logLevel = logging.DEBUG + } + return +} diff --git a/cli/kubernetes/provider.go b/cli/kubernetes/provider.go index 40882b54e..3cae70685 100644 --- a/cli/kubernetes/provider.go +++ b/cli/kubernetes/provider.go @@ -16,6 +16,7 @@ import ( "io" + "github.com/up9inc/mizu/cli/config" "github.com/up9inc/mizu/cli/mizu" "github.com/up9inc/mizu/shared" "github.com/up9inc/mizu/tap/api" @@ -195,6 +196,11 @@ func (provider *Provider) CreateMizuApiServerPod(ctx context.Context, opts *ApiS port := intstr.FromInt(shared.DefaultApiServerPort) + debugMode := "" + if config.Config.DumpLogs { + debugMode = "1" + } + pod := &core.Pod{ ObjectMeta: metav1.ObjectMeta{ Name: opts.PodName, @@ -223,6 +229,10 @@ func (provider *Provider) CreateMizuApiServerPod(ctx context.Context, opts *ApiS Name: shared.MaxEntriesDBSizeBytesEnvVar, Value: strconv.FormatInt(opts.MaxEntriesDBSizeBytes, 10), }, + { + Name: shared.DebugModeEnvVar, + Value: debugMode, + }, }, Resources: core.ResourceRequirements{ Limits: core.ResourceList{ @@ -524,6 +534,11 @@ func (provider *Provider) ApplyMizuTapperDaemonSet(ctx context.Context, namespac "--nodefrag", } + debugMode := "" + if config.Config.DumpLogs { + debugMode = "1" + } + agentContainer := applyconfcore.Container() agentContainer.WithName(tapperPodName) agentContainer.WithImage(podImage) @@ -531,6 +546,7 @@ func (provider *Provider) ApplyMizuTapperDaemonSet(ctx context.Context, namespac agentContainer.WithSecurityContext(applyconfcore.SecurityContext().WithPrivileged(true)) agentContainer.WithCommand(mizuCmd...) agentContainer.WithEnv( + applyconfcore.EnvVar().WithName(shared.DebugModeEnvVar).WithValue(debugMode), applyconfcore.EnvVar().WithName(shared.HostModeEnvVar).WithValue("1"), applyconfcore.EnvVar().WithName(shared.TappedAddressesPerNodeDictEnvVar).WithValue(string(nodeToTappedPodIPMapJsonStr)), applyconfcore.EnvVar().WithName(shared.GoGCEnvVar).WithValue("12800"), diff --git a/shared/consts.go b/shared/consts.go index 6de052da1..cf4d6f208 100644 --- a/shared/consts.go +++ b/shared/consts.go @@ -11,4 +11,5 @@ const ( RulePolicyFileName = "enforce-policy.yaml" GoGCEnvVar = "GOGC" DefaultApiServerPort = 8899 + DebugModeEnvVar = "MIZU_DEBUG" ) diff --git a/shared/logger/logger.go b/shared/logger/logger.go index e8f82e91e..305246550 100644 --- a/shared/logger/logger.go +++ b/shared/logger/logger.go @@ -29,13 +29,10 @@ func InitLogger(logPath string) { logging.SetBackend(backend1Leveled, backend2Formatter) } -func InitLoggerStderrOnly() { - consoleLog := logging.NewLogBackend(os.Stderr, "", 0) +func InitLoggerStderrOnly(level logging.Level) { + backend := logging.NewLogBackend(os.Stderr, "", 0) + backendFormatter := logging.NewBackendFormatter(backend, format) - backend1Formatter := logging.NewBackendFormatter(consoleLog, format) - - backend1Leveled := logging.AddModuleLevel(consoleLog) - backend1Leveled.SetLevel(logging.DEBUG, "") - - logging.SetBackend(backend1Leveled, backend1Formatter) + logging.SetBackend(backendFormatter) + logging.SetLevel(level, "") } diff --git a/tap/passive_tapper.go b/tap/passive_tapper.go index 8673e8fc2..830f707c5 100644 --- a/tap/passive_tapper.go +++ b/tap/passive_tapper.go @@ -124,9 +124,6 @@ func SilentError(t string, s string, a ...interface{}) { func Debug(s string, a ...interface{}) { logger.Log.Debugf(s, a...) } -func Trace(s string, a ...interface{}) { - logger.Log.Infof(s, a...) -} func inArrayInt(arr []int, valueToCheck int) bool { for _, value := range arr { diff --git a/tap/tcp_stream.go b/tap/tcp_stream.go index 2c9b91d33..2ab626a96 100644 --- a/tap/tcp_stream.go +++ b/tap/tcp_stream.go @@ -108,7 +108,7 @@ func (t *tcpStream) ReassembledSG(sg reassembly.ScatterGather, ac reassembly.Ass } else { ident = fmt.Sprintf("%v %v(%s): ", t.net.Reverse(), t.transport.Reverse(), dir) } - Trace("%s: SG reassembled packet with %d bytes (start:%v,end:%v,skip:%d,saved:%d,nb:%d,%d,overlap:%d,%d)", ident, length, start, end, skip, saved, sgStats.Packets, sgStats.Chunks, sgStats.OverlapBytes, sgStats.OverlapPackets) + Debug("%s: SG reassembled packet with %d bytes (start:%v,end:%v,skip:%d,saved:%d,nb:%d,%d,overlap:%d,%d)", ident, length, start, end, skip, saved, sgStats.Packets, sgStats.Chunks, sgStats.OverlapBytes, sgStats.OverlapPackets) if skip == -1 && *allowmissinginit { // this is allowed } else if skip != 0 { @@ -127,7 +127,7 @@ func (t *tcpStream) ReassembledSG(sg reassembly.ScatterGather, ac reassembly.Ass } dnsSize := binary.BigEndian.Uint16(data[:2]) missing := int(dnsSize) - len(data[2:]) - Trace("dnsSize: %d, missing: %d", dnsSize, missing) + Debug("dnsSize: %d, missing: %d", dnsSize, missing) if missing > 0 { Debug("Missing some bytes: %d", missing) sg.KeepFrom(0) @@ -138,7 +138,7 @@ func (t *tcpStream) ReassembledSG(sg reassembly.ScatterGather, ac reassembly.Ass if err != nil { SilentError("DNS-parser", "Failed to decode DNS: %v", err) } else { - Trace("DNS: %s", gopacket.LayerDump(dns)) + Debug("DNS: %s", gopacket.LayerDump(dns)) } if len(data) > 2+int(dnsSize) { sg.KeepFrom(2 + int(dnsSize)) @@ -173,7 +173,7 @@ func (t *tcpStream) ReassembledSG(sg reassembly.ScatterGather, ac reassembly.Ass } func (t *tcpStream) ReassemblyComplete(ac reassembly.AssemblerContext) bool { - Trace("%s: Connection closed", t.ident) + Debug("%s: Connection closed", t.ident) if t.isTapTarget && !t.isClosed { t.Close() }