From e5c607e482209187b0a0d38d7cfaded7342c555a Mon Sep 17 00:00:00 2001 From: sqshq Date: Fri, 31 May 2019 00:36:00 -0400 Subject: [PATCH] telemetry: added global recovery and crash report --- client/backend.go | 17 ++++++++++++++++- main.go | 14 ++++++++++++++ 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/client/backend.go b/client/backend.go index 2774331..7bb3a42 100644 --- a/client/backend.go +++ b/client/backend.go @@ -49,7 +49,22 @@ func (c *BackendClient) ReportUsageStatistics(error string, statistics *metadata } func (c *BackendClient) ReportCrash(error string, statistics *metadata.Statistics) { - // TODO + + req := struct { + Error string + Statistics *metadata.Statistics + }{ + error, + statistics, + } + + buf := new(bytes.Buffer) + err := json.NewEncoder(buf).Encode(req) + if err != nil { + return + } + + _, _ = http.Post(backendUrl+crashPath, jsonContentType, buf) } func (c *BackendClient) RegisterLicenseKey(licenseKey string, statistics *metadata.Statistics) (*metadata.License, error) { diff --git a/main.go b/main.go index 23110b6..71528be 100644 --- a/main.go +++ b/main.go @@ -1,6 +1,7 @@ package main import ( + "fmt" ui "github.com/gizak/termui/v3" "github.com/sqshq/sampler/asset" "github.com/sqshq/sampler/client" @@ -17,6 +18,7 @@ import ( "github.com/sqshq/sampler/data" "github.com/sqshq/sampler/event" "github.com/sqshq/sampler/metadata" + "runtime/debug" "time" ) @@ -72,6 +74,8 @@ func main() { statistics := metadata.GetStatistics(cfg) license := metadata.GetLicense() + defer handleCrash(statistics, opt, bc) + if opt.LicenseKey != nil { registerLicense(statistics, opt, bc) } @@ -105,6 +109,16 @@ func main() { handler.HandleEvents() } +func handleCrash(statistics *metadata.Statistics, opt config.Options, bc *client.BackendClient) { + if rec := recover(); rec != nil { + err := rec.(error) + if !opt.DisableTelemetry { + bc.ReportCrash(fmt.Sprintf("%s\n%s", err.Error(), string(debug.Stack())), statistics) + } + panic(err) + } +} + func registerLicense(statistics *metadata.Statistics, opt config.Options, bc *client.BackendClient) { lc, err := bc.RegisterLicenseKey(*opt.LicenseKey, statistics) if err != nil {