From 06eef002a0cddf6a1d4a5c05fdbf3a607a7a4968 Mon Sep 17 00:00:00 2001 From: Alex Creasy Date: Wed, 6 Nov 2024 14:29:03 +0000 Subject: [PATCH] WIP Signed-off-by: Alex Creasy --- clients/ui/bff/Makefile | 3 +- clients/ui/bff/cmd/main.go | 30 ++++++++++++++++++- clients/ui/bff/internal/api/middleware.go | 3 +- clients/ui/bff/internal/config/environment.go | 1 + clients/ui/bff/internal/integrations/http.go | 5 +++- 5 files changed, 38 insertions(+), 4 deletions(-) diff --git a/clients/ui/bff/Makefile b/clients/ui/bff/Makefile index b8e6ccdd..fba57655 100644 --- a/clients/ui/bff/Makefile +++ b/clients/ui/bff/Makefile @@ -7,6 +7,7 @@ DEV_MODE ?= false DEV_MODE_PORT ?= 8080 # ENVTEST_K8S_VERSION refers to the version of kubebuilder assets to be downloaded by envtest binary. ENVTEST_K8S_VERSION = 1.29.0 +LOG_LEVEL ?= info .PHONY: all all: build @@ -47,7 +48,7 @@ build: fmt vet test .PHONY: run run: fmt vet envtest ENVTEST_ASSETS="$(shell $(ENVTEST) use $(ENVTEST_K8S_VERSION) --bin-dir $(LOCALBIN) -p path)" \ - go run ./cmd/main.go --port=$(PORT) --mock-k8s-client=$(MOCK_K8S_CLIENT) --mock-mr-client=$(MOCK_MR_CLIENT) --dev-mode=$(DEV_MODE) --dev-mode-port=$(DEV_MODE_PORT) + go run ./cmd/main.go --port=$(PORT) --mock-k8s-client=$(MOCK_K8S_CLIENT) --mock-mr-client=$(MOCK_MR_CLIENT) --dev-mode=$(DEV_MODE) --dev-mode-port=$(DEV_MODE_PORT) --log-level=$(LOG_LEVEL) .PHONY: docker-build docker-build: diff --git a/clients/ui/bff/cmd/main.go b/clients/ui/bff/cmd/main.go index eb719229..08f16f38 100644 --- a/clients/ui/bff/cmd/main.go +++ b/clients/ui/bff/cmd/main.go @@ -5,6 +5,7 @@ import ( "flag" "fmt" "os/signal" + "strings" "syscall" "github.com/kubeflow/model-registry/ui/bff/internal/api" @@ -24,9 +25,12 @@ func main() { flag.BoolVar(&cfg.MockMRClient, "mock-mr-client", false, "Use mock Model Registry client") flag.BoolVar(&cfg.DevMode, "dev-mode", false, "Use development mode for access to local K8s cluster") flag.IntVar(&cfg.DevModePort, "dev-mode-port", getEnvAsInt("DEV_MODE_PORT", 8080), "Use port when in development mode") + flag.StringVar(&cfg.LogLevel, "log-level", getEnvAsString("LOG_LEVEL", "info"), "Sets server log level, possible values: debug, info, warn, error, fatal") flag.Parse() - logger := slog.New(slog.NewTextHandler(os.Stdout, nil)) + logger := slog.New(slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{ + Level: getLogLevelFromString(cfg.LogLevel), + })) app, err := api.NewApp(cfg, logger) if err != nil { @@ -86,3 +90,27 @@ func getEnvAsInt(name string, defaultVal int) int { } return defaultVal } + +func getEnvAsString(name string, defaultVal string) string { + if value, exists := os.LookupEnv(name); exists { + return value + } + return defaultVal +} + +func getLogLevelFromString(level string) slog.Level { + switch strings.ToLower(level) { + case "debug": + return slog.LevelDebug + case "info": + return slog.LevelInfo + case "warn": + return slog.LevelWarn + case "error": + return slog.LevelError + case "fatal": + return slog.LevelError + + } + return slog.LevelInfo +} diff --git a/clients/ui/bff/internal/api/middleware.go b/clients/ui/bff/internal/api/middleware.go index 64c5e595..219f4063 100644 --- a/clients/ui/bff/internal/api/middleware.go +++ b/clients/ui/bff/internal/api/middleware.go @@ -14,6 +14,7 @@ type contextKey string const httpClientKey contextKey = "httpClientKey" const userAccessToken = "x-forwarded-access-token" +const defaultLoggerKey contextKey = "defaultLoggerKey" func (app *App) RecoverPanic(next http.Handler) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { @@ -55,7 +56,7 @@ func (app *App) AttachRESTClient(handler func(http.ResponseWriter, *http.Request return } - client, err := integrations.NewHTTPClient(modelRegistryBaseURL, bearerToken) + client, err := integrations.NewHTTPClient(modelRegistryBaseURL, bearerToken, app.logger) if err != nil { app.serverErrorResponse(w, r, fmt.Errorf("failed to create Kubernetes client: %v", err)) return diff --git a/clients/ui/bff/internal/config/environment.go b/clients/ui/bff/internal/config/environment.go index f63dcce8..9d1ca74c 100644 --- a/clients/ui/bff/internal/config/environment.go +++ b/clients/ui/bff/internal/config/environment.go @@ -6,4 +6,5 @@ type EnvConfig struct { MockMRClient bool DevMode bool DevModePort int + LogLevel string } diff --git a/clients/ui/bff/internal/integrations/http.go b/clients/ui/bff/internal/integrations/http.go index 6d58c63a..9bbd3930 100644 --- a/clients/ui/bff/internal/integrations/http.go +++ b/clients/ui/bff/internal/integrations/http.go @@ -5,6 +5,7 @@ import ( "encoding/json" "fmt" "io" + "log/slog" "net/http" "strconv" ) @@ -19,6 +20,7 @@ type HTTPClient struct { client *http.Client baseURL string bearerToken string + logger *slog.Logger } type ErrorResponse struct { @@ -35,7 +37,7 @@ func (e *HTTPError) Error() string { return fmt.Sprintf("HTTP %d: %s - %s", e.StatusCode, e.Code, e.Message) } -func NewHTTPClient(baseURL string, bearerToken string) (HTTPClientInterface, error) { +func NewHTTPClient(baseURL string, bearerToken string, logger *slog.Logger) (HTTPClientInterface, error) { return &HTTPClient{ client: &http.Client{Transport: &http.Transport{ @@ -43,6 +45,7 @@ func NewHTTPClient(baseURL string, bearerToken string) (HTTPClientInterface, err }}, baseURL: baseURL, bearerToken: bearerToken, + logger: logger, }, nil }