Skip to content

Commit

Permalink
fix: telemetry sadness
Browse files Browse the repository at this point in the history
  • Loading branch information
johndietz committed Oct 21, 2023
1 parent d94f8ee commit 2f49f2f
Show file tree
Hide file tree
Showing 5 changed files with 116 additions and 46 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/main.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ env:
on:
push:
branches:
- main
- hotwire
jobs:
publish:
runs-on: self-hosted
Expand Down
5 changes: 1 addition & 4 deletions internal/telemetryShim/heartbeat.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -49,7 +46,7 @@ func HeartbeatWorkloadClusters() error {
}
defer segmentClient.Client.Close()

Transmit(true, segmentClient, segment.MetricKubefirstHeartbeat, "")
Transmit(segmentClient, segment.MetricKubefirstHeartbeat, "")
}
}
}
Expand Down
64 changes: 24 additions & 40 deletions internal/telemetryShim/telemetry.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
}
Expand All @@ -32,59 +40,35 @@ 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()

return segmentClient, nil
}

// 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)
}
}
7 changes: 6 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
84 changes: 84 additions & 0 deletions pkg/segment/constants.go
Original file line number Diff line number Diff line change
@@ -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"
)

0 comments on commit 2f49f2f

Please sign in to comment.