From 6a90ef0b31f0d32db341cfa15b54284339c27573 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Vanzuita?= Date: Tue, 18 Oct 2022 07:56:21 -0300 Subject: [PATCH 1/5] fix: add missing error handler MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: João Vanzuita --- cmd/init.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/cmd/init.go b/cmd/init.go index d2defece6..0ae2abd31 100644 --- a/cmd/init.go +++ b/cmd/init.go @@ -214,6 +214,9 @@ validated and configured.`, awsFlags.HostedZoneName, configs.K1Version, ) + if err != nil { + log.Println(err) + } if globalFlags.UseTelemetry { err = telemetryHandler.SendCountMetric(telemetryInitCompleted) if err != nil { From bc87f81dabc0997efba5ad27013f9541c660ad47 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Vanzuita?= Date: Tue, 18 Oct 2022 07:57:28 -0300 Subject: [PATCH 2/5] fix: add missing error handler MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: João Vanzuita --- cmd/create.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/cmd/create.go b/cmd/create.go index 2fb2919ad..e848c6084 100644 --- a/cmd/create.go +++ b/cmd/create.go @@ -151,6 +151,9 @@ cluster provisioning process spinning up the services, and validates the livenes hostedZoneName, configs.K1Version, ) + if err != nil { + log.Println(err) + } if globalFlags.UseTelemetry { err = telemetryHandler.SendCountMetric(installCompletedTelemetry) if err != nil { From 411eb4382b9be76aa1b7b0033beb18cc733e7ba5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Vanzuita?= Date: Tue, 18 Oct 2022 08:16:16 -0300 Subject: [PATCH 3/5] fix: unit tests for telemetry MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: João Vanzuita --- cmd/create.go | 55 ++++++++++++------------- cmd/init.go | 63 ++++++++++++++++------------- internal/domain/telemetry.go | 8 ++-- internal/domain/telemetry_test.go | 10 ++--- internal/handlers/telemetry.go | 8 +++- internal/handlers/telemetry_test.go | 4 +- 6 files changed, 81 insertions(+), 67 deletions(-) diff --git a/cmd/create.go b/cmd/create.go index e848c6084..e9f8f6452 100644 --- a/cmd/create.go +++ b/cmd/create.go @@ -49,30 +49,31 @@ cluster provisioning process spinning up the services, and validates the livenes hostedZoneName := viper.GetString("aws.hostedzonename") - // Instantiates a SegmentIO client to send messages to the segment API. - segmentIOClient := analytics.New(pkg.SegmentIOWriteKey) + var telemetryHandler handlers.TelemetryHandler + if globalFlags.UseTelemetry { + // Instantiates a SegmentIO client to 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() + // 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) + + telemetryDomain, err := domain.NewTelemetry( + pkg.MetricMgmtClusterInstallStarted, + hostedZoneName, + configs.K1Version, + ) if err != nil { log.Println(err) } - }(segmentIOClient) - - telemetryDomain, err := domain.NewTelemetry( - pkg.MetricMgmtClusterInstallStarted, - hostedZoneName, - configs.K1Version, - ) - if err != nil { - log.Println(err) - } - telemetryService := services.NewSegmentIoService(segmentIOClient) - telemetryHandler := handlers.NewTelemetryHandler(telemetryService) + telemetryService := services.NewSegmentIoService(segmentIOClient) + telemetryHandler := handlers.NewTelemetryHandler(telemetryService) - if globalFlags.UseTelemetry { err = telemetryHandler.SendCountMetric(telemetryDomain) if err != nil { log.Println(err) @@ -146,15 +147,15 @@ cluster provisioning process spinning up the services, and validates the livenes log.Println("sending mgmt cluster install completed metric") - installCompletedTelemetry, err := domain.NewTelemetry( - pkg.MetricMgmtClusterInstallCompleted, - hostedZoneName, - configs.K1Version, - ) - if err != nil { - log.Println(err) - } if globalFlags.UseTelemetry { + installCompletedTelemetry, err := domain.NewTelemetry( + pkg.MetricMgmtClusterInstallCompleted, + hostedZoneName, + configs.K1Version, + ) + if err != nil { + log.Println(err) + } err = telemetryHandler.SendCountMetric(installCompletedTelemetry) if err != nil { log.Println(err) diff --git a/cmd/init.go b/cmd/init.go index 0ae2abd31..9da338323 100644 --- a/cmd/init.go +++ b/cmd/init.go @@ -1,6 +1,7 @@ package cmd import ( + "fmt" "github.com/kubefirst/kubefirst/internal/domain" "log" "strings" @@ -94,31 +95,37 @@ validated and configured.`, log.Println("sending init started metric") - // Instantiates a SegmentIO client to use send messages to the segment API. - segmentIOClient := analytics.New(pkg.SegmentIOWriteKey) + var telemetryHandler handlers.TelemetryHandler + if globalFlags.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, + awsFlags.HostedZoneName, + configs.K1Version, + ) + fmt.Println("---debug---") + fmt.Println(telemetryDomain) + fmt.Println("---debug---") - // 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, - awsFlags.HostedZoneName, - configs.K1Version, - ) - if err != nil { - log.Println(err) - } - telemetryService := services.NewSegmentIoService(segmentIOClient) - telemetryHandler := handlers.NewTelemetryHandler(telemetryService) + telemetryService := services.NewSegmentIoService(segmentIOClient) + telemetryHandler = handlers.NewTelemetryHandler(telemetryService) - if globalFlags.UseTelemetry { err = telemetryHandler.SendCountMetric(telemetryDomain) if err != nil { log.Println(err) @@ -209,15 +216,15 @@ validated and configured.`, log.Println("sending init completed metric") - telemetryInitCompleted, err := domain.NewTelemetry( - pkg.MetricInitCompleted, - awsFlags.HostedZoneName, - configs.K1Version, - ) - if err != nil { - log.Println(err) - } if globalFlags.UseTelemetry { + telemetryInitCompleted, err := domain.NewTelemetry( + pkg.MetricInitCompleted, + awsFlags.HostedZoneName, + configs.K1Version, + ) + if err != nil { + log.Println(err) + } err = telemetryHandler.SendCountMetric(telemetryInitCompleted) if err != nil { log.Println(err) diff --git a/internal/domain/telemetry.go b/internal/domain/telemetry.go index 082aa8f29..a33f81635 100644 --- a/internal/domain/telemetry.go +++ b/internal/domain/telemetry.go @@ -14,18 +14,18 @@ type Telemetry struct { // 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) { +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") + return Telemetry{}, errors.New("unable to create metric, missing metric name") } domain, err := pkg.RemoveSubDomain(domain) if err != nil { - return nil, err + return Telemetry{}, err } - return &Telemetry{ + return Telemetry{ MetricName: metricName, Domain: domain, CLIVersion: CLIVersion, diff --git a/internal/domain/telemetry_test.go b/internal/domain/telemetry_test.go index 5a12873b2..cfd644d2b 100644 --- a/internal/domain/telemetry_test.go +++ b/internal/domain/telemetry_test.go @@ -17,7 +17,7 @@ func TestNewTelemetry(t *testing.T) { tests := []struct { name string args args - want *Telemetry + want Telemetry wantErr bool }{ { @@ -27,7 +27,7 @@ func TestNewTelemetry(t *testing.T) { domain: "https://example.com", CLIVersion: "0.0.0", }, - want: &validTelemetry, + want: validTelemetry, wantErr: false, }, { @@ -37,7 +37,7 @@ func TestNewTelemetry(t *testing.T) { domain: "https://example-com", CLIVersion: "0.0.0", }, - want: nil, + want: Telemetry{}, wantErr: true, }, { @@ -47,7 +47,7 @@ func TestNewTelemetry(t *testing.T) { domain: "", CLIVersion: "0.0.0", }, - want: nil, + want: Telemetry{}, wantErr: true, }, { @@ -57,7 +57,7 @@ func TestNewTelemetry(t *testing.T) { domain: "example.com", CLIVersion: "0.0.0", }, - want: nil, + want: Telemetry{}, wantErr: true, }, } diff --git a/internal/handlers/telemetry.go b/internal/handlers/telemetry.go index d4fff34bd..38562c2ee 100644 --- a/internal/handlers/telemetry.go +++ b/internal/handlers/telemetry.go @@ -1,6 +1,7 @@ package handlers import ( + "fmt" "github.com/kubefirst/kubefirst/internal/domain" "github.com/kubefirst/kubefirst/internal/services" ) @@ -18,7 +19,12 @@ func NewTelemetryHandler(service services.SegmentIoService) TelemetryHandler { } // SendCountMetric validate and handles the metric request to the metric service. -func (handler TelemetryHandler) SendCountMetric(telemetry *domain.Telemetry) error { +func (handler TelemetryHandler) SendCountMetric(telemetry domain.Telemetry) error { + fmt.Println("---debug---") + fmt.Println(telemetry.MetricName) + fmt.Println(telemetry.Domain) + fmt.Println(telemetry.CLIVersion) + fmt.Println("---debug---") err := handler.service.EnqueueCountMetric( telemetry.MetricName, diff --git a/internal/handlers/telemetry_test.go b/internal/handlers/telemetry_test.go index 004e0f18d..233763cfe 100644 --- a/internal/handlers/telemetry_test.go +++ b/internal/handlers/telemetry_test.go @@ -56,7 +56,7 @@ func TestTelemetryHandler_SendCountMetric(t *testing.T) { service services.SegmentIoService } type args struct { - telemetry *domain.Telemetry + telemetry domain.Telemetry } tests := []struct { name string @@ -67,7 +67,7 @@ func TestTelemetryHandler_SendCountMetric(t *testing.T) { { name: "valid telemetry", fields: fields{service: mockedService}, - args: args{telemetry: &validTelemetry}, + args: args{telemetry: validTelemetry}, wantErr: false, }, } From ce371387b62768d1a81d901be049e07b50ebe675 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Vanzuita?= Date: Tue, 18 Oct 2022 08:17:52 -0300 Subject: [PATCH 4/5] chore: clean up MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: João Vanzuita --- cmd/init.go | 5 ----- internal/handlers/telemetry.go | 6 ------ 2 files changed, 11 deletions(-) diff --git a/cmd/init.go b/cmd/init.go index 9da338323..c9d0a3db5 100644 --- a/cmd/init.go +++ b/cmd/init.go @@ -1,7 +1,6 @@ package cmd import ( - "fmt" "github.com/kubefirst/kubefirst/internal/domain" "log" "strings" @@ -116,10 +115,6 @@ validated and configured.`, awsFlags.HostedZoneName, configs.K1Version, ) - fmt.Println("---debug---") - fmt.Println(telemetryDomain) - fmt.Println("---debug---") - if err != nil { log.Println(err) } diff --git a/internal/handlers/telemetry.go b/internal/handlers/telemetry.go index 38562c2ee..1e52f586f 100644 --- a/internal/handlers/telemetry.go +++ b/internal/handlers/telemetry.go @@ -1,7 +1,6 @@ package handlers import ( - "fmt" "github.com/kubefirst/kubefirst/internal/domain" "github.com/kubefirst/kubefirst/internal/services" ) @@ -20,11 +19,6 @@ func NewTelemetryHandler(service services.SegmentIoService) TelemetryHandler { // SendCountMetric validate and handles the metric request to the metric service. func (handler TelemetryHandler) SendCountMetric(telemetry domain.Telemetry) error { - fmt.Println("---debug---") - fmt.Println(telemetry.MetricName) - fmt.Println(telemetry.Domain) - fmt.Println(telemetry.CLIVersion) - fmt.Println("---debug---") err := handler.service.EnqueueCountMetric( telemetry.MetricName, From 8eab370bb4ade71ea6fe8fbe80afe6ed7e4e3424 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Vanzuita?= Date: Tue, 18 Oct 2022 08:22:16 -0300 Subject: [PATCH 5/5] chore: fix conflict MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: João Vanzuita --- cmd/init.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmd/init.go b/cmd/init.go index c9d0a3db5..6b4666d31 100644 --- a/cmd/init.go +++ b/cmd/init.go @@ -1,13 +1,13 @@ package cmd import ( - "github.com/kubefirst/kubefirst/internal/domain" "log" "strings" "time" "github.com/kubefirst/kubefirst/configs" "github.com/kubefirst/kubefirst/internal/aws" + "github.com/kubefirst/kubefirst/internal/domain" "github.com/kubefirst/kubefirst/internal/downloadManager" "github.com/kubefirst/kubefirst/internal/flagset" "github.com/kubefirst/kubefirst/internal/handlers"