Skip to content

Commit

Permalink
Replace logrus with zerolog, add linter (#115)
Browse files Browse the repository at this point in the history
* Replace logrus with zerolog, add linter

* Add default context logger

* bugfix golangci.yaml

* bump dependencies
  • Loading branch information
Richard87 authored Mar 11, 2024
1 parent 7b6b844 commit c59a5bd
Show file tree
Hide file tree
Showing 14 changed files with 133 additions and 69 deletions.
4 changes: 3 additions & 1 deletion .env.template
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,6 @@ SQL_PORT=
CRON_SCHEDULE=
SCHEDULE_NODE_SYNC=
SCHEDULE_POD_SYNC=
APP_NAME_EXCLUDE_LIST=
APP_NAME_EXCLUDE_LIST=
PRETTY_PRINT=1
LOG_LEVEL=trace
11 changes: 4 additions & 7 deletions .github/workflows/pr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,10 @@ jobs:
- uses: actions/checkout@v4
with:
fetch-depth: 2
- uses: actions/setup-go@v4
- uses: actions/setup-go@v5
with:
go-version: '1.21'
- name: Install dependencies
run: go mod download
- name: Install GolangCI Lint
run: curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(go env GOPATH)/bin v1.55.2

- name: golangci-lint
run: golangci-lint run --timeout=30m --max-same-issues=0 --out-format=github-actions
uses: golangci/golangci-lint-action@v4
with:
version: v1.55.2
15 changes: 15 additions & 0 deletions .golangci.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
run:
timeout: 30m

linters:
enable:
- errcheck
- gosimple
- govet
- ineffassign
- staticcheck
- unused
- zerologlint

issues:
max-same-issues: 0
1 change: 1 addition & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ type AppConfig struct {
SQL SQLConfig
AppNameExcludeList []string `envconfig:"optional"`
LogLevel string `envconfig:"default=info"`
PrettyPrint bool `envconfig:"default=false"`
}

// SQLConfig defines configuration settings used to manage connections to SQL Server
Expand Down
7 changes: 5 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ require (
github.com/microsoft/go-mssqldb v1.4.0
github.com/pkg/errors v0.9.1
github.com/robfig/cron/v3 v3.0.1
github.com/sirupsen/logrus v1.9.3
github.com/stretchr/testify v1.8.4
github.com/rs/zerolog v1.32.0
github.com/stretchr/testify v1.9.0
github.com/vrischmann/envconfig v1.3.0
golang.org/x/sync v0.5.0
k8s.io/api v0.29.0
Expand Down Expand Up @@ -40,10 +40,13 @@ require (
github.com/josharian/intern v1.0.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/mailru/easyjson v0.7.7 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.19 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
github.com/sirupsen/logrus v1.9.3 // indirect
github.com/spf13/pflag v1.0.5 // indirect
golang.org/x/crypto v0.17.0 // indirect
golang.org/x/net v0.19.0 // indirect
Expand Down
16 changes: 14 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ github.com/Azure/azure-sdk-for-go/sdk/azcore v1.6.0/go.mod h1:bjGvMhVMb+EEm3VRNQ
github.com/Azure/azure-sdk-for-go/sdk/azidentity v1.3.0/go.mod h1:OQeznEEkTZ9OrhHJoDD8ZDq51FHgXjqtP9z6bEwBq9U=
github.com/Azure/azure-sdk-for-go/sdk/internal v1.3.0/go.mod h1:okt5dMMTOFjX/aovMlrjvvXoPMBVSPzk9185BT0+eZM=
github.com/AzureAD/microsoft-authentication-library-for-go v1.0.0/go.mod h1:kgDmCTgBzIEPFElEF+FK0SdjAor06dRq2Go927dnQ6o=
github.com/coreos/go-systemd/v22 v22.5.0/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc=
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
Expand Down Expand Up @@ -31,6 +32,7 @@ github.com/go-openapi/swag v0.22.4 h1:QLMzNJnMGPRNDCbySlcj1x01tzU8/9LTTL9hZZZogB
github.com/go-openapi/swag v0.22.4/go.mod h1:UzaqsxGiab7freDnrUUra0MwWfN/q7tE4j+VcZ0yl14=
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/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
github.com/gogo/protobuf v1.3.2 h1:Ov1cvc58UF3b5XjBnZv7+opcTcQFZebYjWzi34vdm4Q=
github.com/gogo/protobuf v1.3.2/go.mod h1:P1XiOD3dCwIKUDQYPy72D8LYyHL2YPYrpS2s69NZV8Q=
github.com/golang-jwt/jwt/v4 v4.4.3/go.mod h1:m21LjoU+eqJr34lmDMbreY2eSTRJ1cv77w39/MY0Ch0=
Expand Down Expand Up @@ -87,6 +89,11 @@ github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/kylelemons/godebug v1.1.0/go.mod h1:9/0rRGxNHcop5bhtWyNeEfOS8JIWk580+fNqagV/RAw=
github.com/mailru/easyjson v0.7.7 h1:UGYAvKxe3sBsEDzO8ZeWOSlIQfWFlxbzLZe7hwFURr0=
github.com/mailru/easyjson v0.7.7/go.mod h1:xzfreul335JAWq5oZzymOObrkdz5UnU4kGfJJLY9Nlc=
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
github.com/mattn/go-isatty v0.0.19 h1:JITubQf0MOLdlGRuRq+jtsDlekdYPia9ZFsB8h/APPA=
github.com/mattn/go-isatty v0.0.19/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/microsoft/go-mssqldb v1.4.0 h1:fhRa6Ftf78dtv1Kj4i1YQoK3VUoce4phkBLoaE0NIaQ=
github.com/microsoft/go-mssqldb v1.4.0/go.mod h1:lmWsjHD8XX/Txr0f8ZqgbEZSC+BZjmEQy/Ms+rLrvho=
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
Expand Down Expand Up @@ -118,6 +125,9 @@ github.com/robfig/cron/v3 v3.0.1 h1:WdRxkvbJztn8LMz/QEvLN5sBU+xKpSqwwUO1Pjr4qDs=
github.com/robfig/cron/v3 v3.0.1/go.mod h1:eQICP3HwyT7UooqI/z+Ov+PtYAWygg1TEWWzGIFLtro=
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/rs/xid v1.5.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg=
github.com/rs/zerolog v1.32.0 h1:keLypqrlIjaFsbmJOBdB/qvyF8KEtCWHwobLp5l/mQ0=
github.com/rs/zerolog v1.32.0/go.mod h1:/7mN4D5sKwJLZQ2b/znpjC3/GQWY/xaDXUM0kKWRHss=
github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ=
github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
Expand All @@ -132,8 +142,8 @@ github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
github.com/vrischmann/envconfig v1.3.0 h1:4XIvQTXznxmWMnjouj0ST5lFo/WAYf5Exgl3x82crEk=
github.com/vrischmann/envconfig v1.3.0/go.mod h1:bbvxFYJdRSpXrhS63mBFtKJzkDiNkyArOLXtY6q0kuI=
github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
Expand Down Expand Up @@ -189,9 +199,11 @@ golang.org/x/sys v0.0.0-20210616045830-e2b7044e8c71/go.mod h1:oPkhp1MJrh7nUepCBc
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.15.0 h1:h48lPFYpsTvQJZF4EKyI4aLHaev3CxivZmv7yZig9pc=
golang.org/x/sys v0.15.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
Expand Down
46 changes: 29 additions & 17 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,45 +1,57 @@
package main

import (
"context"
"os"
"os/signal"
"syscall"
"time"

"github.com/equinor/radix-cost-allocation/config"
"github.com/equinor/radix-cost-allocation/run"
_ "github.com/microsoft/go-mssqldb"
log "github.com/sirupsen/logrus"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"github.com/vrischmann/envconfig"
)

var appConfig config.AppConfig

func main() {

ctx, cancel := signal.NotifyContext(context.Background(), syscall.SIGTERM, syscall.SIGTERM)
defer cancel()

if err := envconfig.Init(&appConfig); err != nil {
log.Fatal(err)
log.Fatal().Err(err).Msg("Failed to initialize config")
}

logLevel, err := log.ParseLevel(appConfig.LogLevel)
ctx, err := setupLogger(ctx, appConfig.LogLevel, appConfig.PrettyPrint)
if err != nil {
log.Warnf("Log level '%s' is not valid. Using 'info' level", appConfig.LogLevel)
logLevel = log.InfoLevel
log.Fatal().Err(err).Msg("Failed to initialize logger")
}
log.SetLevel(logLevel)

stopCh := make(chan struct{})

go func() {
if err := run.InitAndStartCollector(appConfig.SQL, appConfig.Schedule, appConfig.AppNameExcludeList, stopCh); err != nil {
log.Fatal(err)
if err := run.InitAndStartCollector(ctx, appConfig.SQL, appConfig.Schedule, appConfig.AppNameExcludeList); err != nil {
log.Fatal().Msg(err.Error())
}
}()

sigTerm := make(chan os.Signal, 1)
go func() {
signal.Notify(sigTerm, syscall.SIGTERM)
signal.Notify(sigTerm, syscall.SIGINT)
}()
<-ctx.Done()
}

<-sigTerm
close(stopCh)
func setupLogger(ctx context.Context, logLevel string, prettyPrint bool) (context.Context, error) {
zerolog.DurationFieldUnit = time.Millisecond
level, err := zerolog.ParseLevel(logLevel)
if err != nil {
return nil, err
}

zerolog.SetGlobalLevel(level)
if prettyPrint {
log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr, TimeFormat: time.TimeOnly})
}
zerolog.DefaultContextLogger = &log.Logger
ctx = log.Logger.WithContext(ctx)
return ctx, nil
}
4 changes: 2 additions & 2 deletions pkg/repository/sqlrepository.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (

mssql "github.com/microsoft/go-mssqldb"
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
"github.com/rs/zerolog/log"
)

const (
Expand Down Expand Up @@ -76,7 +76,7 @@ func (repo *sqlRepository) executeWithTransaction(query string, args ...interfac
}
defer func() {
if err = conn.Close(); err != nil {
log.Errorf("Unable to close connection: %v", err)
log.Error().Err(err).Msg("Unable to close connection")
}
}()

Expand Down
8 changes: 4 additions & 4 deletions pkg/sync/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"github.com/equinor/radix-cost-allocation/pkg/listers"
"github.com/equinor/radix-cost-allocation/pkg/repository"
"github.com/equinor/radix-cost-allocation/pkg/utils/slice"
log "github.com/sirupsen/logrus"
"github.com/rs/zerolog/log"
"golang.org/x/sync/semaphore"
)

Expand Down Expand Up @@ -36,15 +36,15 @@ func (s *ContainerSyncJob) Sync() error {
}
defer s.sem.Release(1)

log.Info("Start syncing containers")
log.Info().Msg("Start syncing containers")
containerDtos := s.filterContainerByAppNameExcludeList(s.containerDtoLister.List())

log.Debugf("Writing %v containers to repository", len(containerDtos))
log.Debug().Msgf("Writing %v containers to repository", len(containerDtos))
if err := s.repository.BulkUpsertContainers(containerDtos); err != nil {
return err
}

log.Info("Finished syncing containers")
log.Info().Msg("Finished syncing containers")
return nil
}

Expand Down
17 changes: 17 additions & 0 deletions pkg/sync/errors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package sync

import (
"errors"
"fmt"
)

// Syncer defines an interface with a Sync method
type Syncer interface {
Sync() error
}

var ErrSyncAlreadyRunning = errors.New("sync is already running")

func NewSyncAlreadyRunningError(name string) error {
return fmt.Errorf("%w: %s", ErrSyncAlreadyRunning, name)
}
8 changes: 4 additions & 4 deletions pkg/sync/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package sync
import (
"github.com/equinor/radix-cost-allocation/pkg/listers"
"github.com/equinor/radix-cost-allocation/pkg/repository"
log "github.com/sirupsen/logrus"
"github.com/rs/zerolog/log"
"golang.org/x/sync/semaphore"
)

Expand Down Expand Up @@ -31,14 +31,14 @@ func (s *NodeSyncJob) Sync() error {
}
defer s.sem.Release(1)

log.Info("Start syncing nodes")
log.Info().Msg("Start syncing nodes")
nodeDtos := s.nodeDtoLister.List()

log.Debugf("Writing %v nodes to repository", len(nodeDtos))
log.Debug().Msgf("Writing %v nodes to repository", len(nodeDtos))
if err := s.repository.BulkUpsertNodes(nodeDtos); err != nil {
return err
}

log.Info("Finished syncing nodes")
log.Info().Msg("Finished syncing nodes")
return nil
}
20 changes: 0 additions & 20 deletions pkg/sync/syncer.go

This file was deleted.

24 changes: 24 additions & 0 deletions pkg/utils/cronlogger/logger.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package cronlogger

import (
"github.com/robfig/cron/v3"
"github.com/rs/zerolog"
)

type CronLogger struct {
logger *zerolog.Logger
}

var _ cron.Logger = CronLogger{}

func New(logger *zerolog.Logger) CronLogger {
return CronLogger{logger: logger}
}

func (c CronLogger) Info(msg string, v ...interface{}) {
c.logger.Info().Msgf(msg, v...)
}

func (c CronLogger) Error(err error, msg string, v ...interface{}) {
c.logger.Error().Err(err).Msgf(msg, v...)
}
Loading

0 comments on commit c59a5bd

Please sign in to comment.