@@ -13,6 +13,7 @@ import (
13
13
"time"
14
14
15
15
"github.com/denisbrodbeck/machineid"
16
+ "github.com/getsentry/sentry-go"
16
17
segment "github.com/segmentio/analytics-go"
17
18
"github.com/spf13/cobra"
18
19
"go.jetpack.io/devbox"
@@ -32,13 +33,14 @@ func Telemetry(opts *TelemetryOpts) Middleware {
32
33
33
34
return & telemetryMiddleware {
34
35
opts : * opts ,
35
- disabled : doNotTrack || opts .TelemetryKey == "" ,
36
+ disabled : doNotTrack || opts .TelemetryKey == "" || opts . SentryDSN == "" ,
36
37
}
37
38
}
38
39
39
40
type TelemetryOpts struct {
40
41
AppName string
41
42
AppVersion string
43
+ SentryDSN string // used by error reporting
42
44
TelemetryKey string
43
45
}
44
46
type telemetryMiddleware struct {
@@ -61,7 +63,7 @@ func (m *telemetryMiddleware) postRun(cmd *cobra.Command, args []string, runErr
61
63
if m .disabled {
62
64
return
63
65
}
64
-
66
+ initSentry ( m . opts )
65
67
segmentClient , _ := segment .NewWithConfig (m .opts .TelemetryKey , segment.Config {
66
68
BatchSize : 1 , /* no batching */
67
69
// Discard logs:
@@ -78,15 +80,47 @@ func (m *telemetryMiddleware) postRun(cmd *cobra.Command, args []string, runErr
78
80
return // Ignore invalid commands
79
81
}
80
82
83
+ var sentryEventID string
84
+ if runErr != nil {
85
+ defer sentry .Flush (2 * time .Second )
86
+ sentryEventID = string (* sentry .CaptureException (runErr ))
87
+ }
88
+
81
89
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
+ },
90
124
})
91
125
}
92
126
@@ -126,14 +160,15 @@ func getPackages(c *cobra.Command) []string {
126
160
}
127
161
128
162
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
137
172
}
138
173
139
174
func trackEvent (client segment.Client , evt * event ) {
@@ -157,6 +192,7 @@ func trackEvent(client segment.Client, evt *event) {
157
192
Set ("command_args" , evt .CommandArgs ).
158
193
Set ("failed" , evt .Failed ).
159
194
Set ("duration" , evt .Duration .Milliseconds ()).
160
- Set ("packages" , evt .Packages ),
195
+ Set ("packages" , evt .Packages ).
196
+ Set ("sentry_event_id" , evt .SentryEventID ),
161
197
})
162
198
}
0 commit comments