Skip to content

Commit 2b0904a

Browse files
authored
Add sentry error reporting to telemetry (#335)
## Summary This replaces #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)
1 parent 2ac95d2 commit 2b0904a

File tree

7 files changed

+69
-23
lines changed

7 files changed

+69
-23
lines changed

.github/workflows/cli-release.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ jobs:
5050
env:
5151
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
5252
TELEMETRY_KEY: ${{ secrets.TELEMETRY_KEY }}
53+
SENTRY_DSN: ${{ secrets.SENTRY_DSN }}
5354
- name: Determine snapshot tag
5455
run: |
5556
TAG=$(ls dist/*_linux_386.tar.gz | cut -d '_' -f 2 | grep -Eo '[0-9]+\.[0-9]+\.[0-9]+-dev')
@@ -90,3 +91,4 @@ jobs:
9091
DISCORD_WEBHOOK_TOKEN: ${{ secrets.DISCORD_WEBHOOK_TOKEN }}
9192
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
9293
TELEMETRY_KEY: ${{ secrets.TELEMETRY_KEY }}
94+
SENTRY_DSN: ${{ secrets.SENTRY_DSN }}

.goreleaser.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ builds:
1010
- -s -w -X go.jetpack.io/devbox/build.Version={{.Version}}
1111
- -s -w -X go.jetpack.io/devbox/build.Commit={{.Commit}}
1212
- -s -w -X go.jetpack.io/devbox/build.CommitDate={{.CommitDate}}
13+
- -s -w -X go.jetpack.io/devbox/build.SentryDSN={{ .Env.SENTRY_DSN }}
1314
- -s -w -X go.jetpack.io/devbox/build.TelemetryKey={{ .Env.TELEMETRY_KEY }}
1415
env:
1516
- CGO_ENABLED=0

boxcli/midcobra/telemetry.go

Lines changed: 55 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"time"
1414

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

3334
return &telemetryMiddleware{
3435
opts: *opts,
35-
disabled: doNotTrack || opts.TelemetryKey == "",
36+
disabled: doNotTrack || opts.TelemetryKey == "" || opts.SentryDSN == "",
3637
}
3738
}
3839

3940
type TelemetryOpts struct {
4041
AppName string
4142
AppVersion string
43+
SentryDSN string // used by error reporting
4244
TelemetryKey string
4345
}
4446
type telemetryMiddleware struct {
@@ -61,7 +63,7 @@ func (m *telemetryMiddleware) postRun(cmd *cobra.Command, args []string, runErr
6163
if m.disabled {
6264
return
6365
}
64-
66+
initSentry(m.opts)
6567
segmentClient, _ := segment.NewWithConfig(m.opts.TelemetryKey, segment.Config{
6668
BatchSize: 1, /* no batching */
6769
// Discard logs:
@@ -78,15 +80,47 @@ func (m *telemetryMiddleware) postRun(cmd *cobra.Command, args []string, runErr
7880
return // Ignore invalid commands
7981
}
8082

83+
var sentryEventID string
84+
if runErr != nil {
85+
defer sentry.Flush(2 * time.Second)
86+
sentryEventID = string(*sentry.CaptureException(runErr))
87+
}
88+
8189
trackEvent(segmentClient, &event{
82-
AppName: m.opts.AppName,
83-
AppVersion: m.opts.AppVersion,
84-
Command: subcmd.CommandPath(),
85-
CommandArgs: subargs,
86-
DeviceID: deviceID(),
87-
Duration: time.Since(m.startTime),
88-
Failed: runErr != nil,
89-
Packages: getPackages(cmd),
90+
AppName: m.opts.AppName,
91+
AppVersion: m.opts.AppVersion,
92+
Command: subcmd.CommandPath(),
93+
CommandArgs: subargs,
94+
DeviceID: deviceID(),
95+
Duration: time.Since(m.startTime),
96+
Failed: runErr != nil,
97+
Packages: getPackages(cmd),
98+
SentryEventID: sentryEventID,
99+
})
100+
}
101+
102+
func initSentry(opts TelemetryOpts) {
103+
sentrySyncTransport := sentry.NewHTTPSyncTransport()
104+
sentrySyncTransport.Timeout = time.Second * 2
105+
release := opts.AppName + "@" + opts.AppVersion
106+
environment := "production"
107+
if opts.AppVersion == "0.0.0-dev" {
108+
environment = "development"
109+
}
110+
111+
_ = sentry.Init(sentry.ClientOptions{
112+
Dsn: opts.SentryDSN,
113+
Environment: environment,
114+
Release: release,
115+
Transport: sentrySyncTransport,
116+
TracesSampleRate: 1,
117+
BeforeSend: func(event *sentry.Event, hint *sentry.EventHint) *sentry.Event {
118+
for i := range event.Exception {
119+
// edit in place and remove error message from tracking
120+
event.Exception[i].Value = ""
121+
}
122+
return event
123+
},
90124
})
91125
}
92126

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

128162
type event struct {
129-
AppName string
130-
AppVersion string
131-
Command string
132-
CommandArgs []string
133-
DeviceID string
134-
Duration time.Duration
135-
Failed bool
136-
Packages []string
163+
AppName string
164+
AppVersion string
165+
Command string
166+
CommandArgs []string
167+
DeviceID string
168+
Duration time.Duration
169+
Failed bool
170+
Packages []string
171+
SentryEventID string
137172
}
138173

139174
func trackEvent(client segment.Client, evt *event) {
@@ -157,6 +192,7 @@ func trackEvent(client segment.Client, evt *event) {
157192
Set("command_args", evt.CommandArgs).
158193
Set("failed", evt.Failed).
159194
Set("duration", evt.Duration.Milliseconds()).
160-
Set("packages", evt.Packages),
195+
Set("packages", evt.Packages).
196+
Set("sentry_event_id", evt.SentryEventID),
161197
})
162198
}

