@@ -2,17 +2,20 @@ package serve
22
33import (
44 "context"
5+ "errors"
56 "fmt"
67 "net"
78 "os"
89 "os/signal"
10+ "strconv"
911 "strings"
1012 "syscall"
1113
1214 "github.com/cloudquery/plugin-sdk/v4/helpers/grpczerolog"
1315 "github.com/cloudquery/plugin-sdk/v4/plugin"
1416 "github.com/cloudquery/plugin-sdk/v4/premium"
1517 "github.com/cloudquery/plugin-sdk/v4/types"
18+ "github.com/getsentry/sentry-go"
1619
1720 pbDestinationV0 "github.com/cloudquery/plugin-pb-go/pb/destination/v0"
1821 pbDestinationV1 "github.com/cloudquery/plugin-pb-go/pb/destination/v1"
@@ -37,13 +40,20 @@ type PluginServe struct {
3740 plugin * plugin.Plugin
3841 args []string
3942 destinationV0V1Server bool
43+ sentryDSN string
4044 testListener bool
4145 testListenerConn * bufconn.Listener
4246 versions []int
4347}
4448
4549type PluginOption func (* PluginServe )
4650
51+ func WithPluginSentryDSN (dsn string ) PluginOption {
52+ return func (s * PluginServe ) {
53+ s .sentryDSN = dsn
54+ }
55+ }
56+
4757// WithDestinationV0V1Server is used to include destination v0 and v1 server to work
4858// with older sources
4959func WithDestinationV0V1Server () PluginOption {
@@ -123,6 +133,11 @@ func (s *PluginServe) newCmdPluginServe() *cobra.Command {
123133 Long : servePluginShort ,
124134 Args : cobra .NoArgs ,
125135 RunE : func (cmd * cobra.Command , _ []string ) error {
136+ doSentry , _ := strconv .ParseBool (os .Getenv ("CQ_SENTRY_ENABLED" ))
137+ if doSentry && noSentry {
138+ return errors .New ("CQ_SENTRY_ENABLED and --no-sentry cannot be used together" )
139+ }
140+
126141 zerologLevel , err := zerolog .ParseLevel (logLevel .String ())
127142 if err != nil {
128143 return err
@@ -204,6 +219,33 @@ func (s *PluginServe) newCmdPluginServe() *cobra.Command {
204219 Versions : []int32 {0 , 1 , 2 , 3 },
205220 })
206221
222+ version := s .plugin .Version ()
223+
224+ if doSentry && len (s .sentryDSN ) > 0 && ! strings .EqualFold (version , "development" ) && ! noSentry {
225+ err = sentry .Init (sentry.ClientOptions {
226+ Dsn : s .sentryDSN ,
227+ Debug : false ,
228+ AttachStacktrace : false ,
229+ Release : version ,
230+ Transport : sentry .NewHTTPSyncTransport (),
231+ ServerName : "oss" , // set to "oss" on purpose to avoid sending any identifying information
232+ // https://docs.sentry.io/platforms/go/configuration/options/#removing-default-integrations
233+ Integrations : func (integrations []sentry.Integration ) []sentry.Integration {
234+ var filteredIntegrations []sentry.Integration
235+ for _ , integration := range integrations {
236+ if integration .Name () == "Modules" {
237+ continue
238+ }
239+ filteredIntegrations = append (filteredIntegrations , integration )
240+ }
241+ return filteredIntegrations
242+ },
243+ })
244+ if err != nil {
245+ log .Error ().Err (err ).Msg ("Error initializing sentry" )
246+ }
247+ }
248+
207249 ctx := cmd .Context ()
208250 c := make (chan os.Signal , 1 )
209251 signal .Notify (c , os .Interrupt , syscall .SIGTERM )
0 commit comments