Skip to content
Merged
Show file tree
Hide file tree
Changes from 10 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
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.8.8
VERSION
6 changes: 3 additions & 3 deletions cmd/blockchaincmd/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -455,7 +455,7 @@ func createBlockchainConfig(cmd *cobra.Command, args []string) error {
}

if vmType == models.SubnetEvm {
err = sendMetrics(cmd, vmType.RepoName(), blockchainName)
err = sendMetrics(vmType.RepoName(), blockchainName)
if err != nil {
return err
}
Expand Down Expand Up @@ -486,7 +486,7 @@ func addSubnetEVMGenesisPrefundedAddress(genesisBytes []byte, address string, ba
return json.MarshalIndent(genesisMap, "", " ")
}

func sendMetrics(cmd *cobra.Command, repoName, blockchainName string) error {
func sendMetrics(repoName, blockchainName string) error {
flags := make(map[string]string)
flags[constants.SubnetType] = repoName
genesis, err := app.LoadEvmGenesis(blockchainName)
Expand All @@ -513,7 +513,7 @@ func sendMetrics(cmd *cobra.Command, repoName, blockchainName string) error {
precompilesJoined := strings.Join(precompiles, ",")
flags[constants.PrecompileType] = precompilesJoined
flags[constants.NumberOfAirdrops] = strconv.Itoa(numAirdropAddresses)
metrics.HandleTracking(cmd, constants.MetricsSubnetCreateCommand, app, flags)
metrics.HandleTracking(app, flags, nil)
return nil
}

Expand Down
2 changes: 1 addition & 1 deletion cmd/blockchaincmd/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -950,7 +950,7 @@ func deployBlockchain(cmd *cobra.Command, args []string) error {

flags := make(map[string]string)
flags[constants.MetricsNetwork] = network.Name()
metrics.HandleTracking(cmd, constants.MetricsSubnetDeployCommand, app, flags)
metrics.HandleTracking(app, flags, nil)

if network.Kind == models.Local && !simulatedPublicNetwork() {
ux.Logger.PrintToUser("")
Expand Down
2 changes: 1 addition & 1 deletion cmd/blockchaincmd/export_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func TestExportImportSubnet(t *testing.T) {
mockAppDownloader := mocks.Downloader{}
mockAppDownloader.On("Download", mock.Anything).Return(testSubnetEVMCompat, nil)

app.Setup(testDir, logging.NoLog{}, nil, "", prompts.NewPrompter(), &mockAppDownloader)
app.Setup(testDir, logging.NoLog{}, nil, "", prompts.NewPrompter(), &mockAppDownloader, nil)
ux.NewUserLog(logging.NoLog{}, io.Discard)
genBytes, err := os.ReadFile("../../" + utils.SubnetEvmGenesisPath)
require.NoError(err)
Expand Down
2 changes: 1 addition & 1 deletion cmd/blockchaincmd/publish_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -415,7 +415,7 @@ func setupTestEnv(t *testing.T) (*require.Assertions, *mocks.Prompter) {
ux.NewUserLog(logging.NoLog{}, io.Discard)
app = &application.Avalanche{}
mockPrompt := mocks.NewPrompter(t)
app.Setup(testDir, logging.NoLog{}, config.New(), "", mockPrompt, application.NewDownloader())
app.Setup(testDir, logging.NoLog{}, config.New(), "", mockPrompt, application.NewDownloader(), nil)

return require, mockPrompt
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/blockchaincmd/upgradecmd/vm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,7 @@ func TestUpdateToCustomBin(t *testing.T) {
ux.NewUserLog(log, os.Stdout)

app = &application.Avalanche{}
app.Setup(testDir, log, config.New(), "", prompts.NewPrompter(), application.NewDownloader())
app.Setup(testDir, log, config.New(), "", prompts.NewPrompter(), application.NewDownloader(), nil)

err = os.MkdirAll(app.GetSubnetDir(), constants.DefaultPerms755)
assert.NoError(err)
Expand Down
6 changes: 3 additions & 3 deletions cmd/nodecmd/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -791,7 +791,7 @@ func createNodes(cmd *cobra.Command, args []string) error {
printResults(cloudConfigMap, publicIPMap, monitoringPublicIP)
ux.Logger.PrintToUser(logging.Green.Wrap("AvalancheGo and Avalanche-CLI installed and node(s) are bootstrapping!"))
}
sendNodeCreateMetrics(cmd, cloudService, network.Name(), numNodesMetricsMap)
sendNodeCreateMetrics(cloudService, network.Name(), numNodesMetricsMap)
return nil
}

Expand Down Expand Up @@ -1403,7 +1403,7 @@ func defaultAvalancheCLIPrefix(region string) (string, error) {
return usr.Username + "-" + region + constants.AvalancheCLISuffix, nil
}

func sendNodeCreateMetrics(cmd *cobra.Command, cloudService, network string, nodes map[string]NumNodes) {
func sendNodeCreateMetrics(cloudService, network string, nodes map[string]NumNodes) {
flags := make(map[string]string)
totalValidatorNodes := 0
totalAPINodes := 0
Expand All @@ -1428,7 +1428,7 @@ func sendNodeCreateMetrics(cmd *cobra.Command, cloudService, network string, nod
populateSubnetVMMetrics(flags, wizSubnet)
flags[constants.MetricsCalledFromWiz] = strconv.FormatBool(true)
}
metrics.HandleTracking(cmd, constants.MetricsNodeCreateCommand, app, flags)
metrics.HandleTracking(app, flags, nil)
}

func getPrometheusTargets(clusterName string) ([]string, []string, []string, error) {
Expand Down
6 changes: 3 additions & 3 deletions cmd/nodecmd/wiz.go
Original file line number Diff line number Diff line change
Expand Up @@ -457,7 +457,7 @@ func wiz(cmd *cobra.Command, args []string) error {
if err := deployClusterYAMLFile(clusterName, subnetName); err != nil {
return err
}
sendNodeWizMetrics(cmd)
sendNodeWizMetrics()
return nil
}

Expand Down Expand Up @@ -889,10 +889,10 @@ func setICMRelayerSecurityGroupRule(clusterName string, awmRelayerHost *models.H
return nil
}

func sendNodeWizMetrics(cmd *cobra.Command) {
func sendNodeWizMetrics() {
flags := make(map[string]string)
populateSubnetVMMetrics(flags, wizSubnet)
metrics.HandleTracking(cmd, constants.MetricsNodeDevnetWizCommand, app, flags)
metrics.HandleTracking(app, flags, nil)
}

func populateSubnetVMMetrics(flags map[string]string, subnetName string) {
Expand Down
8 changes: 2 additions & 6 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ To get started, look at the documentation for the subcommands or jump right
in with avalanche blockchain create myNewBlockchain.`,
PersistentPreRunE: createApp,
Version: Version,
PersistentPostRun: handleTracking,
SilenceErrors: true,
SilenceUsage: true,
}
Expand Down Expand Up @@ -138,7 +137,7 @@ func createApp(cmd *cobra.Command, _ []string) error {
log.Info("-----------")
log.Info(fmt.Sprintf("cmd: %s", strings.Join(os.Args[1:], " ")))
cf := config.New()
app.Setup(baseDir, log, cf, Version, prompts.NewPrompter(), application.NewDownloader())
app.Setup(baseDir, log, cf, Version, prompts.NewPrompter(), application.NewDownloader(), cmd)

if err := initConfig(); err != nil {
return err
Expand Down Expand Up @@ -240,10 +239,6 @@ func checkForUpdates(cmd *cobra.Command, app *application.Avalanche) error {
return nil
}

func handleTracking(cmd *cobra.Command, _ []string) {
metrics.HandleTracking(cmd, cmd.CommandPath(), app, nil)
}

func setupEnv() (string, error) {
// Set base dir
usr, err := user.Current()
Expand Down Expand Up @@ -383,6 +378,7 @@ func Execute() {
app = application.New()
rootCmd := NewRootCmd()
err := rootCmd.Execute()
metrics.HandleTracking(app, nil, err)
cobrautils.HandleErrors(err)
}

Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ require (
github.com/otiai10/copy v1.14.1
github.com/pborman/ansi v1.0.0
github.com/pingcap/errors v0.11.4
github.com/posthog/posthog-go v1.2.24
github.com/posthog/posthog-go v1.4.7
github.com/prometheus/client_golang v1.21.1
github.com/schollz/progressbar/v3 v3.17.1
github.com/shirou/gopsutil v3.21.11+incompatible
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -748,8 +748,8 @@ github.com/pkg/term v0.0.0-20180730021639-bffc007b7fd5/go.mod h1:eCbImbZ95eXtAUI
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 h1:Jamvg5psRIccs7FGNTlIRMkT8wgtp5eCXdBlqhYGL6U=
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/posthog/posthog-go v1.2.24 h1:A+iG4saBJemo++VDlcWovbYf8KFFNUfrCoJtsc40RPA=
github.com/posthog/posthog-go v1.2.24/go.mod h1:uYC2l1Yktc8E+9FAHJ9QZG4vQf/NHJPD800Hsm7DzoM=
github.com/posthog/posthog-go v1.4.7 h1:2DOcy1pLeLbfEG+WgK9S2WOoXk+N2DXYAN5/S9pwOh8=
github.com/posthog/posthog-go v1.4.7/go.mod h1:uYC2l1Yktc8E+9FAHJ9QZG4vQf/NHJPD800Hsm7DzoM=
github.com/prometheus/client_golang v0.9.1/go.mod h1:7SWBe2y4D6OKWSNQJUaRYU/AaXPKyh/dDVn+NZz0KFw=
github.com/prometheus/client_golang v1.0.0/go.mod h1:db9x61etRT2tGnBNRi70OPL5FsnadC4Ky3P0J6CfImo=
github.com/prometheus/client_golang v1.21.1 h1:DOvXXTqVzvkIewV/CDPFdejpMCGeMcbGCQ8YOmu+Ibk=
Expand Down
2 changes: 1 addition & 1 deletion internal/migrations/migrations_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func TestRunMigrations(t *testing.T) {
testDir := t.TempDir()

app := &application.Avalanche{}
app.Setup(testDir, logging.NoLog{}, config.New(), "", prompts.NewPrompter(), application.NewDownloader())
app.Setup(testDir, logging.NoLog{}, config.New(), "", prompts.NewPrompter(), application.NewDownloader(), nil)

type migTest struct {
migs map[int]migrationFunc
Expand Down
4 changes: 2 additions & 2 deletions internal/migrations/subnet_evm_rename_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func TestSubnetEVMRenameMigration(t *testing.T) {
testDir := t.TempDir()

app := &application.Avalanche{}
app.Setup(testDir, logging.NoLog{}, config.New(), "", prompts.NewPrompter(), application.NewDownloader())
app.Setup(testDir, logging.NoLog{}, config.New(), "", prompts.NewPrompter(), application.NewDownloader(), nil)

err := app.CreateSidecar(tt.sc)
require.NoError(err)
Expand Down Expand Up @@ -91,7 +91,7 @@ func TestSubnetEVMRenameMigration_EmptyDir(t *testing.T) {
testDir := t.TempDir()

app := &application.Avalanche{}
app.Setup(testDir, logging.NoLog{}, config.New(), "", prompts.NewPrompter(), application.NewDownloader())
app.Setup(testDir, logging.NoLog{}, config.New(), "", prompts.NewPrompter(), application.NewDownloader(), nil)

emptySubnetName := "emptySubnet"

Expand Down
2 changes: 1 addition & 1 deletion internal/migrations/toplevel_files_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func TestTopLevelFilesMigration(t *testing.T) {
testDir := t.TempDir()

app := &application.Avalanche{}
app.Setup(testDir, logging.NoLog{}, config.New(), "", prompts.NewPrompter(), application.NewDownloader())
app.Setup(testDir, logging.NoLog{}, config.New(), "", prompts.NewPrompter(), application.NewDownloader(), nil)

testSC1 := &models.Sidecar{
Name: "test1",
Expand Down
2 changes: 1 addition & 1 deletion internal/testutils/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func SetupTestInTempDir(t *testing.T) *application.Avalanche {
testDir := t.TempDir()

app := application.New()
app.Setup(testDir, logging.NoLog{}, &config.Config{}, "", nil, nil)
app.Setup(testDir, logging.NoLog{}, &config.Config{}, "", nil, nil, nil)
ux.NewUserLog(logging.NoLog{}, io.Discard)
return app
}
2 changes: 1 addition & 1 deletion pkg/apmintegration/file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ const (
func newTestApp(t *testing.T, testDir string) *application.Avalanche {
tempDir := t.TempDir()
app := application.New()
app.Setup(tempDir, logging.NoLog{}, nil, "", prompts.NewPrompter(), application.NewDownloader())
app.Setup(tempDir, logging.NoLog{}, nil, "", prompts.NewPrompter(), application.NewDownloader(), nil)
app.ApmDir = testDir
return app
}
Expand Down
4 changes: 4 additions & 0 deletions pkg/application/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"github.com/ava-labs/avalanchego/utils/set"
"github.com/ava-labs/subnet-evm/core"

"github.com/spf13/cobra"
"golang.org/x/exp/maps"
)

Expand All @@ -35,6 +36,7 @@ type Avalanche struct {
Apm *apm.APM
ApmDir string
Downloader Downloader
Cmd *cobra.Command
}

func New() *Avalanche {
Expand All @@ -48,13 +50,15 @@ func (app *Avalanche) Setup(
version string,
prompt prompts.Prompter,
downloader Downloader,
cmd *cobra.Command,
) {
app.baseDir = baseDir
app.Log = log
app.Conf = conf
app.Version = version
app.Prompt = prompt
app.Downloader = downloader
app.Cmd = cmd
}

func (app *Avalanche) GetRunFile(prefix string) string {
Expand Down
2 changes: 1 addition & 1 deletion pkg/binutils/release_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func setupInstallDir(require *require.Assertions) *application.Avalanche {
defer os.RemoveAll(rootDir)

app := application.New()
app.Setup(rootDir, logging.NoLog{}, &config.Config{}, "", prompts.NewPrompter(), application.NewDownloader())
app.Setup(rootDir, logging.NoLog{}, &config.Config{}, "", prompts.NewPrompter(), application.NewDownloader(), nil)
return app
}

Expand Down
54 changes: 41 additions & 13 deletions pkg/metrics/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
package metrics

import (
"fmt"
"os"
"path/filepath"
"runtime"
Expand All @@ -15,13 +16,13 @@ import (
"github.com/ava-labs/avalanchego/utils/logging"

"github.com/posthog/posthog-go"
"github.com/spf13/cobra"
)

// telemetryToken value is set at build and install scripts using ldflags
var (
telemetryToken = ""
telemetryInstance = "https://app.posthog.com"
sent = false
)

func GetCLIVersion() string {
Expand Down Expand Up @@ -56,7 +57,20 @@ 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) {
func HandleTracking(
app *application.Avalanche,
flags map[string]string,
err error,
) {
if sent {
Copy link
Contributor

Choose a reason for hiding this comment

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

if user calls command 1, sent will be set to true after tracking info is sent. What about when user calls command 2, since sent would have been set to true here, will the tracking info be sent?

Copy link
Contributor

Choose a reason for hiding this comment

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

I guess best way to test this is see if we can see the second command on the dashboard after first command is called

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

that variable is set per execution. a new execution of CLI with starts with the value set to false.

// avoid sending duplicate information for special commands with more info
return
}
sent = true
if app.Cmd == nil {
// command called with no arguments at all
return
}
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)
Expand All @@ -66,29 +80,33 @@ func HandleTracking(cmd *cobra.Command, commandPath string, app *application.Ava
if !userIsOptedIn(app) {
return
}
if !cmd.HasSubCommands() && CheckCommandIsNotCompletion(cmd) {
trackMetrics(app, commandPath, flags)
if !app.Cmd.HasSubCommands() && CheckCommandIsNotCompletion(app.Cmd.CommandPath()) {
trackMetrics(app, flags, err)
}
}

func CheckCommandIsNotCompletion(cmd *cobra.Command) bool {
result := strings.Fields(cmd.CommandPath())
func CheckCommandIsNotCompletion(commandPath string) bool {
result := strings.Fields(commandPath)
if len(result) >= 2 && result[1] == "completion" {
return false
}
return true
}

func trackMetrics(app *application.Avalanche, commandPath string, flags map[string]string) {
func trackMetrics(app *application.Avalanche, flags map[string]string, cmdErr error) {
if telemetryToken == "" {
telemetryToken = os.Getenv(constants.MetricsAPITokenEnvVarName)
}
if telemetryToken == "" && !utils.IsE2E() {
app.Log.Warn("no token is configured for sending metrics")
}
Comment on lines +100 to +102
Copy link
Contributor

Choose a reason for hiding this comment

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

do we need to users to see this?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

this is only going into avalanche.log, not stdout. it is useful for debugging. not expected to be printed on releases.

if telemetryToken == "" || utils.IsE2E() {
return
}
client, _ := posthog.NewWithConfig(telemetryToken, posthog.Config{Endpoint: telemetryInstance})

defer client.Close()
client, err := posthog.NewWithConfig(telemetryToken, posthog.Config{Endpoint: telemetryInstance})
if err != nil {
app.Log.Warn(fmt.Sprintf("failure creating metrics client: %s", err))
}
Comment on lines +106 to +109
Copy link
Contributor

Choose a reason for hiding this comment

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

same here, why not just print nothing to logs?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

this is only going into avalanche.log, not stdout. we are seeing that not all events seem to be sent, so we need to have some logs on this, so we can see if something is happening and maybe try again based on the error


version := app.Version
if version == "" {
Expand All @@ -98,9 +116,13 @@ func trackMetrics(app *application.Avalanche, commandPath string, flags map[stri
userID := getMetricsUserID(app)

telemetryProperties := make(map[string]interface{})
telemetryProperties["command"] = commandPath
telemetryProperties["command"] = app.Cmd.CommandPath()
telemetryProperties["version"] = version
telemetryProperties["os"] = runtime.GOOS
telemetryProperties["error"] = ""
if err != nil {
telemetryProperties["error"] = cmdErr.Error()
}
insideCodespace := utils.InsideCodespace()
telemetryProperties["insideCodespace"] = insideCodespace
if insideCodespace {
Expand All @@ -110,9 +132,15 @@ func trackMetrics(app *application.Avalanche, commandPath string, flags map[stri
for propertyKey, propertyValue := range flags {
telemetryProperties[propertyKey] = propertyValue
}
_ = client.Enqueue(posthog.Capture{
event := posthog.Capture{
DistinctId: userID,
Event: "cli-command",
Properties: telemetryProperties,
})
}
if err := client.Enqueue(event); err != nil {
app.Log.Warn(fmt.Sprintf("failure sending metrics %#v: %s", telemetryProperties, err))
}
if err := client.Close(); err != nil {
app.Log.Warn(fmt.Sprintf("failure closing metrics client %#v: %s", telemetryProperties, err))
Comment on lines +140 to +144
Copy link
Contributor

Choose a reason for hiding this comment

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

here too

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

this is only going into avalanche.log, not stdout. we are seeing that not all events seem to be sent, so we need to have some logs on this, so we can see if something is happening and maybe try again based on the error

}
}
4 changes: 2 additions & 2 deletions pkg/utils/e2e.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,9 @@ type Config struct {
E2ESuffixList []string
}

// IsE2E checks if the environment variable "RUN_E2E" is set and returns true if it is, false otherwise.
// IsE2E checks if the environment variable "RUN_CLI_E2E" is set and returns true if it is, false otherwise.
func IsE2E() bool {
return os.Getenv("RUN_E2E") != ""
return os.Getenv("RUN_CLI_E2E") != ""
}

// E2EDocker checks if docker and docker-compose are available.
Expand Down
Loading
Loading