@@ -5,10 +5,12 @@ import (
55 "net"
66 "os"
77 "strings"
8+ "time"
89
910 "github.com/cloudquery/plugin-sdk/internal/pb"
1011 "github.com/cloudquery/plugin-sdk/internal/servers"
1112 "github.com/cloudquery/plugin-sdk/plugins"
13+ "github.com/getsentry/sentry-go"
1214 grpczerolog "github.com/grpc-ecosystem/go-grpc-middleware/providers/zerolog/v2"
1315 middleware "github.com/grpc-ecosystem/go-grpc-middleware/v2"
1416 "github.com/grpc-ecosystem/go-grpc-middleware/v2/interceptors/logging"
@@ -23,13 +25,14 @@ type Options struct {
2325 // Required: Source or destination plugin to serve.
2426 SourcePlugin * plugins.SourcePlugin
2527 DestinationPlugin plugins.DestinationPlugin
28+ SentryDsn string
2629}
2730
28- // bufSize used for unit testing grpc server and client
29- const testBufSize = 1024 * 1024
30-
3131const (
3232 serveShort = `Start plugin server`
33+ // bufSize used for unit testing grpc server and client
34+ testBufSize = 1024 * 1024
35+ flushTimeout = 5 * time .Second
3336)
3437
3538// lis used for unit testing grpc server and client
@@ -38,6 +41,7 @@ var testListener *bufconn.Listener
3841func newCmdServe (opts Options ) * cobra.Command {
3942 var address string
4043 var network string
44+ var noSentry bool
4145 logLevel := newEnum ([]string {"trace" , "debug" , "info" , "warn" , "error" }, "info" )
4246 logFormat := newEnum ([]string {"text" , "json" }, "text" )
4347 cmd := & cobra.Command {
@@ -56,6 +60,7 @@ func newCmdServe(opts Options) *cobra.Command {
5660 } else {
5761 logger = log .Output (zerolog.ConsoleWriter {Out : os .Stdout }).Level (zerologLevel )
5862 }
63+
5964 // opts.Plugin.Logger = logger
6065 var listener net.Listener
6166 if network == "test" {
@@ -75,17 +80,39 @@ func newCmdServe(opts Options) *cobra.Command {
7580 middleware .WithStreamServerChain (
7681 logging .StreamServerInterceptor (grpczerolog .InterceptorLogger (logger )),
7782 ),
78- // grpc.ChainStreamInterceptor(grpc_zero),
79- // grpc.ChainUnaryInterceptor(),
8083 )
8184
85+ version := "development"
8286 if opts .SourcePlugin != nil {
8387 opts .SourcePlugin .SetLogger (logger )
8488 pb .RegisterSourceServer (s , & servers.SourceServer {Plugin : opts .SourcePlugin })
89+ version = opts .SourcePlugin .Version ()
8590 }
8691 if opts .DestinationPlugin != nil {
8792 // opts.DestinationPlugin.Logger = logger
8893 pb .RegisterDestinationServer (s , & servers.DestinationServer {Plugin : opts .DestinationPlugin })
94+ version = opts .DestinationPlugin .Version ()
95+ }
96+
97+ if ! noSentry && version != "development" {
98+ err = sentry .Init (sentry.ClientOptions {
99+ Dsn : opts .SentryDsn ,
100+ Release : opts .SourcePlugin .Version (),
101+ // https://docs.sentry.io/platforms/go/configuration/options/#removing-default-integrations
102+ Integrations : func (integrations []sentry.Integration ) []sentry.Integration {
103+ var filteredIntegrations []sentry.Integration
104+ for _ , integration := range integrations {
105+ if integration .Name () == "Modules" {
106+ continue
107+ }
108+ filteredIntegrations = append (filteredIntegrations , integration )
109+ }
110+ return filteredIntegrations
111+ },
112+ })
113+ if err != nil {
114+ log .Error ().Err (err ).Msg ("Error initializing sentry" )
115+ }
89116 }
90117
91118 logger .Info ().Str ("address" , listener .Addr ().String ()).Msg ("server listening" )
@@ -99,6 +126,8 @@ func newCmdServe(opts Options) *cobra.Command {
99126 cmd .Flags ().StringVar (& network , "network" , "tcp" , `the network must be "tcp", "tcp4", "tcp6", "unix" or "unixpacket"` )
100127 cmd .Flags ().Var (logLevel , "log-level" , fmt .Sprintf ("log level. one of: %s" , strings .Join (logLevel .Allowed , "," )))
101128 cmd .Flags ().Var (logFormat , "log-format" , fmt .Sprintf ("log format. one of: %s" , strings .Join (logFormat .Allowed , "," )))
129+ cmd .Flags ().BoolVar (& noSentry , "no-sentry" , false , "disable sentry" )
130+
102131 return cmd
103132}
104133
@@ -113,7 +142,10 @@ func newCmdRoot(opts Options) *cobra.Command {
113142
114143func Serve (opts Options ) {
115144 if err := newCmdRoot (opts ).Execute (); err != nil {
145+ sentry .CaptureMessage (err .Error ())
146+ sentry .Flush (flushTimeout )
116147 fmt .Println (err )
117148 os .Exit (1 )
118149 }
150+ sentry .Flush (flushTimeout )
119151}
0 commit comments