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

Feat remove subdomain telemetry merge gitlab attempt #563

Merged
merged 7 commits into from
Oct 14, 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
28 changes: 18 additions & 10 deletions cmd/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ package cmd

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"
"net/http"
"time"

"github.com/kubefirst/kubefirst/internal/gitlab"
Expand Down Expand Up @@ -49,9 +49,6 @@ cluster provisioning process spinning up the services, and validates the livenes

hostedZoneName := viper.GetString("aws.hostedzonename")

// instantiate http client with default values
httpClient := http.DefaultClient

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

Expand All @@ -64,12 +61,19 @@ cluster provisioning process spinning up the services, and validates the livenes
}
}(segmentIOClient)

telemetryDomain, err := domain.NewTelemetry(
pkg.MetricMgmtClusterInstallStarted,
hostedZoneName,
configs.K1Version,
)
if err != nil {
log.Println(err)
}
telemetryService := services.NewSegmentIoService(segmentIOClient)
telemetryHandler := handlers.NewTelemetry(httpClient, telemetryService)
telemetryHandler := handlers.NewTelemetryHandler(telemetryService)

// todo: confirm K1version works for release go-releaser
if globalFlags.UseTelemetry {
err = telemetryHandler.SendCountMetric(pkg.MetricMgmtClusterInstallStarted, hostedZoneName, configs.K1Version)
err = telemetryHandler.SendCountMetric(telemetryDomain)
if err != nil {
log.Println(err)
}
Expand Down Expand Up @@ -106,7 +110,7 @@ cluster provisioning process spinning up the services, and validates the livenes
break
}
}

informUser("Removing self-signed Argo certificate", globalFlags.SilentMode)
clientset, err := k8s.GetClientSet(globalFlags.DryRun)
if err != nil {
Expand Down Expand Up @@ -141,10 +145,14 @@ cluster provisioning process spinning up the services, and validates the livenes
}

log.Println("sending mgmt cluster install completed metric")
// todo: confirm K1version works for release go-releaser

installCompletedTelemetry, err := domain.NewTelemetry(
pkg.MetricMgmtClusterInstallCompleted,
hostedZoneName,
configs.K1Version,
)
if globalFlags.UseTelemetry {
err = telemetryHandler.SendCountMetric(pkg.MetricMgmtClusterInstallCompleted, hostedZoneName, configs.K1Version)
err = telemetryHandler.SendCountMetric(installCompletedTelemetry)
if err != nil {
log.Println(err)
}
Expand Down
26 changes: 19 additions & 7 deletions cmd/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package cmd
import (
"errors"
"fmt"
"github.com/kubefirst/kubefirst/internal/domain"
"log"
"net/http"
"os"
"strings"
"time"
Expand Down Expand Up @@ -101,7 +101,6 @@ validated and configured.`,
}

log.Println("sending init started metric")
httpClient := http.DefaultClient

// Instantiates a SegmentIO client to use send messages to the segment API.
segmentIOClient := analytics.New(pkg.SegmentIOWriteKey)
Expand All @@ -115,11 +114,20 @@ validated and configured.`,
}
}(segmentIOClient)

// validate telemetryDomain data
telemetryDomain, err := domain.NewTelemetry(
pkg.MetricInitStarted,
awsFlags.HostedZoneName,
configs.K1Version,
)
if err != nil {
log.Println(err)
}
telemetryService := services.NewSegmentIoService(segmentIOClient)
telemetryHandler := handlers.NewTelemetry(httpClient, telemetryService)
// todo: confirm K1version works for release go-releaser
telemetryHandler := handlers.NewTelemetryHandler(telemetryService)

