From b40b4499206fe1e83083547126ddba0578922440 Mon Sep 17 00:00:00 2001 From: nicufk Date: Wed, 20 Mar 2024 01:25:25 -0300 Subject: [PATCH] fix: use cluster/environment ids for cli telemetry (#5215) (#5216) --- api/v1/testkube.yaml | 4 ++++ internal/app/api/v1/handlers.go | 1 + pkg/api/v1/testkube/model_server_info.go | 2 ++ pkg/telemetry/payload.go | 3 +-- pkg/telemetry/telemetry.go | 23 ++++++++++++++++++++--- 5 files changed, 28 insertions(+), 5 deletions(-) diff --git a/api/v1/testkube.yaml b/api/v1/testkube.yaml index 37fc6c51ebc..7340ac28d2f 100644 --- a/api/v1/testkube.yaml +++ b/api/v1/testkube.yaml @@ -5840,6 +5840,10 @@ components: type: string description: server installaton namespace example: "my-testkube" + clusterId: + type: string + description: cluster id + example: "my-cluster-id" context: type: string description: currently configured testkube API context diff --git a/internal/app/api/v1/handlers.go b/internal/app/api/v1/handlers.go index a4a700f17fa..f021533d777 100644 --- a/internal/app/api/v1/handlers.go +++ b/internal/app/api/v1/handlers.go @@ -65,6 +65,7 @@ func (s *TestkubeAPI) InfoHandler() fiber.Handler { Version: version.Version, Namespace: s.Namespace, Context: apiContext, + ClusterId: s.Config.ClusterID, EnvId: envID, OrgId: orgID, HelmchartVersion: s.helmchartVersion, diff --git a/pkg/api/v1/testkube/model_server_info.go b/pkg/api/v1/testkube/model_server_info.go index 5f3f0193b96..db602feb95e 100644 --- a/pkg/api/v1/testkube/model_server_info.go +++ b/pkg/api/v1/testkube/model_server_info.go @@ -17,6 +17,8 @@ type ServerInfo struct { Commit string `json:"commit,omitempty"` // server installaton namespace Namespace string `json:"namespace,omitempty"` + // cluster id + ClusterId string `json:"clusterId,omitempty"` // currently configured testkube API context Context string `json:"context,omitempty"` // cloud organization id diff --git a/pkg/telemetry/payload.go b/pkg/telemetry/payload.go index ddde292e62b..36f0525a7b0 100644 --- a/pkg/telemetry/payload.go +++ b/pkg/telemetry/payload.go @@ -74,7 +74,6 @@ type RunContext struct { } func NewCLIPayload(context RunContext, id, name, version, category, clusterType string) Payload { - machineID := GetMachineID() return Payload{ ClientID: id, UserID: id, @@ -86,7 +85,7 @@ func NewCLIPayload(context RunContext, id, name, version, category, clusterType EventCategory: category, AppVersion: version, AppName: "kubectl-testkube", - MachineID: machineID, + MachineID: GetMachineID(), OperatingSystem: runtime.GOOS, Architecture: runtime.GOARCH, Context: context, diff --git a/pkg/telemetry/telemetry.go b/pkg/telemetry/telemetry.go index 62907969bee..e90164b76ff 100644 --- a/pkg/telemetry/telemetry.go +++ b/pkg/telemetry/telemetry.go @@ -11,6 +11,7 @@ import ( "github.com/spf13/cobra" v1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "github.com/kubeshop/testkube/cmd/kubectl-testkube/commands/common" "github.com/kubeshop/testkube/cmd/kubectl-testkube/config" httpclient "github.com/kubeshop/testkube/pkg/http" "github.com/kubeshop/testkube/pkg/k8sclient" @@ -42,7 +43,7 @@ func SendCmdEvent(cmd *cobra.Command, version string) (string, error) { command = "root" } - payload := NewCLIPayload(getCurrentContext(), GetMachineID(), command, version, "cli_command_execution", GetClusterType()) + payload := NewCLIPayload(getCurrentContext(), getUserID(cmd), command, version, "cli_command_execution", GetClusterType()) return sendData(senders, payload) } @@ -89,13 +90,13 @@ func SendCmdAttemptEvent(cmd *cobra.Command, version string) (string, error) { command += "_attempt" // TODO pass error - payload := NewCLIPayload(getCurrentContext(), GetMachineID(), command, version, "cli_command_execution", GetClusterType()) + payload := NewCLIPayload(getCurrentContext(), getUserID(cmd), command, version, "cli_command_execution", GetClusterType()) return sendData(senders, payload) } // SendCmdInitEvent will send CLI event to GA func SendCmdInitEvent(cmd *cobra.Command, version string) (string, error) { - payload := NewCLIPayload(getCurrentContext(), GetMachineID(), "init", version, "cli_command_execution", GetClusterType()) + payload := NewCLIPayload(getCurrentContext(), getUserID(cmd), "init", version, "cli_command_execution", GetClusterType()) return sendData(senders, payload) } @@ -152,6 +153,22 @@ func getCurrentContext() RunContext { } } +func getUserID(cmd *cobra.Command) string { + id := "command-cli-user" + client, _, err := common.GetClient(cmd) + if err == nil && client != nil { + info, err := client.GetServerInfo() + if err == nil && info.ClusterId != "" { + id = info.ClusterId + } + } + data, err := config.Load() + if err != nil || data.CloudContext.EnvironmentId == "" { + return id + } + return data.CloudContext.EnvironmentId +} + func GetClusterType() string { clientset, err := k8sclient.ConnectToK8s()