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

fix: add missing error handler #574

Merged
merged 5 commits into from
Oct 18, 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
52 changes: 28 additions & 24 deletions cmd/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
if err != nil {
log.Println(err)
}
}(segmentIOClient)

// 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()
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)
Expand Down Expand Up @@ -146,12 +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 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)
Expand Down
57 changes: 31 additions & 26 deletions cmd/init.go
Original file line number Diff line number Diff line change
@@ -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"
Expand Down Expand Up @@ -94,31 +94,33 @@ 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()
// 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)
}
}(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)
Expand Down Expand Up @@ -209,12 +211,15 @@ validated and configured.`,

log.Println("sending init completed metric")

telemetryInitCompleted, err := domain.NewTelemetry(
pkg.MetricInitCompleted,
awsFlags.HostedZoneName,
configs.K1Version,
)
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)
Expand Down
8 changes: 4 additions & 4 deletions internal/domain/telemetry.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
10 changes: 5 additions & 5 deletions internal/domain/telemetry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func TestNewTelemetry(t *testing.T) {
tests := []struct {
name string
args args
want *Telemetry
want Telemetry
wantErr bool
}{
{
Expand All @@ -27,7 +27,7 @@ func TestNewTelemetry(t *testing.T) {
domain: "https://example.com",
CLIVersion: "0.0.0",
},
want: &validTelemetry,
want: validTelemetry,
wantErr: false,
},
{
Expand All @@ -37,7 +37,7 @@ func TestNewTelemetry(t *testing.T) {
domain: "https://example-com",
CLIVersion: "0.0.0",
},
want: nil,
want: Telemetry{},
wantErr: true,
},
{
Expand All @@ -47,7 +47,7 @@ func TestNewTelemetry(t *testing.T) {
domain: "",
CLIVersion: "0.0.0",
},
want: nil,
want: Telemetry{},
wantErr: true,
},
{
Expand All @@ -57,7 +57,7 @@ func TestNewTelemetry(t *testing.T) {
domain: "example.com",
CLIVersion: "0.0.0",
},
want: nil,
want: Telemetry{},
wantErr: true,
},
}
Expand Down
2 changes: 1 addition & 1 deletion internal/handlers/telemetry.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ 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 {

err := handler.service.EnqueueCountMetric(
telemetry.MetricName,
Expand Down
4 changes: 2 additions & 2 deletions internal/handlers/telemetry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
},
}
Expand Down