Skip to content

Commit

Permalink
fix: use cluster/environment ids for cli telemetry (#5215) (#5216)
Browse files Browse the repository at this point in the history
  • Loading branch information
nicufk authored Mar 20, 2024
1 parent 8319648 commit b40b449
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 5 deletions.
4 changes: 4 additions & 0 deletions api/v1/testkube.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions internal/app/api/v1/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 2 additions & 0 deletions pkg/api/v1/testkube/model_server_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 1 addition & 2 deletions pkg/telemetry/payload.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand Down
23 changes: 20 additions & 3 deletions pkg/telemetry/telemetry.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
}

Expand Down Expand Up @@ -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)
}

Expand Down Expand Up @@ -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()
Expand Down

0 comments on commit b40b449

Please sign in to comment.