Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

enable metrics by default, and set random user id #2644

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions pkg/constants/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ const (
ConfigAPMCredentialsFileKey = "credentials-file"
ConfigAPMAdminAPIEndpointKey = "admin-api-endpoint"
ConfigNodeConfigKey = "node-config"
ConfigMetricsUserIDKey = "MetricsUserID"
ConfigMetricsEnabledKey = "MetricsEnabled"
ConfigUpdatesDisabledKey = "UpdatesDisabled"
ConfigAuthorizeCloudAccessKey = "AuthorizeCloudAccess"
Expand Down
39 changes: 25 additions & 14 deletions pkg/metrics/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,16 @@
package metrics

import (
"crypto/sha256"
"encoding/base64"
"fmt"
"os"
"os/user"
"path/filepath"
"runtime"
"strings"

"github.com/ava-labs/avalanche-cli/pkg/application"
"github.com/ava-labs/avalanche-cli/pkg/constants"

"github.com/ava-labs/avalanche-cli/pkg/utils"
"github.com/ava-labs/avalanche-cli/pkg/ux"
"github.com/ava-labs/avalanchego/utils/logging"

"github.com/posthog/posthog-go"
"github.com/spf13/cobra"
Expand All @@ -40,14 +37,32 @@ func GetCLIVersion() string {
return string(content)
}

func userIsOptedIn(app *application.Avalanche) bool {
if app.Conf.ConfigFileExists() && app.Conf.GetConfigBoolValue(constants.ConfigMetricsEnabledKey) {
return true
func getMetricsUserID(app *application.Avalanche) string {
if !app.Conf.ConfigFileExists() || !app.Conf.ConfigValueIsSet(constants.ConfigMetricsUserIDKey) {
userID := utils.RandomString(20)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why are we replacing user id?

Current implementation
hash := sha256.Sum256([]byte(codespaceName)) userID = base64.StdEncoding.EncodeToString(hash[:])

SHA 256 is irreversible

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and it eliminates the need to check for app config files

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

legal considers this approach, totally independant of user name for id generation, to be the most
beneficial for the purpose cc @javiertc @learyce

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

checking app config file is not too much of an problem IMO

if err := app.Conf.SetConfigValue(constants.ConfigMetricsUserIDKey, userID); err != nil {
ux.Logger.PrintToUser(logging.Red.Wrap("failure initializing metrics id: %s"), err)
}
return userID
}
return false
return app.Conf.GetConfigStringValue(constants.ConfigMetricsUserIDKey)
}

func notInitialized(app *application.Avalanche) bool {
return !app.Conf.ConfigFileExists() || !app.Conf.ConfigValueIsSet(constants.ConfigMetricsEnabledKey)
}

func userIsOptedIn(app *application.Avalanche) bool {
return app.Conf.ConfigFileExists() && app.Conf.GetConfigBoolValue(constants.ConfigMetricsEnabledKey)
}

func HandleTracking(cmd *cobra.Command, commandPath string, app *application.Avalanche, flags map[string]string) {
if notInitialized(app) {
if err := app.Conf.SetConfigValue(constants.ConfigMetricsEnabledKey, true); err != nil {
ux.Logger.PrintToUser(logging.Red.Wrap("failure initializing metrics default: %s"), err)
}
_ = getMetricsUserID(app)
}
if !userIsOptedIn(app) {
return
}
Expand Down Expand Up @@ -80,9 +95,7 @@ func trackMetrics(app *application.Avalanche, commandPath string, flags map[stri
version = GetCLIVersion()
}

usr, _ := user.Current() // use empty string if err
hash := sha256.Sum256([]byte(fmt.Sprintf("%s%s", usr.Username, usr.Uid)))
userID := base64.StdEncoding.EncodeToString(hash[:])
userID := getMetricsUserID(app)

telemetryProperties := make(map[string]interface{})
telemetryProperties["command"] = commandPath
Expand All @@ -93,8 +106,6 @@ func trackMetrics(app *application.Avalanche, commandPath string, flags map[stri
if insideCodespace {
codespaceName := os.Getenv(constants.CodespaceNameEnvVar)
telemetryProperties["codespace"] = codespaceName
hash := sha256.Sum256([]byte(codespaceName))
userID = base64.StdEncoding.EncodeToString(hash[:])
}
for propertyKey, propertyValue := range flags {
telemetryProperties[propertyKey] = propertyValue
Expand Down
Loading