@@ -31,6 +31,7 @@ import (
3131 "time"
3232
3333 "github.com/coreos/go-systemd/v22/daemon"
34+ "github.com/getsentry/sentry-go"
3435 grpc_prometheus "github.com/grpc-ecosystem/go-grpc-prometheus"
3536 "github.com/jessevdk/go-flags"
3637 "github.com/kr/pretty"
@@ -54,39 +55,69 @@ func main() {
5455 signal .Notify (sigCh , syscall .SIGTERM , syscall .SIGINT )
5556
5657 var opts struct {
57- ConfigFile string `short:"f" long:"config-file" description:"specifying a config file"`
58- ConfigType string `short:"t" long:"config-type" description:"specifying config type (toml, yaml, json)" default:"toml"`
59- ConfigAutoReload bool `short:"a" long:"config-auto-reload" description:"activate config auto reload on changes"`
60- ConfigStrict bool `long:"config-strict" description:"make any config error fatal"`
61- LogLevel string `short:"l" long:"log-level" description:"specifying log level"`
62- LogPlain bool `short:"p" long:"log-plain" description:"use plain format for logging (json by default)"`
63- UseSyslog string `short:"s" long:"syslog" description:"use syslogd"`
64- Facility string `long:"syslog-facility" description:"specify syslog facility"`
65- DisableStdlog bool `long:"disable-stdlog" description:"disable standard logging"`
66- CPUs int `long:"cpus" description:"specify the number of CPUs to be used"`
67- GrpcHosts string `long:"api-hosts" description:"specify the hosts that gobgpd listens on" default:":50051"`
68- GracefulRestart bool `short:"r" long:"graceful-restart" description:"flag restart-state in graceful-restart capability"`
69- Dry bool `short:"d" long:"dry-run" description:"check configuration"`
70- PProfHost string `long:"pprof-host" description:"specify the host that gobgpd listens on for pprof and metrics" default:"localhost:6060"`
71- PProfDisable bool `long:"pprof-disable" description:"disable pprof profiling"`
72- MetricsPath string `long:"metrics-path" description:"specify path for prometheus metrics, empty value disables them" default:"/metrics"`
73- UseSdNotify bool `long:"sdnotify" description:"use sd_notify protocol"`
74- TLS bool `long:"tls" description:"enable TLS authentication for gRPC API"`
75- TLSCertFile string `long:"tls-cert-file" description:"The TLS cert file"`
76- TLSKeyFile string `long:"tls-key-file" description:"The TLS key file"`
77- TLSClientCAFile string `long:"tls-client-ca-file" description:"Optional TLS client CA file to authenticate clients against"`
78- Version bool `long:"version" description:"show version number"`
58+ ConfigFile string `short:"f" long:"config-file" description:"specifying a config file"`
59+ ConfigType string `short:"t" long:"config-type" description:"specifying config type (toml, yaml, json)" default:"toml"`
60+ ConfigAutoReload bool `short:"a" long:"config-auto-reload" description:"activate config auto reload on changes"`
61+ ConfigStrict bool `long:"config-strict" description:"make any config error fatal"`
62+ LogLevel string `short:"l" long:"log-level" description:"specifying log level"`
63+ LogPlain bool `short:"p" long:"log-plain" description:"use plain format for logging (json by default)"`
64+ UseSyslog string `short:"s" long:"syslog" description:"use syslogd"`
65+ Facility string `long:"syslog-facility" description:"specify syslog facility"`
66+ DisableStdlog bool `long:"disable-stdlog" description:"disable standard logging"`
67+ CPUs int `long:"cpus" description:"specify the number of CPUs to be used"`
68+ GrpcHosts string `long:"api-hosts" description:"specify the hosts that gobgpd listens on" default:":50051"`
69+ GracefulRestart bool `short:"r" long:"graceful-restart" description:"flag restart-state in graceful-restart capability"`
70+ Dry bool `short:"d" long:"dry-run" description:"check configuration"`
71+ PProfHost string `long:"pprof-host" description:"specify the host that gobgpd listens on for pprof and metrics" default:"localhost:6060"`
72+ PProfDisable bool `long:"pprof-disable" description:"disable pprof profiling"`
73+ MetricsPath string `long:"metrics-path" description:"specify path for prometheus metrics, empty value disables them" default:"/metrics"`
74+ UseSdNotify bool `long:"sdnotify" description:"use sd_notify protocol"`
75+ TLS bool `long:"tls" description:"enable TLS authentication for gRPC API"`
76+ TLSCertFile string `long:"tls-cert-file" description:"The TLS cert file"`
77+ TLSKeyFile string `long:"tls-key-file" description:"The TLS key file"`
78+ TLSClientCAFile string `long:"tls-client-ca-file" description:"Optional TLS client CA file to authenticate clients against"`
79+ Version bool `long:"version" description:"show version number"`
80+ SentryDSN string `long:"sentry-dsn" description:"Sentry DSN" default:""`
81+ SentryEnvironment string `long:"sentry-environment" description:"Sentry environment" default:"development"`
82+ SentrySampleRate float64 `long:"sentry-sample-rate" description:"Sentry traces sample rate" default:"1.0"`
83+ SentryDebug bool `long:"sentry-debug" description:"Sentry debug mode"`
7984 }
8085 _ , err := flags .Parse (& opts )
8186 if err != nil {
82- os . Exit ( 1 )
87+ logger . Fatalf ( "Error parsing flags: %v" , err )
8388 }
8489
8590 if opts .Version {
8691 fmt .Println ("gobgpd version" , version .Version ())
8792 os .Exit (0 )
8893 }
8994
95+ // if Sentry DSN is provided, initialize Sentry
96+ // We would like to capture errors and exceptions, but not traces
97+ if opts .SentryDSN != "" {
98+ logger .Debugf ("Initializing Sentry, Env: %s, Release: %s, SampleRate: %f, Debug: %t" ,
99+ opts .SentryEnvironment , version .Version (), opts .SentrySampleRate , opts .SentryDebug )
100+ err := sentry .Init (sentry.ClientOptions {
101+ Dsn : opts .SentryDSN ,
102+ SampleRate : opts .SentrySampleRate ,
103+ Debug : opts .SentryDebug ,
104+ Release : version .Version (),
105+ Environment : opts .SentryEnvironment ,
106+ // Disable tracing as it's not relevant for now
107+ EnableTracing : false ,
108+ TracesSampleRate : 0.0 ,
109+ })
110+ if err != nil {
111+ logger .Fatalf ("sentry.Init: %s" , err )
112+ }
113+ // Flush buffered events before the program terminates.
114+ defer sentry .Flush (2 * time .Second )
115+
116+ if opts .SentryDebug {
117+ sentry .CaptureMessage ("Sentry debug mode enabled on gobgpd" )
118+ }
119+ }
120+
90121 if opts .CPUs == 0 {
91122 runtime .GOMAXPROCS (runtime .NumCPU ())
92123 } else {
0 commit comments