Skip to content

Commit

Permalink
Redesign logging (#99)
Browse files Browse the repository at this point in the history
* Redesign logging

* Codescan fixes

* Codescan fixes

* Fix an Info log with parameters
  • Loading branch information
mhmxs authored Jun 8, 2022
1 parent cd5bc96 commit 34b69e2
Show file tree
Hide file tree
Showing 20 changed files with 390 additions and 176 deletions.
13 changes: 13 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -154,3 +154,16 @@ The project is using standard Go error handling with the following rules:
}
```
`package.IsCustomError(err)`

### Logging standards

The project is using `klog` on top of `zap` logging. The log engine is configured via command line arguments:
* `--v` verbosity of info logs, default value is 3, allowed values are 0-5.
* `--zap-encoder` log message encoder, default value is `console`, allowed values are `console`, `json`.

Logging rules:
* Fatal: core component stopped to work, better to terminate the application
* Error: application doesn't work properly, operator has to wake up and fix the problem
* Info: must to know information, included recoverable errors, this level ignores `--v` settings
* V(1-3).Info: good to know information
* V(4-5).Info: debug logs, do not use in production, may contains sensitive information
23 changes: 15 additions & 8 deletions cmd/kubernetes-kms-vault/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,15 @@ import (
"time"

"github.com/ondat/trousseau/internal/config"
"github.com/ondat/trousseau/internal/logger"
"github.com/ondat/trousseau/internal/metrics"
"github.com/ondat/trousseau/internal/server"
"github.com/ondat/trousseau/internal/utils"
"github.com/ondat/trousseau/internal/version"
"google.golang.org/grpc"
pb "k8s.io/apiserver/pkg/storage/value/encrypt/envelope/v1beta1"
json "k8s.io/component-base/logs/json"

// json "k8s.io/component-base/logs/json"
"k8s.io/klog/v2"
)

Expand All @@ -33,7 +35,7 @@ const (

var (
listenAddr = flag.String("listen-addr", "unix:///opt/vaultkms.socket", "gRPC listen address")
logFormatJSON = flag.Bool("log-format-json", false, "set log formatter to json")
logEncoder = flag.String("zap-encoder", "console", "set log encoder [console, json]")
configFilePath = flag.String("config-file-path", "./config.yaml", "Path for Vault Provider config file")
healthzPort = flag.Int("healthz-port", healthPort, "port for health check")
healthzPath = flag.String("healthz-path", "/healthz", "path for health check")
Expand All @@ -44,17 +46,22 @@ var (

func main() {
klog.InitFlags(nil)

flag.Parse()

if *logFormatJSON {
klog.SetLogger(json.JSONLogger)
v := flag.CommandLine.Lookup("v").Value.String()

logLevel, err := strconv.Atoi(v)
if err != nil {
klog.Fatalln("Invalid verbosity level", "level", v)
}

klog.SetLogger(logger.NewLogger(klog.Level(logLevel), *logEncoder))

ctx := withShutdownSignal(context.Background())

// initialize metrics exporter
go func() {
//nolint:govet // We know err is a shadow
err := metrics.Serve(*metricsBackend, *metricsAddress)
if err != nil {
klog.Errorln(err)
Expand Down Expand Up @@ -97,7 +104,7 @@ func main() {
os.Exit(1)
}

klog.Infof("Listening for connections on address: %v", listener.Addr())
klog.InfoS("Listening for connections", "address", listener.Addr())

go func() {
if err := s.Serve(listener); err != nil {
Expand Down Expand Up @@ -129,7 +136,7 @@ func main() {

<-ctx.Done()
// gracefully stop the grpc server
klog.Infof("terminating the server")
klog.Info("Terminating the server")
s.GracefulStop()
klog.Flush()
// using os.Exit skips running deferred functions
Expand All @@ -146,7 +153,7 @@ func withShutdownSignal(ctx context.Context) context.Context {

go func() {
<-signalChan
klog.Info("received shutdown signal")
klog.Info("Received shutdown signal")
cancel()
}()

Expand Down
64 changes: 32 additions & 32 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,28 @@ go 1.18

replace github.com/ondat/trousseau => ./

replace go.opentelemetry.io/otel/sdk => go.opentelemetry.io/otel/sdk v1.7.0

require (
github.com/hashicorp/vault/api v1.1.1
github.com/spf13/viper v1.8.1
github.com/go-logr/logr v1.2.3
github.com/go-logr/zapr v1.2.3
github.com/hashicorp/vault/api v1.0.5-0.20200519221902-385fac77e20f
github.com/spf13/viper v1.12.0
github.com/stretchr/testify v1.7.1
go.opentelemetry.io/otel v1.6.3
go.opentelemetry.io/otel v1.7.0
go.opentelemetry.io/otel/exporters/metric/prometheus v0.20.0
go.opentelemetry.io/otel/metric v0.21.0
google.golang.org/grpc v1.46.0
k8s.io/apiserver v0.22.1
k8s.io/component-base v0.22.1
k8s.io/klog/v2 v2.9.0
go.uber.org/zap v1.19.0
google.golang.org/grpc v1.46.2
k8s.io/apiserver v0.24.1
k8s.io/klog/v2 v2.60.1
)

require (
github.com/beorn7/perks v1.0.1 // indirect
github.com/cenkalti/backoff/v3 v3.2.2 // indirect
github.com/cespare/xxhash/v2 v2.1.1 // indirect
github.com/cespare/xxhash/v2 v2.1.2 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/fatih/color v1.10.0 // indirect
github.com/fsnotify/fsnotify v1.4.9 // indirect
github.com/go-logr/logr v1.2.3 // indirect
github.com/fsnotify/fsnotify v1.5.4 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/protobuf v1.5.2 // indirect
Expand All @@ -37,41 +38,40 @@ require (
github.com/hashicorp/go-sockaddr v1.0.2 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/hashicorp/vault/sdk v0.2.1 // indirect
github.com/magiconair/properties v1.8.5 // indirect
github.com/mattn/go-colorable v0.1.8 // indirect
github.com/magiconair/properties v1.8.6 // indirect
github.com/matttproud/golang_protobuf_extensions v1.0.2-0.20181231171920-c182affec369 // indirect
github.com/mitchellh/go-homedir v1.1.0 // indirect
github.com/mitchellh/mapstructure v1.4.1 // indirect
github.com/pelletier/go-toml v1.9.3 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/pelletier/go-toml v1.9.5 // indirect
github.com/pelletier/go-toml/v2 v2.0.1 // indirect
github.com/pierrec/lz4 v2.6.1+incompatible // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/prometheus/client_golang v1.11.0 // indirect
github.com/prometheus/client_golang v1.12.1 // indirect
github.com/prometheus/client_model v0.2.0 // indirect
github.com/prometheus/common v0.26.0 // indirect
github.com/prometheus/procfs v0.6.0 // indirect
github.com/prometheus/common v0.32.1 // indirect
github.com/prometheus/procfs v0.7.3 // indirect
github.com/ryanuber/go-glob v1.0.0 // indirect
github.com/spf13/afero v1.6.0 // indirect
github.com/spf13/cast v1.3.1 // indirect
github.com/spf13/afero v1.8.2 // indirect
github.com/spf13/cast v1.5.0 // indirect
github.com/spf13/jwalterweatherman v1.1.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/subosito/gotenv v1.2.0 // indirect
github.com/subosito/gotenv v1.3.0 // indirect
go.opentelemetry.io/otel/internal/metric v0.21.0 // indirect
go.opentelemetry.io/otel/sdk v1.0.0-RC1 // indirect
go.opentelemetry.io/otel/sdk/export/metric v0.21.0 // indirect
go.opentelemetry.io/otel/sdk/metric v0.21.0 // indirect
go.opentelemetry.io/otel/trace v1.6.3 // indirect
go.opentelemetry.io/otel/trace v1.7.0 // indirect
go.uber.org/atomic v1.7.0 // indirect
go.uber.org/multierr v1.6.0 // indirect
go.uber.org/zap v1.17.0 // indirect
golang.org/x/crypto v0.0.0-20210711020723-a769d52b0f97 // indirect
golang.org/x/net v0.0.0-20210726213435-c6fcb2dbf985 // indirect
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c // indirect
golang.org/x/crypto v0.0.0-20220411220226-7b82a4e95df4 // indirect
golang.org/x/net v0.0.0-20220520000938-2e3eb7b945c2 // indirect
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a // indirect
golang.org/x/text v0.3.7 // indirect
golang.org/x/time v0.0.0-20210723032227-1f47c861a9ac // indirect
google.golang.org/genproto v0.0.0-20210602131652-f16073e35f0c // indirect
google.golang.org/protobuf v1.27.1 // indirect
gopkg.in/ini.v1 v1.62.0 // indirect
golang.org/x/time v0.0.0-20220210224613-90d013bbcef8 // indirect
google.golang.org/genproto v0.0.0-20220519153652-3a47de7e79bd // indirect
google.golang.org/protobuf v1.28.0 // indirect
gopkg.in/ini.v1 v1.66.4 // indirect
gopkg.in/square/go-jose.v2 v2.6.0 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect
gopkg.in/yaml.v3 v3.0.0 // indirect
)
Loading

0 comments on commit 34b69e2

Please sign in to comment.