Skip to content
This repository has been archived by the owner on Apr 22, 2024. It is now read-only.

Commit

Permalink
Log loaded config at debug level (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
sergicastro authored Feb 12, 2024
1 parent a7ac38b commit 4263c42
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 0 deletions.
10 changes: 10 additions & 0 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
)
Expand Down
5 changes: 5 additions & 0 deletions internal/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
20 changes: 20 additions & 0 deletions internal/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
}

}
2 changes: 2 additions & 0 deletions internal/logging.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (

const (
Authz = "authz"
Config = "config"
Default = "default"
Requests = "requests"
Server = "server"
Expand All @@ -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",
Expand Down

0 comments on commit 4263c42

Please sign in to comment.