Skip to content

Commit

Permalink
add log level
Browse files Browse the repository at this point in the history
  • Loading branch information
alexei-led committed May 11, 2023
1 parent f9b6818 commit b8572fd
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 24 deletions.
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@
GO = go
BIN = $(CURDIR)/.bin
LINT_CONFIG = $(CURDIR)/.golangci.yaml
LDFLAGS_VERSION := -X main.version=$(VERSION) -X main.gitCommit=$(COMMIT) -X main.gitBranch=$(BRANCH) -X \"main.buildDate=$(DATE)\"

MODULE = $(shell $(GO) list -m)
DATE ?= $(shell date +%FT%T%z)
VERSION ?= $(shell git describe --tags --always --dirty --match="v*" 2> /dev/null || \
VERSION ?= $(shell git describe --tags --always --dirty --match="[0-9]*.[0-9]*.[0-9]*" 2> /dev/null || \
cat $(CURDIR)/.version 2> /dev/null || echo v0)
COMMIT ?= $(shell git rev-parse --short HEAD 2>/dev/null)
BRANCH ?= $(shell git rev-parse --abbrev-ref HEAD 2>/dev/null)

LDFLAGS_VERSION := -X main.version=$(VERSION) -X main.gitCommit=$(COMMIT) -X main.gitBranch=$(BRANCH) -X \"main.buildDate=$(DATE)\"

# platforms and architectures for release; default to MacOS (darwin) and arm64 (M1/M2)
TARGETOS := $(or $(TARGETOS), darwin)
Expand Down
97 changes: 75 additions & 22 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,16 +75,47 @@ func run(ctx context.Context, log *logrus.Entry, cfg config.Config) error {
return nil
}

func mainCmd(c *cli.Context) error {
ctx := signals.SetupSignalHandler()
cfg := config.LoadConfig(c)

func prepareLogler(cfg config.Config, c *cli.Context) *logrus.Entry {
logger := logrus.New()

// set debug log level
switch level := c.String("log-level"); level {
case "debug", "DEBUG":
logger.SetLevel(logrus.DebugLevel)
case "info", "INFO":
logger.SetLevel(logrus.InfoLevel)
case "warning", "WARNING":
logger.SetLevel(logrus.WarnLevel)
case "error", "ERROR":
logger.SetLevel(logrus.ErrorLevel)
case "fatal", "FATAL":
logger.SetLevel(logrus.FatalLevel)
case "panic", "PANIC":
logger.SetLevel(logrus.PanicLevel)
default:
logger.SetLevel(logrus.WarnLevel)
}

logger.SetFormatter(&logrus.TextFormatter{
FullTimestamp: true,
})
if c.Bool("json") {
logger.SetFormatter(&logrus.JSONFormatter{})
}

log := logger.WithFields(logrus.Fields{
"cluster": cfg.ClusterName,
"version": version,
})

return log
}

func runCmd(c *cli.Context) error {
ctx := signals.SetupSignalHandler()
cfg := config.LoadConfig(c)
log := prepareLogler(cfg, c)

if err := run(ctx, log, cfg); err != nil {
log.Fatalf("eks-lens agent failed: %v", err)
}
Expand All @@ -94,28 +125,50 @@ func mainCmd(c *cli.Context) error {

func main() {
app := &cli.App{
Flags: []cli.Flag{
&cli.StringFlag{
Name: "custer-name",
Usage: "EKS cluster name",
Required: true,
EnvVars: []string{"CLUSTER_NAME"},
},
&cli.StringFlag{
Name: "kubeconfig",
Usage: "Path to kubeconfig file",
EnvVars: []string{"KUBECONFIG"},
},
&cli.StringFlag{
Name: "stream-name",
Usage: "Amazon Kinesis Data Stream name",
Required: true,
EnvVars: []string{"STREAM_NAME"},
Commands: []*cli.Command{
{
Name: "run",
Usage: "run eks-lens agent",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "cluster-name",
Usage: "EKS cluster name",
Required: true,
EnvVars: []string{"CLUSTER_NAME"},
Category: "Configuration",
},
&cli.StringFlag{
Name: "kubeconfig",
Usage: "Path to kubeconfig file",
EnvVars: []string{"KUBECONFIG"},
Category: "Configuration",
},
&cli.StringFlag{
Name: "stream-name",
Usage: "Amazon Kinesis Data Stream name",
Required: true,
EnvVars: []string{"STREAM_NAME"},
Category: "Configuration",
},
&cli.StringFlag{
Name: "log-level",
Usage: "set log level (debug, info, warning(*), error, fatal, panic)",
Value: "warning",
EnvVars: []string{"LOG_LEVEL"},
Category: "Logging",
},
&cli.BoolFlag{
Name: "json",
Usage: "produce log in JSON format: Logstash and Splunk friendly",
EnvVars: []string{"LOG_JSON"},
Category: "Logging",
},
},
Action: runCmd,
},
},
Name: "eks-lens-agent",
Usage: "eks-lens-agent is a data collection agent for EKS Lens",
Action: mainCmd,
Version: version,
}
cli.VersionPrinter = func(c *cli.Context) {
Expand Down
4 changes: 4 additions & 0 deletions deploy/deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,14 @@ spec:
containers:
- name: eks-lens-agent
image: ghcr.io/doitintl/eks-lens-agent:latest
args:
- run
env:
- name: CLUSTER_NAME
value: gaia-cost
- name: STREAM_NAME
value: eks-lens
- name: LOG_LEVEL
value: debug
imagePullPolicy: Always
restartPolicy: Always

0 comments on commit b8572fd

Please sign in to comment.