From d88764aa3419857ec239b57c3519cc8d49c5e228 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Arturo=20Filast=C3=B2?= Date: Fri, 29 Jun 2018 15:29:05 +0200 Subject: [PATCH] Add support for crash reporting via sentry and change the send_crash_reports key --- cmd/ooni/main.go | 16 +++++----- config/parser.go | 6 ++++ config/settings.go | 10 +++--- config/testdata/valid-config.json | 6 ++-- data/default-config.json | 4 +-- internal/crashreport/crashreport.go | 47 +++++++++++++++++++++++++++++ internal/onboard/onboard.go | 2 +- 7 files changed, 72 insertions(+), 19 deletions(-) create mode 100644 internal/crashreport/crashreport.go diff --git a/cmd/ooni/main.go b/cmd/ooni/main.go index 601c6ff..2b56450 100644 --- a/cmd/ooni/main.go +++ b/cmd/ooni/main.go @@ -3,7 +3,6 @@ package main import ( // commands "github.com/apex/log" - "github.com/getsentry/raven-go" _ "github.com/ooni/probe-cli/internal/cli/geoip" _ "github.com/ooni/probe-cli/internal/cli/info" @@ -16,14 +15,15 @@ import ( _ "github.com/ooni/probe-cli/internal/cli/version" "github.com/ooni/probe-cli/internal/cli/app" + "github.com/ooni/probe-cli/internal/crashreport" ) func main() { - raven.SetDSN("https://cb4510e090f64382ac371040c19b2258:8448daeebfa643c289ef398f8645980b@sentry.io/1234954") - - err := app.Run() - if err == nil { - return - } - log.WithError(err).Fatal("main exit") + crashreport.CapturePanicAndWait(func() { + err := app.Run() + if err == nil { + return + } + log.WithError(err).Fatal("main exit") + }, nil) } diff --git a/config/parser.go b/config/parser.go index 8ebbe0b..a312fd0 100644 --- a/config/parser.go +++ b/config/parser.go @@ -5,6 +5,8 @@ import ( "io/ioutil" "sync" + "github.com/apex/log" + "github.com/ooni/probe-cli/internal/crashreport" "github.com/ooni/probe-cli/utils" "github.com/pkg/errors" ) @@ -39,6 +41,10 @@ func ParseConfig(b []byte) (*Config, error) { if err := c.Validate(); err != nil { return nil, errors.Wrap(err, "validating") } + if c.Advanced.SendCrashReports == false { + log.Info("Disabling crash reporting.") + crashreport.Disabled = true + } return &c, nil } diff --git a/config/settings.go b/config/settings.go index 0935772..c8ddf06 100644 --- a/config/settings.go +++ b/config/settings.go @@ -110,17 +110,17 @@ type Notifications struct { // Sharing settings type Sharing struct { - IncludeIP bool `json:"include_ip"` - IncludeASN bool `json:"include_asn"` - IncludeGPS bool `json:"include_gps"` - UploadResults bool `json:"upload_results"` - SendCrashReports bool `json:"send_crash_reports"` + IncludeIP bool `json:"include_ip"` + IncludeASN bool `json:"include_asn"` + IncludeGPS bool `json:"include_gps"` + UploadResults bool `json:"upload_results"` } // Advanced settings type Advanced struct { IncludeCountry bool `json:"include_country"` UseDomainFronting bool `json:"use_domain_fronting"` + SendCrashReports bool `json:"send_crash_reports"` } // AutomatedTesting settings diff --git a/config/testdata/valid-config.json b/config/testdata/valid-config.json index 3171bdc..c6c3875 100644 --- a/config/testdata/valid-config.json +++ b/config/testdata/valid-config.json @@ -7,8 +7,7 @@ "include_ip": false, "include_asn": true, "include_gps": true, - "upload_results": true, - "send_crash_reports": true + "upload_results": true }, "notifications": { "enabled": true, @@ -58,6 +57,7 @@ }, "advanced": { "include_country": true, - "use_domain_fronting": false + "use_domain_fronting": false, + "send_crash_reports": true } } diff --git a/data/default-config.json b/data/default-config.json index 79438b1..83e6a1a 100644 --- a/data/default-config.json +++ b/data/default-config.json @@ -8,7 +8,6 @@ "include_asn": true, "include_gps": true, "upload_results": true, - "send_crash_reports": true }, "notifications": { "enabled": true, @@ -58,6 +57,7 @@ }, "advanced": { "include_country": true, - "use_domain_fronting": false + "use_domain_fronting": false, + "send_crash_reports": true }, } diff --git a/internal/crashreport/crashreport.go b/internal/crashreport/crashreport.go new file mode 100644 index 0000000..21e2c1c --- /dev/null +++ b/internal/crashreport/crashreport.go @@ -0,0 +1,47 @@ +package crashreport + +import ( + "github.com/getsentry/raven-go" +) + +// Disabled flag is used to globally disable crash reporting and make all the +// crash reporting logic a no-op. +var Disabled = false + +// CapturePanic is a wrapper around raven.CapturePanic that becomes a noop if +// `Disabled` is set to true. +func CapturePanic(f func(), tags map[string]string) (interface{}, string) { + if Disabled == true { + return nil, "" + } + return raven.CapturePanic(f, tags) +} + +// CapturePanicAndWait is a wrapper around raven.CapturePanicAndWait that becomes a noop if +// `Disabled` is set to true. +func CapturePanicAndWait(f func(), tags map[string]string) (interface{}, string) { + if Disabled == true { + return nil, "" + } + return raven.CapturePanicAndWait(f, tags) +} + +// CaptureError is a wrapper around raven.CaptureError +func CaptureError(err error, tags map[string]string) string { + if Disabled == true { + return "" + } + return raven.CaptureError(err, tags) +} + +// CaptureErrorAndWait is a wrapper around raven.CaptureErrorAndWait +func CaptureErrorAndWait(err error, tags map[string]string) string { + if Disabled == true { + return "" + } + return raven.CaptureErrorAndWait(err, tags) +} + +func init() { + raven.SetDSN("https://cb4510e090f64382ac371040c19b2258:8448daeebfa643c289ef398f8645980b@sentry.io/1234954") +} diff --git a/internal/onboard/onboard.go b/internal/onboard/onboard.go index b112f09..fa8a133 100644 --- a/internal/onboard/onboard.go +++ b/internal/onboard/onboard.go @@ -127,10 +127,10 @@ func Onboarding(config *config.Config) error { config.Lock() config.InformedConsent = true config.Advanced.IncludeCountry = settings.IncludeCountry + config.Advanced.SendCrashReports = settings.SendCrashReports config.Sharing.IncludeIP = settings.IncludeIP config.Sharing.IncludeASN = settings.IncludeNetwork config.Sharing.UploadResults = settings.UploadResults - config.Sharing.SendCrashReports = settings.SendCrashReports config.Unlock() if err := config.Write(); err != nil {