Skip to content

Commit

Permalink
Merge pull request #274 from gatewayd-io/add-sentry-to-all-commands
Browse files Browse the repository at this point in the history
Add Sentry to all commands
  • Loading branch information
mostafa authored Jun 7, 2023
2 parents bed36d2 + 8f8182f commit c4f6a3d
Show file tree
Hide file tree
Showing 7 changed files with 96 additions and 2 deletions.
23 changes: 23 additions & 0 deletions cmd/config_init.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package cmd

import (
"log"

"github.com/gatewayd-io/gatewayd/config"
"github.com/getsentry/sentry-go"
"github.com/spf13/cobra"
)

Expand All @@ -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)
},
}
Expand All @@ -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
}
23 changes: 23 additions & 0 deletions cmd/config_lint.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package cmd

import (
"log"

"github.com/gatewayd-io/gatewayd/config"
"github.com/getsentry/sentry-go"
"github.com/spf13/cobra"
)

Expand All @@ -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)
},
}
Expand All @@ -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
}
23 changes: 23 additions & 0 deletions cmd/plugin_init.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package cmd

import (
"log"

"github.com/gatewayd-io/gatewayd/config"
"github.com/getsentry/sentry-go"
"github.com/spf13/cobra"
)

Expand All @@ -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)
},
}
Expand All @@ -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
}
21 changes: 21 additions & 0 deletions cmd/plugin_install.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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(
Expand Down Expand Up @@ -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
}
2 changes: 2 additions & 0 deletions cmd/plugin_lint.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
4 changes: 2 additions & 2 deletions cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 2 additions & 0 deletions cmd/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit c4f6a3d

Please sign in to comment.