-
Notifications
You must be signed in to change notification settings - Fork 116
Add error to metrics #2719
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
Add error to metrics #2719
Changes from 10 commits
4b6785e
1a5d143
63cd778
8ee8fc6
0545c23
e876777
63337c4
f0a5d9b
c1c6811
b07bfb8
7da3e77
c07d25f
c650dbf
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1 @@ | ||
| 1.8.8 | ||
| VERSION | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,6 +3,7 @@ | |
| package metrics | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "os" | ||
| "path/filepath" | ||
| "runtime" | ||
|
|
@@ -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 { | ||
|
|
@@ -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 { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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?
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
|
|
@@ -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
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. do we need to users to see this?
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same here, why not just print nothing to logs?
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 == "" { | ||
|
|
@@ -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 { | ||
|
|
@@ -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
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. here too
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.