Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: add wrapper for telemetry #741

Merged
merged 2 commits into from
Nov 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 6 additions & 64 deletions cmd/local/local.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,24 @@ package local
import (
"context"
"fmt"
"github.com/kubefirst/kubefirst/configs"
"github.com/kubefirst/kubefirst/internal/wrappers"
"log"
"sync"
"time"

"github.com/go-git/go-git/v5/plumbing"
"github.com/kubefirst/kubefirst/configs"
"github.com/kubefirst/kubefirst/internal/argocd"
"github.com/kubefirst/kubefirst/internal/domain"
"github.com/kubefirst/kubefirst/internal/gitClient"
"github.com/kubefirst/kubefirst/internal/githubWrapper"
"github.com/kubefirst/kubefirst/internal/handlers"
"github.com/kubefirst/kubefirst/internal/helm"
"github.com/kubefirst/kubefirst/internal/k3d"
"github.com/kubefirst/kubefirst/internal/k8s"
"github.com/kubefirst/kubefirst/internal/metaphor"
"github.com/kubefirst/kubefirst/internal/progressPrinter"
"github.com/kubefirst/kubefirst/internal/services"
"github.com/kubefirst/kubefirst/internal/terraform"
"github.com/kubefirst/kubefirst/internal/vault"
"github.com/kubefirst/kubefirst/pkg"
"github.com/segmentio/analytics-go"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
Expand All @@ -35,7 +32,6 @@ var (
enableConsole bool
gitOpsBranch string
gitOpsRepo string
awsHostedZone string
metaphorBranch string
adminEmail string
templateTag string
Expand Down Expand Up @@ -66,8 +62,6 @@ func NewCommand() *cobra.Command {
"when running a built version, and ldflag is set for the Kubefirst version, it will use this tag value to clone the templates (gitops and metaphor's)",
)

localCmd.AddCommand(NewCommandConnect())

// on error, doesnt show helper/usage
localCmd.SilenceUsage = true

Expand All @@ -87,42 +81,13 @@ func runLocal(cmd *cobra.Command, args []string) error {
progressPrinter.AddTracker("step-base", "Setup base cluster", 2)
progressPrinter.AddTracker("step-apps", "Install apps to cluster", 4)

if useTelemetry {
progressPrinter.AddTracker("step-telemetry", pkg.SendTelemetry, 1)
}

progressPrinter.SetupProgress(progressPrinter.TotalOfTrackers(), silentMode)

// telemetry
if useTelemetry {
// Instantiates a SegmentIO client to send messages to the segment API.
segmentIOClientStart := analytics.New(pkg.SegmentIOWriteKey)

// SegmentIO library works with queue that is based on timing, we explicit close the http client connection
// to force flush in case there is still some pending message in the SegmentIO library queue.
defer func(segmentIOClient analytics.Client) {
err := segmentIOClient.Close()
if err != nil {
log.Println(err)
}
}(segmentIOClientStart)

telemetryDomainStart, err := domain.NewTelemetry(
pkg.MetricMgmtClusterInstallStarted,
"",
configs.K1Version,
)
if err != nil {
progressPrinter.AddTracker("step-telemetry", pkg.SendTelemetry, 2)
if err := wrappers.SendTelemetry("", pkg.MetricMgmtClusterInstallStarted); err != nil {
log.Println(err)
}
telemetryServiceStart := services.NewSegmentIoService(segmentIOClientStart)
telemetryHandlerStart := handlers.NewTelemetryHandler(telemetryServiceStart)

err = telemetryHandlerStart.SendCountMetric(telemetryDomainStart)
if err != nil {
log.Println(err)
}

progressPrinter.IncrementTracker("step-telemetry", 1)
}

Expand Down Expand Up @@ -486,33 +451,10 @@ func runLocal(cmd *cobra.Command, args []string) error {
log.Println("sending mgmt cluster install completed metric")

if useTelemetry {
// Instantiates a SegmentIO client to send messages to the segment API.
segmentIOClientCompleted := analytics.New(pkg.SegmentIOWriteKey)

// SegmentIO library works with queue that is based on timing, we explicit close the http client connection
// to force flush in case there is still some pending message in the SegmentIO library queue.
defer func(segmentIOClientCompleted analytics.Client) {
err := segmentIOClientCompleted.Close()
if err != nil {
log.Println(err)
}
}(segmentIOClientCompleted)

telemetryDomainCompleted, err := domain.NewTelemetry(
pkg.MetricMgmtClusterInstallCompleted,
"",
configs.K1Version,
)
if err != nil {
log.Println(err)
}
telemetryServiceCompleted := services.NewSegmentIoService(segmentIOClientCompleted)
telemetryHandlerCompleted := handlers.NewTelemetryHandler(telemetryServiceCompleted)

err = telemetryHandlerCompleted.SendCountMetric(telemetryDomainCompleted)
if err != nil {
if err = wrappers.SendTelemetry("", pkg.MetricMgmtClusterInstallCompleted); err != nil {
log.Println(err)
}
progressPrinter.IncrementTracker("step-telemetry", 1)
}

log.Println("Kubefirst installation finished successfully")
Expand Down
53 changes: 8 additions & 45 deletions cmd/local/prerun.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,13 @@ import (
"github.com/dustin/go-humanize"
"github.com/kubefirst/kubefirst/configs"
"github.com/kubefirst/kubefirst/internal/addon"
"github.com/kubefirst/kubefirst/internal/domain"
"github.com/kubefirst/kubefirst/internal/downloadManager"
"github.com/kubefirst/kubefirst/internal/handlers"
"github.com/kubefirst/kubefirst/internal/progressPrinter"
"github.com/kubefirst/kubefirst/internal/repo"
"github.com/kubefirst/kubefirst/internal/services"
"github.com/kubefirst/kubefirst/internal/wrappers"
"github.com/kubefirst/kubefirst/pkg"
"github.com/segmentio/analytics-go"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"log"
Expand All @@ -27,34 +26,8 @@ func validateLocal(cmd *cobra.Command, args []string) error {

log.Println("sending init started metric")

var telemetryHandler handlers.TelemetryHandler
if useTelemetry {
// Instantiates a SegmentIO client to use send messages to the segment API.
segmentIOClient := analytics.New(pkg.SegmentIOWriteKey)

// SegmentIO library works with queue that is based on timing, we explicit close the http client connection
// to force flush in case there is still some pending message in the SegmentIO library queue.
defer func(segmentIOClient analytics.Client) {
err := segmentIOClient.Close()
if err != nil {
log.Println(err)
}
}(segmentIOClient)

// validate telemetryDomain data
telemetryDomain, err := domain.NewTelemetry(
pkg.MetricInitStarted,
awsHostedZone,
configs.K1Version,
)
if err != nil {
log.Println(err)
}
telemetryService := services.NewSegmentIoService(segmentIOClient)
telemetryHandler = handlers.NewTelemetryHandler(telemetryService)

err = telemetryHandler.SendCountMetric(telemetryDomain)
if err != nil {
if err := wrappers.SendTelemetry("", pkg.MetricInitStarted); err != nil {
log.Println(err)
}
}
Expand Down Expand Up @@ -103,16 +76,15 @@ func validateLocal(cmd *cobra.Command, args []string) error {
viper.Set("cluster-name", pkg.LocalClusterName)
viper.Set("adminemail", adminEmail)

// todo: set constants
viper.Set("argocd.local.service", "http://localhost:8080")
viper.Set("gitlab.local.service", "http://localhost:8888")
viper.Set("vault.local.service", "http://localhost:8200")
viper.Set("argocd.local.service", pkg.ArgoCDLocalURL)
viper.Set("vault.local.service", pkg.VaultLocalURL)

// addons
addon.AddAddon("github")
addon.AddAddon("k3d")
// used for letsencrypt notifications and the gitlab root account

atlantisWebhookSecret := pkg.Random(20)
viper.Set("github.atlantis.webhook.secret", atlantisWebhookSecret)
viper.Set("github.atlantis.webhook.secret", pkg.Random(20))

err = viper.WriteConfig()
if err != nil {
Expand Down Expand Up @@ -206,16 +178,7 @@ func validateLocal(cmd *cobra.Command, args []string) error {
pkg.InformUser("init is done!\n", silentMode)

if useTelemetry {
telemetryInitCompleted, err := domain.NewTelemetry(
pkg.MetricInitCompleted,
awsHostedZone,
configs.K1Version,
)
if err != nil {
log.Println(err)
}
err = telemetryHandler.SendCountMetric(telemetryInitCompleted)
if err != nil {
if err = wrappers.SendTelemetry("", pkg.MetricInitCompleted); err != nil {
log.Println(err)
}
}
Expand Down
45 changes: 45 additions & 0 deletions internal/wrappers/segmentIo.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package wrappers

import (
"github.com/kubefirst/kubefirst/configs"
"github.com/kubefirst/kubefirst/internal/domain"
"github.com/kubefirst/kubefirst/internal/handlers"
"github.com/kubefirst/kubefirst/internal/services"
"github.com/kubefirst/kubefirst/pkg"
"github.com/segmentio/analytics-go"
"log"
)

// SendTelemetry is a wrapper function that instantiate SegmentIO handler, service, and sends a track activity to
// SegmentIO.
func SendTelemetry(hostedZone string, metricName string) error {
// Instantiates a SegmentIO client to use send messages to the segment API.
segmentIOClient := analytics.New(pkg.SegmentIOWriteKey)

// SegmentIO library works with queue that is based on timing, we explicit close the http client connection
// to force flush in case there is still some pending message in the SegmentIO library queue.
defer func(segmentIOClient analytics.Client) {
err := segmentIOClient.Close()
if err != nil {
log.Println(err)
}
}(segmentIOClient)

// validate telemetryDomain data
telemetryDomain, err := domain.NewTelemetry(
metricName,
hostedZone,
configs.K1Version,
)
if err != nil {
return err
}
telemetryService := services.NewSegmentIoService(segmentIOClient)
telemetryHandler := handlers.NewTelemetryHandler(telemetryService)

err = telemetryHandler.SendCountMetric(telemetryDomain)
if err != nil {
return err
}
return nil
}