Skip to content

Commit eb5d8eb

Browse files
authored
feat: robust logging (#289)
* feat: robust logging On-behalf-of: @SAP [email protected] Signed-off-by: Artem Shcherbatiuk <[email protected]>
1 parent abca747 commit eb5d8eb

33 files changed

+455
-832
lines changed

.testcoverage.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ exclude:
66
- common/config/config.go
77
- mocks
88
- common/apis/*
9+
- export_test.go
10+
- export_test_integration.go
911
# remove it later:
1012
- listener/reconciler/clusteraccess/subroutines.go
11-
- listener/reconciler/singlecluster
13+

cmd/gateway.go

Lines changed: 9 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -8,55 +8,50 @@ import (
88
"time"
99

1010
openmfpcontext "github.com/openmfp/golang-commons/context"
11+
"github.com/openmfp/golang-commons/logger"
1112
"github.com/openmfp/golang-commons/sentry"
1213
"github.com/openmfp/golang-commons/traces"
1314
"github.com/prometheus/client_golang/prometheus/promhttp"
1415
"github.com/spf13/cobra"
1516
ctrl "sigs.k8s.io/controller-runtime"
1617

17-
"github.com/openmfp/golang-commons/logger"
18-
1918
"github.com/openmfp/kubernetes-graphql-gateway/gateway/manager"
2019
)
2120

2221
var gatewayCmd = &cobra.Command{
2322
Use: "gateway",
2423
Short: "Run the GQL Gateway",
2524
Example: "go run main.go gateway",
26-
RunE: func(_ *cobra.Command, _ []string) error {
27-
log, err := setupLogger(defaultCfg.Log.Level)
28-
if err != nil {
29-
return fmt.Errorf("failed to setup logger: %w", err)
30-
}
31-
32-
log.Info().Str("LogLevel", log.GetLevel().String()).Msg("Starting gateway server...")
25+
Run: func(_ *cobra.Command, _ []string) {
26+
log.Info().Str("LogLevel", log.GetLevel().String()).Msg("Starting the Gateway...")
3327

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

3731
if err := initializeSentry(ctx, log); err != nil {
38-
return err
32+
log.Fatal().Err(err).Msg("Failed to initialize Sentry")
3933
}
4034

4135
ctrl.SetLogger(log.Logr())
4236

4337
gatewayInstance, err := manager.NewGateway(ctx, log, appCfg)
4438
if err != nil {
45-
log.Error().Err(err).Msg("Error creating gateway")
46-
return fmt.Errorf("failed to create gateway: %w", err)
39+
log.Fatal().Err(err).Msg("Failed to create gateway")
4740
}
4841

4942
tracingShutdown, err := initializeTracing(ctx, log)
5043
if err != nil {
51-
return err
44+
log.Fatal().Err(err).Msg("Failed to initialize tracing")
5245
}
5346
defer func() {
5447
if err := tracingShutdown(ctx); err != nil {
5548
log.Error().Err(err).Msg("failed to shutdown TracerProvider")
5649
}
5750
}()
5851

59-
return runServers(ctx, log, gatewayInstance)
52+
if err := runServers(ctx, log, gatewayInstance); err != nil {
53+
log.Fatal().Err(err).Msg("Failed to run servers")
54+
}
6055
},
6156
}
6257

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

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

9184
shutdown, err := traces.InitLocalProvider(ctx, defaultCfg.Tracing.Collector, false)
9285
if err != nil {
9386
log.Fatal().Err(err).Msg("unable to start local TracerProvider")
94-
return nil, err
9587
}
9688
return shutdown, nil
9789
}
@@ -189,11 +181,3 @@ func runServers(ctx context.Context, log *logger.Logger, gatewayInstance http.Ha
189181
log.Info().Msg("Server shut down successfully")
190182
return nil
191183
}
192-
193-
// setupLogger initializes the logger with the given log level
194-
func setupLogger(logLevel string) (*logger.Logger, error) {
195-
loggerCfg := logger.DefaultConfig()
196-
loggerCfg.Name = "crdGateway"
197-
loggerCfg.Level = logLevel
198-
return logger.New(loggerCfg)
199-
}

cmd/listener.go

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package cmd
33
import (
44
"context"
55
"crypto/tls"
6-
"os"
76

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

2322
gatewayv1alpha1 "github.com/openmfp/kubernetes-graphql-gateway/common/apis/v1alpha1"
23+
"github.com/openmfp/kubernetes-graphql-gateway/listener/pkg/apischema"
24+
"github.com/openmfp/kubernetes-graphql-gateway/listener/pkg/workspacefile"
2425
"github.com/openmfp/kubernetes-graphql-gateway/listener/reconciler"
2526
"github.com/openmfp/kubernetes-graphql-gateway/listener/reconciler/clusteraccess"
2627
"github.com/openmfp/kubernetes-graphql-gateway/listener/reconciler/kcp"
@@ -74,6 +75,8 @@ var listenCmd = &cobra.Command{
7475
}
7576
},
7677
Run: func(cmd *cobra.Command, args []string) {
78+
log.Info().Str("LogLevel", log.GetLevel().String()).Msg("Starting the Listener...")
79+
7780
ctx := ctrl.SetupSignalHandler()
7881
restCfg := ctrl.GetConfigOrDie()
7982

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

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

114115
// Start virtual workspace watching if path is configured
115116
if appCfg.Listener.VirtualWorkspacesConfigPath != "" {
116117
go func() {
117118
if err := kcpReconciler.StartVirtualWorkspaceWatching(ctx, appCfg.Listener.VirtualWorkspacesConfigPath); err != nil {
118-
log.Error().Err(err).Msg("failed to start virtual workspace watching")
119-
os.Exit(1)
119+
log.Fatal().Err(err).Msg("failed to start virtual workspace watching")
120120
}
121121
}()
122122
}
123123

124124
reconcilerInstance = kcpReconciler
125125
} else {
126-
reconcilerInstance, err = clusteraccess.CreateMultiClusterReconciler(appCfg, reconcilerOpts, log)
126+
ioHandler, err := workspacefile.NewIOHandler(appCfg.OpenApiDefinitionsPath)
127+
if err != nil {
128+
log.Fatal().Err(err).Msg("unable to create IO handler")
129+
}
130+
131+
reconcilerInstance, err = clusteraccess.NewClusterAccessReconciler(ctx, appCfg, reconcilerOpts, ioHandler, apischema.NewResolver(log), log)
127132
if err != nil {
128-
log.Error().Err(err).Msg("unable to create cluster access reconciler")
129-
os.Exit(1)
133+
log.Fatal().Err(err).Msg("unable to create cluster access reconciler")
130134
}
131135
}
132136

133137
// Setup reconciler with its own manager and start everything
134138
if err := startManagerWithReconciler(ctx, reconcilerInstance); err != nil {
135-
os.Exit(1)
139+
log.Fatal().Err(err).Msg("failed to start manager with reconciler")
136140
}
137141
},
138142
}

cmd/root.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,14 @@ func initConfig() {
8181
v.SetDefault("gateway-url-graphql-suffix", "graphql")
8282
}
8383

84+
// setupLogger initializes the logger with the given log level
85+
func setupLogger(logLevel string) (*logger.Logger, error) {
86+
loggerCfg := logger.DefaultConfig()
87+
loggerCfg.Name = "crdGateway"
88+
loggerCfg.Level = logLevel
89+
return logger.New(loggerCfg)
90+
}
91+
8492
func Execute() {
8593
cobra.CheckErr(rootCmd.Execute())
8694
}

0 commit comments

Comments
 (0)