change: integrate sentry sdk for panic reporting and analysis

This commit is contained in:
debugtalk
2022-03-24 19:46:21 +08:00
parent 90c7ba7364
commit 62b21dab22
6 changed files with 172 additions and 3 deletions

View File

@@ -1,9 +1,24 @@
package main
import (
"os"
"time"
"github.com/getsentry/sentry-go"
"github.com/rs/zerolog/log"
"github.com/httprunner/httprunner/hrp/cmd"
)
func main() {
defer func() {
if err := recover(); err != nil {
// report panic to sentry
sentry.CurrentHub().Recover(err)
sentry.Flush(time.Second * 5)
log.Error().Interface("err", err).Msg("recover panic")
os.Exit(1)
}
}()
cmd.Execute()
}

View File

@@ -1,12 +1,19 @@
package ga
import (
"fmt"
"github.com/denisbrodbeck/machineid"
"github.com/getsentry/sentry-go"
"github.com/google/uuid"
"github.com/rs/zerolog/log"
"github.com/httprunner/httprunner/hrp/internal/version"
)
const (
trackingID = "UA-114587036-1" // Tracking ID for Google Analytics
sentryDSN = "https://cff5efc69b1a4325a4cf873f1e70c13a@o334324.ingest.sentry.io/6070292"
)
var gaClient *GAClient
@@ -18,6 +25,23 @@ func init() {
clientID = nodeUUID.String()
}
gaClient = NewGAClient(trackingID, clientID)
// init sentry sdk
err = sentry.Init(sentry.ClientOptions{
Dsn: sentryDSN,
Release: fmt.Sprintf("httprunner@%s", version.VERSION),
AttachStacktrace: true,
})
if err != nil {
log.Error().Err(err).Msg("init sentry sdk failed!")
return
}
sentry.ConfigureScope(func(scope *sentry.Scope) {
scope.SetLevel(sentry.LevelError)
scope.SetUser(sentry.User{
ID: clientID,
})
})
}
func SendEvent(e IEvent) error {

View File

@@ -1058,11 +1058,11 @@ func (s *Summary) genHTMLReport() error {
return err
}
file, err := os.OpenFile(fmt.Sprintf(reportPath, s.Time.StartAt.Unix()), os.O_WRONLY|os.O_CREATE, 0666)
defer file.Close()
if err != nil {
log.Error().Err(err).Msg("open file failed")
return err
}
defer file.Close()
writer := bufio.NewWriter(file)
tmpl := template.Must(template.New("report").Parse(reportTemplate))
err = tmpl.Execute(writer, s)