Skip to content

Commit

Permalink
Add sentry error reporting to telemetry (jetify-com#335)
Browse files Browse the repository at this point in the history
## Summary
This replaces jetify-com#78

This PR does the following:
1. logs error to sentry. Sentry has built in support for removing
sensitive data.
2. link the telemetry event to the sentry error event id

TODO: update github action environment variables

## How was it tested?
go build
devbox plan (on an invalid project config)
  • Loading branch information
LucilleH authored Dec 3, 2022
1 parent 2ac95d2 commit 2b0904a
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 23 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/cli-release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ jobs:
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
TELEMETRY_KEY: ${{ secrets.TELEMETRY_KEY }}
SENTRY_DSN: ${{ secrets.SENTRY_DSN }}
- name: Determine snapshot tag
run: |
TAG=$(ls dist/*_linux_386.tar.gz | cut -d '_' -f 2 | grep -Eo '[0-9]+\.[0-9]+\.[0-9]+-dev')
Expand Down Expand Up @@ -90,3 +91,4 @@ jobs:
DISCORD_WEBHOOK_TOKEN: ${{ secrets.DISCORD_WEBHOOK_TOKEN }}
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
TELEMETRY_KEY: ${{ secrets.TELEMETRY_KEY }}
SENTRY_DSN: ${{ secrets.SENTRY_DSN }}
1 change: 1 addition & 0 deletions .goreleaser.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ builds:
- -s -w -X go.jetpack.io/devbox/build.Version={{.Version}}
- -s -w -X go.jetpack.io/devbox/build.Commit={{.Commit}}
- -s -w -X go.jetpack.io/devbox/build.CommitDate={{.CommitDate}}
- -s -w -X go.jetpack.io/devbox/build.SentryDSN={{ .Env.SENTRY_DSN }}
- -s -w -X go.jetpack.io/devbox/build.TelemetryKey={{ .Env.TELEMETRY_KEY }}
env:
- CGO_ENABLED=0
Expand Down
74 changes: 55 additions & 19 deletions boxcli/midcobra/telemetry.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"time"

"github.com/denisbrodbeck/machineid"
"github.com/getsentry/sentry-go"
segment "github.com/segmentio/analytics-go"
"github.com/spf13/cobra"
"go.jetpack.io/devbox"
Expand All @@ -32,13 +33,14 @@ func Telemetry(opts *TelemetryOpts) Middleware {

return &telemetryMiddleware{
opts: *opts,
disabled: doNotTrack || opts.TelemetryKey == "",
disabled: doNotTrack || opts.TelemetryKey == "" || opts.SentryDSN == "",
}
}

type TelemetryOpts struct {
AppName string
AppVersion string
SentryDSN string // used by error reporting
TelemetryKey string
}
type telemetryMiddleware struct {
Expand All @@ -61,7 +63,7 @@ func (m *telemetryMiddleware) postRun(cmd *cobra.Command, args []string, runErr
if m.disabled {
return
}

initSentry(m.opts)
segmentClient, _ := segment.NewWithConfig(m.opts.TelemetryKey, segment.Config{
BatchSize: 1, /* no batching */
// Discard logs:
Expand All @@ -78,15 +80,47 @@ func (m *telemetryMiddleware) postRun(cmd *cobra.Command, args []string, runErr
return // Ignore invalid commands
}

var sentryEventID string
if runErr != nil {
defer sentry.Flush(2 * time.Second)
sentryEventID = string(*sentry.CaptureException(runErr))
}

trackEvent(segmentClient, &event{
AppName: m.opts.AppName,
AppVersion: m.opts.AppVersion,
Command: subcmd.CommandPath(),
CommandArgs: subargs,
DeviceID: deviceID(),
Duration: time.Since(m.startTime),
Failed: runErr != nil,
Packages: getPackages(cmd),
AppName: m.opts.AppName,
AppVersion: m.opts.AppVersion,
Command: subcmd.CommandPath(),
CommandArgs: subargs,
DeviceID: deviceID(),
Duration: time.Since(m.startTime),
Failed: runErr != nil,
Packages: getPackages(cmd),
SentryEventID: sentryEventID,
})
}

func initSentry(opts TelemetryOpts) {
sentrySyncTransport := sentry.NewHTTPSyncTransport()
sentrySyncTransport.Timeout = time.Second * 2
release := opts.AppName + "@" + opts.AppVersion
environment := "production"
if opts.AppVersion == "0.0.0-dev" {
environment = "development"
}

_ = sentry.Init(sentry.ClientOptions{
Dsn: opts.SentryDSN,
Environment: environment,
Release: release,
Transport: sentrySyncTransport,
TracesSampleRate: 1,
BeforeSend: func(event *sentry.Event, hint *sentry.EventHint) *sentry.Event {
for i := range event.Exception {
// edit in place and remove error message from tracking
event.Exception[i].Value = ""
}
return event
},
})
}

Expand Down Expand Up @@ -126,14 +160,15 @@ func getPackages(c *cobra.Command) []string {
}

type event struct {
AppName string
AppVersion string
Command string
CommandArgs []string
DeviceID string
Duration time.Duration
Failed bool
Packages []string
AppName string
AppVersion string
Command string
CommandArgs []string
DeviceID string
Duration time.Duration
Failed bool
Packages []string
SentryEventID string
}

func trackEvent(client segment.Client, evt *event) {
Expand All @@ -157,6 +192,7 @@ func trackEvent(client segment.Client, evt *event) {
Set("command_args", evt.CommandArgs).
Set("failed", evt.Failed).
Set("duration", evt.Duration.Milliseconds()).
Set("packages", evt.Packages),
Set("packages", evt.Packages).
Set("sentry_event_id", evt.SentryEventID),
})
}
1 change: 1 addition & 0 deletions boxcli/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ func Execute(ctx context.Context, args []string) int {
exe.AddMiddleware(midcobra.Telemetry(&midcobra.TelemetryOpts{
AppName: "devbox",
AppVersion: build.Version,
SentryDSN: build.SentryDSN,
TelemetryKey: build.TelemetryKey,
}))
exe.AddMiddleware(debugMiddleware)
Expand Down
1 change: 1 addition & 0 deletions build/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@ var (
Commit = "none"
CommitDate = "unknown"

SentryDSN = "" // Disabled by default
TelemetryKey = "" // Disabled by default
)
5 changes: 3 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ require (
github.com/creekorful/mvnparser v1.5.0
github.com/denisbrodbeck/machineid v1.0.1
github.com/fatih/color v1.13.0
github.com/google/go-cmp v0.5.8
github.com/google/go-cmp v0.5.9
github.com/imdario/mergo v0.3.13
github.com/mholt/archiver/v4 v4.0.0-alpha.7
github.com/pelletier/go-toml/v2 v2.0.5
Expand All @@ -36,11 +36,12 @@ require (
github.com/cpuguy83/go-md2man/v2 v2.0.2 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/dsnet/compress v0.0.1 // indirect
github.com/getsentry/sentry-go v0.15.0 // indirect
github.com/golang/snappy v0.0.4 // indirect
github.com/google/uuid v1.3.0 // indirect
github.com/inconshreveable/mousetrap v1.0.1 // indirect
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51 // indirect
github.com/klauspost/compress v1.15.5 // indirect
github.com/klauspost/compress v1.15.11 // indirect
github.com/klauspost/pgzip v1.2.5 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.16 // indirect
Expand Down
8 changes: 6 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,13 @@ github.com/emicklei/proto v1.6.15 h1:XbpwxmuOPrdES97FrSfpyy67SSCV/wBIKXqgJzh6hNw
github.com/fatih/color v1.7.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4=
github.com/fatih/color v1.13.0 h1:8LOYc1KYPPmyKMuN8QV2DNRWNbLo6LZ0iLs8+mlH53w=
github.com/fatih/color v1.13.0/go.mod h1:kLAiJbzzSOZDVNGyDpeOxJ47H46qBXwg5ILebYFFOfk=
github.com/getsentry/sentry-go v0.15.0 h1:CP9bmA7pralrVUedYZsmIHWpq/pBtXTSew7xvVpfLaA=
github.com/getsentry/sentry-go v0.15.0/go.mod h1:RZPJKSw+adu8PBNygiri/A98FqVr2HtRckJk9XVxJ9I=
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b h1:VKtxabqXZkF25pY9ekfRL6a582T4P37/31XEstQ5p58=
github.com/golang/snappy v0.0.4 h1:yAGX7huGHXlcLOEtBnF4w7FQwA26wojNCwOYAEhLjQM=
github.com/golang/snappy v0.0.4/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q=
github.com/google/go-cmp v0.5.8 h1:e6P7q2lk1O+qJJb4BtCQXlK8vWEO8V1ZeuEdJNOqZyg=
github.com/google/go-cmp v0.5.8/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=
github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/hinshun/vt10x v0.0.0-20220119200601-820417d04eec h1:qv2VnGeEQHchGaZ/u7lxST/RaJw+cv273q79D81Xbog=
Expand All @@ -54,6 +56,8 @@ github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51/go.mod h1:C
github.com/klauspost/compress v1.4.1/go.mod h1:RyIbtBH6LamlWaDj8nUwkbUhJ87Yi3uG0guNDohfE1A=
github.com/klauspost/compress v1.15.5 h1:qyCLMz2JCrKADihKOh9FxnW3houKeNsp2h5OEz0QSEA=
github.com/klauspost/compress v1.15.5/go.mod h1:PhcZ0MbTNciWF3rruxRgKxI5NkcHHrHUDtV4Yw2GlzU=
github.com/klauspost/compress v1.15.11 h1:Lcadnb3RKGin4FYM/orgq0qde+nc15E5Cbqg4B9Sx9c=
github.com/klauspost/compress v1.15.11/go.mod h1:QPwzmACJjUTFsnSHH934V6woptycfrDDJnH7hvFVbGM=
github.com/klauspost/cpuid v1.2.0/go.mod h1:Pj4uuM528wm8OyEC2QMXAi2YiTZ96dNQPGgoMS4s3ek=
github.com/klauspost/pgzip v1.2.5 h1:qnWYvvKqedOF2ulHpMG72XQol4ILEJ8k2wwRl/Km8oE=
github.com/klauspost/pgzip v1.2.5/go.mod h1:Ch1tH69qFZu15pkjo5kYi6mth2Zzwzt50oCQKQE9RUs=
Expand Down

0 comments on commit 2b0904a

Please sign in to comment.