|
| 1 | +// Copyright 2022 Jetpack Technologies Inc and contributors. All rights reserved. |
| 2 | +// Use of this source code is governed by the license in the LICENSE file. |
| 3 | + |
| 4 | +package midcobra |
| 5 | + |
| 6 | +import ( |
| 7 | + "fmt" |
| 8 | + "os" |
| 9 | + "runtime" |
| 10 | + "strconv" |
| 11 | + "time" |
| 12 | + |
| 13 | + "github.com/denisbrodbeck/machineid" |
| 14 | + segment "github.com/segmentio/analytics-go" |
| 15 | + "github.com/spf13/cobra" |
| 16 | +) |
| 17 | + |
| 18 | +// We collect some light telemetry to be able to improve devbox over time. |
| 19 | +// We're aware how important privacy is and value it ourselves, so we have |
| 20 | +// the following rules: |
| 21 | +// 1. We only collect anonymized data – nothing that is personally identifiable |
| 22 | +// 2. Data is only stored in SOC 2 compliant systems, and we are SOC 2 compliant ourselves. |
| 23 | +// 3. Users should always have the ability to opt-out. |
| 24 | +func Telemetry(opts *TelemetryOpts) Middleware { |
| 25 | + doNotTrack, err := strconv.ParseBool(os.Getenv("DO_NOT_TRACK")) // https://consoledonottrack.com/ |
| 26 | + if err != nil { |
| 27 | + doNotTrack = false |
| 28 | + } |
| 29 | + |
| 30 | + return &telemetryMiddleware{ |
| 31 | + opts: *opts, |
| 32 | + disabled: doNotTrack || opts.TelemetryKey == "", |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +type TelemetryOpts struct { |
| 37 | + AppName string |
| 38 | + AppVersion string |
| 39 | + TelemetryKey string |
| 40 | +} |
| 41 | +type telemetryMiddleware struct { |
| 42 | + // Setup: |
| 43 | + opts TelemetryOpts |
| 44 | + disabled bool |
| 45 | + |
| 46 | + // Used during execution: |
| 47 | + startTime time.Time |
| 48 | +} |
| 49 | + |
| 50 | +// telemetryMiddleware implements interface Middleware (compile-time check) |
| 51 | +var _ Middleware = (*telemetryMiddleware)(nil) |
| 52 | + |
| 53 | +func (m *telemetryMiddleware) preRun(cmd *cobra.Command, args []string) { |
| 54 | + m.startTime = time.Now() |
| 55 | +} |
| 56 | + |
| 57 | +func (m *telemetryMiddleware) postRun(cmd *cobra.Command, args []string, runErr error) { |
| 58 | + if m.disabled { |
| 59 | + return |
| 60 | + } |
| 61 | + |
| 62 | + segmentClient := segment.New(m.opts.TelemetryKey) |
| 63 | + defer func() { |
| 64 | + _ = segmentClient.Close() |
| 65 | + }() |
| 66 | + |
| 67 | + subcmd, subargs, parseErr := getSubcommand(cmd, args) |
| 68 | + if parseErr != nil { |
| 69 | + return // Ignore invalid commands |
| 70 | + } |
| 71 | + |
| 72 | + trackEvent(segmentClient, &event{ |
| 73 | + AppName: m.opts.AppName, |
| 74 | + AppVersion: m.opts.AppVersion, |
| 75 | + Command: subcmd.CommandPath(), |
| 76 | + CommandArgs: subargs, |
| 77 | + DeviceID: deviceID(), |
| 78 | + Duration: time.Since(m.startTime), |
| 79 | + Failed: runErr != nil, |
| 80 | + }) |
| 81 | +} |
| 82 | + |
| 83 | +func deviceID() string { |
| 84 | + salt := "64ee464f-9450-4b14-8d9c-014c0012ac1a" |
| 85 | + hashedID, _ := machineid.ProtectedID(salt) // Ensure machine id is hashed and non-identifiable |
| 86 | + return hashedID |
| 87 | +} |
| 88 | + |
| 89 | +func getSubcommand(c *cobra.Command, args []string) (subcmd *cobra.Command, subargs []string, err error) { |
| 90 | + if c.TraverseChildren { |
| 91 | + subcmd, subargs, err = c.Traverse(args) |
| 92 | + } else { |
| 93 | + subcmd, subargs, err = c.Find(args) |
| 94 | + } |
| 95 | + return subcmd, subargs, err |
| 96 | +} |
| 97 | + |
| 98 | +type event struct { |
| 99 | + AppName string |
| 100 | + AppVersion string |
| 101 | + Command string |
| 102 | + CommandArgs []string |
| 103 | + DeviceID string |
| 104 | + Duration time.Duration |
| 105 | + Failed bool |
| 106 | +} |
| 107 | + |
| 108 | +func trackEvent(client segment.Client, evt *event) { |
| 109 | + _ = client.Enqueue(segment.Track{ // Ignore errors, telemetry is best effort |
| 110 | + AnonymousId: evt.DeviceID, // Use device id instead |
| 111 | + Event: fmt.Sprintf("[%s] Command: %s", evt.AppName, evt.Command), |
| 112 | + Context: &segment.Context{ |
| 113 | + Device: segment.DeviceInfo{ |
| 114 | + Id: evt.DeviceID, |
| 115 | + }, |
| 116 | + App: segment.AppInfo{ |
| 117 | + Name: evt.AppName, |
| 118 | + Version: evt.AppVersion, |
| 119 | + }, |
| 120 | + OS: segment.OSInfo{ |
| 121 | + Name: runtime.GOOS, |
| 122 | + }, |
| 123 | + }, |
| 124 | + Properties: segment.NewProperties(). |
| 125 | + Set("command", evt.Command). |
| 126 | + Set("command_args", evt.CommandArgs). |
| 127 | + Set("failed", evt.Failed). |
| 128 | + Set("duration", evt.Duration.Milliseconds()), |
| 129 | + }) |
| 130 | +} |
0 commit comments