boxcli/root.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ func Execute(ctx context.Context, args []string) int {
4949
exe.AddMiddleware(midcobra.Telemetry(&midcobra.TelemetryOpts{
5050
AppName: "devbox",
5151
AppVersion: build.Version,
52+
SentryDSN: build.SentryDSN,
5253
TelemetryKey: build.TelemetryKey,
5354
}))
5455
exe.AddMiddleware(debugMiddleware)

build/build.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,6 @@ var (
66
Commit = "none"
77
CommitDate = "unknown"
88

9+
SentryDSN = "" // Disabled by default
910
TelemetryKey = "" // Disabled by default
1011
)

go.mod

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ require (
1212
github.com/creekorful/mvnparser v1.5.0
1313
github.com/denisbrodbeck/machineid v1.0.1
1414
github.com/fatih/color v1.13.0
15-
github.com/google/go-cmp v0.5.8
15+
github.com/google/go-cmp v0.5.9
1616
github.com/imdario/mergo v0.3.13
1717
github.com/mholt/archiver/v4 v4.0.0-alpha.7
1818
github.com/pelletier/go-toml/v2 v2.0.5
@@ -36,11 +36,12 @@ require (
3636
github.com/cpuguy83/go-md2man/v2 v2.0.2 // indirect
3737
github.com/davecgh/go-spew v1.1.1 // indirect
3838
github.com/dsnet/compress v0.0.1 // indirect
39+
github.com/getsentry/sentry-go v0.15.0 // indirect
3940
github.com/golang/snappy v0.0.4 // indirect
4041
github.com/google/uuid v1.3.0 // indirect
4142
github.com/inconshreveable/mousetrap v1.0.1 // indirect
4243
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51 // indirect
43-
github.com/klauspost/compress v1.15.5 // indirect
44+
github.com/klauspost/compress v1.15.11 // indirect
4445
github.com/klauspost/pgzip v1.2.5 // indirect
4546
github.com/mattn/go-colorable v0.1.13 // indirect
4647
github.com/mattn/go-isatty v0.0.16 // indirect

go.sum

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,13 @@ github.com/emicklei/proto v1.6.15 h1:XbpwxmuOPrdES97FrSfpyy67SSCV/wBIKXqgJzh6hNw
3636
github.com/fatih/color v1.7.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4=
3737
github.com/fatih/color v1.13.0 h1:8LOYc1KYPPmyKMuN8QV2DNRWNbLo6LZ0iLs8+mlH53w=
3838
github.com/fatih/color v1.13.0/go.mod h1:kLAiJbzzSOZDVNGyDpeOxJ47H46qBXwg5ILebYFFOfk=
39+
github.com/getsentry/sentry-go v0.15.0 h1:CP9bmA7pralrVUedYZsmIHWpq/pBtXTSew7xvVpfLaA=
40+
github.com/getsentry/sentry-go v0.15.0/go.mod h1:RZPJKSw+adu8PBNygiri/A98FqVr2HtRckJk9XVxJ9I=
3941
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b h1:VKtxabqXZkF25pY9ekfRL6a582T4P37/31XEstQ5p58=
4042
github.com/golang/snappy v0.0.4 h1:yAGX7huGHXlcLOEtBnF4w7FQwA26wojNCwOYAEhLjQM=
4143
github.com/golang/snappy v0.0.4/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q=
42-
github.com/google/go-cmp v0.5.8 h1:e6P7q2lk1O+qJJb4BtCQXlK8vWEO8V1ZeuEdJNOqZyg=
43-
github.com/google/go-cmp v0.5.8/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
44+
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
45+
github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
4446
github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=
4547
github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
4648
github.com/hinshun/vt10x v0.0.0-20220119200601-820417d04eec h1:qv2VnGeEQHchGaZ/u7lxST/RaJw+cv273q79D81Xbog=
@@ -54,6 +56,8 @@ github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51/go.mod h1:C
5456
github.com/klauspost/compress v1.4.1/go.mod h1:RyIbtBH6LamlWaDj8nUwkbUhJ87Yi3uG0guNDohfE1A=
5557
github.com/klauspost/compress v1.15.5 h1:qyCLMz2JCrKADihKOh9FxnW3houKeNsp2h5OEz0QSEA=
5658
github.com/klauspost/compress v1.15.5/go.mod h1:PhcZ0MbTNciWF3rruxRgKxI5NkcHHrHUDtV4Yw2GlzU=
59+
github.com/klauspost/compress v1.15.11 h1:Lcadnb3RKGin4FYM/orgq0qde+nc15E5Cbqg4B9Sx9c=
60+
github.com/klauspost/compress v1.15.11/go.mod h1:QPwzmACJjUTFsnSHH934V6woptycfrDDJnH7hvFVbGM=
5761
github.com/klauspost/cpuid v1.2.0/go.mod h1:Pj4uuM528wm8OyEC2QMXAi2YiTZ96dNQPGgoMS4s3ek=
5862
github.com/klauspost/pgzip v1.2.5 h1:qnWYvvKqedOF2ulHpMG72XQol4ILEJ8k2wwRl/Km8oE=
5963
github.com/klauspost/pgzip v1.2.5/go.mod h1:Ch1tH69qFZu15pkjo5kYi6mth2Zzwzt50oCQKQE9RUs=

0 commit comments

Comments
 (0)