Skip to content

Commit

Permalink
feat: add telemetry to detect Homebrew installations (#297)
Browse files Browse the repository at this point in the history
Signed-off-by: Darren Murray <darren.murray@lacework.net>
  • Loading branch information
dmurray-lacework authored Jan 21, 2021
1 parent 08a3e61 commit fa81abc
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 20 deletions.
2 changes: 1 addition & 1 deletion cli/cmd/cli_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ var promptIconsFunc = func(icons *survey.IconSet) {
// UpdateCommand returns the command that a user should run to update the cli
// to the latest available version (unix specific command)
func (c *cliState) UpdateCommand() string {
if os.Getenv("LW_HOMEBREW_INSTALL") != "" {
if os.Getenv(HomebrewInstall) != "" {
return `
$ brew upgrade lacework-cli
`
Expand Down
51 changes: 32 additions & 19 deletions cli/cmd/honeyvent.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ const (
// disable telemetry sent to Honeycomb
DisableTelemetry = "LW_TELEMETRY_DISABLE"

// HomebrewInstall is an environment variable that denotes the
// install method was via homebrew package manager
HomebrewInstall = "LW_HOMEBREW_INSTALL"

// List of Features
//
// A feature within the Lacework CLI is any functionality that
Expand Down Expand Up @@ -81,18 +85,19 @@ const (

// Honeyvent defines what a Honeycomb event looks like for the Lacework CLI
type Honeyvent struct {
Version string `json:"version"`
Os string `json:"os"`
Arch string `json:"arch"`
Command string `json:"command,omitempty"`
Args []string `json:"args,omitempty"`
Account string `json:"account,omitempty"`
Profile string `json:"profile,omitempty"`
ApiKey string `json:"api_key,omitempty"`
Feature string `json:"feature,omitempty"`
FeatureData interface{} `json:"feature.data,omitempty"`
DurationMs int64 `json:"duration_ms,omitempty"`
Error string `json:"error,omitempty"`
Version string `json:"version"`
Os string `json:"os"`
Arch string `json:"arch"`
Command string `json:"command,omitempty"`
Args []string `json:"args,omitempty"`
Account string `json:"account,omitempty"`
Profile string `json:"profile,omitempty"`
ApiKey string `json:"api_key,omitempty"`
Feature string `json:"feature,omitempty"`
FeatureData interface{} `json:"feature.data,omitempty"`
DurationMs int64 `json:"duration_ms,omitempty"`
Error string `json:"error,omitempty"`
InstallMethod string `json:"install_method,omitempty"`

// tracing data for multiple events, this is useful for specific features
// within the Lacework CLI such as daily version check, polling mechanism, etc.
Expand All @@ -112,13 +117,14 @@ func (c *cliState) InitHoneyvent() {
_ = libhoney.Init(hc)

c.Event = &Honeyvent{
Os: runtime.GOOS,
Arch: runtime.GOARCH,
Version: Version,
Profile: c.Profile,
Account: c.Account,
ApiKey: c.KeyID,
TraceID: newID(),
Os: runtime.GOOS,
Arch: runtime.GOARCH,
Version: Version,
Profile: c.Profile,
Account: c.Account,
ApiKey: c.KeyID,
TraceID: newID(),
InstallMethod: installMethod(),
}
}

Expand Down Expand Up @@ -191,3 +197,10 @@ func (e *Honeyvent) AddFeatureField(key string, value interface{}) {
e.FeatureData = v
}
}

func installMethod() string {
if os.Getenv(HomebrewInstall) != "" {
return "HOMEBREW"
}
return ""
}
17 changes: 17 additions & 0 deletions cli/cmd/honeyvent_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ func TestHoneyventDefaultParameters(t *testing.T) {
assert.NotEmpty(t, cli.Event.TraceID)
assert.Empty(t, cli.Event.SpanID)
assert.Empty(t, cli.Event.ParentID)
assert.Empty(t, cli.Event.InstallMethod)
}

func TestSendHoneyventTracingFields(t *testing.T) {
Expand Down Expand Up @@ -105,3 +106,19 @@ func TestSendHoneyventDisableTelemetry(t *testing.T) {
// this validates that the environment variable is not sending
// events when it is set (disabled)
}

func TestSendHoneyventHomebrewInstall(t *testing.T) {
// testing that the install method will be "Homebrew"
// environment variable 'LW_HOMEBREW_INSTALL' is set
os.Setenv(HomebrewInstall, "1")
defer os.Setenv(HomebrewInstall, "")

// init honeyvent as InstallMethod is set on init
cli.InitHoneyvent()

// mocking sending honeyvent
cli.SendHoneyvent()

assert.NotEmpty(t, cli.Event.InstallMethod)
assert.Equal(t, "HOMEBREW", cli.Event.InstallMethod)
}

0 comments on commit fa81abc

Please sign in to comment.