Skip to content

Commit ded509b

Browse files
authored
[Telemetry] Add support to collect Toolkit installation mode (#5598)
1 parent 05d3f86 commit ded509b

7 files changed

Lines changed: 46 additions & 25 deletions

File tree

Makefile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ TERRAFORM_FOLDERS=$(shell find ./modules ./community/modules ./tools -type f -na
1616
PACKER_FOLDERS=$(shell find ./modules ./community/modules ./tools -type f -name "*.pkr.hcl" -not -path '*/\.*' -exec dirname "{}" \; | sort -u)
1717
BINARY_TARGETS := ghpc gcluster
1818
INSTALL_DIRS := . ~/bin /usr/local/bin
19+
INSTALLATION_MODE = SOURCE
1920

2021
ifneq (, $(shell which git))
2122
## GIT IS PRESENT
@@ -33,7 +34,7 @@ endif
3334

3435
gcluster: warn-go-version warn-terraform-version warn-packer-version $(shell find ./cmd ./pkg gcluster.go -type f)
3536
$(info **************** building gcluster ************************)
36-
@go build -ldflags="-X 'main.gitTagVersion=$(GIT_TAG_VERSION)' -X 'main.gitBranch=$(GIT_BRANCH)' -X 'main.gitCommitInfo=$(GIT_COMMIT_INFO)' -X 'main.gitCommitHash=$(GIT_COMMIT_HASH)' -X 'main.gitInitialHash=$(GIT_INITIAL_HASH)' -X 'main.gitIsOfficial=$(GIT_IS_OFFICIAL)'" gcluster.go
37+
@go build -ldflags="-X 'main.gitTagVersion=$(GIT_TAG_VERSION)' -X 'main.gitBranch=$(GIT_BRANCH)' -X 'main.gitCommitInfo=$(GIT_COMMIT_INFO)' -X 'main.gitCommitHash=$(GIT_COMMIT_HASH)' -X 'main.gitInitialHash=$(GIT_INITIAL_HASH)' -X 'main.gitIsOfficial=$(GIT_IS_OFFICIAL)' -X 'main.installationMode=$(INSTALLATION_MODE)'" gcluster.go
3738
@ln -sf gcluster ghpc
3839

3940
ghpc: gcluster

cmd/root.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,10 @@ var (
4545
GitIsOfficial string
4646
)
4747

48+
var (
49+
InstallationMode string // Toolkit installation mode like "SOURCE", "BINARY", etc.
50+
)
51+
4852
var (
4953
annotation = make(map[string]string)
5054
rootCmd = &cobra.Command{

gcluster.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ var gitCommitHash string
3535
var gitInitialHash string
3636
var gitIsOfficial string
3737

38+
// Whether the Toolkit is installed from source, binary, etc.
39+
var installationMode string
40+
3841
func main() {
3942
if err := dependencies.PatchPath(); err != nil {
4043
logging.Fatal("Failed to patch PATH with custom binaries directories: %v", err)
@@ -47,6 +50,7 @@ func main() {
4750
cmd.GitCommitHash = gitCommitHash
4851
cmd.GitInitialHash = gitInitialHash
4952
cmd.GitIsOfficial = gitIsOfficial
53+
cmd.InstallationMode = installationMode
5054
if err := cmd.Execute(); err != nil {
5155
os.Exit(1)
5256
}

pkg/telemetry/collector.go

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,14 @@ var (
4141
)
4242

4343
// NewCollector creates and initializes a new Telemetry Collector.
44-
func NewCollector(cmd *cobra.Command, args []string) *Collector {
44+
func NewCollector(cmd *cobra.Command, args []string, installationMode string) *Collector {
4545
return &Collector{
46-
eventCmd: cmd,
47-
eventArgs: args,
48-
eventStartTime: time.Now(),
49-
blueprint: getBlueprint(args),
50-
metadata: make(map[string]string),
46+
eventCmd: cmd,
47+
eventArgs: args,
48+
eventStartTime: time.Now(),
49+
blueprint: getBlueprint(args),
50+
installationMode: installationMode,
51+
metadata: make(map[string]string),
5152
}
5253
}
5354

@@ -70,6 +71,7 @@ func (c *Collector) CollectMetrics(errorCode int) {
7071
c.metadata[OS_VERSION] = getOSVersion()
7172
c.metadata[TERRAFORM_VERSION] = getTerraformVersion()
7273
c.metadata[BILLING_ACCOUNT_ID] = getBillingAccountId(c.blueprint)
74+
c.metadata[INSTALLATION_MODE] = c.installationMode
7375
c.metadata[IS_TEST_DATA] = getIsTestData()
7476
c.metadata[EXIT_CODE] = strconv.Itoa(errorCode)
7577
}

pkg/telemetry/collector_test.go

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ import (
3434
func TestNewCollector(t *testing.T) {
3535
cmd := &cobra.Command{Use: "test"}
3636
// Passing nil for args prevents getBlueprint from attempting to read a file
37-
c := NewCollector(cmd, nil)
37+
c := NewCollector(cmd, nil, SOURCE)
3838

3939
if c == nil {
4040
t.Fatal("Expected NewCollector to return a valid Collector, got nil")
@@ -61,20 +61,23 @@ func TestCollectMetrics_Extensible(t *testing.T) {
6161
OS_VERSION,
6262
TERRAFORM_VERSION,
6363
BILLING_ACCOUNT_ID,
64+
INSTALLATION_MODE,
6465
IS_TEST_DATA,
6566
EXIT_CODE,
6667
}
6768

6869
tests := []struct {
69-
name string
70-
errorCode int
71-
setupCmd func(cmd *cobra.Command) // Hook to configure the command
72-
setupCollector func(c *Collector) // Hook to mock internal collector state
73-
expectedValues map[string]string
70+
name string
71+
errorCode int
72+
installationMode string
73+
setupCmd func(cmd *cobra.Command) // Hook to configure the command
74+
setupCollector func(c *Collector) // Hook to mock internal collector state
75+
expectedValues map[string]string
7476
}{
7577
{
76-
name: "Success exit code",
77-
errorCode: 0,
78+
name: "Success exit code",
79+
errorCode: 0,
80+
installationMode: SOURCE,
7881
setupCmd: func(cmd *cobra.Command) {
7982
// Define dummy flags for the mock command
8083
cmd.Flags().Bool("force", false, "Force execution")
@@ -119,11 +122,13 @@ func TestCollectMetrics_Extensible(t *testing.T) {
119122
OS_VERSION: getOSVersion(), // Dynamically expect the current OS version
120123
TERRAFORM_VERSION: getTerraformVersion(), // Dynamically expect the current Terraform version
121124
BILLING_ACCOUNT_ID: "",
125+
INSTALLATION_MODE: SOURCE,
122126
},
123127
},
124128
{
125-
name: "Failure exit code with missing region, zone, and machine type",
126-
errorCode: 1,
129+
name: "Failure exit code with missing region, zone, and machine type",
130+
errorCode: 1,
131+
installationMode: BINARY,
127132
setupCmd: func(cmd *cobra.Command) {
128133
// No flags set
129134
},
@@ -145,6 +150,7 @@ func TestCollectMetrics_Extensible(t *testing.T) {
145150
TERRAFORM_VERSION: getTerraformVersion(), // Verify Terraform version is still collected on failure
146151
MACHINE_TYPE: "", // Verify empty machine type when no matching modules exist
147152
BILLING_ACCOUNT_ID: "",
153+
INSTALLATION_MODE: BINARY,
148154
},
149155
},
150156
}
@@ -159,7 +165,7 @@ func TestCollectMetrics_Extensible(t *testing.T) {
159165
}
160166

161167
// Initialize the collector
162-
c := NewCollector(cmd, []string{})
168+
c := NewCollector(cmd, []string{}, tt.installationMode)
163169

164170
// Execute the setup function to apply the blueprint state to the collector
165171
if tt.setupCollector != nil {
@@ -191,7 +197,7 @@ func TestBuildConcordEvent(t *testing.T) {
191197
childCmd := &cobra.Command{Use: "deploy"}
192198
rootCmd.AddCommand(childCmd)
193199

194-
c := NewCollector(childCmd, nil)
200+
c := NewCollector(childCmd, nil, SOURCE)
195201
c.CollectMetrics(0)
196202

197203
event := c.BuildConcordEvent()

pkg/telemetry/telemetry_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ func TestExecute(t *testing.T) {
102102

103103
// Setup dummy cobra command and collector
104104
cmd := &cobra.Command{Use: "test-execute"}
105-
c := NewCollector(cmd, nil)
105+
c := NewCollector(cmd, nil, SOURCE)
106106

107107
// Trigger Execute. Since Execute doesn't return anything, we verify
108108
// it functions without panicking or failing.

pkg/telemetry/types.go

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,18 @@ const (
3030
timeout2Sec = 2 * time.Second
3131
CLUSTER_TOOLKIT = "CLUSTER_TOOLKIT"
3232
CONCORD = "CONCORD"
33+
SOURCE = "SOURCE"
34+
BINARY = "BINARY"
3335
)
3436

3537
// Collector encapsulates the telemetry state (avoids global variables).
3638
type Collector struct {
37-
eventCmd *cobra.Command
38-
eventArgs []string
39-
eventStartTime time.Time
40-
blueprint config.Blueprint
41-
metadata map[string]string
39+
eventCmd *cobra.Command
40+
eventArgs []string
41+
eventStartTime time.Time
42+
blueprint config.Blueprint
43+
installationMode string
44+
metadata map[string]string
4245

4346
mu sync.Mutex // Protects state against concurrent access
4447
}
@@ -90,6 +93,7 @@ const (
9093
OS_VERSION = "CLUSTER_TOOLKIT_OS_VERSION"
9194
TERRAFORM_VERSION = "CLUSTER_TOOLKIT_TERRAFORM_VERSION"
9295
BILLING_ACCOUNT_ID = "CLUSTER_TOOLKIT_BILLING_ACCOUNT_ID"
96+
INSTALLATION_MODE = "CLUSTER_TOOLKIT_INSTALLATION_MODE"
9397
IS_TEST_DATA = "CLUSTER_TOOLKIT_IS_TEST_DATA"
9498
EXIT_CODE = "CLUSTER_TOOLKIT_EXIT_CODE"
9599
)

0 commit comments

Comments
 (0)