Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RHCLOUD-35286: Ensure log verbosity can be adjusted #155

Merged
merged 12 commits into from
Oct 1, 2024
5 changes: 5 additions & 0 deletions .inventory-api.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,8 @@ storage:
user:
password:
dbname:
log:
level: "info"
livez: true
readyz: true

29 changes: 29 additions & 0 deletions cmd/.inventory-api.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
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"
livez: true
readyz: true


67 changes: 54 additions & 13 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"os"
"path/filepath"
"strings"
"strconv"

"github.com/spf13/cobra"
Expand All @@ -29,16 +30,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,
Expand Down Expand Up @@ -98,14 +92,20 @@ func init() {
options.Storage.Postgres.Password = clowder.LoadedConfig.Database.Password
options.Storage.Postgres.DbName = clowder.LoadedConfig.Database.Name
}

migrateCmd := migrate.NewCommand(options.Storage, log.With(rootLog, "group", "storage"))

// 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()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why call initConfig?

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 {
Expand Down Expand Up @@ -146,11 +146,52 @@ func initConfig() {
if err := viper.ReadInConfig(); err != nil {
panic(err)
} else {
logger.Infof("Using config file: %s", viper.ConfigFileUsed())
log.Infof("Using config file: %s", viper.ConfigFileUsed())
}

// put the values into the options struct.
if err := viper.Unmarshal(&options); err != nil {
panic(err)
}
}

func getLogLevel() string {

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
}
3 changes: 3 additions & 0 deletions cmd/root_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ 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"))
})
}
}
Expand Down
5 changes: 4 additions & 1 deletion internal/authz/kessel/kessel.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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...)
}

Expand Down
6 changes: 5 additions & 1 deletion internal/data/health/health.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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
}

Expand Down
24 changes: 22 additions & 2 deletions internal/service/health/health.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -14,21 +15,40 @@ type HealthService struct {
Config *authz.CompletedConfig
}

var (
livezLogged = false
readyzLogged = false
)

func New(c *biz.HealthUsecase) *HealthService {
return &HealthService{
Ctl: c,
}
}

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")
} else {
if !livezLogged {
log.Infof("Livez logs disabled")
livezLogged = true
}
}
return &pb.GetLivezResponse{
Status: "OK",
Code: 200,
}, nil
}

func (s *HealthService) GetReadyz(ctx context.Context, req *pb.GetReadyzRequest) (*pb.GetReadyzResponse, error) {

if viper.GetBool("log.readyz") {
log.Infof("Performing readyz check")
} else {
if !readyzLogged {
log.Infof("Readyz logs disabled")
readyzLogged = true
}
}
return s.Ctl.IsBackendAvailable(ctx)
}
5 changes: 5 additions & 0 deletions inventory-api-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,8 @@ storage:
user: "postgres"
password: "yPsw5e6ab4bvAGe5H"
dbname: "spicedb"
log:
level: "info"
Adam0Brien marked this conversation as resolved.
Show resolved Hide resolved
livez: true
readyz: true

5 changes: 5 additions & 0 deletions sso-inventory-api.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,8 @@ storage:
user: "postgres"
password: "yPsw5e6ab4bvAGe5H"
dbname: "spicedb"
log:
level: "info"
livez: true
readyz: true

Loading