Repository: code-ready/crc
Pull request #1832 (2021-01-11 13:53:32)

Add OS version in Segment

openshift-ci-robot: [APPROVALNOTIFIER] This PR is **APPROVED**

This pull-request has been approved by: *<a href="https://github.com/code-ready/crc/pull/1832#" title="Author self-approved">guillaumerose</a>*

The full list of commands accepted by this bot can be found [here](https://go.k8s.io/bot-commands?repo=code-ready%2Fcrc).

The pull request process is described [here](https://git.k8s.io/community/contributors/guide/owners.md#the-code-review-process)

<details >
Needs approval from an approver in each of these files:

- ~~[OWNERS](https://github.com/code-ready/crc/blob/master/OWNERS)~~ [guillaumerose]

Approvers can indicate their approval by writing `/approve` in a comment
Approvers can cancel approval by writing `/approve cancel` in a comment
</details>
<!-- META={"approvers":[]} -->
openshift-ci-robot: [APPROVALNOTIFIER] This PR is **APPROVED**

This pull-request has been approved by: *<a href="https://github.com/code-ready/crc/pull/1832#pullrequestreview-566867551" title="Approved">gbraad</a>*, *<a href="https://github.com/code-ready/crc/pull/1832#" title="Author self-approved">guillaumerose</a>*

The full list of commands accepted by this bot can be found [here](https://go.k8s.io/bot-commands?repo=code-ready%2Fcrc).

The pull request process is described [here](https://git.k8s.io/community/contributors/guide/owners.md#the-code-review-process)

<details >
Needs approval from an approver in each of these files:

- ~~[OWNERS](https://github.com/code-ready/crc/blob/master/OWNERS)~~ [gbraad,guillaumerose]

Approvers can indicate their approval by writing `/approve` in a comment
Approvers can cancel approval by writing `/approve cancel` in a comment
</details>
<!-- META={"approvers":[]} -->

Patch:
diff --git a/pkg/crc/segment/segment.go b/pkg/crc/segment/segment.go
index f81d6131c..de7d41a4e 100644
--- a/pkg/crc/segment/segment.go
+++ b/pkg/crc/segment/segment.go
@@ -4,7 +4,6 @@ import (
 	"io/ioutil"
 	"os"
 	"path/filepath"
-	"runtime"
 	"strings"
 	"time"
 
@@ -31,12 +30,6 @@ func NewClient(config *crcConfig.Config) (*Client, error) {
 
 func newCustomClient(config *crcConfig.Config, telemetryFilePath, segmentEndpoint string) (*Client, error) {
 	client, err := analytics.NewWithConfig("cvpHsNcmGCJqVzf6YxrSnVlwFSAZaYtp", analytics.Config{
-		DefaultContext: &analytics.Context{
-			App: analytics.AppInfo{
-				Name:    constants.DefaultName,
-				Version: version.GetCRCVersion(),
-			},
-		},
 		Endpoint: segmentEndpoint,
 		Logger:   &loggingAdapter{},
 	})
@@ -67,13 +60,13 @@ func (c *Client) Upload(action string, duration time.Duration, err error) error
 
 	if err := c.segmentClient.Enqueue(analytics.Identify{
 		AnonymousId: anonymousID,
-		Traits: analytics.NewTraits().
-			Set("os", runtime.GOOS),
+		Traits:      traits(),
 	}); err != nil {
 		return err
 	}
 
 	properties := analytics.NewProperties().
+		Set("version", version.GetCRCVersion()).
 		Set("success", err == nil).
 		Set("duration", duration.Milliseconds())
 	if err != nil {
diff --git a/pkg/crc/segment/traits_darwin.go b/pkg/crc/segment/traits_darwin.go
new file mode 100644
index 000000000..c7c088a5a
--- /dev/null
+++ b/pkg/crc/segment/traits_darwin.go
@@ -0,0 +1,20 @@
+package segment
+
+import (
+	"runtime"
+	"strings"
+
+	crcos "github.com/code-ready/crc/pkg/os"
+	"github.com/segmentio/analytics-go"
+)
+
+func traits() analytics.Traits {
+	base := analytics.NewTraits().
+		Set("os", runtime.GOOS)
+
+	version, _, err := crcos.RunWithDefaultLocale("sw_vers", "-productVersion")
+	if err != nil {
+		return base
+	}
+	return base.Set("os_version", strings.TrimSpace(version))
+}
diff --git a/pkg/crc/segment/traits_linux.go b/pkg/crc/segment/traits_linux.go
new file mode 100644
index 000000000..50aa41030
--- /dev/null
+++ b/pkg/crc/segment/traits_linux.go
@@ -0,0 +1,21 @@
+package segment
+
+import (
+	"runtime"
+
+	"github.com/code-ready/crc/pkg/os/linux"
+	"github.com/segmentio/analytics-go"
+)
+
+func traits() analytics.Traits {
+	base := analytics.NewTraits().
+		Set("os", runtime.GOOS)
+
+	details, err := linux.GetOsRelease()
+	if err != nil {
+		return base
+	}
+	return base.
+		Set("os_release_name", details.Name).
+		Set("os_release_version_id", details.VersionID)
+}
diff --git a/pkg/crc/segment/traits_windows.go b/pkg/crc/segment/traits_windows.go
new file mode 100644
index 000000000..589562f9e
--- /dev/null
+++ b/pkg/crc/segment/traits_windows.go
@@ -0,0 +1,26 @@
+package segment
+
+import (
+	"runtime"
+	"strings"
+
+	"github.com/code-ready/crc/pkg/os/windows/powershell"
+	"github.com/segmentio/analytics-go"
+)
+
+func traits() analytics.Traits {
+	base := analytics.NewTraits().
+		Set("os", runtime.GOOS)
+
+	releaseID, _, err := powershell.Execute(`(Get-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion" -Name ReleaseId).ReleaseId`)
+	if err != nil {
+		return base
+	}
+	editionID, _, err := powershell.Execute(`(Get-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion").EditionID`)
+	if err != nil {
+		return base
+	}
+	return base.
+		Set("os_edition_id", strings.TrimSpace(editionID)).
+		Set("os_release_id", strings.TrimSpace(releaseID))
+}


Test patch:
diff --git a/pkg/crc/segment/segment_test.go b/pkg/crc/segment/segment_test.go
index 0eabde50c..4b3f4df49 100644
--- a/pkg/crc/segment/segment_test.go
+++ b/pkg/crc/segment/segment_test.go
@@ -27,20 +27,11 @@ type segmentResponse struct {
 			OS string `json:"os"`
 		} `json:"traits"`
 		Properties struct {
-			Error string `json:"error"`
+			Error   string `json:"error"`
+			Version string `json:"version"`
 		} `json:"properties"`
 		Type string `json:"type"`
 	} `json:"batch"`
-	Context struct {
-		App struct {
-			Name    string `json:"name"`
-			Version string `json:"version"`
-		} `json:"app"`
-		Library struct {
-			Name    string `json:"name"`
-			Version string `json:"version"`
-		} `json:"library"`
-	} `json:"context"`
 	MessageID string `json:"messageId"`
 }
 
@@ -97,8 +88,7 @@ func TestClientUploadWithConsent(t *testing.T) {
 		require.Equal(t, s.Batch[0].Traits.OS, runtime.GOOS)
 		require.Equal(t, s.Batch[1].Type, "track")
 		require.Equal(t, s.Batch[1].Properties.Error, "an error occurred")
-		require.Equal(t, s.Context.App.Name, "crc")
-		require.Equal(t, s.Context.App.Version, version.GetCRCVersion())
+		require.Equal(t, s.Batch[1].Properties.Version, version.GetCRCVersion())
 	default:
 		require.Fail(t, "server should receive data")
 	}