3
3
package metrics
4
4
5
5
import (
6
- "crypto/sha256"
7
- "encoding/base64"
8
- "fmt"
9
6
"os"
10
- "os/user"
11
7
"path/filepath"
12
8
"runtime"
13
9
"strings"
14
10
15
11
"github.com/ava-labs/avalanche-cli/pkg/application"
16
12
"github.com/ava-labs/avalanche-cli/pkg/constants"
17
-
18
13
"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"
19
16
20
17
"github.com/posthog/posthog-go"
21
18
"github.com/spf13/cobra"
@@ -40,14 +37,32 @@ func GetCLIVersion() string {
40
37
return string (content )
41
38
}
42
39
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
46
47
}
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 )
48
57
}
49
58
50
59
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
+ }
51
66
if ! userIsOptedIn (app ) {
52
67
return
53
68
}
@@ -80,9 +95,7 @@ func trackMetrics(app *application.Avalanche, commandPath string, flags map[stri
80
95
version = GetCLIVersion ()
81
96
}
82
97
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 )
86
99
87
100
telemetryProperties := make (map [string ]interface {})
88
101
telemetryProperties ["command" ] = commandPath
@@ -93,8 +106,6 @@ func trackMetrics(app *application.Avalanche, commandPath string, flags map[stri
93
106
if insideCodespace {
94
107
codespaceName := os .Getenv (constants .CodespaceNameEnvVar )
95
108
telemetryProperties ["codespace" ] = codespaceName
96
- hash := sha256 .Sum256 ([]byte (codespaceName ))
97
- userID = base64 .StdEncoding .EncodeToString (hash [:])
98
109
}
99
110
for propertyKey , propertyValue := range flags {
100
111
telemetryProperties [propertyKey ] = propertyValue
0 commit comments