@@ -5,10 +5,12 @@ import (
5
5
"net"
6
6
"os"
7
7
"strings"
8
+ "time"
8
9
9
10
"github.com/cloudquery/plugin-sdk/internal/pb"
10
11
"github.com/cloudquery/plugin-sdk/internal/servers"
11
12
"github.com/cloudquery/plugin-sdk/plugins"
13
+ "github.com/getsentry/sentry-go"
12
14
grpczerolog "github.com/grpc-ecosystem/go-grpc-middleware/providers/zerolog/v2"
13
15
middleware "github.com/grpc-ecosystem/go-grpc-middleware/v2"
14
16
"github.com/grpc-ecosystem/go-grpc-middleware/v2/interceptors/logging"
@@ -23,13 +25,14 @@ type Options struct {
23
25
// Required: Source or destination plugin to serve.
24
26
SourcePlugin * plugins.SourcePlugin
25
27
DestinationPlugin plugins.DestinationPlugin
28
+ SentryDsn string
26
29
}
27
30
28
- // bufSize used for unit testing grpc server and client
29
- const testBufSize = 1024 * 1024
30
-
31
31
const (
32
32
serveShort = `Start plugin server`
33
+ // bufSize used for unit testing grpc server and client
34
+ testBufSize = 1024 * 1024
35
+ flushTimeout = 5 * time .Second
33
36
)
34
37
35
38
// lis used for unit testing grpc server and client
@@ -38,6 +41,7 @@ var testListener *bufconn.Listener
38
41
func newCmdServe (opts Options ) * cobra.Command {
39
42
var address string
40
43
var network string
44
+ var noSentry bool
41
45
logLevel := newEnum ([]string {"trace" , "debug" , "info" , "warn" , "error" }, "info" )
42
46
logFormat := newEnum ([]string {"text" , "json" }, "text" )
43
47
cmd := & cobra.Command {
@@ -56,6 +60,7 @@ func newCmdServe(opts Options) *cobra.Command {
56
60
} else {
57
61
logger = log .Output (zerolog.ConsoleWriter {Out : os .Stdout }).Level (zerologLevel )
58
62
}
63
+
59
64
// opts.Plugin.Logger = logger
60
65
var listener net.Listener
61
66
if network == "test" {
@@ -75,17 +80,39 @@ func newCmdServe(opts Options) *cobra.Command {
75
80
middleware .WithStreamServerChain (
76
81
logging .StreamServerInterceptor (grpczerolog .InterceptorLogger (logger )),
77
82
),
78
- // grpc.ChainStreamInterceptor(grpc_zero),
79
- // grpc.ChainUnaryInterceptor(),
80
83
)
81
84
85
+ version := "development"
82
86
if opts .SourcePlugin != nil {
83
87
opts .SourcePlugin .SetLogger (logger )
84
88
pb .RegisterSourceServer (s , & servers.SourceServer {Plugin : opts .SourcePlugin })
89
+ version = opts .SourcePlugin .Version ()
85
90
}
86
91
if opts .DestinationPlugin != nil {
87
92
// opts.DestinationPlugin.Logger = logger
88
93
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
+ }
89
116
}
90
117
91
118
logger .Info ().Str ("address" , listener .Addr ().String ()).Msg ("server listening" )
@@ -99,6 +126,8 @@ func newCmdServe(opts Options) *cobra.Command {
99
126
cmd .Flags ().StringVar (& network , "network" , "tcp" , `the network must be "tcp", "tcp4", "tcp6", "unix" or "unixpacket"` )
100
127
cmd .Flags ().Var (logLevel , "log-level" , fmt .Sprintf ("log level. one of: %s" , strings .Join (logLevel .Allowed , "," )))
101
128
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
+
102
131
return cmd
103
132
}
104
133
@@ -113,7 +142,10 @@ func newCmdRoot(opts Options) *cobra.Command {
113
142
114
143
func Serve (opts Options ) {
115
144
if err := newCmdRoot (opts ).Execute (); err != nil {
145
+ sentry .CaptureMessage (err .Error ())
146
+ sentry .Flush (flushTimeout )
116
147
fmt .Println (err )
117
148
os .Exit (1 )
118
149
}
150
+ sentry .Flush (flushTimeout )
119
151
}
0 commit comments