Skip to content

Commit

Permalink
move metrics and types back to metrics client
Browse files Browse the repository at this point in the history
  • Loading branch information
jarededwards committed Oct 22, 2023
1 parent 4e5a38d commit a3f42f5
Show file tree
Hide file tree
Showing 24 changed files with 63 additions and 67 deletions.
10 changes: 5 additions & 5 deletions internal/controller/argocd.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,16 +56,16 @@ func (clctrl *ClusterController) InstallArgoCD() error {
argoCDInstallPath := fmt.Sprintf("github.com:kubefirst/manifests/argocd/cloud?ref=%s", pkg.KubefirstManifestRepoRef)
log.Infof("installing argocd")

telemetry.SendCountMetric(clctrl.SegmentClient, telemetry.ArgoCDInstallStarted, err.Error())
telemetry.SendEvent(clctrl.SegmentClient, telemetry.ArgoCDInstallStarted, err.Error())
err = argocd.ApplyArgoCDKustomize(kcfg.Clientset, argoCDInstallPath)
if err != nil {

telemetry.SendCountMetric(clctrl.SegmentClient, telemetry.ArgoCDInstallFailed, err.Error())
telemetry.SendEvent(clctrl.SegmentClient, telemetry.ArgoCDInstallFailed, err.Error())

return err
}

telemetry.SendCountMetric(clctrl.SegmentClient, telemetry.ArgoCDInstallCompleted, err.Error())
telemetry.SendEvent(clctrl.SegmentClient, telemetry.ArgoCDInstallCompleted, err.Error())

// Wait for ArgoCD to be ready
_, err = k8s.VerifyArgoCDReadiness(kcfg.Clientset, true, 300)
Expand Down Expand Up @@ -194,7 +194,7 @@ func (clctrl *ClusterController) DeployRegistryApplication() error {
}
}

