From a7c62f2122ccd31b719a0c6d2f1256127b590d28 Mon Sep 17 00:00:00 2001 From: Adam0Brien Date: Mon, 23 Sep 2024 13:02:27 +0100 Subject: [PATCH 01/11] RHCLOUD-35286: Ensure log verbosity can be adjusted --- cmd/root.go | 67 +++++++++++++++++++++++++++++++------- inventory-api-compose.yaml | 2 ++ 2 files changed, 58 insertions(+), 11 deletions(-) diff --git a/cmd/root.go b/cmd/root.go index 5266f086..e2f9bdf1 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -4,6 +4,7 @@ import ( "fmt" "os" "path/filepath" + "strings" "github.com/spf13/cobra" "github.com/spf13/viper" @@ -27,16 +28,9 @@ var ( Name = "inventory-api" cfgFile string - rootLog = log.With(log.NewStdLogger(os.Stdout), - "ts", log.DefaultTimestamp, - "caller", log.DefaultCaller, - "service.name", Name, - "service.version", Version, - "trace.id", tracing.TraceID(), - "span.id", tracing.SpanID(), - ) + logger *log.Helper - logger = log.NewHelper(log.NewFilter(rootLog, log.FilterLevel(log.LevelInfo))) + baseLogger log.Logger rootCmd = &cobra.Command{ Use: Name, @@ -88,13 +82,17 @@ func init() { if err != nil { panic(err) } - migrateCmd := migrate.NewCommand(options.Storage, log.With(rootLog, "group", "storage")) + + logLevel := getLogLevel() + logger, baseLogger = initLogger(logLevel) + + migrateCmd := migrate.NewCommand(options.Storage, baseLogger) rootCmd.AddCommand(migrateCmd) err = viper.BindPFlags(migrateCmd.Flags()) if err != nil { panic(err) } - serveCmd := serve.NewCommand(options.Server, options.Storage, options.Authn, options.Authz, options.Eventing, log.With(rootLog, "group", "server")) + serveCmd := serve.NewCommand(options.Server, options.Storage, options.Authn, options.Authz, options.Eventing, baseLogger) rootCmd.AddCommand(serveCmd) err = viper.BindPFlags(serveCmd.Flags()) if err != nil { @@ -143,3 +141,50 @@ func initConfig() { panic(err) } } + +func getLogLevel() string { + viper.SetConfigFile("./inventory-api-compose.yaml") + + if err := viper.ReadInConfig(); err != nil { + fmt.Printf("Error reading config file: %v. Using default log level 'info'.\n", err) + return "info" // Default log level in case of error + } + + logLevel := viper.GetString("log.level") + fmt.Printf("Log Level is set to: %s\n", logLevel) + return logLevel +} + +// initLogger initializes the logger based on the provided log level +func initLogger(logLevel string) (*log.Helper, log.Logger) { + // Convert logLevel string to log.Level type + var level log.Level + switch strings.ToLower(logLevel) { + case "debug": + level = log.LevelDebug + case "info": + level = log.LevelInfo + case "warn": + level = log.LevelWarn + case "error": + level = log.LevelError + case "fatal": + level = log.LevelFatal + default: + fmt.Printf("Invalid log level '%s' provided. Defaulting to 'info' level.\n", logLevel) + level = log.LevelInfo + } + + rootLogger := log.With(log.NewStdLogger(os.Stdout), + "ts", log.DefaultTimestamp, + "caller", log.DefaultCaller, + "service.name", Name, + "service.version", Version, + "trace.id", tracing.TraceID(), + "span.id", tracing.SpanID(), + ) + filteredLogger := log.NewFilter(rootLogger, log.FilterLevel(level)) + helperLogger := log.NewHelper(filteredLogger) + + return helperLogger, filteredLogger +} diff --git a/inventory-api-compose.yaml b/inventory-api-compose.yaml index 6184e4f5..87ef0284 100644 --- a/inventory-api-compose.yaml +++ b/inventory-api-compose.yaml @@ -25,3 +25,5 @@ storage: user: "postgres" password: "yPsw5e6ab4bvAGe5H" dbname: "spicedb" +log: + level: "info" From e4de71b6eb3fbbe900b84d205edcfb2c79f41481 Mon Sep 17 00:00:00 2001 From: Adam0Brien Date: Wed, 25 Sep 2024 16:25:14 +0100 Subject: [PATCH 02/11] Working for inventory-up-sso --- cmd/root.go | 13 ++++--------- sso-inventory-api.yaml | 2 ++ 2 files changed, 6 insertions(+), 9 deletions(-) diff --git a/cmd/root.go b/cmd/root.go index e2f9bdf1..cf09655d 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -83,6 +83,7 @@ func init() { panic(err) } + initConfig() logLevel := getLogLevel() logger, baseLogger = initLogger(logLevel) @@ -108,12 +109,12 @@ func initConfig() { if configFilePath, exists := os.LookupEnv("INVENTORY_API_CONFIG"); exists { absPath, err := filepath.Abs(configFilePath) if err != nil { - log.Fatalf("Failed to resolve absolute path for config file: %v", err) + //log.Fatalf("Failed to resolve absolute path for config file: %v", err) } // Set the config file path viper.SetConfigFile(absPath) if err := viper.ReadInConfig(); err != nil { - log.Fatalf("Error reading INVENTORY_API_CONFIG file, %s", err) + //log.Fatalf("Error reading INVENTORY_API_CONFIG file, %s", err) } } else { home, err := os.UserHomeDir() @@ -133,7 +134,7 @@ func initConfig() { if err := viper.ReadInConfig(); err != nil { panic(err) } else { - logger.Infof("Using config file: %s", viper.ConfigFileUsed()) + //logger.Infof("Using config file: %s", viper.ConfigFileUsed()) } // put the values into the options struct. @@ -143,12 +144,6 @@ func initConfig() { } func getLogLevel() string { - viper.SetConfigFile("./inventory-api-compose.yaml") - - if err := viper.ReadInConfig(); err != nil { - fmt.Printf("Error reading config file: %v. Using default log level 'info'.\n", err) - return "info" // Default log level in case of error - } logLevel := viper.GetString("log.level") fmt.Printf("Log Level is set to: %s\n", logLevel) diff --git a/sso-inventory-api.yaml b/sso-inventory-api.yaml index f38f950c..6ad9ed6a 100644 --- a/sso-inventory-api.yaml +++ b/sso-inventory-api.yaml @@ -23,3 +23,5 @@ storage: user: "postgres" password: "yPsw5e6ab4bvAGe5H" dbname: "spicedb" +log: + level: "warn" \ No newline at end of file From 09ce9e513941d97471cf6a4c7f5b55a9704eb751 Mon Sep 17 00:00:00 2001 From: Adam0Brien Date: Wed, 25 Sep 2024 16:30:07 +0100 Subject: [PATCH 03/11] Replace logs with prints --- cmd/root.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/cmd/root.go b/cmd/root.go index cf09655d..4795d404 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -109,12 +109,12 @@ func initConfig() { if configFilePath, exists := os.LookupEnv("INVENTORY_API_CONFIG"); exists { absPath, err := filepath.Abs(configFilePath) if err != nil { - //log.Fatalf("Failed to resolve absolute path for config file: %v", err) + fmt.Errorf("Failed to resolve absolute path for config file: %v", err) } // Set the config file path viper.SetConfigFile(absPath) if err := viper.ReadInConfig(); err != nil { - //log.Fatalf("Error reading INVENTORY_API_CONFIG file, %s", err) + fmt.Errorf("Error reading INVENTORY_API_CONFIG file, %s", err) } } else { home, err := os.UserHomeDir() @@ -134,7 +134,7 @@ func initConfig() { if err := viper.ReadInConfig(); err != nil { panic(err) } else { - //logger.Infof("Using config file: %s", viper.ConfigFileUsed()) + fmt.Printf("Using config file: %s", viper.ConfigFileUsed()) } // put the values into the options struct. From e2931b53ee02619d417effa226c79dcb756a742a Mon Sep 17 00:00:00 2001 From: Adam0Brien Date: Wed, 25 Sep 2024 16:35:38 +0100 Subject: [PATCH 04/11] Replace logs with prints --- .inventory-api.yaml | 2 ++ cmd/.inventory-api.yaml | 1 + cmd/root.go | 4 ++-- cmd/root_test.go | 1 + inventory-api-compose.yaml | 2 +- 5 files changed, 7 insertions(+), 3 deletions(-) create mode 120000 cmd/.inventory-api.yaml diff --git a/.inventory-api.yaml b/.inventory-api.yaml index 076302b7..d7a84417 100644 --- a/.inventory-api.yaml +++ b/.inventory-api.yaml @@ -21,3 +21,5 @@ storage: user: password: dbname: +log: + level: "info" diff --git a/cmd/.inventory-api.yaml b/cmd/.inventory-api.yaml new file mode 120000 index 00000000..61a6f04e --- /dev/null +++ b/cmd/.inventory-api.yaml @@ -0,0 +1 @@ +../.inventory-api.yaml \ No newline at end of file diff --git a/cmd/root.go b/cmd/root.go index 4795d404..20d9bda7 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -109,12 +109,12 @@ func initConfig() { if configFilePath, exists := os.LookupEnv("INVENTORY_API_CONFIG"); exists { absPath, err := filepath.Abs(configFilePath) if err != nil { - fmt.Errorf("Failed to resolve absolute path for config file: %v", err) + fmt.Printf("Failed to resolve absolute path for config file: %v", err) } // Set the config file path viper.SetConfigFile(absPath) if err := viper.ReadInConfig(); err != nil { - fmt.Errorf("Error reading INVENTORY_API_CONFIG file, %s", err) + fmt.Printf("Error reading INVENTORY_API_CONFIG file, %s", err) } } else { home, err := os.UserHomeDir() diff --git a/cmd/root_test.go b/cmd/root_test.go index 91d3e708..b0fb25ee 100644 --- a/cmd/root_test.go +++ b/cmd/root_test.go @@ -53,6 +53,7 @@ func TestRootCommand(t *testing.T) { assertCommandCalled(t, command, mocked) assert.Equal(t, "postgres", viper.GetString("storage.database")) + assert.Equal(t, "info", viper.GetString("log.level")) }) } } diff --git a/inventory-api-compose.yaml b/inventory-api-compose.yaml index 87ef0284..59e0f58e 100644 --- a/inventory-api-compose.yaml +++ b/inventory-api-compose.yaml @@ -26,4 +26,4 @@ storage: password: "yPsw5e6ab4bvAGe5H" dbname: "spicedb" log: - level: "info" + level: "warn" From b54babca0b1eb2382f863a2d9eaf16d930c73827 Mon Sep 17 00:00:00 2001 From: Adam0Brien Date: Thu, 26 Sep 2024 16:08:59 +0100 Subject: [PATCH 05/11] Add inventory-api to cmd folder --- cmd/.inventory-api.yaml | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) mode change 120000 => 100644 cmd/.inventory-api.yaml diff --git a/cmd/.inventory-api.yaml b/cmd/.inventory-api.yaml deleted file mode 120000 index 61a6f04e..00000000 --- a/cmd/.inventory-api.yaml +++ /dev/null @@ -1 +0,0 @@ -../.inventory-api.yaml \ No newline at end of file diff --git a/cmd/.inventory-api.yaml b/cmd/.inventory-api.yaml new file mode 100644 index 00000000..d7a84417 --- /dev/null +++ b/cmd/.inventory-api.yaml @@ -0,0 +1,25 @@ +server: + public_url: http://localhost:8081 + http: + address: localhost:8081 + grpc: + address: localhost:9081 +authn: + allow-unauthenticated: true +authz: + impl: allow-all +eventing: + eventer: stdout + kafka: +storage: + database: sqlite3 + sqlite3: + dsn: inventory.db + postgres: + host: "localhost" + port: "5432" + user: + password: + dbname: +log: + level: "info" From df96212b8dc783e83b70245be8ad8b2cd2a38923 Mon Sep 17 00:00:00 2001 From: Adam0Brien Date: Thu, 26 Sep 2024 16:48:28 +0100 Subject: [PATCH 06/11] Add toggle for livez logs --- .inventory-api.yaml | 1 + cmd/.inventory-api.yaml | 1 + go.mod | 6 ++++++ go.sum | 10 ++++++++++ internal/service/health/health.go | 5 ++++- inventory-api-compose.yaml | 4 +++- sso-inventory-api.yaml | 3 ++- 7 files changed, 27 insertions(+), 3 deletions(-) diff --git a/.inventory-api.yaml b/.inventory-api.yaml index d7a84417..7db2e31b 100644 --- a/.inventory-api.yaml +++ b/.inventory-api.yaml @@ -23,3 +23,4 @@ storage: dbname: log: level: "info" + livez: false diff --git a/cmd/.inventory-api.yaml b/cmd/.inventory-api.yaml index d7a84417..7db2e31b 100644 --- a/cmd/.inventory-api.yaml +++ b/cmd/.inventory-api.yaml @@ -23,3 +23,4 @@ storage: dbname: log: level: "info" + livez: false diff --git a/go.mod b/go.mod index 8f6def13..74dc9eb3 100644 --- a/go.mod +++ b/go.mod @@ -45,6 +45,7 @@ require ( github.com/containerd/platforms v0.2.1 // indirect github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect github.com/docker/docker v27.2.0+incompatible // indirect + github.com/envoyproxy/protoc-gen-validate v1.1.0 // indirect github.com/fsnotify/fsnotify v1.7.0 // indirect github.com/go-jose/go-jose/v4 v4.0.2 // indirect github.com/go-kratos/aegis v0.2.0 // indirect @@ -55,6 +56,7 @@ require ( github.com/gorilla/mux v1.8.1 // indirect github.com/grpc-ecosystem/go-grpc-middleware v1.4.0 // indirect github.com/hashicorp/hcl v1.0.0 // indirect + github.com/iancoleman/strcase v0.3.0 // indirect github.com/inconshreveable/mousetrap v1.1.0 // indirect github.com/jackc/pgpassfile v1.0.0 // indirect github.com/jackc/pgservicefile v0.0.0-20231201235250-de7065d80cb9 // indirect @@ -64,6 +66,7 @@ require ( github.com/jinzhu/now v1.1.5 // indirect github.com/json-iterator/go v1.1.12 // indirect github.com/klauspost/compress v1.17.9 // indirect + github.com/lyft/protoc-gen-star/v2 v2.0.4-0.20230330145011-496ad1ac90a4 // indirect github.com/magiconair/properties v1.8.7 // indirect github.com/mattn/go-sqlite3 v1.14.22 // indirect github.com/mitchellh/mapstructure v1.5.0 // indirect @@ -92,11 +95,14 @@ require ( go.uber.org/zap v1.21.0 // indirect golang.org/x/crypto v0.27.0 // indirect golang.org/x/exp v0.0.0-20240325151524-a685a6edb6d8 // indirect + golang.org/x/mod v0.17.0 // indirect golang.org/x/net v0.29.0 // indirect golang.org/x/oauth2 v0.22.0 // indirect golang.org/x/sync v0.8.0 // indirect golang.org/x/sys v0.25.0 // indirect golang.org/x/text v0.18.0 // indirect + golang.org/x/tools v0.21.1-0.20240508182429-e35e4ccd0d2d // indirect google.golang.org/genproto/googleapis/rpc v0.0.0-20240903143218-8af14fe29dc1 // indirect + google.golang.org/grpc/cmd/protoc-gen-go-grpc v1.5.1 // indirect gopkg.in/ini.v1 v1.67.0 // indirect ) diff --git a/go.sum b/go.sum index 0697b5ac..b0c948a8 100644 --- a/go.sum +++ b/go.sum @@ -226,6 +226,8 @@ github.com/hashicorp/go-version v1.6.0 h1:feTTfFNnjP967rlCxM/I9g701jU+RN74YKx2mO github.com/hashicorp/go-version v1.6.0/go.mod h1:fltr4n8CU8Ke44wwGCBoEymUuxUHl09ZGVZPK5anwXA= github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4= github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ= +github.com/iancoleman/strcase v0.3.0 h1:nTXanmYxhfFAMjZL34Ov6gkzEsSJZ5DbhxWjvSASxEI= +github.com/iancoleman/strcase v0.3.0/go.mod h1:iwCmte+B7n89clKwxIoIXy/HfoL7AsD47ZCWhYzw7ho= github.com/imdario/mergo v0.3.16 h1:wwQJbIsHYGMUyLSPrEq1CT16AhnhNJQ51+4fdHUnCl4= github.com/imdario/mergo v0.3.16/go.mod h1:WBLT9ZmE3lPoWsEzCh9LPo3TiwVN+ZKEjmz+hD27ysY= github.com/in-toto/in-toto-golang v0.5.0 h1:hb8bgwr0M2hGdDsLjkJ3ZqJ8JFLL/tgYdAxF/XEFBbY= @@ -268,6 +270,8 @@ github.com/kylelemons/godebug v1.1.0 h1:RPNrshWIDI6G2gRW9EHilWtl7Z6Sb1BR0xunSBf0 github.com/kylelemons/godebug v1.1.0/go.mod h1:9/0rRGxNHcop5bhtWyNeEfOS8JIWk580+fNqagV/RAw= github.com/lufia/plan9stats v0.0.0-20230326075908-cb1d2100619a h1:N9zuLhTvBSRt0gWSiJswwQ2HqDmtX/ZCDJURnKUt1Ik= github.com/lufia/plan9stats v0.0.0-20230326075908-cb1d2100619a/go.mod h1:JKx41uQRwqlTZabZc+kILPrO/3jlKnQ2Z8b7YiVw5cE= +github.com/lyft/protoc-gen-star/v2 v2.0.4-0.20230330145011-496ad1ac90a4 h1:sIXJOMrYnQZJu7OB7ANSF4MYri2fTEGIsRLz6LwI4xE= +github.com/lyft/protoc-gen-star/v2 v2.0.4-0.20230330145011-496ad1ac90a4/go.mod h1:amey7yeodaJhXSbf/TlLvWiqQfLOSpEk//mLlc+axEk= github.com/magiconair/properties v1.8.7 h1:IeQXZAiQcpL9mgcAe1Nu6cX9LLw6ExEHKjN0VQdvPDY= github.com/magiconair/properties v1.8.7/go.mod h1:Dhd985XPs7jluiymwWYZ0G4Z61jb3vdS329zhj2hYo0= github.com/mailru/easyjson v0.7.7 h1:UGYAvKxe3sBsEDzO8ZeWOSlIQfWFlxbzLZe7hwFURr0= @@ -520,6 +524,8 @@ golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91 golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs= golang.org/x/mod v0.12.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs= golang.org/x/mod v0.14.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c= +golang.org/x/mod v0.17.0 h1:zY54UmvipHiNd+pm+m0x9KhZ9hl1/7QNMyxXbc6ICqA= +golang.org/x/mod v0.17.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c= golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20190213061140-3a22650c66bd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= @@ -603,6 +609,8 @@ golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU= golang.org/x/tools v0.13.0/go.mod h1:HvlwmtVNQAhOuCjW7xxvovg8wbNq7LwfXh/k7wXUl58= golang.org/x/tools v0.17.0/go.mod h1:xsh6VxdV005rRVaS6SSAf9oiAqljS7UZUacMZ8Bnsps= +golang.org/x/tools v0.21.1-0.20240508182429-e35e4ccd0d2d h1:vU5i/LfpvrRCpgM/VPfJLg5KjxD3E+hfT1SH+d9zLwg= +golang.org/x/tools v0.21.1-0.20240508182429-e35e4ccd0d2d/go.mod h1:aiJjzUbINMkxbQROHiO6hDPo2LHcIPhhQsa9DLh0yGk= golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= @@ -625,6 +633,8 @@ google.golang.org/grpc v1.27.0/go.mod h1:qbnxyOmOxrQa7FizSgH+ReBfzJrCY1pSN7KXBS8 google.golang.org/grpc v1.29.1/go.mod h1:itym6AZVZYACWQqET3MqgPpjcuV5QH3BxFS3IjizoKk= google.golang.org/grpc v1.66.2 h1:3QdXkuq3Bkh7w+ywLdLvM56cmGvQHUMZpiCzt6Rqaoo= google.golang.org/grpc v1.66.2/go.mod h1:s3/l6xSSCURdVfAnL+TqCNMyTDAGN6+lZeVxnZR128Y= +google.golang.org/grpc/cmd/protoc-gen-go-grpc v1.5.1 h1:F29+wU6Ee6qgu9TddPgooOdaqsxTMunOoj8KA5yuS5A= +google.golang.org/grpc/cmd/protoc-gen-go-grpc v1.5.1/go.mod h1:5KF+wpkbTSbGcR9zteSqZV6fqFOWBl4Yde8En8MryZA= google.golang.org/protobuf v1.34.2 h1:6xV6lTsCfpGD21XK49h7MhtcApnLqkfYgPcdHftf6hg= google.golang.org/protobuf v1.34.2/go.mod h1:qYOHts0dSfpeUzUFpOMr/WGzszTmLH+DiWniOlNbLDw= gopkg.in/cenkalti/backoff.v1 v1.1.0 h1:Arh75ttbsvlpVA7WtVpH4u9h6Zl46xuptxqLxPiSo4Y= diff --git a/internal/service/health/health.go b/internal/service/health/health.go index 9033e6bd..e0187849 100644 --- a/internal/service/health/health.go +++ b/internal/service/health/health.go @@ -6,6 +6,7 @@ import ( pb "github.com/project-kessel/inventory-api/api/kessel/inventory/v1" "github.com/project-kessel/inventory-api/internal/authz" biz "github.com/project-kessel/inventory-api/internal/biz/health" + "github.com/spf13/viper" ) type HealthService struct { @@ -21,7 +22,9 @@ func New(c *biz.HealthUsecase) *HealthService { } func (s *HealthService) GetLivez(ctx context.Context, req *pb.GetLivezRequest) (*pb.GetLivezResponse, error) { - log.Infof("Performing livez check") + if viper.GetBool("log.livez") { + log.Infof("Performing livez check") + } return &pb.GetLivezResponse{ Status: "OK", Code: 200, diff --git a/inventory-api-compose.yaml b/inventory-api-compose.yaml index 59e0f58e..c8478e42 100644 --- a/inventory-api-compose.yaml +++ b/inventory-api-compose.yaml @@ -26,4 +26,6 @@ storage: password: "yPsw5e6ab4bvAGe5H" dbname: "spicedb" log: - level: "warn" + level: "info" + livez: false + diff --git a/sso-inventory-api.yaml b/sso-inventory-api.yaml index 6ad9ed6a..cd70969b 100644 --- a/sso-inventory-api.yaml +++ b/sso-inventory-api.yaml @@ -24,4 +24,5 @@ storage: password: "yPsw5e6ab4bvAGe5H" dbname: "spicedb" log: - level: "warn" \ No newline at end of file + level: "info" + livez: false \ No newline at end of file From db239cf26c14e1cde05f08b6e8d7a1a4b9406128 Mon Sep 17 00:00:00 2001 From: Adam0Brien Date: Thu, 26 Sep 2024 22:36:23 +0100 Subject: [PATCH 07/11] Add toggle for readyz logs --- cmd/.inventory-api.yaml | 1 + go.mod | 6 ------ go.sum | 10 ---------- internal/authz/kessel/kessel.go | 5 ++++- internal/data/health/health.go | 6 +++++- internal/service/health/health.go | 4 +++- inventory-api-compose.yaml | 2 ++ sso-inventory-api.yaml | 4 +++- 8 files changed, 18 insertions(+), 20 deletions(-) diff --git a/cmd/.inventory-api.yaml b/cmd/.inventory-api.yaml index 7db2e31b..556862e4 100644 --- a/cmd/.inventory-api.yaml +++ b/cmd/.inventory-api.yaml @@ -24,3 +24,4 @@ storage: log: level: "info" livez: false + readyz: false diff --git a/go.mod b/go.mod index 74dc9eb3..8f6def13 100644 --- a/go.mod +++ b/go.mod @@ -45,7 +45,6 @@ require ( github.com/containerd/platforms v0.2.1 // indirect github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect github.com/docker/docker v27.2.0+incompatible // indirect - github.com/envoyproxy/protoc-gen-validate v1.1.0 // indirect github.com/fsnotify/fsnotify v1.7.0 // indirect github.com/go-jose/go-jose/v4 v4.0.2 // indirect github.com/go-kratos/aegis v0.2.0 // indirect @@ -56,7 +55,6 @@ require ( github.com/gorilla/mux v1.8.1 // indirect github.com/grpc-ecosystem/go-grpc-middleware v1.4.0 // indirect github.com/hashicorp/hcl v1.0.0 // indirect - github.com/iancoleman/strcase v0.3.0 // indirect github.com/inconshreveable/mousetrap v1.1.0 // indirect github.com/jackc/pgpassfile v1.0.0 // indirect github.com/jackc/pgservicefile v0.0.0-20231201235250-de7065d80cb9 // indirect @@ -66,7 +64,6 @@ require ( github.com/jinzhu/now v1.1.5 // indirect github.com/json-iterator/go v1.1.12 // indirect github.com/klauspost/compress v1.17.9 // indirect - github.com/lyft/protoc-gen-star/v2 v2.0.4-0.20230330145011-496ad1ac90a4 // indirect github.com/magiconair/properties v1.8.7 // indirect github.com/mattn/go-sqlite3 v1.14.22 // indirect github.com/mitchellh/mapstructure v1.5.0 // indirect @@ -95,14 +92,11 @@ require ( go.uber.org/zap v1.21.0 // indirect golang.org/x/crypto v0.27.0 // indirect golang.org/x/exp v0.0.0-20240325151524-a685a6edb6d8 // indirect - golang.org/x/mod v0.17.0 // indirect golang.org/x/net v0.29.0 // indirect golang.org/x/oauth2 v0.22.0 // indirect golang.org/x/sync v0.8.0 // indirect golang.org/x/sys v0.25.0 // indirect golang.org/x/text v0.18.0 // indirect - golang.org/x/tools v0.21.1-0.20240508182429-e35e4ccd0d2d // indirect google.golang.org/genproto/googleapis/rpc v0.0.0-20240903143218-8af14fe29dc1 // indirect - google.golang.org/grpc/cmd/protoc-gen-go-grpc v1.5.1 // indirect gopkg.in/ini.v1 v1.67.0 // indirect ) diff --git a/go.sum b/go.sum index b0c948a8..0697b5ac 100644 --- a/go.sum +++ b/go.sum @@ -226,8 +226,6 @@ github.com/hashicorp/go-version v1.6.0 h1:feTTfFNnjP967rlCxM/I9g701jU+RN74YKx2mO github.com/hashicorp/go-version v1.6.0/go.mod h1:fltr4n8CU8Ke44wwGCBoEymUuxUHl09ZGVZPK5anwXA= github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4= github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ= -github.com/iancoleman/strcase v0.3.0 h1:nTXanmYxhfFAMjZL34Ov6gkzEsSJZ5DbhxWjvSASxEI= -github.com/iancoleman/strcase v0.3.0/go.mod h1:iwCmte+B7n89clKwxIoIXy/HfoL7AsD47ZCWhYzw7ho= github.com/imdario/mergo v0.3.16 h1:wwQJbIsHYGMUyLSPrEq1CT16AhnhNJQ51+4fdHUnCl4= github.com/imdario/mergo v0.3.16/go.mod h1:WBLT9ZmE3lPoWsEzCh9LPo3TiwVN+ZKEjmz+hD27ysY= github.com/in-toto/in-toto-golang v0.5.0 h1:hb8bgwr0M2hGdDsLjkJ3ZqJ8JFLL/tgYdAxF/XEFBbY= @@ -270,8 +268,6 @@ github.com/kylelemons/godebug v1.1.0 h1:RPNrshWIDI6G2gRW9EHilWtl7Z6Sb1BR0xunSBf0 github.com/kylelemons/godebug v1.1.0/go.mod h1:9/0rRGxNHcop5bhtWyNeEfOS8JIWk580+fNqagV/RAw= github.com/lufia/plan9stats v0.0.0-20230326075908-cb1d2100619a h1:N9zuLhTvBSRt0gWSiJswwQ2HqDmtX/ZCDJURnKUt1Ik= github.com/lufia/plan9stats v0.0.0-20230326075908-cb1d2100619a/go.mod h1:JKx41uQRwqlTZabZc+kILPrO/3jlKnQ2Z8b7YiVw5cE= -github.com/lyft/protoc-gen-star/v2 v2.0.4-0.20230330145011-496ad1ac90a4 h1:sIXJOMrYnQZJu7OB7ANSF4MYri2fTEGIsRLz6LwI4xE= -github.com/lyft/protoc-gen-star/v2 v2.0.4-0.20230330145011-496ad1ac90a4/go.mod h1:amey7yeodaJhXSbf/TlLvWiqQfLOSpEk//mLlc+axEk= github.com/magiconair/properties v1.8.7 h1:IeQXZAiQcpL9mgcAe1Nu6cX9LLw6ExEHKjN0VQdvPDY= github.com/magiconair/properties v1.8.7/go.mod h1:Dhd985XPs7jluiymwWYZ0G4Z61jb3vdS329zhj2hYo0= github.com/mailru/easyjson v0.7.7 h1:UGYAvKxe3sBsEDzO8ZeWOSlIQfWFlxbzLZe7hwFURr0= @@ -524,8 +520,6 @@ golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91 golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs= golang.org/x/mod v0.12.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs= golang.org/x/mod v0.14.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c= -golang.org/x/mod v0.17.0 h1:zY54UmvipHiNd+pm+m0x9KhZ9hl1/7QNMyxXbc6ICqA= -golang.org/x/mod v0.17.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c= golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20190213061140-3a22650c66bd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= @@ -609,8 +603,6 @@ golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU= golang.org/x/tools v0.13.0/go.mod h1:HvlwmtVNQAhOuCjW7xxvovg8wbNq7LwfXh/k7wXUl58= golang.org/x/tools v0.17.0/go.mod h1:xsh6VxdV005rRVaS6SSAf9oiAqljS7UZUacMZ8Bnsps= -golang.org/x/tools v0.21.1-0.20240508182429-e35e4ccd0d2d h1:vU5i/LfpvrRCpgM/VPfJLg5KjxD3E+hfT1SH+d9zLwg= -golang.org/x/tools v0.21.1-0.20240508182429-e35e4ccd0d2d/go.mod h1:aiJjzUbINMkxbQROHiO6hDPo2LHcIPhhQsa9DLh0yGk= golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= @@ -633,8 +625,6 @@ google.golang.org/grpc v1.27.0/go.mod h1:qbnxyOmOxrQa7FizSgH+ReBfzJrCY1pSN7KXBS8 google.golang.org/grpc v1.29.1/go.mod h1:itym6AZVZYACWQqET3MqgPpjcuV5QH3BxFS3IjizoKk= google.golang.org/grpc v1.66.2 h1:3QdXkuq3Bkh7w+ywLdLvM56cmGvQHUMZpiCzt6Rqaoo= google.golang.org/grpc v1.66.2/go.mod h1:s3/l6xSSCURdVfAnL+TqCNMyTDAGN6+lZeVxnZR128Y= -google.golang.org/grpc/cmd/protoc-gen-go-grpc v1.5.1 h1:F29+wU6Ee6qgu9TddPgooOdaqsxTMunOoj8KA5yuS5A= -google.golang.org/grpc/cmd/protoc-gen-go-grpc v1.5.1/go.mod h1:5KF+wpkbTSbGcR9zteSqZV6fqFOWBl4Yde8En8MryZA= google.golang.org/protobuf v1.34.2 h1:6xV6lTsCfpGD21XK49h7MhtcApnLqkfYgPcdHftf6hg= google.golang.org/protobuf v1.34.2/go.mod h1:qYOHts0dSfpeUzUFpOMr/WGzszTmLH+DiWniOlNbLDw= gopkg.in/cenkalti/backoff.v1 v1.1.0 h1:Arh75ttbsvlpVA7WtVpH4u9h6Zl46xuptxqLxPiSo4Y= diff --git a/internal/authz/kessel/kessel.go b/internal/authz/kessel/kessel.go index 94fe7760..a80b3fa6 100644 --- a/internal/authz/kessel/kessel.go +++ b/internal/authz/kessel/kessel.go @@ -3,6 +3,7 @@ package kessel import ( "context" "fmt" + "github.com/spf13/viper" "github.com/go-kratos/kratos/v2/log" authzapi "github.com/project-kessel/inventory-api/internal/authz/api" @@ -24,7 +25,9 @@ func (a *KesselAuthz) Health(ctx context.Context) (*kesselv1.GetReadyzResponse, if err != nil { return nil, err } - log.Infof("Checking relations-api readyz endpoint") + if viper.GetBool("log.readyz") { + log.Infof("Checking relations-api readyz endpoint") + } return a.HealthService.GetReadyz(ctx, &kesselv1.GetReadyzRequest{}, opts...) } diff --git a/internal/data/health/health.go b/internal/data/health/health.go index c53f61ee..48a241b5 100644 --- a/internal/data/health/health.go +++ b/internal/data/health/health.go @@ -6,6 +6,7 @@ import ( pb "github.com/project-kessel/inventory-api/api/kessel/inventory/v1" "github.com/project-kessel/inventory-api/internal/authz" authzapi "github.com/project-kessel/inventory-api/internal/authz/api" + "github.com/spf13/viper" "gorm.io/gorm" ) @@ -46,9 +47,12 @@ func (r *healthRepo) IsBackendAvailable(ctx context.Context) (*pb.GetReadyzRespo return newResponse("RELATIONS-API UNHEALTHY", 500), nil } if authz.CheckAuthorizer(r.CompletedAuth) == "Kessel" { - log.Infof("Storage type %s and relations-api %s", storageType, health.GetStatus()) + if viper.GetBool("log.readyz") { + log.Infof("Storage type %s and relations-api %s", storageType, health.GetStatus()) + } return newResponse("STORAGE "+storageType+" and RELATIONS-API", 200), nil } + return newResponse("Storage type "+storageType, 200), nil } diff --git a/internal/service/health/health.go b/internal/service/health/health.go index e0187849..36438c2e 100644 --- a/internal/service/health/health.go +++ b/internal/service/health/health.go @@ -32,6 +32,8 @@ func (s *HealthService) GetLivez(ctx context.Context, req *pb.GetLivezRequest) ( } func (s *HealthService) GetReadyz(ctx context.Context, req *pb.GetReadyzRequest) (*pb.GetReadyzResponse, error) { - + if viper.GetBool("log.readyz") { + log.Infof("Performing readyz check") + } return s.Ctl.IsBackendAvailable(ctx) } diff --git a/inventory-api-compose.yaml b/inventory-api-compose.yaml index c8478e42..298ad49f 100644 --- a/inventory-api-compose.yaml +++ b/inventory-api-compose.yaml @@ -28,4 +28,6 @@ storage: log: level: "info" livez: false + readyz: false + diff --git a/sso-inventory-api.yaml b/sso-inventory-api.yaml index cd70969b..b7bbfe16 100644 --- a/sso-inventory-api.yaml +++ b/sso-inventory-api.yaml @@ -25,4 +25,6 @@ storage: dbname: "spicedb" log: level: "info" - livez: false \ No newline at end of file + livez: false + readyz: false + From 4a500ba3b5d9fdcef3904547a98c6ce00f8cec9d Mon Sep 17 00:00:00 2001 From: Adam0Brien Date: Mon, 30 Sep 2024 09:39:24 +0100 Subject: [PATCH 08/11] added logs to inform users livez/readyz are disabled --- .inventory-api.yaml | 4 +++- cmd/.inventory-api.yaml | 6 ++++-- internal/service/health/health.go | 4 ++++ inventory-api-compose.yaml | 4 ++-- sso-inventory-api.yaml | 4 ++-- 5 files changed, 15 insertions(+), 7 deletions(-) diff --git a/.inventory-api.yaml b/.inventory-api.yaml index 7db2e31b..ea9f8ab5 100644 --- a/.inventory-api.yaml +++ b/.inventory-api.yaml @@ -23,4 +23,6 @@ storage: dbname: log: level: "info" - livez: false + livez: true + readyz: true + diff --git a/cmd/.inventory-api.yaml b/cmd/.inventory-api.yaml index 556862e4..e36fd150 100644 --- a/cmd/.inventory-api.yaml +++ b/cmd/.inventory-api.yaml @@ -23,5 +23,7 @@ storage: dbname: log: level: "info" - livez: false - readyz: false + livez: true + readyz: true + + diff --git a/internal/service/health/health.go b/internal/service/health/health.go index 36438c2e..d01c24ae 100644 --- a/internal/service/health/health.go +++ b/internal/service/health/health.go @@ -24,6 +24,8 @@ func New(c *biz.HealthUsecase) *HealthService { func (s *HealthService) GetLivez(ctx context.Context, req *pb.GetLivezRequest) (*pb.GetLivezResponse, error) { if viper.GetBool("log.livez") { log.Infof("Performing livez check") + } else { + log.Infof("Livez logs disabled") } return &pb.GetLivezResponse{ Status: "OK", @@ -34,6 +36,8 @@ func (s *HealthService) GetLivez(ctx context.Context, req *pb.GetLivezRequest) ( func (s *HealthService) GetReadyz(ctx context.Context, req *pb.GetReadyzRequest) (*pb.GetReadyzResponse, error) { if viper.GetBool("log.readyz") { log.Infof("Performing readyz check") + } else { + log.Infof("Readyz logs disabled") } return s.Ctl.IsBackendAvailable(ctx) } diff --git a/inventory-api-compose.yaml b/inventory-api-compose.yaml index 298ad49f..d3c0d6e5 100644 --- a/inventory-api-compose.yaml +++ b/inventory-api-compose.yaml @@ -27,7 +27,7 @@ storage: dbname: "spicedb" log: level: "info" - livez: false - readyz: false + livez: true + readyz: true diff --git a/sso-inventory-api.yaml b/sso-inventory-api.yaml index b7bbfe16..08b0e195 100644 --- a/sso-inventory-api.yaml +++ b/sso-inventory-api.yaml @@ -25,6 +25,6 @@ storage: dbname: "spicedb" log: level: "info" - livez: false - readyz: false + livez: true + readyz: true From 54bb577391741ffde78c989af7ecacbb99bcd177 Mon Sep 17 00:00:00 2001 From: Adam0Brien Date: Mon, 30 Sep 2024 10:08:07 +0100 Subject: [PATCH 09/11] Ensure that readyz/livez disabled log is only shown once --- cmd/root_test.go | 2 ++ internal/service/health/health.go | 15 +++++++++++++-- inventory-api-compose.yaml | 1 - 3 files changed, 15 insertions(+), 3 deletions(-) diff --git a/cmd/root_test.go b/cmd/root_test.go index b0fb25ee..0148348b 100644 --- a/cmd/root_test.go +++ b/cmd/root_test.go @@ -54,6 +54,8 @@ func TestRootCommand(t *testing.T) { assertCommandCalled(t, command, mocked) assert.Equal(t, "postgres", viper.GetString("storage.database")) assert.Equal(t, "info", viper.GetString("log.level")) + //assert.Equal(t, true, viper.GetBool("log.livez")) + //assert.Equal(t, true, viper.GetBool("log.readyz")) }) } } diff --git a/internal/service/health/health.go b/internal/service/health/health.go index d01c24ae..d8e2c579 100644 --- a/internal/service/health/health.go +++ b/internal/service/health/health.go @@ -15,6 +15,11 @@ type HealthService struct { Config *authz.CompletedConfig } +var ( + livezLogged = false + readyzLogged = false +) + func New(c *biz.HealthUsecase) *HealthService { return &HealthService{ Ctl: c, @@ -25,7 +30,10 @@ func (s *HealthService) GetLivez(ctx context.Context, req *pb.GetLivezRequest) ( if viper.GetBool("log.livez") { log.Infof("Performing livez check") } else { - log.Infof("Livez logs disabled") + if !livezLogged { + log.Infof("Livez logs disabled") + livezLogged = true + } } return &pb.GetLivezResponse{ Status: "OK", @@ -37,7 +45,10 @@ func (s *HealthService) GetReadyz(ctx context.Context, req *pb.GetReadyzRequest) if viper.GetBool("log.readyz") { log.Infof("Performing readyz check") } else { - log.Infof("Readyz logs disabled") + if !readyzLogged { + log.Infof("Readyz logs disabled") + readyzLogged = true + } } return s.Ctl.IsBackendAvailable(ctx) } diff --git a/inventory-api-compose.yaml b/inventory-api-compose.yaml index d3c0d6e5..27f3f942 100644 --- a/inventory-api-compose.yaml +++ b/inventory-api-compose.yaml @@ -30,4 +30,3 @@ log: livez: true readyz: true - From 308df2862257d0cbdd58c82d67667cff01ce0aee Mon Sep 17 00:00:00 2001 From: Adam0Brien Date: Mon, 30 Sep 2024 11:25:48 +0100 Subject: [PATCH 10/11] Add back log.Fatalf --- cmd/root.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/cmd/root.go b/cmd/root.go index 20d9bda7..f8fa7e97 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -109,12 +109,12 @@ func initConfig() { if configFilePath, exists := os.LookupEnv("INVENTORY_API_CONFIG"); exists { absPath, err := filepath.Abs(configFilePath) if err != nil { - fmt.Printf("Failed to resolve absolute path for config file: %v", err) + log.Fatalf("Failed to resolve absolute path for config file: %v", err) } // Set the config file path viper.SetConfigFile(absPath) if err := viper.ReadInConfig(); err != nil { - fmt.Printf("Error reading INVENTORY_API_CONFIG file, %s", err) + log.Fatalf("Error reading INVENTORY_API_CONFIG file, %s", err) } } else { home, err := os.UserHomeDir() @@ -134,7 +134,7 @@ func initConfig() { if err := viper.ReadInConfig(); err != nil { panic(err) } else { - fmt.Printf("Using config file: %s", viper.ConfigFileUsed()) + log.Infof("Using config file: %s", viper.ConfigFileUsed()) } // put the values into the options struct. From e8e4bbe78207a9b2aef5970cedd3c01f90500b9d Mon Sep 17 00:00:00 2001 From: Adam0Brien Date: Mon, 30 Sep 2024 13:50:02 +0100 Subject: [PATCH 11/11] Add TODO for explicit initConfig call --- cmd/root.go | 2 ++ cmd/root_test.go | 4 ++-- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/cmd/root.go b/cmd/root.go index f8fa7e97..51e438a0 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -83,6 +83,8 @@ func init() { panic(err) } + // TODO: Find a cleaner approach than explicitly calling initConfig + // Here we are calling the initConfig to ensure that the log level can be pulled from the inventory configuration file initConfig() logLevel := getLogLevel() logger, baseLogger = initLogger(logLevel) diff --git a/cmd/root_test.go b/cmd/root_test.go index 0148348b..81b20d8a 100644 --- a/cmd/root_test.go +++ b/cmd/root_test.go @@ -54,8 +54,8 @@ func TestRootCommand(t *testing.T) { assertCommandCalled(t, command, mocked) assert.Equal(t, "postgres", viper.GetString("storage.database")) assert.Equal(t, "info", viper.GetString("log.level")) - //assert.Equal(t, true, viper.GetBool("log.livez")) - //assert.Equal(t, true, viper.GetBool("log.readyz")) + assert.Equal(t, true, viper.GetBool("log.livez")) + assert.Equal(t, true, viper.GetBool("log.readyz")) }) } }