Skip to content

Commit ebf6227

Browse files
authored
Small improvements to telemetry (#379)
## Summary - Add subcommand, args and pkgs information to Sentry (it's already in Amplitude) - Assign an anonymized user id for Sentry - Add shell info. - Detect WSL ## How was it tested? Built locally.
1 parent 41c5a2b commit ebf6227

File tree

6 files changed

+58
-21
lines changed

6 files changed

+58
-21
lines changed

internal/boxcli/midcobra/telemetry.go

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,8 @@ import (
88
"io"
99
"log"
1010
"os"
11-
"runtime"
1211
"time"
1312

14-
"github.com/denisbrodbeck/machineid"
1513
"github.com/getsentry/sentry-go"
1614
segment "github.com/segmentio/analytics-go"
1715
"github.com/spf13/cobra"
@@ -83,6 +81,16 @@ func (m *telemetryMiddleware) postRun(cmd *cobra.Command, args []string, runErr
8381
return // Ignore invalid commands
8482
}
8583

84+
pkgs := getPackages(cmd)
85+
86+
sentry.ConfigureScope(func(scope *sentry.Scope) {
87+
scope.SetTag("command", subcmd.CommandPath())
88+
scope.SetContext("command", map[string]interface{}{
89+
"subcommand": subcmd.CommandPath(),
90+
"args": subargs,
91+
"packages": pkgs,
92+
})
93+
})
8694
// verified with manual testing that the sentryID returned by CaptureException
8795
// is the same as m.ExecutionID, since we set EventID = m.ExecutionID in sentry.Init
8896
sentry.CaptureException(runErr)
@@ -96,11 +104,12 @@ func (m *telemetryMiddleware) postRun(cmd *cobra.Command, args []string, runErr
96104
AppVersion: m.opts.AppVersion,
97105
Command: subcmd.CommandPath(),
98106
CommandArgs: subargs,
99-
DeviceID: deviceID(),
107+
DeviceID: telemetry.DeviceID(),
100108
Duration: time.Since(m.startTime),
101109
Failed: runErr != nil,
102-
Packages: getPackages(cmd),
110+
Packages: pkgs,
103111
SentryEventID: sentryEventID,
112+
Shell: os.Getenv("SHELL"),
104113
})
105114
}
106115

@@ -109,12 +118,6 @@ func (m *telemetryMiddleware) withExecutionID(execID string) Middleware {
109118
return m
110119
}
111120

112-
func deviceID() string {
113-
salt := "64ee464f-9450-4b14-8d9c-014c0012ac1a"
114-
hashedID, _ := machineid.ProtectedID(salt) // Ensure machine id is hashed and non-identifiable
115-
return hashedID
116-
}
117-
118121
func getSubcommand(c *cobra.Command, args []string) (subcmd *cobra.Command, subargs []string, err error) {
119122
if c.TraverseChildren {
120123
subcmd, subargs, err = c.Traverse(args)
@@ -154,6 +157,7 @@ type event struct {
154157
Failed bool
155158
Packages []string
156159
SentryEventID string
160+
Shell string
157161
}
158162

159163
func trackEvent(client segment.Client, evt *event) {
@@ -169,7 +173,7 @@ func trackEvent(client segment.Client, evt *event) {
169173
Version: evt.AppVersion,
170174
},
171175
OS: segment.OSInfo{
172-
Name: runtime.GOOS,
176+
Name: telemetry.OS(),
173177
},
174178
},
175179
Properties: segment.NewProperties().
@@ -178,6 +182,7 @@ func trackEvent(client segment.Client, evt *event) {
178182
Set("failed", evt.Failed).
179183
Set("duration", evt.Duration.Milliseconds()).
180184
Set("packages", evt.Packages).
181-
Set("sentry_event_id", evt.SentryEventID),
185+
Set("sentry_event_id", evt.SentryEventID).
186+
Set("shell", evt.Shell),
182187
})
183188
}

internal/cloud/mutagen/install.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,11 @@ import (
88

99
"github.com/cavaliergopher/grab/v3"
1010
"go.jetpack.io/devbox/internal/debug"
11+
"go.jetpack.io/devbox/internal/fileutil"
1112
)
1213

1314
func InstallMutagenOnce(binPath string) error {
14-
if IsFile(binPath) {
15+
if fileutil.IsFile(binPath) {
1516
// Already installed, do nothing
1617
// TODO: ideally we would check that the right version
1718
// is installed, and maybe we should also validate
@@ -43,7 +44,7 @@ func Install(url string, installDir string) error {
4344
if err != nil {
4445
return err
4546
}
46-
err = Untar(tarReader, installDir)
47+
err = fileutil.Untar(tarReader, installDir)
4748
if err != nil {
4849
return err
4950
}

internal/cloud/mutagen/fileutil.go renamed to internal/fileutil/fileutil.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package mutagen
1+
package fileutil
22

33
import (
44
"os"
@@ -54,7 +54,12 @@ func IsSymlink(path string) bool {
5454
return (info.Mode().Type() & os.ModeSymlink) == os.ModeSymlink
5555
}
5656

57-
func ExistsOrErr(path string) error {
57+
func Exists(path string) bool {
58+
err := TryExists(path)
59+
return err == nil
60+
}
61+
62+
func TryExists(path string) error {
5863
_, err := os.Stat(path)
5964
return err
6065
}

internal/cloud/mutagen/untar.go renamed to internal/fileutil/untar.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package mutagen
1+
package fileutil
22

33
// TODO: publish as it's own shared package that other binaries
44
// can use.
@@ -15,7 +15,7 @@ import (
1515
)
1616

1717
func Untar(archive io.Reader, destPath string) error {
18-
err := ExistsOrErr(destPath)
18+
err := TryExists(destPath)
1919
if err != nil {
2020
return err
2121
}

internal/telemetry/sentry.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,12 @@ func (s *Sentry) Init(appName, appVersion, executionID string) {
4848
return event
4949
},
5050
})
51+
sentry.ConfigureScope(func(scope *sentry.Scope) {
52+
scope.SetUser(sentry.User{ID: DeviceID()})
53+
scope.SetContext("os", map[string]interface{}{
54+
"name": OS(),
55+
})
56+
})
5157
}
5258

5359
// CaptureException

internal/telemetry/telemetry.go

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,34 @@ package telemetry
22

33
import (
44
"os"
5+
"runtime"
56
"strconv"
7+
8+
"github.com/denisbrodbeck/machineid"
9+
"go.jetpack.io/devbox/internal/fileutil"
610
)
711

812
func DoNotTrack() bool {
913
// https://consoledonottrack.com/
10-
doNotTrack_, err := strconv.ParseBool(os.Getenv("DO_NOT_TRACK"))
14+
doNotTrack, err := strconv.ParseBool(os.Getenv("DO_NOT_TRACK"))
1115
if err != nil {
12-
doNotTrack_ = false
16+
doNotTrack = false
1317
}
14-
return doNotTrack_
18+
return doNotTrack
19+
}
20+
21+
func DeviceID() string {
22+
salt := "64ee464f-9450-4b14-8d9c-014c0012ac1a"
23+
hashedID, _ := machineid.ProtectedID(salt) // Ensure machine id is hashed and non-identifiable
24+
return hashedID
25+
}
26+
27+
func OS() string {
28+
os := runtime.GOOS
29+
// Special case for WSL, which is reported as 'linux' otherwise.
30+
if fileutil.Exists("/proc/sys/fs/binfmt_misc/WSLInterop") || fileutil.Exists("/run/WSL") {
31+
os = "wsl"
32+
}
33+
34+
return os
1535
}

0 commit comments

Comments
 (0)