telemetry.SendCountMetric(clctrl.SegmentClient, telemetry.CreateRegistryStarted, err.Error())
telemetry.SendEvent(clctrl.SegmentClient, telemetry.CreateRegistryStarted, err.Error())
argocdClient, err := argocdapi.NewForConfig(kcfg.RestConfig)
if err != nil {
return err
Expand Down Expand Up @@ -239,7 +239,7 @@ func (clctrl *ClusterController) DeployRegistryApplication() error {

_, _ = argocdClient.ArgoprojV1alpha1().Applications("argocd").Create(context.Background(), registryApplicationObject, metav1.CreateOptions{})

telemetry.SendCountMetric(clctrl.SegmentClient, telemetry.CreateRegistryCompleted, err.Error())
telemetry.SendEvent(clctrl.SegmentClient, telemetry.CreateRegistryCompleted, err.Error())

err = clctrl.MdbCl.UpdateCluster(clctrl.ClusterName, "argocd_create_registry_check", true)
if err != nil {
Expand Down
6 changes: 3 additions & 3 deletions internal/controller/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func (clctrl *ClusterController) CreateCluster() error {
tfEntrypoint := clctrl.ProviderConfig.GitopsDir + fmt.Sprintf("/terraform/%s", clctrl.CloudProvider)
tfEnvs := map[string]string{}

telemetry.SendCountMetric(clctrl.SegmentClient, telemetry.CloudTerraformApplyStarted, err.Error())
telemetry.SendEvent(clctrl.SegmentClient, telemetry.CloudTerraformApplyStarted, err.Error())

log.Infof("creating %s cluster", clctrl.CloudProvider)

Expand Down Expand Up @@ -86,12 +86,12 @@ func (clctrl *ClusterController) CreateCluster() error {
if err != nil {
return err
}
telemetry.SendCountMetric(clctrl.SegmentClient, telemetry.CloudTerraformApplyFailed, err.Error())
telemetry.SendEvent(clctrl.SegmentClient, telemetry.CloudTerraformApplyFailed, err.Error())
return fmt.Errorf(msg)
}

log.Infof("created %s cloud resources", clctrl.CloudProvider)
telemetry.SendCountMetric(clctrl.SegmentClient, telemetry.CloudTerraformApplyCompleted, err.Error())
telemetry.SendEvent(clctrl.SegmentClient, telemetry.CloudTerraformApplyCompleted, err.Error())

err = clctrl.MdbCl.UpdateCluster(clctrl.ClusterName, "cloud_terraform_apply_failed_check", false)
if err != nil {
Expand Down
7 changes: 3 additions & 4 deletions internal/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
google "github.com/kubefirst/kubefirst-api/pkg/google"
"github.com/kubefirst/kubefirst-api/pkg/handlers"
"github.com/kubefirst/kubefirst-api/pkg/providerConfigs"
"github.com/kubefirst/kubefirst-api/pkg/segment"
pkgtypes "github.com/kubefirst/kubefirst-api/pkg/types"
"github.com/kubefirst/metrics-client/pkg/telemetry"
"github.com/kubefirst/runtime/pkg"
Expand Down Expand Up @@ -172,17 +171,17 @@ func (clctrl *ClusterController) InitController(def *pkgtypes.ClusterDefinition)
UserId: machineID,
MetricName: telemetry.ClusterInstallStarted,
},
Client: analytics.New(segment.SegmentIOWriteKey),
Client: analytics.New(telemetry.SegmentIOWriteKey),
}
if err != nil {
log.Warn(err)
}
defer segClient.Client.Close()

if clctrl.SegmentClient.UseTelemetry {
telemetry.SendCountMetric(&segClient, telemetry.ClusterInstallStarted, err.Error())
telemetry.SendEvent(&segClient, telemetry.ClusterInstallStarted, err.Error())
}
clctrl.Telemetry = &segClient
clctrl.SegmentClient = &segClient

//Copy Cluster Definiion to Cluster Controller
clctrl.AlertsEmail = def.AdminEmail
Expand Down
6 changes: 3 additions & 3 deletions internal/controller/domain.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func (clctrl *ClusterController) DomainLivenessTest() error {
}

if !cl.DomainLivenessCheck {
telemetry.SendCountMetric(clctrl.SegmentClient, telemetry.DomainLivenessStarted, err.Error())
telemetry.SendEvent(clctrl.SegmentClient, telemetry.DomainLivenessStarted, err.Error())

switch clctrl.DnsProvider {
case "aws":
Expand All @@ -46,7 +46,7 @@ func (clctrl *ClusterController) DomainLivenessTest() error {
// domain id
domainId, err := civoConf.GetDNSInfo(clctrl.DomainName, clctrl.CloudRegion)
if err != nil {
telemetry.SendCountMetric(clctrl.SegmentClient, telemetry.DomainLivenessFailed, err.Error())
telemetry.SendEvent(clctrl.SegmentClient, telemetry.DomainLivenessFailed, err.Error())
log.Info(err.Error())
}

Expand Down Expand Up @@ -121,7 +121,7 @@ func (clctrl *ClusterController) DomainLivenessTest() error {
return err
}

telemetry.SendCountMetric(clctrl.SegmentClient, telemetry.DomainLivenessCompleted, err.Error())
telemetry.SendEvent(clctrl.SegmentClient, telemetry.DomainLivenessCompleted, err.Error())

log.Infof("domain %s verified", clctrl.DomainName)
}
Expand Down
6 changes: 3 additions & 3 deletions internal/controller/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func (clctrl *ClusterController) RunGitTerraform() error {
}

// //* create teams and repositories in github
telemetry.SendCountMetric(clctrl.SegmentClient, telemetry.GitTerraformApplyStarted, err.Error())
telemetry.SendEvent(clctrl.SegmentClient, telemetry.GitTerraformApplyStarted, err.Error())

log.Infof("Creating %s resources with terraform", clctrl.GitProvider)

Expand Down Expand Up @@ -113,12 +113,12 @@ func (clctrl *ClusterController) RunGitTerraform() error {
if err != nil {
msg := fmt.Sprintf("error creating %s resources with terraform %s: %s", clctrl.GitProvider, tfEntrypoint, err)
log.Error(msg)
telemetry.SendCountMetric(clctrl.SegmentClient, telemetry.GitTerraformApplyFailed, err.Error())
telemetry.SendEvent(clctrl.SegmentClient, telemetry.GitTerraformApplyFailed, err.Error())
return fmt.Errorf(msg)
}

log.Infof("created git projects and groups for %s.com/%s", clctrl.GitProvider, clctrl.GitAuth.Owner)
telemetry.SendCountMetric(clctrl.SegmentClient, telemetry.GitTerraformApplyCompleted, err.Error())
telemetry.SendEvent(clctrl.SegmentClient, telemetry.GitTerraformApplyCompleted, err.Error())

err = clctrl.MdbCl.UpdateCluster(clctrl.ClusterName, "git_terraform_apply_check", true)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion internal/controller/kbot.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func (clctrl *ClusterController) InitializeBot() error {
clctrl.GitAuth.PrivateKey, clctrl.GitAuth.PublicKey, err = internalssh.CreateSshKeyPair()
if err != nil {
log.Errorf("error generating ssh keys: %s", err)
telemetry.SendCountMetric(clctrl.SegmentClient, telemetry.KbotSetupFailed, err.Error())
telemetry.SendEvent(clctrl.SegmentClient, telemetry.KbotSetupFailed, err.Error())
return err
}

Expand Down
8 changes: 4 additions & 4 deletions internal/controller/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ func (clctrl *ClusterController) RepositoryPush() error {
gitopsDir := clctrl.ProviderConfig.GitopsDir
metaphorDir := clctrl.ProviderConfig.MetaphorDir

telemetry.SendCountMetric(clctrl.SegmentClient, telemetry.GitopsRepoPushStarted, err.Error())
telemetry.SendEvent(clctrl.SegmentClient, telemetry.GitopsRepoPushStarted, err.Error())
gitopsRepo, err := git.PlainOpen(gitopsDir)
if err != nil {
log.Infof("error opening repo at: %s", gitopsDir)
Expand Down Expand Up @@ -250,7 +250,7 @@ func (clctrl *ClusterController) RepositoryPush() error {
)
if err != nil {
msg := fmt.Sprintf("error pushing detokenized gitops repository to remote %s: %s", clctrl.ProviderConfig.DestinationGitopsRepoURL, err)
telemetry.SendCountMetric(clctrl.SegmentClient, telemetry.GitopsRepoPushFailed, err.Error())
telemetry.SendEvent(clctrl.SegmentClient, telemetry.GitopsRepoPushFailed, err.Error())
return fmt.Errorf(msg)
}

Expand All @@ -266,14 +266,14 @@ func (clctrl *ClusterController) RepositoryPush() error {
)
if err != nil {
msg := fmt.Sprintf("error pushing detokenized metaphor repository to remote %s: %s", clctrl.ProviderConfig.DestinationMetaphorRepoURL, err)
telemetry.SendCountMetric(clctrl.SegmentClient, telemetry.GitopsRepoPushFailed, err.Error())
telemetry.SendEvent(clctrl.SegmentClient, telemetry.GitopsRepoPushFailed, err.Error())
return fmt.Errorf(msg)
}

log.Infof("successfully pushed gitops and metaphor repositories to git@%s/%s", clctrl.GitHost, clctrl.GitAuth.Owner)
// todo delete the local gitops repo and re-clone it
// todo that way we can stop worrying about which origin we're going to push to
telemetry.SendCountMetric(clctrl.SegmentClient, telemetry.GitopsRepoPushCompleted, err.Error())
telemetry.SendEvent(clctrl.SegmentClient, telemetry.GitopsRepoPushCompleted, err.Error())

err = clctrl.MdbCl.UpdateCluster(clctrl.ClusterName, "gitops_pushed_check", true)
if err != nil {
Expand Down
14 changes: 7 additions & 7 deletions internal/controller/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func (clctrl *ClusterController) StateStoreCredentials() error {
}

var stateStoreData pkgtypes.StateStoreCredentials
telemetry.SendCountMetric(clctrl.SegmentClient, telemetry.StateStoreCredentialsCreateStarted, err.Error())
telemetry.SendEvent(clctrl.SegmentClient, telemetry.StateStoreCredentialsCreateStarted, err.Error())

if !cl.StateStoreCredsCheck {
switch clctrl.CloudProvider {
Expand Down Expand Up @@ -100,7 +100,7 @@ func (clctrl *ClusterController) StateStoreCredentials() error {
if err != nil {
msg := fmt.Sprintf("error creating spaces bucket %s: %s", clctrl.KubefirstStateStoreBucketName, err)
log.Error(msg)
telemetry.SendCountMetric(clctrl.SegmentClient, telemetry.StateStoreCredentialsCreateFailed, err.Error())
telemetry.SendEvent(clctrl.SegmentClient, telemetry.StateStoreCredentialsCreateFailed, err.Error())
return fmt.Errorf(msg)
}

Expand All @@ -126,7 +126,7 @@ func (clctrl *ClusterController) StateStoreCredentials() error {
if err != nil {
msg := fmt.Sprintf("error creating google bucket %s: %s", clctrl.KubefirstStateStoreBucketName, err)
log.Error()
telemetry.SendCountMetric(clctrl.SegmentClient, telemetry.StateStoreCreateFailed, msg)
telemetry.SendEvent(clctrl.SegmentClient, telemetry.StateStoreCreateFailed, msg)
return fmt.Errorf(msg)
}

Expand All @@ -141,7 +141,7 @@ func (clctrl *ClusterController) StateStoreCredentials() error {

objst, err := vultrConf.CreateObjectStorage(clctrl.KubefirstStateStoreBucketName)
if err != nil {
telemetry.SendCountMetric(clctrl.SegmentClient, telemetry.StateStoreCreateFailed, err.Error())
telemetry.SendEvent(clctrl.SegmentClient, telemetry.StateStoreCreateFailed, err.Error())
log.Error(err.Error())
return err
}
Expand Down Expand Up @@ -202,14 +202,14 @@ func (clctrl *ClusterController) StateStoreCreate() error {
Context: context.Background(),
}

telemetry.SendCountMetric(clctrl.SegmentClient, telemetry.StateStoreCreateStarted, err.Error())
telemetry.SendEvent(clctrl.SegmentClient, telemetry.StateStoreCreateStarted, err.Error())

accessKeyId := cl.StateStoreCredentials.AccessKeyID
log.Infof("access key id %s", accessKeyId)

bucket, err := civoConf.CreateStorageBucket(accessKeyId, clctrl.KubefirstStateStoreBucketName, clctrl.CloudRegion)
if err != nil {
telemetry.SendCountMetric(clctrl.SegmentClient, telemetry.StateStoreCreateFailed, err.Error())
telemetry.SendEvent(clctrl.SegmentClient, telemetry.StateStoreCreateFailed, err.Error())
log.Error(err.Error())
return err
}
Expand All @@ -229,7 +229,7 @@ func (clctrl *ClusterController) StateStoreCreate() error {
return err
}

telemetry.SendCountMetric(clctrl.SegmentClient, telemetry.StateStoreCreateCompleted, err.Error())
telemetry.SendEvent(clctrl.SegmentClient, telemetry.StateStoreCreateCompleted, err.Error())
log.Infof("%s state store bucket created", clctrl.CloudProvider)
}
}
Expand Down
6 changes: 3 additions & 3 deletions internal/controller/users.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func (clctrl *ClusterController) RunUsersTerraform() error {
}
}

telemetry.SendCountMetric(clctrl.SegmentClient, telemetry.UsersTerraformApplyStarted, err.Error())
telemetry.SendEvent(clctrl.SegmentClient, telemetry.UsersTerraformApplyStarted, err.Error())
log.Info("applying users terraform")

tfEnvs := map[string]string{}
Expand Down Expand Up @@ -80,11 +80,11 @@ func (clctrl *ClusterController) RunUsersTerraform() error {
err = terraformext.InitApplyAutoApprove(terraformClient, tfEntrypoint, tfEnvs)
if err != nil {
log.Errorf("error applying users terraform: %s", err)
telemetry.SendCountMetric(clctrl.SegmentClient, telemetry.UsersTerraformApplyFailed, err.Error())
telemetry.SendEvent(clctrl.SegmentClient, telemetry.UsersTerraformApplyFailed, err.Error())
return err
}
log.Info("executed users terraform successfully")
telemetry.SendCountMetric(clctrl.SegmentClient, telemetry.UsersTerraformApplyCompleted, err.Error())
telemetry.SendEvent(clctrl.SegmentClient, telemetry.UsersTerraformApplyCompleted, err.Error())

clctrl.VaultAuth.RootToken = tfEnvs["VAULT_TOKEN"]
err = clctrl.MdbCl.UpdateCluster(clctrl.ClusterName, "vault_auth.root_token", clctrl.VaultAuth.RootToken)
Expand Down
12 changes: 6 additions & 6 deletions internal/controller/vault.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ func (clctrl *ClusterController) InitializeVault() error {
}
}

telemetry.SendCountMetric(clctrl.SegmentClient, telemetry.VaultInitializationStarted, err.Error())
telemetry.SendEvent(clctrl.SegmentClient, telemetry.VaultInitializationStarted, err.Error())

switch clctrl.CloudProvider {
case "aws", "google":
Expand Down Expand Up @@ -150,11 +150,11 @@ func (clctrl *ClusterController) InitializeVault() error {
_, err = k8s.WaitForJobComplete(kcfg.Clientset, job, 240)
if err != nil {
msg := fmt.Sprintf("could not run vault unseal job: %s", err)
telemetry.SendCountMetric(clctrl.SegmentClient, telemetry.VaultInitializationFailed, err.Error())
telemetry.SendEvent(clctrl.SegmentClient, telemetry.VaultInitializationFailed, err.Error())
log.Error(msg)
}
}
telemetry.SendCountMetric(clctrl.SegmentClient, telemetry.VaultInitializationCompleted, err.Error())
telemetry.SendEvent(clctrl.SegmentClient, telemetry.VaultInitializationCompleted, err.Error())

err = clctrl.MdbCl.UpdateCluster(clctrl.ClusterName, "vault_initialized_check", true)
if err != nil {
Expand Down Expand Up @@ -196,7 +196,7 @@ func (clctrl *ClusterController) RunVaultTerraform() error {
return err
}
}
telemetry.SendCountMetric(clctrl.SegmentClient, telemetry.VaultTerraformApplyStarted, err.Error())
telemetry.SendEvent(clctrl.SegmentClient, telemetry.VaultTerraformApplyStarted, err.Error())

tfEnvs := map[string]string{}

Expand Down Expand Up @@ -250,12 +250,12 @@ func (clctrl *ClusterController) RunVaultTerraform() error {
err = terraformext.InitApplyAutoApprove(terraformClient, tfEntrypoint, tfEnvs)
if err != nil {
log.Errorf("error applying vault terraform: %s", err)
telemetry.SendCountMetric(clctrl.SegmentClient, telemetry.VaultTerraformApplyFailed, err.Error())
telemetry.SendEvent(clctrl.SegmentClient, telemetry.VaultTerraformApplyFailed, err.Error())
return err
}

log.Info("vault terraform executed successfully")
telemetry.SendCountMetric(clctrl.SegmentClient, telemetry.VaultTerraformApplyCompleted, err.Error())
telemetry.SendEvent(clctrl.SegmentClient, telemetry.VaultTerraformApplyCompleted, err.Error())

err = clctrl.MdbCl.UpdateCluster(clctrl.ClusterName, "vault_terraform_apply_check", true)
if err != nil {
Expand Down
3 changes: 1 addition & 2 deletions internal/router/api/v1/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
"github.com/kubefirst/kubefirst-api/internal/services"
"github.com/kubefirst/kubefirst-api/internal/types"
"github.com/kubefirst/kubefirst-api/internal/utils"
"github.com/kubefirst/kubefirst-api/pkg/segment"
pkgtypes "github.com/kubefirst/kubefirst-api/pkg/types"
"github.com/kubefirst/kubefirst-api/providers/aws"
"github.com/kubefirst/kubefirst-api/providers/civo"
Expand Down Expand Up @@ -82,7 +81,7 @@ func DeleteCluster(c *gin.Context) {
UserId: machineID,
MetricName: telemetry.ClusterDeleteStarted,
},
Client: analytics.New(segment.SegmentIOWriteKey),
Client: analytics.New(telemetry.SegmentIOWriteKey),
}
defer segClient.Client.Close()

Expand Down
5 changes: 2 additions & 3 deletions internal/router/api/v1/telemetry.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import (
"github.com/gin-gonic/gin"
"github.com/kubefirst/kubefirst-api/internal/db"
"github.com/kubefirst/kubefirst-api/internal/types"
"github.com/kubefirst/kubefirst-api/pkg/segment"
"github.com/kubefirst/metrics-client/pkg/telemetry"
"github.com/segmentio/analytics-go"
)
Expand Down Expand Up @@ -55,7 +54,7 @@ func PostTelemetry(c *gin.Context) {
UserId: machineID,
MetricName: telemetry.ClusterDeleteStarted,
},
Client: analytics.New(segment.SegmentIOWriteKey),
Client: analytics.New(telemetry.SegmentIOWriteKey),
}
defer segClient.Client.Close()
useTelemetry := true
Expand Down Expand Up @@ -94,7 +93,7 @@ func PostTelemetry(c *gin.Context) {
return
}

telemetry.SendCountMetric(&segClient, req.Event, err.Error())
telemetry.SendEvent(&segClient, req.Event, err.Error())

c.JSON(http.StatusOK, true)
}
9 changes: 4 additions & 5 deletions internal/telemetry/heartbeat.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,16 @@ import (

"github.com/kubefirst/kubefirst-api/internal/constants"
"github.com/kubefirst/kubefirst-api/internal/db"
"github.com/kubefirst/kubefirst-api/pkg/segment"
"github.com/kubefirst/metrics-client/pkg/telemetry"
"github.com/segmentio/analytics-go"
)

func Heartbeat(segmentClient *telemetry.SegmentClient, dbClient *db.MongoDBClient) {
telemetry.SendCountMetric(segmentClient, telemetry.KubefirstHeartbeat, "")
telemetry.SendEvent(segmentClient, telemetry.KubefirstHeartbeat, "")
HeartbeatWorkloadClusters(segmentClient, dbClient)

for range time.Tick(time.Second * 30) {
telemetry.SendCountMetric(segmentClient, telemetry.KubefirstHeartbeat, "")
telemetry.SendEvent(segmentClient, telemetry.KubefirstHeartbeat, "")
HeartbeatWorkloadClusters(segmentClient, dbClient)
}
}
Expand Down Expand Up @@ -46,11 +45,11 @@ func HeartbeatWorkloadClusters(segmentClient *telemetry.SegmentClient, dbClient
UserId: segmentClient.TelemetryEvent.MachineID,
MetricName: telemetry.KubefirstHeartbeat,
},
Client: analytics.New(segment.SegmentIOWriteKey),
Client: analytics.New(telemetry.SegmentIOWriteKey),
}
defer workloadClient.Client.Close()

telemetry.SendCountMetric(&workloadClient, telemetry.KubefirstHeartbeat, "")
telemetry.SendEvent(&workloadClient, telemetry.KubefirstHeartbeat, "")
}
}
}
Expand Down
Loading

0 comments on commit a3f42f5

Please sign in to comment.