From 3d6df67e6c3bcd43e3917dac7ec7cac3aa005020 Mon Sep 17 00:00:00 2001 From: John Dietz Date: Sat, 21 Oct 2023 02:04:04 -0400 Subject: [PATCH] adding missing methods and machineid to type --- internal/telemetryShim/telemetry.go | 1 - pkg/segment/constants.go | 1 + pkg/segment/send.go | 52 +++++++++++++++++++++++++++++ 3 files changed, 53 insertions(+), 1 deletion(-) create mode 100644 pkg/segment/send.go diff --git a/internal/telemetryShim/telemetry.go b/internal/telemetryShim/telemetry.go index 24ca3a34..e1eae1a4 100644 --- a/internal/telemetryShim/telemetry.go +++ b/internal/telemetryShim/telemetry.go @@ -60,7 +60,6 @@ func SetupTelemetry(cl pkgtypes.Cluster) (*segment.SegmentClient, error) { KubefirstTeamInfo: os.Getenv("KUBEFIRST_TEAM_INFO"), MachineID: machineID, } - segmentClient.SetupClient() return segmentClient, nil } diff --git a/pkg/segment/constants.go b/pkg/segment/constants.go index 75e4c6da..a497b7fe 100644 --- a/pkg/segment/constants.go +++ b/pkg/segment/constants.go @@ -97,4 +97,5 @@ type SegmentClient struct { KubefirstClient string KubefirstTeam string KubefirstTeamInfo string + MachineID string } diff --git a/pkg/segment/send.go b/pkg/segment/send.go new file mode 100644 index 00000000..c7fa9c1f --- /dev/null +++ b/pkg/segment/send.go @@ -0,0 +1,52 @@ +package segment + +import ( + "fmt" + + "github.com/denisbrodbeck/machineid" + "github.com/kubefirst/runtime/pkg" + "github.com/segmentio/analytics-go" +) + +// SendCountMetric +func (c *SegmentClient) SendCountMetric( + metricName string, + errorMessage string, +) string { + strippedDomainName, err := pkg.RemoveSubdomainV2(c.DomainName) + if err != nil { + return "error stripping domain name from value" + } + machineID, _ := machineid.ID() + if metricName == MetricInitStarted { + err := c.Client.Enqueue(analytics.Identify{ + UserId: strippedDomainName, + Type: "identify", + }) + if err != nil { + return fmt.Sprintf("error sending identify to segment: %s", err.Error()) + } + } + err = c.Client.Enqueue(analytics.Track{ + UserId: strippedDomainName, + Event: metricName, + Properties: analytics.NewProperties(). + Set("cli_version", c.CliVersion). + Set("cloud_provider", c.CloudProvider). + Set("cluster_id", c.ClusterID). + Set("cluster_type", c.ClusterType). + Set("domain", strippedDomainName). + Set("git_provider", c.GitProvider). + Set("client", c.KubefirstClient). + Set("kubefirst_team", c.KubefirstTeam). + Set("kubefirst_team_info", c.KubefirstTeamInfo). + Set("machine_id", machineID). + Set("error", errorMessage). + Set("install_method", c.InstallMethod), + }) + if err != nil { + return fmt.Sprintf("error sending track to segment: %s", err.Error()) + } + + return "" +}