Skip to content

Commit

Permalink
feat: enhance init command telemetry (#5026)
Browse files Browse the repository at this point in the history
  • Loading branch information
haneabogdan committed Feb 27, 2024
1 parent 668909c commit c54567b
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 22 deletions.
26 changes: 17 additions & 9 deletions cmd/kubectl-testkube/commands/cloud/init.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package cloud

import (
"fmt"

"github.com/spf13/cobra"

"github.com/kubeshop/testkube/cmd/kubectl-testkube/commands/common"
Expand Down Expand Up @@ -43,23 +45,27 @@ func NewInitCmd() *cobra.Command {
ui.NL()

currentContext, err := common.GetCurrentKubernetesContext()
sendErrTelemetry(cmd, cfg, "k8s_context")
ui.ExitOnError("getting current context", err)
if err != nil {
sendErrTelemetry(cmd, cfg, "k8s_context", err)
ui.ExitOnError("getting current context", err)
}
ui.Alert("Current kubectl context:", currentContext)
ui.NL()

ok := ui.Confirm("Do you want to continue?")
if !ok {
ui.Errf("Testkube installation cancelled")
sendErrTelemetry(cmd, cfg, "user_cancel")
sendErrTelemetry(cmd, cfg, "user_cancel", err)
return
}
}

spinner := ui.NewSpinner("Installing Testkube")
err = common.HelmUpgradeOrInstallTestkubeCloud(options, cfg, false)
sendErrTelemetry(cmd, cfg, "helm_install")
ui.ExitOnError("Installing Testkube", err)
if err != nil {
sendErrTelemetry(cmd, cfg, "helm_install", err)
ui.ExitOnError("Installing Testkube", err)
}
spinner.Success()

ui.NL()
Expand All @@ -68,11 +74,11 @@ func NewInitCmd() *cobra.Command {
var token, refreshToken string
if !common.IsUserLoggedIn(cfg, options) {
token, refreshToken, err = common.LoginUser(options.Master.URIs.Auth)
sendErrTelemetry(cmd, cfg, "login")
sendErrTelemetry(cmd, cfg, "login", err)
ui.ExitOnError("user login", err)
}
err = common.PopulateLoginDataToContext(options.Master.OrgId, options.Master.EnvId, token, refreshToken, options, cfg)
sendErrTelemetry(cmd, cfg, "setting_context")
sendErrTelemetry(cmd, cfg, "setting_context", err)
ui.ExitOnError("Setting cloud environment context", err)

ui.Info(" Happy Testing! 🚀")
Expand All @@ -88,10 +94,12 @@ func NewInitCmd() *cobra.Command {
return cmd
}

func sendErrTelemetry(cmd *cobra.Command, clientCfg config.Data, errType string) {
func sendErrTelemetry(cmd *cobra.Command, clientCfg config.Data, errType string, errorLogs error) {
var errorStackTrace string
errorStackTrace = fmt.Sprintf("%+v", errorLogs)
if clientCfg.TelemetryEnabled {
ui.Debug("collecting anonymous telemetry data, you can disable it by calling `kubectl testkube disable telemetry`")
out, err := telemetry.SendCmdErrorEvent(cmd, common.Version, errType)
out, err := telemetry.SendCmdErrorEvent(cmd, common.Version, errType, errorStackTrace)
if ui.Verbose && err != nil {
ui.Err(err)
}
Expand Down
33 changes: 22 additions & 11 deletions cmd/kubectl-testkube/commands/pro/init.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package pro

import (
"fmt"

"github.com/spf13/cobra"

"github.com/kubeshop/testkube/cmd/kubectl-testkube/commands/common"
Expand Down Expand Up @@ -38,23 +40,28 @@ func NewInitCmd() *cobra.Command {
ui.NL()

currentContext, err := common.GetCurrentKubernetesContext()
sendErrTelemetry(cmd, cfg, "k8s_context")
ui.ExitOnError("getting current context", err)

if err != nil {
sendErrTelemetry(cmd, cfg, "k8s_context", err)
ui.ExitOnError("getting current context", err)
}
ui.Alert("Current kubectl context:", currentContext)
ui.NL()

ok := ui.Confirm("Do you want to continue?")
if !ok {
ui.Errf("Testkube installation cancelled")
sendErrTelemetry(cmd, cfg, "user_cancel")
sendErrTelemetry(cmd, cfg, "user_cancel", err)
return
}
}

spinner := ui.NewSpinner("Installing Testkube")
err = common.HelmUpgradeOrInstallTestkubeCloud(options, cfg, false)
sendErrTelemetry(cmd, cfg, "helm_install")
ui.ExitOnError("Installing Testkube", err)
if err != nil {
sendErrTelemetry(cmd, cfg, "helm_install", err)
ui.ExitOnError("Installing Testkube", err)
}
spinner.Success()

ui.NL()
Expand All @@ -63,13 +70,14 @@ func NewInitCmd() *cobra.Command {
var token, refreshToken string
if !common.IsUserLoggedIn(cfg, options) {
token, refreshToken, err = common.LoginUser(options.Master.URIs.Auth)
sendErrTelemetry(cmd, cfg, "login")
sendErrTelemetry(cmd, cfg, "login", err)
ui.ExitOnError("user login", err)
}
err = common.PopulateLoginDataToContext(options.Master.OrgId, options.Master.EnvId, token, refreshToken, options, cfg)
sendErrTelemetry(cmd, cfg, "setting_context")
ui.ExitOnError("Setting Pro environment context", err)

if err != nil {
sendErrTelemetry(cmd, cfg, "setting_context", err)
ui.ExitOnError("Setting Pro environment context", err)
}
ui.Info(" Happy Testing! 🚀")
ui.NL()
},
Expand All @@ -84,13 +92,16 @@ func NewInitCmd() *cobra.Command {
return cmd
}

func sendErrTelemetry(cmd *cobra.Command, clientCfg config.Data, errType string) {
func sendErrTelemetry(cmd *cobra.Command, clientCfg config.Data, errType string, errorLogs error) {
var errorStackTrace string
errorStackTrace = fmt.Sprintf("%+v", errorLogs)
if clientCfg.TelemetryEnabled {
ui.Debug("collecting anonymous telemetry data, you can disable it by calling `kubectl testkube disable telemetry`")
out, err := telemetry.SendCmdErrorEvent(cmd, common.Version, errType)
out, err := telemetry.SendCmdErrorEvent(cmd, common.Version, errType, errorStackTrace)
if ui.Verbose && err != nil {
ui.Err(err)
}

ui.Debug("telemetry send event response", out)
}
}
Expand Down
1 change: 1 addition & 0 deletions pkg/telemetry/payload.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ type Params struct {
ClusterType string `json:"cluster_type,omitempty"`
Error string `json:"error,omitempty"`
ErrorType string `json:"error_type,omitempty"`
ErrorStackTrace string `json:"error_stacktrace,omitempty"`
}

type Event struct {
Expand Down
3 changes: 2 additions & 1 deletion pkg/telemetry/sender_sio.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,8 @@ func mapProperties(params Params) analytics.Properties {
Set("cloudEnvironmentId", params.Context.EnvironmentId).
Set("machineId", params.MachineID).
Set("clusterType", params.ClusterType).
Set("errorType", params.ErrorType)
Set("errorType", params.ErrorType).
Set("errorStackTrace", params.ErrorStackTrace)

if params.DataSource != "" {
properties = properties.Set("dataSource", params.DataSource)
Expand Down
3 changes: 2 additions & 1 deletion pkg/telemetry/telemetry.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func SendCmdEvent(cmd *cobra.Command, version string) (string, error) {
return sendData(senders, payload)
}

func SendCmdErrorEvent(cmd *cobra.Command, version, errType string) (string, error) {
func SendCmdErrorEvent(cmd *cobra.Command, version, errType string, errorStackTrace string) (string, error) {
// get all sub-commands passed to cli
command := strings.TrimPrefix(cmd.CommandPath(), "kubectl-testkube ")
if command == "" {
Expand All @@ -72,6 +72,7 @@ func SendCmdErrorEvent(cmd *cobra.Command, version, errType string) (string, err
Context: getCurrentContext(),
ClusterType: GetClusterType(),
ErrorType: errType,
ErrorStackTrace: errorStackTrace,
},
}},
}
Expand Down

0 comments on commit c54567b

Please sign in to comment.