telemetry: added global recovery and crash report

This commit is contained in:
sqshq 2019-05-31 00:36:00 -04:00
parent 4495c8f8e8
commit e5c607e482
2 changed files with 30 additions and 1 deletions

View File

@ -49,7 +49,22 @@ func (c *BackendClient) ReportUsageStatistics(error string, statistics *metadata
} }
func (c *BackendClient) ReportCrash(error string, statistics *metadata.Statistics) { 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) { func (c *BackendClient) RegisterLicenseKey(licenseKey string, statistics *metadata.Statistics) (*metadata.License, error) {

14
main.go
View File

@ -1,6 +1,7 @@
package main package main
import ( import (
"fmt"
ui "github.com/gizak/termui/v3" ui "github.com/gizak/termui/v3"
"github.com/sqshq/sampler/asset" "github.com/sqshq/sampler/asset"
"github.com/sqshq/sampler/client" "github.com/sqshq/sampler/client"
@ -17,6 +18,7 @@ import (
"github.com/sqshq/sampler/data" "github.com/sqshq/sampler/data"
"github.com/sqshq/sampler/event" "github.com/sqshq/sampler/event"
"github.com/sqshq/sampler/metadata" "github.com/sqshq/sampler/metadata"
"runtime/debug"
"time" "time"
) )
@ -72,6 +74,8 @@ func main() {
statistics := metadata.GetStatistics(cfg) statistics := metadata.GetStatistics(cfg)
license := metadata.GetLicense() license := metadata.GetLicense()
defer handleCrash(statistics, opt, bc)
if opt.LicenseKey != nil { if opt.LicenseKey != nil {
registerLicense(statistics, opt, bc) registerLicense(statistics, opt, bc)
} }
@ -105,6 +109,16 @@ func main() {
handler.HandleEvents() 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) { func registerLicense(statistics *metadata.Statistics, opt config.Options, bc *client.BackendClient) {
lc, err := bc.RegisterLicenseKey(*opt.LicenseKey, statistics) lc, err := bc.RegisterLicenseKey(*opt.LicenseKey, statistics)
if err != nil { if err != nil {