-
Notifications
You must be signed in to change notification settings - Fork 184
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[devbox] Add anonymized telemetry (#40)
## Summary Add anonymized telemetry to understand usage of devbox and further improve it. We allow users to opt-out via an environment variable. To add telemetry I implemented a small "midcobra" framework, that make it possible to add "middlware" to cobra CLIs. The plan is to re-use the midcobra functionality in other binaries like `envsec`. ## How was it tested? Built locally, ran, and checked telemetry logs.
- Loading branch information
Showing
8 changed files
with
232 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
// Copyright 2022 Jetpack Technologies Inc and contributors. All rights reserved. | ||
// Use of this source code is governed by the license in the LICENSE file. | ||
|
||
package midcobra | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/spf13/cobra" | ||
) | ||
|
||
type Executable interface { | ||
AddMiddleware(mids ...Middleware) | ||
Execute(ctx context.Context, args []string) int | ||
} | ||
|
||
type Middleware interface { | ||
preRun(cmd *cobra.Command, args []string) | ||
postRun(cmd *cobra.Command, args []string, runErr error) | ||
} | ||
|
||
func New(cmd *cobra.Command) Executable { | ||
return &midcobraExecutable{ | ||
cmd: cmd, | ||
middlewares: []Middleware{}, | ||
} | ||
} | ||
|
||
type midcobraExecutable struct { | ||
cmd *cobra.Command | ||
middlewares []Middleware | ||
} | ||
|
||
var _ Executable = (*midcobraExecutable)(nil) | ||
|
||
func (ex *midcobraExecutable) AddMiddleware(mids ...Middleware) { | ||
ex.middlewares = append(ex.middlewares, mids...) | ||
} | ||
|
||
func (ex *midcobraExecutable) Execute(ctx context.Context, args []string) int { | ||
// Ensure cobra uses the same arguments | ||
ex.cmd.SetArgs(args) | ||
|
||
// Run the 'pre' hooks | ||
for _, m := range ex.middlewares { | ||
m.preRun(ex.cmd, args) | ||
} | ||
|
||
// Execute the cobra command: | ||
err := ex.cmd.ExecuteContext(ctx) | ||
|
||
// Run the 'post' hooks. Note that unlike the default PostRun cobra functionality these | ||
// run even if the command resulted in an error. This is useful when we still want to clean up | ||
// before the program exists or we want to log something. The error, if any, gets passed | ||
// to the post hook. | ||
for _, m := range ex.middlewares { | ||
m.postRun(ex.cmd, args, err) | ||
} | ||
|
||
if err != nil { | ||
return 1 // Error exit code | ||
} else { | ||
return 0 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
// Copyright 2022 Jetpack Technologies Inc and contributors. All rights reserved. | ||
// Use of this source code is governed by the license in the LICENSE file. | ||
|
||
package midcobra | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"runtime" | ||
"strconv" | ||
"time" | ||
|
||
"github.com/denisbrodbeck/machineid" | ||
segment "github.com/segmentio/analytics-go" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
// We collect some light telemetry to be able to improve devbox over time. | ||
// We're aware how important privacy is and value it ourselves, so we have | ||
// the following rules: | ||
// 1. We only collect anonymized data – nothing that is personally identifiable | ||
// 2. Data is only stored in SOC 2 compliant systems, and we are SOC 2 compliant ourselves. | ||
// 3. Users should always have the ability to opt-out. | ||
func Telemetry(opts *TelemetryOpts) Middleware { | ||
doNotTrack, err := strconv.ParseBool(os.Getenv("DO_NOT_TRACK")) // https://consoledonottrack.com/ | ||
if err != nil { | ||
doNotTrack = false | ||
} | ||
|
||
return &telemetryMiddleware{ | ||
opts: *opts, | ||
disabled: doNotTrack || opts.TelemetryKey == "", | ||
} | ||
} | ||
|
||
type TelemetryOpts struct { | ||
AppName string | ||
AppVersion string | ||
TelemetryKey string | ||
} | ||
type telemetryMiddleware struct { | ||
// Setup: | ||
opts TelemetryOpts | ||
disabled bool | ||
|
||
// Used during execution: | ||
startTime time.Time | ||
} | ||
|
||
// telemetryMiddleware implements interface Middleware (compile-time check) | ||
var _ Middleware = (*telemetryMiddleware)(nil) | ||
|
||
func (m *telemetryMiddleware) preRun(cmd *cobra.Command, args []string) { | ||
m.startTime = time.Now() | ||
} | ||
|
||
func (m *telemetryMiddleware) postRun(cmd *cobra.Command, args []string, runErr error) { | ||
if m.disabled { | ||
return | ||
} | ||
|
||
segmentClient := segment.New(m.opts.TelemetryKey) | ||
defer func() { | ||
_ = segmentClient.Close() | ||
}() | ||
|
||
subcmd, subargs, parseErr := getSubcommand(cmd, args) | ||
if parseErr != nil { | ||
return // Ignore invalid commands | ||
} | ||
|
||
trackEvent(segmentClient, &event{ | ||
AppName: m.opts.AppName, | ||
AppVersion: m.opts.AppVersion, | ||
Command: subcmd.CommandPath(), | ||
CommandArgs: subargs, | ||
DeviceID: deviceID(), | ||
Duration: time.Since(m.startTime), | ||
Failed: runErr != nil, | ||
}) | ||
} | ||
|
||
func deviceID() string { | ||
salt := "64ee464f-9450-4b14-8d9c-014c0012ac1a" | ||
hashedID, _ := machineid.ProtectedID(salt) // Ensure machine id is hashed and non-identifiable | ||
return hashedID | ||
} | ||
|
||
func getSubcommand(c *cobra.Command, args []string) (subcmd *cobra.Command, subargs []string, err error) { | ||
if c.TraverseChildren { | ||
subcmd, subargs, err = c.Traverse(args) | ||
} else { | ||
subcmd, subargs, err = c.Find(args) | ||
} | ||
return subcmd, subargs, err | ||
} | ||
|
||
type event struct { | ||
AppName string | ||
AppVersion string | ||
Command string | ||
CommandArgs []string | ||
DeviceID string | ||
Duration time.Duration | ||
Failed bool | ||
} | ||
|
||
func trackEvent(client segment.Client, evt *event) { | ||
_ = client.Enqueue(segment.Track{ // Ignore errors, telemetry is best effort | ||
AnonymousId: evt.DeviceID, // Use device id instead | ||
Event: fmt.Sprintf("[%s] Command: %s", evt.AppName, evt.Command), | ||
Context: &segment.Context{ | ||
Device: segment.DeviceInfo{ | ||
Id: evt.DeviceID, | ||
}, | ||
App: segment.AppInfo{ | ||
Name: evt.AppName, | ||
Version: evt.AppVersion, | ||
}, | ||
OS: segment.OSInfo{ | ||
Name: runtime.GOOS, | ||
}, | ||
}, | ||
Properties: segment.NewProperties(). | ||
Set("command", evt.Command). | ||
Set("command_args", evt.CommandArgs). | ||
Set("failed", evt.Failed). | ||
Set("duration", evt.Duration.Milliseconds()), | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,4 +5,6 @@ var ( | |
Version = "0.0.0-dev" | ||
Commit = "none" | ||
CommitDate = "unknown" | ||
|
||
TelemetryKey = "" // Disabled by default | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters