Skip to content

feat: robust logging #289

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

Merged
merged 8 commits into from
Aug 5, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .testcoverage.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ exclude:
- common/config/config.go
- mocks
- common/apis/*
- export_test.go
- export_test_integration.go
# remove it later:
- listener/reconciler/clusteraccess/subroutines.go
- listener/reconciler/singlecluster

34 changes: 9 additions & 25 deletions cmd/gateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,55 +8,50 @@ import (
"time"

openmfpcontext "github.com/openmfp/golang-commons/context"
"github.com/openmfp/golang-commons/logger"
"github.com/openmfp/golang-commons/sentry"
"github.com/openmfp/golang-commons/traces"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/spf13/cobra"
ctrl "sigs.k8s.io/controller-runtime"

"github.com/openmfp/golang-commons/logger"

"github.com/openmfp/kubernetes-graphql-gateway/gateway/manager"
)

var gatewayCmd = &cobra.Command{
Use: "gateway",
Short: "Run the GQL Gateway",
Example: "go run main.go gateway",
RunE: func(_ *cobra.Command, _ []string) error {
log, err := setupLogger(defaultCfg.Log.Level)
if err != nil {
return fmt.Errorf("failed to setup logger: %w", err)
}

log.Info().Str("LogLevel", log.GetLevel().String()).Msg("Starting gateway server...")
Run: func(_ *cobra.Command, _ []string) {
log.Info().Str("LogLevel", log.GetLevel().String()).Msg("Starting the Gateway...")

ctx, _, shutdown := openmfpcontext.StartContext(log, appCfg, 1*time.Second)
defer shutdown()

if err := initializeSentry(ctx, log); err != nil {
return err
log.Fatal().Err(err).Msg("Failed to initialize Sentry")
}

ctrl.SetLogger(log.Logr())

gatewayInstance, err := manager.NewGateway(ctx, log, appCfg)
if err != nil {
log.Error().Err(err).Msg("Error creating gateway")
return fmt.Errorf("failed to create gateway: %w", err)
log.Fatal().Err(err).Msg("Failed to create gateway")
}

tracingShutdown, err := initializeTracing(ctx, log)
if err != nil {
return err
log.Fatal().Err(err).Msg("Failed to initialize tracing")
}
defer func() {
if err := tracingShutdown(ctx); err != nil {
log.Error().Err(err).Msg("failed to shutdown TracerProvider")
}
}()

return runServers(ctx, log, gatewayInstance)
if err := runServers(ctx, log, gatewayInstance); err != nil {
log.Fatal().Err(err).Msg("Failed to run servers")
}
},
}

Expand All @@ -71,7 +66,6 @@ func initializeSentry(ctx context.Context, log *logger.Logger) error {
)
if err != nil {
log.Fatal().Err(err).Msg("Sentry init failed")
return err
}

defer openmfpcontext.Recover(log)
Expand All @@ -83,15 +77,13 @@ func initializeTracing(ctx context.Context, log *logger.Logger) (func(ctx contex
shutdown, err := traces.InitProvider(ctx, defaultCfg.Tracing.Collector)
if err != nil {
log.Fatal().Err(err).Msg("unable to start gRPC-Sidecar TracerProvider")
return nil, err
}
return shutdown, nil
}

shutdown, err := traces.InitLocalProvider(ctx, defaultCfg.Tracing.Collector, false)
if err != nil {
log.Fatal().Err(err).Msg("unable to start local TracerProvider")
return nil, err
}
return shutdown, nil
}
Expand Down Expand Up @@ -189,11 +181,3 @@ func runServers(ctx context.Context, log *logger.Logger, gatewayInstance http.Ha
log.Info().Msg("Server shut down successfully")
return nil
}

// setupLogger initializes the logger with the given log level
func setupLogger(logLevel string) (*logger.Logger, error) {
loggerCfg := logger.DefaultConfig()
loggerCfg.Name = "crdGateway"
loggerCfg.Level = logLevel
return logger.New(loggerCfg)
}
26 changes: 15 additions & 11 deletions cmd/listener.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package cmd
import (
"context"
"crypto/tls"
"os"

kcpapis "github.com/kcp-dev/kcp/sdk/apis/apis/v1alpha1"
kcpcore "github.com/kcp-dev/kcp/sdk/apis/core/v1alpha1"
Expand All @@ -21,6 +20,8 @@ import (
"sigs.k8s.io/controller-runtime/pkg/webhook"

gatewayv1alpha1 "github.com/openmfp/kubernetes-graphql-gateway/common/apis/v1alpha1"
"github.com/openmfp/kubernetes-graphql-gateway/listener/pkg/apischema"
"github.com/openmfp/kubernetes-graphql-gateway/listener/pkg/workspacefile"
"github.com/openmfp/kubernetes-graphql-gateway/listener/reconciler"
"github.com/openmfp/kubernetes-graphql-gateway/listener/reconciler/clusteraccess"
"github.com/openmfp/kubernetes-graphql-gateway/listener/reconciler/kcp"
Expand Down Expand Up @@ -74,6 +75,8 @@ var listenCmd = &cobra.Command{
}
},
Run: func(cmd *cobra.Command, args []string) {
log.Info().Str("LogLevel", log.GetLevel().String()).Msg("Starting the Listener...")

ctx := ctrl.SetupSignalHandler()
restCfg := ctrl.GetConfigOrDie()

Expand All @@ -90,8 +93,7 @@ var listenCmd = &cobra.Command{
Scheme: scheme,
})
if err != nil {
log.Error().Err(err).Msg("failed to create client from config")
os.Exit(1)
log.Fatal().Err(err).Msg("failed to create client from config")
}

reconcilerOpts := reconciler.ReconcilerOpts{
Expand All @@ -107,32 +109,34 @@ var listenCmd = &cobra.Command{
if appCfg.EnableKcp {
kcpReconciler, err := kcp.NewKCPReconciler(appCfg, reconcilerOpts, log)
if err != nil {
log.Error().Err(err).Msg("unable to create KCP reconciler")
os.Exit(1)
log.Fatal().Err(err).Msg("unable to create KCP reconciler")
}

// Start virtual workspace watching if path is configured
if appCfg.Listener.VirtualWorkspacesConfigPath != "" {
go func() {
if err := kcpReconciler.StartVirtualWorkspaceWatching(ctx, appCfg.Listener.VirtualWorkspacesConfigPath); err != nil {
log.Error().Err(err).Msg("failed to start virtual workspace watching")
os.Exit(1)
log.Fatal().Err(err).Msg("failed to start virtual workspace watching")
}
}()
}

reconcilerInstance = kcpReconciler
} else {
reconcilerInstance, err = clusteraccess.CreateMultiClusterReconciler(appCfg, reconcilerOpts, log)
ioHandler, err := workspacefile.NewIOHandler(appCfg.OpenApiDefinitionsPath)
if err != nil {
log.Fatal().Err(err).Msg("unable to create IO handler")
}

reconcilerInstance, err = clusteraccess.NewClusterAccessReconciler(ctx, appCfg, reconcilerOpts, ioHandler, apischema.NewResolver(log), log)
if err != nil {
log.Error().Err(err).Msg("unable to create cluster access reconciler")
os.Exit(1)
log.Fatal().Err(err).Msg("unable to create cluster access reconciler")
}
}

// Setup reconciler with its own manager and start everything
if err := startManagerWithReconciler(ctx, reconcilerInstance); err != nil {
os.Exit(1)
log.Fatal().Err(err).Msg("failed to start manager with reconciler")
}
},
}
Expand Down
8 changes: 8 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,14 @@ func initConfig() {
v.SetDefault("gateway-url-graphql-suffix", "graphql")
}

// setupLogger initializes the logger with the given log level
func setupLogger(logLevel string) (*logger.Logger, error) {
loggerCfg := logger.DefaultConfig()
loggerCfg.Name = "crdGateway"
loggerCfg.Level = logLevel
return logger.New(loggerCfg)
}

func Execute() {
cobra.CheckErr(rootCmd.Execute())
}
Loading