From b5caee0771e6c013c8c29ce45ef12ab295ac23b0 Mon Sep 17 00:00:00 2001 From: John Dietz Date: Mon, 11 Dec 2023 19:01:22 -0500 Subject: [PATCH] adjusting api to leverage new telemetry (#247) * adjusting api to leverage new telemetry * adopt metrics-client v0.3.0 and implement parent_cluster_id * setting parent cluster id --- .../kubefirst-api/templates/deployment.yaml | 6 ++- go.mod | 2 +- go.sum | 2 + internal/controller/controller.go | 5 +- internal/env/env.go | 3 +- main.go | 3 +- pkg/segment/segment.go | 52 +++++++++++++++++++ providers/aws/create.go | 3 +- providers/civo/create.go | 3 +- providers/digitalocean/create.go | 4 +- providers/google/create.go | 3 +- providers/vultr/create.go | 3 +- 12 files changed, 70 insertions(+), 19 deletions(-) create mode 100644 pkg/segment/segment.go diff --git a/charts/kubefirst-api/templates/deployment.yaml b/charts/kubefirst-api/templates/deployment.yaml index 58ed9b20..bbf0a9d0 100644 --- a/charts/kubefirst-api/templates/deployment.yaml +++ b/charts/kubefirst-api/templates/deployment.yaml @@ -94,6 +94,8 @@ spec: value: {{ .Values.global.kubefirstTeamInfo | default "" | quote }} - name: KUBEFIRST_VERSION value: {{ .Values.global.kubefirstVersion | default "unset" }} + - name: PARENT_CLUSTER_ID + value: {{ .Values.global.clusterId | default $clusterId }} - name: USE_TELEMETRY value: {{ .Values.global.useTelemetry | default "true" | quote }} - name: IS_CLUSTER_ZERO @@ -170,8 +172,6 @@ spec: imagePullPolicy: {{ .Values.image.hook.pullPolicy }} args: [ "transmit", - "--type", - "cluster-zero", ] env: - name: CLOUD_PROVIDER @@ -194,5 +194,7 @@ spec: value: {{ .Values.global.kubefirstTeamInfo | default "" | quote }} - name: KUBEFIRST_VERSION value: {{ .Values.global.kubefirstVersion | default "unset" }} + - name: PARENT_CLUSTER_ID + value: {{ .Values.global.clusterId | default $clusterId }} - name: USE_TELEMETRY value: {{ .Values.global.useTelemetry | default "true" | quote }} diff --git a/go.mod b/go.mod index 22f8e580..cb75ea96 100644 --- a/go.mod +++ b/go.mod @@ -26,7 +26,7 @@ require ( github.com/google/go-github/v52 v52.0.0 github.com/hashicorp/vault/api v1.9.0 github.com/joho/godotenv v1.5.1 - github.com/kubefirst/metrics-client v0.2.8 + github.com/kubefirst/metrics-client v0.3.0 github.com/kubefirst/runtime v0.3.35 github.com/minio/minio-go/v7 v7.0.49 github.com/otiai10/copy v1.7.0 diff --git a/go.sum b/go.sum index 6186b3b0..b55e6220 100644 --- a/go.sum +++ b/go.sum @@ -740,6 +740,8 @@ github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= github.com/kubefirst/metrics-client v0.2.8 h1:JfaeiBifZD/DpyYW2QVHcrhH/KWA98OmM+7c7M12qMc= github.com/kubefirst/metrics-client v0.2.8/go.mod h1:GR7wsMcyYhd+EU67PeuMCBYFE6OJ7P/j5OI5BLOoRMc= +github.com/kubefirst/metrics-client v0.3.0 h1:zCug82pEzeWhHhpeYQvdhytRNDxrLxX18dPQ5PSxY3s= +github.com/kubefirst/metrics-client v0.3.0/go.mod h1:GR7wsMcyYhd+EU67PeuMCBYFE6OJ7P/j5OI5BLOoRMc= github.com/kubefirst/runtime v0.3.35 h1:wn430Irf0E1vc3X0WX3lYBpyhQ5TN6xxMcargILA9uI= github.com/kubefirst/runtime v0.3.35/go.mod h1:0CnYy+8JTG+/0f3QlkTQJqTT654Su6JXk30OufFVY98= github.com/kylelemons/godebug v1.1.0/go.mod h1:9/0rRGxNHcop5bhtWyNeEfOS8JIWk580+fNqagV/RAw= diff --git a/internal/controller/controller.go b/internal/controller/controller.go index 910e61b5..b3242d0e 100644 --- a/internal/controller/controller.go +++ b/internal/controller/controller.go @@ -159,13 +159,12 @@ func (clctrl *ClusterController) InitController(def *pkgtypes.ClusterDefinition) KubefirstTeam: env.KubefirstTeam, KubefirstTeamInfo: env.KubefirstTeamInfo, MachineID: env.ClusterId, - MetricName: telemetry.ClusterInstallStarted, + ParentClusterId: env.ParentClusterId, + MetricName: telemetry.ClusterInstallCompleted, UserId: env.ClusterId, } clctrl.TelemetryEvent = telemetryEvent - telemetry.SendEvent(clctrl.TelemetryEvent, telemetry.ClusterInstallStarted, "") - //Copy Cluster Definiion to Cluster Controller clctrl.AlertsEmail = def.AdminEmail clctrl.CloudProvider = def.CloudProvider diff --git a/internal/env/env.go b/internal/env/env.go index 8fa20b8c..5858c30c 100644 --- a/internal/env/env.go +++ b/internal/env/env.go @@ -24,7 +24,8 @@ type Env struct { KubefirstTeamInfo string `env:"KUBEFIRST_TEAM_INFO"` AWSRegion string `env:"AWS_REGION"` AWSProfile string `env:"AWS_PROFILE"` - IsClusterZero string `env:"IS_CLUSTER_ZERO" envDefault:"false"` + IsClusterZero string `env:"IS_CLUSTER_ZERO"` + ParentClusterId string `env:"PARENT_CLUSTER_ID"` InCluster string `env:"IN_CLUSTER" envDefault:"false"` EnterpriseApiUrl string `env:"ENTERPRISE_API_URL"` } diff --git a/main.go b/main.go index 8f573035..28e2d041 100644 --- a/main.go +++ b/main.go @@ -96,7 +96,8 @@ func main() { KubefirstTeam: env.KubefirstTeam, KubefirstTeamInfo: env.KubefirstTeamInfo, MachineID: env.ClusterId, - MetricName: telemetry.ClusterInstallStarted, + MetricName: telemetry.ClusterInstallCompleted, + ParentClusterId: env.ParentClusterId, UserId: env.ClusterId, } diff --git a/pkg/segment/segment.go b/pkg/segment/segment.go new file mode 100644 index 00000000..a606ea4e --- /dev/null +++ b/pkg/segment/segment.go @@ -0,0 +1,52 @@ +package segment + +import ( + "os" + + "github.com/denisbrodbeck/machineid" + "github.com/kubefirst/metrics-client/pkg/telemetry" + + "github.com/segmentio/analytics-go" + log "github.com/sirupsen/logrus" +) + +const ( + kubefirstClient string = "api" +) + +func InitClient() *telemetry.SegmentClient { + + machineID, err := machineid.ID() + if err != nil { + log.Info("machine id FAILED") + } + sc := analytics.New(telemetry.SegmentIOWriteKey) + + kubefirstVersion := os.Getenv("KUBEFIRST_VERSION") + if kubefirstVersion == "" { + kubefirstVersion = "development" + } + + c := telemetry.SegmentClient{ + TelemetryEvent: telemetry.TelemetryEvent{ + CliVersion: kubefirstVersion, + CloudProvider: os.Getenv("CLOUD_PROVIDER"), + ClusterID: os.Getenv("CLUSTER_ID"), + ClusterType: os.Getenv("CLUSTER_TYPE"), + DomainName: os.Getenv("DOMAIN_NAME"), + GitProvider: os.Getenv("GIT_PROVIDER"), + InstallMethod: os.Getenv("INSTALL_METHOD"), + KubefirstClient: kubefirstClient, + KubefirstTeam: os.Getenv("KUBEFIRST_TEAM"), + KubefirstTeamInfo: os.Getenv("KUBEFIRST_TEAM_INFO"), + MachineID: machineID, + ParentClusterId: os.Getenv("PARENT_CLUSTER_ID"), + ErrorMessage: "", + UserId: machineID, + MetricName: telemetry.ClusterInstallCompleted, + }, + Client: sc, + } + + return &c +} diff --git a/providers/aws/create.go b/providers/aws/create.go index de636e68..79b5c8fd 100644 --- a/providers/aws/create.go +++ b/providers/aws/create.go @@ -13,7 +13,6 @@ import ( "github.com/kubefirst/kubefirst-api/internal/db" "github.com/kubefirst/kubefirst-api/internal/services" pkgtypes "github.com/kubefirst/kubefirst-api/pkg/types" - "github.com/kubefirst/metrics-client/pkg/telemetry" awsinternal "github.com/kubefirst/runtime/pkg/aws" "github.com/kubefirst/runtime/pkg/k8s" log "github.com/sirupsen/logrus" @@ -253,7 +252,7 @@ func CreateAWSCluster(definition *pkgtypes.ClusterDefinition) error { return err } - telemetry.SendEvent(ctrl.TelemetryEvent, telemetry.ClusterInstallCompleted, "") + log.Info("cluster creation complete") // Create default service entries cl, _ := db.Client.GetCluster(ctrl.ClusterName) diff --git a/providers/civo/create.go b/providers/civo/create.go index 8d3c7d60..f737b00a 100644 --- a/providers/civo/create.go +++ b/providers/civo/create.go @@ -14,7 +14,6 @@ import ( "github.com/kubefirst/kubefirst-api/internal/db" "github.com/kubefirst/kubefirst-api/internal/services" pkgtypes "github.com/kubefirst/kubefirst-api/pkg/types" - "github.com/kubefirst/metrics-client/pkg/telemetry" "github.com/kubefirst/runtime/pkg/k8s" "github.com/kubefirst/runtime/pkg/ssl" log "github.com/sirupsen/logrus" @@ -233,7 +232,7 @@ func CreateCivoCluster(definition *pkgtypes.ClusterDefinition) error { return err } - telemetry.SendEvent(ctrl.TelemetryEvent, telemetry.ClusterInstallCompleted, "") + log.Info("cluster creation complete") // Create default service entries cl, _ := db.Client.GetCluster(ctrl.ClusterName) diff --git a/providers/digitalocean/create.go b/providers/digitalocean/create.go index 78173e88..b625c92b 100644 --- a/providers/digitalocean/create.go +++ b/providers/digitalocean/create.go @@ -15,7 +15,6 @@ import ( "github.com/kubefirst/kubefirst-api/internal/db" "github.com/kubefirst/kubefirst-api/internal/services" pkgtypes "github.com/kubefirst/kubefirst-api/pkg/types" - "github.com/kubefirst/metrics-client/pkg/telemetry" "github.com/kubefirst/runtime/pkg/k8s" "github.com/kubefirst/runtime/pkg/ssl" log "github.com/sirupsen/logrus" @@ -229,8 +228,7 @@ func CreateDigitaloceanCluster(definition *pkgtypes.ClusterDefinition) error { return err } - // Telemetry handler - telemetry.SendEvent(ctrl.TelemetryEvent, telemetry.ClusterInstallCompleted, "") + log.Info("cluster creation complete") // Create default service entries cl, _ := db.Client.GetCluster(ctrl.ClusterName) diff --git a/providers/google/create.go b/providers/google/create.go index f28749da..75b35175 100644 --- a/providers/google/create.go +++ b/providers/google/create.go @@ -16,7 +16,6 @@ import ( "github.com/kubefirst/kubefirst-api/internal/services" "github.com/kubefirst/kubefirst-api/pkg/google" pkgtypes "github.com/kubefirst/kubefirst-api/pkg/types" - "github.com/kubefirst/metrics-client/pkg/telemetry" "github.com/kubefirst/runtime/pkg/k8s" log "github.com/sirupsen/logrus" ) @@ -252,7 +251,7 @@ func CreateGoogleCluster(definition *pkgtypes.ClusterDefinition) error { return err } - telemetry.SendEvent(ctrl.TelemetryEvent, telemetry.ClusterInstallCompleted, "") + log.Info("cluster creation complete") // Create default service entries cl, _ := db.Client.GetCluster(ctrl.ClusterName) diff --git a/providers/vultr/create.go b/providers/vultr/create.go index 041c09f7..cea5a136 100644 --- a/providers/vultr/create.go +++ b/providers/vultr/create.go @@ -14,7 +14,6 @@ import ( "github.com/kubefirst/kubefirst-api/internal/db" "github.com/kubefirst/kubefirst-api/internal/services" pkgtypes "github.com/kubefirst/kubefirst-api/pkg/types" - "github.com/kubefirst/metrics-client/pkg/telemetry" "github.com/kubefirst/runtime/pkg/k8s" "github.com/kubefirst/runtime/pkg/ssl" log "github.com/sirupsen/logrus" @@ -224,7 +223,7 @@ func CreateVultrCluster(definition *pkgtypes.ClusterDefinition) error { return err } - telemetry.SendEvent(ctrl.TelemetryEvent, telemetry.ClusterInstallCompleted, "") + log.Info("cluster creation complete") // Create default service entries cl, _ := db.Client.GetCluster(ctrl.ClusterName)