if globalFlags.UseTelemetry {
err = telemetryHandler.SendCountMetric(pkg.MetricInitStarted, awsFlags.HostedZoneName, configs.K1Version)
err = telemetryHandler.SendCountMetric(telemetryDomain)
if err != nil {
log.Println(err)
}
Expand Down Expand Up @@ -208,10 +216,14 @@ validated and configured.`,
progressPrinter.IncrementTracker("step-gitops", 1)

log.Println("sending init completed metric")
// todo: confirm K1version works for release go-releaser

telemetryInitCompleted, err := domain.NewTelemetry(
pkg.MetricInitCompleted,
awsFlags.HostedZoneName,
configs.K1Version,
)
if globalFlags.UseTelemetry {
err = telemetryHandler.SendCountMetric(pkg.MetricInitCompleted, awsFlags.HostedZoneName, configs.K1Version)
err = telemetryHandler.SendCountMetric(telemetryInitCompleted)
if err != nil {
log.Println(err)
}
Expand Down
33 changes: 33 additions & 0 deletions internal/domain/telemetry.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package domain

import (
"errors"
"github.com/kubefirst/kubefirst/pkg"
)

// Telemetry data that will be consumed by handlers and services
type Telemetry struct {
MetricName string
Domain string
CLIVersion string
}

// NewTelemetry is the Telemetry domain. When instantiating new Telemetries, we're able to validate domain specific
// values. In this way, domain, handlers and services can work in isolation, and Domain host business logic.
func NewTelemetry(metricName string, domain string, CLIVersion string) (*Telemetry, error) {

if len(metricName) == 0 {
return nil, errors.New("unable to create metric, missing metric name")
}

domain, err := pkg.RemoveSubDomain(domain)
if err != nil {
return nil, err
}

return &Telemetry{
MetricName: metricName,
Domain: domain,
CLIVersion: CLIVersion,
}, nil
}
76 changes: 76 additions & 0 deletions internal/domain/telemetry_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package domain

import (
"reflect"
"testing"
)

func TestNewTelemetry(t *testing.T) {

validTelemetry := Telemetry{MetricName: "test metric", Domain: "example.com", CLIVersion: "0.0.0"}

type args struct {
metricName string
domain string
CLIVersion string
}
tests := []struct {
name string
args args
want *Telemetry
wantErr bool
}{
{
name: "valid domain",
args: args{
metricName: "test metric",
domain: "https://example.com",
CLIVersion: "0.0.0",
},
want: &validTelemetry,
wantErr: false,
},
{
name: "invalid domain",
args: args{
metricName: "test metric",
domain: "https://example-com",
CLIVersion: "0.0.0",
},
want: nil,
wantErr: true,
},
{
name: "empty domain",
args: args{
metricName: "test metric",
domain: "",
CLIVersion: "0.0.0",
},
want: nil,
wantErr: true,
},
{
name: "missing telemetry name",
args: args{
metricName: "",
domain: "example.com",
CLIVersion: "0.0.0",
},
want: nil,
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := NewTelemetry(tt.args.metricName, tt.args.domain, tt.args.CLIVersion)
if (err != nil) != tt.wantErr {
t.Errorf("NewTelemetry() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("NewTelemetry() got = %v, want %v", got, tt.want)
}
})
}
}
25 changes: 11 additions & 14 deletions internal/handlers/telemetry.go
Original file line number Diff line number Diff line change
@@ -1,33 +1,30 @@
package handlers

import (
"errors"
"github.com/kubefirst/kubefirst/internal/domain"
"github.com/kubefirst/kubefirst/internal/services"
"github.com/kubefirst/kubefirst/pkg"
)

// TelemetryHandler hosts handler requirements.
type TelemetryHandler struct {
httpClient pkg.HTTPDoer
service services.SegmentIoService
service services.SegmentIoService
}

// NewTelemetry instantiate a new Telemetry struct.
func NewTelemetry(httpClient pkg.HTTPDoer, service services.SegmentIoService) TelemetryHandler {
// NewTelemetryHandler instantiate a new Telemetry handler.
func NewTelemetryHandler(service services.SegmentIoService) TelemetryHandler {
return TelemetryHandler{
httpClient: httpClient,
service: service,
service: service,
}
}

// SendCountMetric validate and handles the metric request to the metric service.
func (handler TelemetryHandler) SendCountMetric(metricName string, domain string, cliVersion string) error {
func (handler TelemetryHandler) SendCountMetric(telemetry *domain.Telemetry) error {

if len(metricName) == 0 {
return errors.New("unable to send metric, missing metric name")
}

err := handler.service.EnqueueCountMetric(metricName, domain, cliVersion)
err := handler.service.EnqueueCountMetric(
telemetry.MetricName,
telemetry.Domain,
telemetry.CLIVersion,
)
if err != nil {
return err
}
Expand Down
Loading