4
4
package midcobra
5
5
6
6
import (
7
- "fmt"
8
7
"os"
9
8
"runtime/trace"
10
9
"sort"
11
- "strings"
12
- "time"
13
10
14
- segment "github.com/segmentio/analytics-go"
15
11
"github.com/spf13/cobra"
16
12
"github.com/spf13/pflag"
17
13
18
14
"go.jetpack.io/devbox"
19
15
"go.jetpack.io/devbox/internal/boxcli/featureflag"
20
- "go.jetpack.io/devbox/internal/build"
21
16
"go.jetpack.io/devbox/internal/envir"
22
17
"go.jetpack.io/devbox/internal/impl/devopt"
23
18
"go.jetpack.io/devbox/internal/telemetry"
@@ -33,24 +28,13 @@ func Telemetry() Middleware {
33
28
return & telemetryMiddleware {}
34
29
}
35
30
36
- type telemetryMiddleware struct {
37
- // Used during execution:
38
- startTime time.Time
39
- }
31
+ type telemetryMiddleware struct {}
40
32
41
33
// telemetryMiddleware implements interface Middleware (compile-time check)
42
34
var _ Middleware = (* telemetryMiddleware )(nil )
43
35
44
36
func (m * telemetryMiddleware ) preRun (cmd * cobra.Command , args []string ) {
45
- m .startTime = telemetry .CommandStartTime ()
46
-
47
- telemetry .Start (telemetry .AppDevbox )
48
- ctx := cmd .Context ()
49
- defer trace .StartRegion (ctx , "telemetryPreRun" ).End ()
50
- if ! telemetry .Enabled () {
51
- trace .Log (ctx , "telemetry" , "telemetry is disabled" )
52
- return
53
- }
37
+ telemetry .Start ()
54
38
}
55
39
56
40
func (m * telemetryMiddleware ) postRun (cmd * cobra.Command , args []string , runErr error ) {
@@ -74,127 +58,12 @@ func (m *telemetryMiddleware) postRun(cmd *cobra.Command, args []string, runErr
74
58
meta .InShell = envir .IsDevboxShellEnabled ()
75
59
meta .InBrowser = envir .IsInBrowser ()
76
60
meta .InCloud = envir .IsDevboxCloud ()
77
- telemetry .Error (runErr , meta )
78
-
79
- if ! telemetry .Enabled () {
80
- return
81
- }
82
- evt := m .newEventIfValid (cmd , args , runErr )
83
- if evt == nil {
84
- return
85
- }
86
- m .trackEvent (evt ) // Segment
87
- }
88
-
89
- // Consider renaming this to commandEvent
90
- // since it has info about the specific command run.
91
- type event struct {
92
- telemetry.Event
93
- Command string
94
- CommandArgs []string
95
- CommandError error
96
- CommandHidden bool
97
- Failed bool
98
- Packages []string
99
- CommitHash string // the nikpkgs commit hash in devbox.json
100
- InDevboxShell bool
101
- DevboxEnv map [string ]any // Devbox-specific environment variables
102
- SentryEventID string
103
- Shell string
104
- }
105
-
106
- // newEventIfValid creates a new telemetry event, but returns nil if we cannot construct
107
- // a valid event.
108
- func (m * telemetryMiddleware ) newEventIfValid (cmd * cobra.Command , args []string , runErr error ) * event {
109
- subcmd , flags , parseErr := getSubcommand (cmd , args )
110
- if parseErr != nil {
111
- // Ignore invalid commands
112
- return nil
113
- }
114
-
115
- pkgs , hash := getPackagesAndCommitHash (cmd )
116
61
117
- // an empty userID means that we do not have a github username saved
118
- userID := telemetry .UserIDFromGithubUsername ()
119
-
120
- devboxEnv := map [string ]interface {}{}
121
- for _ , e := range os .Environ () {
122
- if strings .HasPrefix (e , "DEVBOX" ) && strings .Contains (e , "=" ) {
123
- key := strings .Split (e , "=" )[0 ]
124
- devboxEnv [key ] = os .Getenv (key )
125
- }
126
- }
127
-
128
- return & event {
129
- Event : telemetry.Event {
130
- AnonymousID : telemetry .DeviceID ,
131
- AppName : telemetry .AppDevbox ,
132
- AppVersion : build .Version ,
133
- CloudRegion : os .Getenv (envir .DevboxRegion ),
134
- Duration : time .Since (m .startTime ),
135
- OsName : build .OS (),
136
- UserID : userID ,
137
- },
138
- Command : subcmd .CommandPath (),
139
- CommandArgs : flags ,
140
- CommandError : runErr ,
141
- // The command is hidden if either the top-level command is hidden or
142
- // the specific sub-command that was executed is hidden.
143
- CommandHidden : cmd .Hidden || subcmd .Hidden ,
144
- Failed : runErr != nil ,
145
- Packages : pkgs ,
146
- CommitHash : hash ,
147
- InDevboxShell : envir .IsDevboxShellEnabled (),
148
- DevboxEnv : devboxEnv ,
149
- Shell : os .Getenv (envir .Shell ),
150
- }
151
- }
152
-
153
- func (m * telemetryMiddleware ) trackEvent (evt * event ) {
154
- if evt == nil || evt .CommandHidden {
62
+ if runErr != nil {
63
+ telemetry .Error (runErr , meta )
155
64
return
156
65
}
157
-
158
- if evt .CommandError != nil {
159
- evt .SentryEventID = telemetry .ExecutionID
160
- }
161
- segmentClient := telemetry .NewSegmentClient (build .TelemetryKey )
162
- defer func () {
163
- _ = segmentClient .Close ()
164
- }()
165
-
166
- // deliberately ignore error
167
- _ = segmentClient .Enqueue (segment.Identify {
168
- AnonymousId : evt .AnonymousID ,
169
- UserId : evt .UserID ,
170
- })
171
-
172
- _ = segmentClient .Enqueue (segment.Track { // Ignore errors, telemetry is best effort
173
- AnonymousId : evt .AnonymousID , // Use device id instead
174
- Event : fmt .Sprintf ("[%s] Command: %s" , evt .AppName , evt .Command ),
175
- Context : & segment.Context {
176
- Device : segment.DeviceInfo {
177
- Id : evt .AnonymousID ,
178
- },
179
- App : segment.AppInfo {
180
- Name : evt .AppName ,
181
- Version : evt .AppVersion ,
182
- },
183
- OS : segment.OSInfo {
184
- Name : evt .OsName ,
185
- },
186
- },
187
- Properties : segment .NewProperties ().
188
- Set ("cloud_region" , evt .CloudRegion ).
189
- Set ("command" , evt .Command ).
190
- Set ("command_args" , evt .CommandArgs ).
191
- Set ("failed" , evt .Failed ).
192
- Set ("duration" , evt .Duration .Milliseconds ()).
193
- Set ("packages" , evt .Packages ).
194
- Set ("sentry_event_id" , evt .SentryEventID ).
195
- Set ("shell" , evt .Shell ),
196
- UserId : evt .UserID ,
197
- })
66
+ telemetry .Event (telemetry .EventCommandSuccess , meta )
198
67
}
199
68
200
69
func getSubcommand (cmd * cobra.Command , args []string ) (subcmd * cobra.Command , flags []string , err error ) {
0 commit comments