Skip to content

Commit 400597a

Browse files
committed
enable metrics by default, and set random user id
1 parent aac0572 commit 400597a

File tree

2 files changed

+26
-14
lines changed

2 files changed

+26
-14
lines changed

pkg/constants/constants.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,7 @@ const (
165165
ConfigAPMCredentialsFileKey = "credentials-file"
166166
ConfigAPMAdminAPIEndpointKey = "admin-api-endpoint"
167167
ConfigNodeConfigKey = "node-config"
168+
ConfigMetricsUserIDKey = "MetricsUserID"
168169
ConfigMetricsEnabledKey = "MetricsEnabled"
169170
ConfigUpdatesDisabledKey = "UpdatesDisabled"
170171
ConfigAuthorizeCloudAccessKey = "AuthorizeCloudAccess"

pkg/metrics/metrics.go

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,16 @@
33
package metrics
44

55
import (
6-
"crypto/sha256"
7-
"encoding/base64"
8-
"fmt"
96
"os"
10-
"os/user"
117
"path/filepath"
128
"runtime"
139
"strings"
1410

1511
"github.com/ava-labs/avalanche-cli/pkg/application"
1612
"github.com/ava-labs/avalanche-cli/pkg/constants"
17-
1813
"github.com/ava-labs/avalanche-cli/pkg/utils"
14+
"github.com/ava-labs/avalanche-cli/pkg/ux"
15+
"github.com/ava-labs/avalanchego/utils/logging"
1916

2017
"github.com/posthog/posthog-go"
2118
"github.com/spf13/cobra"
@@ -40,14 +37,32 @@ func GetCLIVersion() string {
4037
return string(content)
4138
}
4239

43-
func userIsOptedIn(app *application.Avalanche) bool {
44-
if app.Conf.ConfigFileExists() && app.Conf.GetConfigBoolValue(constants.ConfigMetricsEnabledKey) {
45-
return true
40+
func getMetricsUserID(app *application.Avalanche) string {
41+
if !app.Conf.ConfigFileExists() || !app.Conf.ConfigValueIsSet(constants.ConfigMetricsUserIDKey) {
42+
userID := utils.RandomString(20)
43+
if err := app.Conf.SetConfigValue(constants.ConfigMetricsUserIDKey, userID); err != nil {
44+
ux.Logger.PrintToUser(logging.Red.Wrap("failure initializing metrics id: %s"), err)
45+
}
46+
return userID
4647
}
47-
return false
48+
return app.Conf.GetConfigStringValue(constants.ConfigMetricsUserIDKey)
49+
}
50+
51+
func notInitialized(app *application.Avalanche) bool {
52+
return !app.Conf.ConfigFileExists() || !app.Conf.ConfigValueIsSet(constants.ConfigMetricsEnabledKey)
53+
}
54+
55+
func userIsOptedIn(app *application.Avalanche) bool {
56+
return app.Conf.ConfigFileExists() && app.Conf.GetConfigBoolValue(constants.ConfigMetricsEnabledKey)
4857
}
4958

5059
func HandleTracking(cmd *cobra.Command, commandPath string, app *application.Avalanche, flags map[string]string) {
60+
if notInitialized(app) {
61+
if err := app.Conf.SetConfigValue(constants.ConfigMetricsEnabledKey, true); err != nil {
62+
ux.Logger.PrintToUser(logging.Red.Wrap("failure initializing metrics default: %s"), err)
63+
}
64+
_ = getMetricsUserID(app)
65+
}
5166
if !userIsOptedIn(app) {
5267
return
5368
}
@@ -80,9 +95,7 @@ func trackMetrics(app *application.Avalanche, commandPath string, flags map[stri
8095
version = GetCLIVersion()
8196
}
8297

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

87100
telemetryProperties := make(map[string]interface{})
88101
telemetryProperties["command"] = commandPath
@@ -93,8 +106,6 @@ func trackMetrics(app *application.Avalanche, commandPath string, flags map[stri
93106
if insideCodespace {
94107
codespaceName := os.Getenv(constants.CodespaceNameEnvVar)
95108
telemetryProperties["codespace"] = codespaceName
96-
hash := sha256.Sum256([]byte(codespaceName))
97-
userID = base64.StdEncoding.EncodeToString(hash[:])
98109
}
99110
for propertyKey, propertyValue := range flags {
100111
telemetryProperties[propertyKey] = propertyValue

0 commit comments

Comments
 (0)