Skip to content

Commit 49ea072

Browse files
committed
feat: add sentry support to capture unexpected crashes
doc: add readme entry and sentry doc
1 parent acfc34e commit 49ea072

File tree

5 files changed

+91
-24
lines changed

5 files changed

+91
-24
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ Try [a binary release](https://github.com/osrg/gobgp/releases/latest).
5050
- [Confederation](docs/sources/bgp-confederation.md)
5151
- Data Center Networking
5252
- [Unnumbered BGP](docs/sources/unnumbered-bgp.md)
53+
- [Sentry](docs/sources/sentry.md)
5354

5455
### Externals
5556

cmd/gobgpd/main.go

Lines changed: 54 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -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 {

docs/sources/sentry.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# Sentry
2+
3+
GoBGP supports Sentry for error and exception tracking.
4+
5+
To enable Sentry, set the `--sentry-dsn` flag to your Sentry DSN.
6+
7+
```bash
8+
$ gobgpd --sentry-dsn=<your-dsn>
9+
```
10+
11+
In addition, you can set the `--sentry-environment` flag to your Sentry environment.
12+
13+
```bash
14+
$ gobgpd --sentry-dsn=<your-dsn> --sentry-environment=<your-environment>
15+
```
16+
17+
You can also set the `--sentry-sample-rate` flag to the sample rate of Sentry traces.
18+
19+
```bash
20+
$ gobgpd --sentry-dsn=<your-dsn> --sentry-sample-rate=<your-sample-rate>
21+
```
22+
23+
Finally, you can set the `--sentry-debug` flag to enable Sentry debug mode.
24+
25+
```bash
26+
$ gobgpd --sentry-dsn=<your-dsn> --sentry-debug=true
27+
```
28+
29+
When Sentry debug mode is enabled, there is a message logged to Sentry when the program starts.
30+
This is particularly useful to verify that Sentry is working as expected.

go.mod

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ require (
88
github.com/fsnotify/fsnotify v1.6.0
99
github.com/go-test/deep v1.1.0
1010
github.com/google/go-cmp v0.5.9
11-
github.com/google/uuid v1.3.1
11+
github.com/google/uuid v1.6.0
1212
github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0
1313
github.com/jessevdk/go-flags v1.5.0
1414
github.com/k-sone/critbitgo v1.4.0
@@ -31,6 +31,7 @@ require (
3131
github.com/cespare/xxhash/v2 v2.2.0 // indirect
3232
github.com/davecgh/go-spew v1.1.1 // indirect
3333
github.com/eapache/queue v1.1.0 // indirect
34+
github.com/getsentry/sentry-go v0.30.0 // indirect
3435
github.com/golang/protobuf v1.5.3 // indirect
3536
github.com/hashicorp/hcl v1.0.0 // indirect
3637
github.com/inconshreveable/mousetrap v1.1.0 // indirect

go.sum

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,8 @@ github.com/frankban/quicktest v1.14.4 h1:g2rn0vABPOOXmZUj+vbmUp0lPoXEMuhTpIluN0X
7575
github.com/frankban/quicktest v1.14.4/go.mod h1:4ptaffx2x8+WTWXmUCuVU6aPUX1/Mz7zb5vbUoiM6w0=
7676
github.com/fsnotify/fsnotify v1.6.0 h1:n+5WquG0fcWoWp6xPWfHdbskMCQaFnG6PfBrh1Ky4HY=
7777
github.com/fsnotify/fsnotify v1.6.0/go.mod h1:sl3t1tCWJFWoRz9R8WJCbQihKKwmorjAbSClcnxKAGw=
78+
github.com/getsentry/sentry-go v0.30.0 h1:lWUwDnY7sKHaVIoZ9wYqRHJ5iEmoc0pqcRqFkosKzBo=
79+
github.com/getsentry/sentry-go v0.30.0/go.mod h1:WU9B9/1/sHDqeV8T+3VwwbjeR5MSXs/6aqG3mqZrezA=
7880
github.com/go-gl/glfw v0.0.0-20190409004039-e6da0acd62b1/go.mod h1:vR7hzQXu2zJy9AVAgeJqvqgH9Q5CA+iKCZ2gyEVpxRU=
7981
github.com/go-gl/glfw/v3.3/glfw v0.0.0-20191125211704-12ad95a8df72/go.mod h1:tQ2UAYgL5IevRw8kRxooKSPJfGvJ9fJQFa0TUsXzTg8=
8082
github.com/go-gl/glfw/v3.3/glfw v0.0.0-20200222043503-6f7a984d4dc4/go.mod h1:tQ2UAYgL5IevRw8kRxooKSPJfGvJ9fJQFa0TUsXzTg8=
@@ -140,6 +142,8 @@ github.com/google/renameio v0.1.0/go.mod h1:KWCgfxg9yswjAJkECMjeO8J8rahYeXnNhOm4
140142
github.com/google/uuid v1.1.2/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
141143
github.com/google/uuid v1.3.1 h1:KjJaJ9iWZ3jOFZIf1Lqf4laDRCasjl0BCmnEGxkdLb4=
142144
github.com/google/uuid v1.3.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
145+
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
146+
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
143147
github.com/googleapis/gax-go/v2 v2.0.4/go.mod h1:0Wqv26UfaUD9n4G6kQubkQ+KchISgw+vpHVxEJEs9eg=
144148
github.com/googleapis/gax-go/v2 v2.0.5/go.mod h1:DWXyrwAJ9X0FpwwEdw+IPEYBICEFu5mhpdKc/us6bOk=
145149
github.com/googleapis/google-cloud-go-testing v0.0.0-20200911160855-bcd43fbb19e8/go.mod h1:dvDLG8qkwmyD9a/MJJN3XJcT3xFxOKAvTZGvuZmac9g=

0 commit comments

Comments
 (0)