From dc177cf3e54c451801987e6595b389f3713598bd Mon Sep 17 00:00:00 2001 From: Sergi Castro Date: Mon, 12 Feb 2024 16:32:26 +0100 Subject: [PATCH] Log loaded config at debug level --- cmd/main.go | 10 ++++++++++ internal/config.go | 5 +++++ internal/config_test.go | 20 ++++++++++++++++++++ internal/logging.go | 2 ++ 4 files changed, 37 insertions(+) diff --git a/cmd/main.go b/cmd/main.go index 29c81b7..a4113e4 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -21,6 +21,7 @@ import ( "github.com/tetratelabs/log" "github.com/tetratelabs/run" "github.com/tetratelabs/run/pkg/signal" + "github.com/tetratelabs/telemetry" "github.com/tetrateio/authservice-go/internal" "github.com/tetrateio/authservice-go/internal/server" @@ -34,11 +35,20 @@ func main() { authzServer = server.New(&configFile.Config, authz.Register) ) + configLog := run.NewPreRunner("config-log", func() error { + cfgLog := internal.Logger(internal.Config) + if cfgLog.Level() == telemetry.LevelDebug { + cfgLog.Debug("configuration loaded", "config", internal.ConfigToJSONString(&configFile.Config)) + } + return nil + }) + g := run.Group{Logger: internal.Logger(internal.Default)} g.Register( configFile, // load the configuration logging, // set up the logging system + configLog, // log the configuration authzServer, // start the server &signal.Handler{}, // handle graceful termination ) diff --git a/internal/config.go b/internal/config.go index 2e74d8c..fb9437d 100644 --- a/internal/config.go +++ b/internal/config.go @@ -66,3 +66,8 @@ func (l *LocalConfigFile) Validate() error { return l.Config.ValidateAll() } + +func ConfigToJSONString(c *configv1.Config) string { + b, _ := protojson.Marshal(c) + return string(b) +} diff --git a/internal/config_test.go b/internal/config_test.go index 917d2f2..8d7dac5 100644 --- a/internal/config_test.go +++ b/internal/config_test.go @@ -95,3 +95,23 @@ func TestLoadMock(t *testing.T) { require.NoError(t, err) require.True(t, proto.Equal(want, &cfg.Config)) } + +func TestConfigToJSONString(t *testing.T) { + tests := []struct { + name string + config *configv1.Config + want string + }{ + {"nil", nil, "{}"}, + {"empty", &configv1.Config{}, "{}"}, + {"simple", &configv1.Config{ListenPort: 8080}, `{"listenPort":8080}`}, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got := ConfigToJSONString(tt.config) + require.JSONEq(t, tt.want, got) + }) + } + +} diff --git a/internal/logging.go b/internal/logging.go index 9aba02f..f98ea8b 100644 --- a/internal/logging.go +++ b/internal/logging.go @@ -28,6 +28,7 @@ import ( const ( Authz = "authz" + Config = "config" Default = "default" Requests = "requests" Server = "server" @@ -37,6 +38,7 @@ const ( // scopes contains the list of all logging scopes var scopes = map[string]string{ Authz: "Envoy ext-authz filter implementation messages", + Config: "Configuration messages", Default: "Default", Requests: "Logs all requests and responses received by the server", Server: "Server request handling messages",