From 2f49f2fab5ce1a7a2afd9dd6caf9c808d5f216fe Mon Sep 17 00:00:00 2001 From: John Dietz Date: Sat, 21 Oct 2023 01:22:07 -0400 Subject: [PATCH] fix: telemetry sadness --- .github/workflows/main.yaml | 2 +- internal/telemetryShim/heartbeat.go | 5 +- internal/telemetryShim/telemetry.go | 64 +++++++++------------- main.go | 7 ++- pkg/segment/constants.go | 84 +++++++++++++++++++++++++++++ 5 files changed, 116 insertions(+), 46 deletions(-) create mode 100644 pkg/segment/constants.go diff --git a/.github/workflows/main.yaml b/.github/workflows/main.yaml index 880ce00f..a79bbbc5 100644 --- a/.github/workflows/main.yaml +++ b/.github/workflows/main.yaml @@ -5,7 +5,7 @@ env: on: push: branches: - - main + - hotwire jobs: publish: runs-on: self-hosted diff --git a/internal/telemetryShim/heartbeat.go b/internal/telemetryShim/heartbeat.go index 4e01a3ce..b4105e90 100644 --- a/internal/telemetryShim/heartbeat.go +++ b/internal/telemetryShim/heartbeat.go @@ -25,9 +25,6 @@ func HeartbeatWorkloadClusters() error { } kubefirstTeam := os.Getenv("KUBEFIRST_TEAM") - if kubefirstTeam == "" { - kubefirstTeam = "undefined" - } for _, cluster := range clusters { if cluster.Status == constants.ClusterStatusProvisioned { @@ -49,7 +46,7 @@ func HeartbeatWorkloadClusters() error { } defer segmentClient.Client.Close() - Transmit(true, segmentClient, segment.MetricKubefirstHeartbeat, "") + Transmit(segmentClient, segment.MetricKubefirstHeartbeat, "") } } } diff --git a/internal/telemetryShim/telemetry.go b/internal/telemetryShim/telemetry.go index 43ef4b68..9b9a2f81 100644 --- a/internal/telemetryShim/telemetry.go +++ b/internal/telemetryShim/telemetry.go @@ -10,17 +10,25 @@ import ( "os" "time" + "github.com/denisbrodbeck/machineid" pkgtypes "github.com/kubefirst/kubefirst-api/pkg/types" + "github.com/kubefirst/runtime/pkg" "github.com/kubefirst/runtime/pkg/segment" + "github.com/segmentio/analytics-go" log "github.com/sirupsen/logrus" ) // Heartbeat func Heartbeat(segmentClient *segment.SegmentClient) { - TransmitClusterZero(true, segmentClient, segment.MetricKubefirstHeartbeat, "") + // sent one heartbeat for the mgmt cluster + Transmit(segmentClient, segment.MetricKubefirstHeartbeat, "") + // workload HeartbeatWorkloadClusters() - for range time.Tick(time.Minute * 20) { - TransmitClusterZero(true, segmentClient, segment.MetricKubefirstHeartbeat, "") + //TODO! DIETZ - NO WAY + for range time.Tick(time.Minute * 2) { + // sent one heartbeat for the mgmt cluster + Transmit(segmentClient, segment.MetricKubefirstHeartbeat, "") + // workload HeartbeatWorkloadClusters() } } @@ -32,38 +40,25 @@ func SetupTelemetry(cl pkgtypes.Cluster) (*segment.SegmentClient, error) { kubefirstVersion = "development" } + strippedDomainName, err := pkg.RemoveSubdomainV2(cl.DomainName) + if err != nil { + return &segment.SegmentClient{}, nil + } + machineID, _ := machineid.ID() + // Segment Client segmentClient := &segment.SegmentClient{ + Client: analytics.New(segment.SegmentIOWriteKey), CliVersion: kubefirstVersion, CloudProvider: cl.CloudProvider, ClusterID: cl.ClusterID, ClusterType: cl.ClusterType, - DomainName: cl.DomainName, + DomainName: strippedDomainName, GitProvider: cl.GitProvider, KubefirstClient: "api", KubefirstTeam: cl.KubefirstTeam, KubefirstTeamInfo: os.Getenv("KUBEFIRST_TEAM_INFO"), - } - segmentClient.SetupClient() - - return segmentClient, nil -} - -func SetupInitialTelemetry(clusterID string, clusterType string, installMethod string) (*segment.SegmentClient, error) { - kubefirstVersion := os.Getenv("KUBEFIRST_VERSION") - if kubefirstVersion == "" { - kubefirstVersion = "development" - } - - // Segment Client - segmentClient := &segment.SegmentClient{ - CliVersion: kubefirstVersion, - ClusterID: clusterID, - ClusterType: clusterType, - KubefirstClient: "api", - KubefirstTeam: os.Getenv("KUBEFIRST_TEAM"), - KubefirstTeamInfo: os.Getenv("KUBEFIRST_TEAM_INFO"), - InstallMethod: installMethod, + MachineID: machineID, } segmentClient.SetupClient() @@ -71,20 +66,9 @@ func SetupInitialTelemetry(clusterID string, clusterType string, installMethod s } // Transmit sends a metric via Segment -func Transmit(useTelemetry bool, segmentClient *segment.SegmentClient, metricName string, errorMessage string) { - if useTelemetry { - segmentMsg := segmentClient.SendCountMetric(metricName, errorMessage) - if segmentMsg != "" { - log.Info(segmentMsg) - } - } -} - -func TransmitClusterZero(useTelemetry bool, segmentClient *segment.SegmentClient, metricName string, errorMessage string) { - if useTelemetry { - segmentMsg := segmentClient.SendCountClusterZeroMetric(metricName, errorMessage) - if segmentMsg != "" { - log.Info(segmentMsg) - } +func Transmit(segmentClient *segment.SegmentClient, metricName string, errorMessage string) { + segmentMsg := segmentClient.SendCountMetric(metricName, errorMessage) + if segmentMsg != "" { + log.Info(segmentMsg) } } diff --git a/main.go b/main.go index f405a602..b1bea2d7 100644 --- a/main.go +++ b/main.go @@ -110,8 +110,13 @@ func main() { docs.SwaggerInfo.BasePath = "/api/v1" docs.SwaggerInfo.Schemes = []string{"http"} + cl, err := db.Client.GetCluster(os.Getenv("CLUSTER_NAME")) + if err != nil { + log.Warn(err) + } + // Telemetry handler - segmentClient, err := telemetryShim.SetupInitialTelemetry(os.Getenv("CLUSTER_ID"), os.Getenv("CLUSTER_TYPE"), os.Getenv("INSTALL_METHOD")) + segmentClient, err := telemetryShim.SetupTelemetry(cl) if err != nil { log.Warn(err) } diff --git a/pkg/segment/constants.go b/pkg/segment/constants.go new file mode 100644 index 00000000..6eaa2d35 --- /dev/null +++ b/pkg/segment/constants.go @@ -0,0 +1,84 @@ +/* +Copyright (C) 2021-2023, Kubefirst + +This program is licensed under MIT. +See the LICENSE file for more details. +*/ +package segment + +// SegmentIO constants +// SegmentIOWriteKey The write key is the unique identifier for a source that tells Segment which source data comes +// from, to which workspace the data belongs, and which destinations should receive the data. +const ( + SegmentIOWriteKey = "0gAYkX5RV3vt7s4pqCOOsDb6WHPLT30M" + + // Heartbeat + MetricKubefirstHeartbeat = "kubefirst.heartbeat" + + // Install + MetricKubefirstInstalled = "kubefirst.installed" + + // Init + MetricInitStarted = "kubefirst.init.started" + MetricInitCompleted = "kubefirst.init.completed" + + MetricCloudCredentialsCheckStarted = "kubefirst.init.cloud_credentials_check.started" + MetricCloudCredentialsCheckCompleted = "kubefirst.init.cloud_credentials_check.completed" + MetricCloudCredentialsCheckFailed = "kubefirst.init.cloud_credentials_check.failed" + + MetricDomainLivenessStarted = "kubefirst.init.domain_liveness.started" + MetricDomainLivenessCompleted = "kubefirst.init.domain_liveness.completed" + MetricDomainLivenessFailed = "kubefirst.init.domain_liveness.failed" + + MetricStateStoreCreateStarted = "kubefirst.init.state_store_create.started" + MetricStateStoreCreateCompleted = "kubefirst.init.state_store_create.completed" + MetricStateStoreCreateFailed = "kubefirst.init.state_store_create.failed" + + MetricGitCredentialsCheckStarted = "kubefirst.init.git_credentials_check.started" + MetricGitCredentialsCheckCompleted = "kubefirst.init.git_credentials_check.completed" + MetricGitCredentialsCheckFailed = "kubefirst.init.git_credentials_check.failed" + + MetricKbotSetupStarted = "kubefirst.init.kbot_setup.started" + MetricKbotSetupCompleted = "kubefirst.init.kbot_setup.completed" + MetricKbotSetupFailed = "kubefirst.init.kbot_setup.failed" + + // Create + MetricClusterInstallStarted = "kubefirst.cluster_install.started" + MetricClusterInstallCompleted = "kubefirst.cluster_install.completed" + + MetricGitTerraformApplyStarted = "kubefirst.git_terraform_apply.started" + MetricGitTerraformApplyCompleted = "kubefirst.git_terraform_apply.completed" + MetricGitTerraformApplyFailed = "kubefirst.git_terraform_apply.failed" + + MetricGitopsRepoPushStarted = "kubefirst.gitops_repo_push.started" + MetricGitopsRepoPushCompleted = "kubefirst.gitops_repo_push.completed" + MetricGitopsRepoPushFailed = "kubefirst.gitops_repo_push.failed" + + MetricCloudTerraformApplyStarted = "kubefirst.cloud_terraform_apply.started" + MetricCloudTerraformApplyCompleted = "kubefirst.cloud_terraform_apply.completed" + MetricCloudTerraformApplyFailed = "kubefirst.cloud_terraform_apply.failed" + + MetricArgoCDInstallStarted = "kubefirst.argocd_install.started" + MetricArgoCDInstallCompleted = "kubefirst.argocd_install.completed" + MetricArgoCDInstallFailed = "kubefirst.argocd_install.failed" + + MetricCreateRegistryStarted = "kubefirst.create_registry.started" + MetricCreateRegistryCompleted = "kubefirst.create_registry.completed" + MetricCreateRegistryFailed = "kubefirst.create_registry.failed" + + MetricVaultInitializationStarted = "kubefirst.vault_initialization.started" + MetricVaultInitializationCompleted = "kubefirst.vault_initialization.completed" + MetricVaultInitializationFailed = "kubefirst.vault_initialization.failed" + + MetricVaultTerraformApplyStarted = "kubefirst.vault_terraform_apply.started" + MetricVaultTerraformApplyCompleted = "kubefirst.vault_terraform_apply.completed" + MetricVaultTerraformApplyFailed = "kubefirst.vault_terraform_apply.failed" + + MetricUsersTerraformApplyStarted = "kubefirst.users_terraform_apply.started" + MetricUsersTerraformApplyCompleted = "kubefirst.users_terraform_apply.completed" + MetricUsersTerraformApplyFailed = "kubefirst.users_terraform_apply.failed" + + // Delete + MetricClusterDeleteStarted = "kubefirst.cluster_delete.started" + MetricClusterDeleteCompleted = "kubefirst.cluster_delete.completed" +)