-
Notifications
You must be signed in to change notification settings - Fork 24
feat: Add optional Sentry support #2386
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,17 +2,20 @@ package serve | |
|
|
||
| import ( | ||
| "context" | ||
| "errors" | ||
| "fmt" | ||
| "net" | ||
| "os" | ||
| "os/signal" | ||
| "strconv" | ||
| "strings" | ||
| "syscall" | ||
|
|
||
| "github.com/cloudquery/plugin-sdk/v4/helpers/grpczerolog" | ||
| "github.com/cloudquery/plugin-sdk/v4/plugin" | ||
| "github.com/cloudquery/plugin-sdk/v4/premium" | ||
| "github.com/cloudquery/plugin-sdk/v4/types" | ||
| "github.com/getsentry/sentry-go" | ||
|
|
||
| pbDestinationV0 "github.com/cloudquery/plugin-pb-go/pb/destination/v0" | ||
| pbDestinationV1 "github.com/cloudquery/plugin-pb-go/pb/destination/v1" | ||
|
|
@@ -37,13 +40,20 @@ type PluginServe struct { | |
| plugin *plugin.Plugin | ||
| args []string | ||
| destinationV0V1Server bool | ||
| sentryDSN string | ||
| testListener bool | ||
| testListenerConn *bufconn.Listener | ||
| versions []int | ||
| } | ||
|
|
||
| type PluginOption func(*PluginServe) | ||
|
|
||
| func WithPluginSentryDSN(dsn string) PluginOption { | ||
| return func(s *PluginServe) { | ||
| s.sentryDSN = dsn | ||
| } | ||
| } | ||
|
|
||
| // WithDestinationV0V1Server is used to include destination v0 and v1 server to work | ||
| // with older sources | ||
| func WithDestinationV0V1Server() PluginOption { | ||
|
|
@@ -123,6 +133,11 @@ func (s *PluginServe) newCmdPluginServe() *cobra.Command { | |
| Long: servePluginShort, | ||
| Args: cobra.NoArgs, | ||
| RunE: func(cmd *cobra.Command, _ []string) error { | ||
| doSentry, _ := strconv.ParseBool(os.Getenv("CQ_SENTRY_ENABLED")) | ||
| if doSentry && noSentry { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For future us, but we'll probably want to remove noSentry. |
||
| return errors.New("CQ_SENTRY_ENABLED and --no-sentry cannot be used together") | ||
| } | ||
|
|
||
| zerologLevel, err := zerolog.ParseLevel(logLevel.String()) | ||
| if err != nil { | ||
| return err | ||
|
|
@@ -204,6 +219,33 @@ func (s *PluginServe) newCmdPluginServe() *cobra.Command { | |
| Versions: []int32{0, 1, 2, 3}, | ||
| }) | ||
|
|
||
| version := s.plugin.Version() | ||
|
|
||
| if doSentry && len(s.sentryDSN) > 0 && !strings.EqualFold(version, "development") && !noSentry { | ||
| err = sentry.Init(sentry.ClientOptions{ | ||
| Dsn: s.sentryDSN, | ||
| Debug: false, | ||
| AttachStacktrace: false, | ||
| Release: version, | ||
| Transport: sentry.NewHTTPSyncTransport(), | ||
| ServerName: "oss", // set to "oss" on purpose to avoid sending any identifying information | ||
| // https://docs.sentry.io/platforms/go/configuration/options/#removing-default-integrations | ||
| Integrations: func(integrations []sentry.Integration) []sentry.Integration { | ||
| var filteredIntegrations []sentry.Integration | ||
| for _, integration := range integrations { | ||
| if integration.Name() == "Modules" { | ||
| continue | ||
| } | ||
| filteredIntegrations = append(filteredIntegrations, integration) | ||
| } | ||
| return filteredIntegrations | ||
| }, | ||
|
Comment on lines
+231
to
+242
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice!
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Very old code :) |
||
| }) | ||
| if err != nil { | ||
| log.Error().Err(err).Msg("Error initializing sentry") | ||
| } | ||
| } | ||
|
|
||
| ctx := cmd.Context() | ||
| c := make(chan os.Signal, 1) | ||
| signal.Notify(c, os.Interrupt, syscall.SIGTERM) | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is the thing to enable sentry.