diff --git a/cmd/config_init.go b/cmd/config_init.go index d56fab8a..8c8edd2a 100644 --- a/cmd/config_init.go +++ b/cmd/config_init.go @@ -1,7 +1,10 @@ package cmd import ( + "log" + "github.com/gatewayd-io/gatewayd/config" + "github.com/getsentry/sentry-go" "github.com/spf13/cobra" ) @@ -12,6 +15,24 @@ var configInitCmd = &cobra.Command{ Use: "init", Short: "Create or overwrite the GatewayD global config", Run: func(cmd *cobra.Command, args []string) { + // Enable Sentry. + if enableSentry { + // Initialize Sentry. + err := sentry.Init(sentry.ClientOptions{ + Dsn: DSN, + TracesSampleRate: config.DefaultTraceSampleRate, + AttachStacktrace: config.DefaultAttachStacktrace, + }) + if err != nil { + log.Fatal("Sentry initialization failed: ", err) + } + + // Flush buffered events before the program terminates. + defer sentry.Flush(config.DefaultFlushTimeout) + // Recover from panics and report the error to Sentry. + defer sentry.Recover() + } + generateConfig(cmd, Global, globalConfigFile, force) }, } @@ -25,4 +46,6 @@ func init() { &globalConfigFile, // Already exists in run.go "config", "c", config.GetDefaultConfigFilePath(config.GlobalConfigFilename), "Global config file") + configInitCmd.Flags().BoolVar( + &enableSentry, "sentry", true, "Enable Sentry") // Already exists in run.go } diff --git a/cmd/config_lint.go b/cmd/config_lint.go index 0691007a..bcd143e2 100644 --- a/cmd/config_lint.go +++ b/cmd/config_lint.go @@ -1,7 +1,10 @@ package cmd import ( + "log" + "github.com/gatewayd-io/gatewayd/config" + "github.com/getsentry/sentry-go" "github.com/spf13/cobra" ) @@ -10,6 +13,24 @@ var configLintCmd = &cobra.Command{ Use: "lint", Short: "Lint the GatewayD global config", Run: func(cmd *cobra.Command, args []string) { + // Enable Sentry. + if enableSentry { + // Initialize Sentry. + err := sentry.Init(sentry.ClientOptions{ + Dsn: DSN, + TracesSampleRate: config.DefaultTraceSampleRate, + AttachStacktrace: config.DefaultAttachStacktrace, + }) + if err != nil { + log.Fatal("Sentry initialization failed: ", err) + } + + // Flush buffered events before the program terminates. + defer sentry.Flush(config.DefaultFlushTimeout) + // Recover from panics and report the error to Sentry. + defer sentry.Recover() + } + lintConfig(cmd, Global, globalConfigFile) }, } @@ -21,4 +42,6 @@ func init() { &globalConfigFile, // Already exists in run.go "config", "c", config.GetDefaultConfigFilePath(config.GlobalConfigFilename), "Global config file") + configLintCmd.Flags().BoolVar( + &enableSentry, "sentry", true, "Enable Sentry") // Already exists in run.go } diff --git a/cmd/plugin_init.go b/cmd/plugin_init.go index 52566a63..38eead32 100644 --- a/cmd/plugin_init.go +++ b/cmd/plugin_init.go @@ -1,7 +1,10 @@ package cmd import ( + "log" + "github.com/gatewayd-io/gatewayd/config" + "github.com/getsentry/sentry-go" "github.com/spf13/cobra" ) @@ -10,6 +13,24 @@ var pluginInitCmd = &cobra.Command{ Use: "init", Short: "Create or overwrite the GatewayD plugins config", Run: func(cmd *cobra.Command, args []string) { + // Enable Sentry. + if enableSentry { + // Initialize Sentry. + err := sentry.Init(sentry.ClientOptions{ + Dsn: DSN, + TracesSampleRate: config.DefaultTraceSampleRate, + AttachStacktrace: config.DefaultAttachStacktrace, + }) + if err != nil { + log.Fatal("Sentry initialization failed: ", err) + } + + // Flush buffered events before the program terminates. + defer sentry.Flush(config.DefaultFlushTimeout) + // Recover from panics and report the error to Sentry. + defer sentry.Recover() + } + generateConfig(cmd, Plugins, pluginConfigFile, force) }, } @@ -23,4 +44,6 @@ func init() { &pluginConfigFile, // Already exists in run.go "plugin-config", "p", config.GetDefaultConfigFilePath(config.PluginsConfigFilename), "Plugin config file") + pluginInitCmd.Flags().BoolVar( + &enableSentry, "sentry", true, "Enable Sentry") // Already exists in run.go } diff --git a/cmd/plugin_install.go b/cmd/plugin_install.go index e872162c..ac06774c 100644 --- a/cmd/plugin_install.go +++ b/cmd/plugin_install.go @@ -17,6 +17,7 @@ import ( "github.com/codingsince1985/checksum" "github.com/gatewayd-io/gatewayd/config" + "github.com/getsentry/sentry-go" "github.com/google/go-github/v53/github" "github.com/spf13/cobra" "gopkg.in/yaml.v3" @@ -39,6 +40,24 @@ var pluginInstallCmd = &cobra.Command{ Short: "Install a plugin from a local or remote location", Example: " gatewayd plugin install github.com/gatewayd-io/gatewayd-plugin-cache@latest", Run: func(cmd *cobra.Command, args []string) { + // Enable Sentry. + if enableSentry { + // Initialize Sentry. + err := sentry.Init(sentry.ClientOptions{ + Dsn: DSN, + TracesSampleRate: config.DefaultTraceSampleRate, + AttachStacktrace: config.DefaultAttachStacktrace, + }) + if err != nil { + log.Fatal("Sentry initialization failed: ", err) + } + + // Flush buffered events before the program terminates. + defer sentry.Flush(config.DefaultFlushTimeout) + // Recover from panics and report the error to Sentry. + defer sentry.Recover() + } + // Validate the number of arguments. if len(args) < 1 { log.Fatal( @@ -403,4 +422,6 @@ func init() { &pluginOutputDir, "output-dir", "o", "./plugins", "Output directory for the plugin") pluginInstallCmd.Flags().BoolVarP( &pullOnly, "pull-only", "", false, "Only pull the plugin, don't install it") + pluginInstallCmd.Flags().BoolVar( + &enableSentry, "sentry", true, "Enable Sentry") // Already exists in run.go } diff --git a/cmd/plugin_lint.go b/cmd/plugin_lint.go index 6981deb2..a1eb745c 100644 --- a/cmd/plugin_lint.go +++ b/cmd/plugin_lint.go @@ -21,4 +21,6 @@ func init() { &pluginConfigFile, // Already exists in run.go "plugin-config", "p", config.GetDefaultConfigFilePath(config.PluginsConfigFilename), "Plugin config file") + pluginLintCmd.Flags().BoolVar( + &enableSentry, "sentry", true, "Enable Sentry") // Already exists in run.go } diff --git a/cmd/run.go b/cmd/run.go index 35459f27..6c756882 100644 --- a/cmd/run.go +++ b/cmd/run.go @@ -88,13 +88,13 @@ var runCmd = &cobra.Command{ // Initialize Sentry. err := sentry.Init(sentry.ClientOptions{ - Dsn: "https://e22f42dbb3e0433fbd9ea32453faa598@o4504550475038720.ingest.sentry.io/4504550481723392", + Dsn: DSN, TracesSampleRate: config.DefaultTraceSampleRate, AttachStacktrace: config.DefaultAttachStacktrace, }) if err != nil { span.RecordError(err) - log.Fatalf("sentry.Init: %s", err) + log.Fatal("Sentry initialization failed: ", err) } // Flush buffered events before the program terminates. diff --git a/cmd/utils.go b/cmd/utils.go index 16ec7e0d..e95d30a3 100644 --- a/cmd/utils.go +++ b/cmd/utils.go @@ -29,6 +29,8 @@ const ( var ( Global configFileType = "global" Plugins configFileType = "plugins" + + DSN = "https://e22f42dbb3e0433fbd9ea32453faa598@o4504550475038720.ingest.sentry.io/4504550481723392" ) // generateConfig generates a config file of the given type.