Skip to content

Commit

Permalink
feat: report bug to sentry
Browse files Browse the repository at this point in the history
  • Loading branch information
julienrbrt committed Aug 29, 2024
1 parent b453874 commit f1ee49a
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 5 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ require (
github.com/cosmos/gogoproto v1.6.0
github.com/emicklei/proto v1.12.2
github.com/emicklei/proto-contrib v0.15.0
github.com/getsentry/sentry-go v0.27.0
github.com/go-delve/delve v1.21.0
github.com/go-git/go-git/v5 v5.12.0
github.com/go-openapi/analysis v0.23.0
Expand Down Expand Up @@ -213,7 +214,6 @@ require (
github.com/firefart/nonamedreturns v1.0.5 // indirect
github.com/fsnotify/fsnotify v1.7.0 // indirect
github.com/fzipp/gocyclo v0.6.0 // indirect
github.com/getsentry/sentry-go v0.27.0 // indirect
github.com/ghostiam/protogetter v0.3.6 // indirect
github.com/go-chi/chi/v5 v5.0.12 // indirect
github.com/go-critic/go-critic v0.11.4 // indirect
Expand Down
7 changes: 5 additions & 2 deletions ignite/cmd/ignite/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,13 @@ import (
"github.com/ignite/cli/v29/ignite/pkg/xstrings"
)

const exitCodeOK, exitCodeError = 0, 1

func main() {
os.Exit(run())
}

func run() int {
const exitCodeOK, exitCodeError = 0, 1
ctx := clictx.From(context.Background())
cmd, cleanUp, err := ignitecmd.New(ctx)
if err != nil {
Expand All @@ -41,6 +42,7 @@ func run() int {
}
var wg sync.WaitGroup
analytics.SendMetric(&wg, subCmd)
analytics.SendErrors(&wg, ctx)

err = cmd.ExecuteContext(ctx)
if err != nil {
Expand Down Expand Up @@ -77,7 +79,8 @@ func run() int {
return exitCodeError
}

wg.Wait() // waits for all metrics to be sent
// waits for analytics to finish
wg.Wait()

return exitCodeOK
}
Expand Down
26 changes: 24 additions & 2 deletions ignite/internal/analytics/analytics.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package analytics
import (
"context"
"encoding/json"
"fmt"
"os"
"path/filepath"
"strconv"
Expand All @@ -16,6 +17,7 @@ import (
"github.com/ignite/cli/v29/ignite/pkg/gitpod"
"github.com/ignite/cli/v29/ignite/pkg/matomo"
"github.com/ignite/cli/v29/ignite/pkg/randstr"
"github.com/ignite/cli/v29/ignite/pkg/sentry"
"github.com/ignite/cli/v29/ignite/version"
)

Expand Down Expand Up @@ -52,6 +54,7 @@ func SendMetric(wg *sync.WaitGroup, cmd *cobra.Command) {
}

dntInfo, err := checkDNT()
fmt.Println(dntInfo, err)
if err != nil || dntInfo.DoNotTrack {
return
}
Expand Down Expand Up @@ -99,11 +102,30 @@ func SendMetric(wg *sync.WaitGroup, cmd *cobra.Command) {
}()
}

// SendErrors send command errors to Sentry.
func SendErrors(wg *sync.WaitGroup, ctx context.Context) {

Check failure on line 106 in ignite/internal/analytics/analytics.go

View workflow job for this annotation

GitHub Actions / Lint Go code

context-as-argument: context.Context should be the first parameter of a function (revive)
dntInfo, err := checkDNT()
if err != nil || dntInfo.DoNotTrack {
return
}

closeSentry, err := sentry.InitSentry(ctx)
wg.Add(1)
go func() {
defer wg.Done()
if err == nil {
defer closeSentry()
}
}()
}

// checkDNT check if the user allow to track data or if the DO_NOT_TRACK
// env var is set https://consoledonottrack.com/
func checkDNT() (anonIdentity, error) {
if dnt, err := strconv.ParseBool(os.Getenv(envDoNotTrack)); err != nil || dnt {
return anonIdentity{DoNotTrack: true}, nil
if dnt := os.Getenv(envDoNotTrack); dnt != "" {
if dnt, err := strconv.ParseBool(dnt); err != nil || dnt {
return anonIdentity{DoNotTrack: true}, nil
}
}

globalPath, err := config.DirPath()
Expand Down
46 changes: 46 additions & 0 deletions ignite/pkg/sentry/sentry.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package sentry

import (
"context"
"fmt"
"strings"
"time"

"github.com/getsentry/sentry-go"

"github.com/ignite/cli/v29/ignite/version"
)

const IgniteDNS = "https://[email protected]/0"

func InitSentry(ctx context.Context) (deferMe func(), err error) {
sentrySyncTransport := sentry.NewHTTPSyncTransport()
sentrySyncTransport.Timeout = time.Second * 3

igniteInfo, err := version.GetInfo(ctx)
if err != nil {
return nil, fmt.Errorf("failed to init sentry: %w", err)

Check failure on line 22 in ignite/pkg/sentry/sentry.go

View workflow job for this annotation

GitHub Actions / Lint Go code

use of `fmt.Errorf` forbidden because "fmt.Errorf should be replaced by '\"github.com/ignite/cli/ignite/pkg/errors\"'" (forbidigo)
}

if err := sentry.Init(sentry.ClientOptions{
Dsn: IgniteDNS,
Transport: sentrySyncTransport,
Environment: getEnvironment(igniteInfo.CLIVersion),
Release: fmt.Sprintf("ignite@%s", igniteInfo.CLIVersion),
SampleRate: 1.0, // get all events
}); err != nil {
return nil, fmt.Errorf("failed to init sentry: %w", err)

Check failure on line 32 in ignite/pkg/sentry/sentry.go

View workflow job for this annotation

GitHub Actions / Lint Go code

use of `fmt.Errorf` forbidden because "fmt.Errorf should be replaced by '\"github.com/ignite/cli/ignite/pkg/errors\"'" (forbidigo)
}

return func() {
sentry.Flush(time.Second * 2)
}, nil
}

func getEnvironment(igniteVersion string) string {
if strings.Contains(igniteVersion, "dev") {
return "development"
} else {

Check failure on line 43 in ignite/pkg/sentry/sentry.go

View workflow job for this annotation

GitHub Actions / Lint Go code

indent-error-flow: if block ends with a return statement, so drop this else and outdent its block (revive)
return "production"
}
}

0 comments on commit f1ee49a

Please sign in to comment.