-
Notifications
You must be signed in to change notification settings - Fork 288
[Telemetry] Set up a base skeleton framework #5319
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
Draft
kadupoornima
wants to merge
10
commits into
GoogleCloudPlatform:develop
Choose a base branch
from
kadupoornima:telemetry-1
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from 2 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
d581e18
base telemetry framework
kadupoornima cbbd05d
Merge branch 'develop' into telemetry-1
kadupoornima dddf72f
.
kadupoornima 713b58d
..
kadupoornima 6054d8d
Merge branch 'develop' into telemetry-1
kadupoornima e0becb4
.
kadupoornima a364885
.
kadupoornima f0007a5
Merge branch 'develop' into telemetry-1
kadupoornima 831a403
Merge branch 'develop' into telemetry-1
kadupoornima 6e28478
.
kadupoornima File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| // Copyright 2026 "Google LLC" | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| package telemetry | ||
|
|
||
| import ( | ||
| "strconv" | ||
| "time" | ||
|
|
||
| "github.com/spf13/cobra" | ||
| ) | ||
|
|
||
| var ( | ||
| metadata = make(map[string]string) | ||
| eventStartTime time.Time | ||
| ) | ||
|
|
||
| func CollectPreMetrics(cmd *cobra.Command, args []string) { | ||
| eventStartTime = time.Now() | ||
|
|
||
| metadata[COMMAND_NAME] = getCommandName(cmd) | ||
| metadata[IS_TEST_DATA] = getIsTestData() | ||
|
|
||
| } | ||
|
|
||
| func CollectPostMetrics(errorCode int) { | ||
| metadata[RUNTIME_MS] = getRuntime() | ||
| metadata[EXIT_CODE] = strconv.Itoa(errorCode) | ||
| } | ||
|
|
||
| func getCommandName(cmd *cobra.Command) string { | ||
| return cmd.Name() | ||
| } | ||
|
|
||
| func getIsTestData() string { | ||
| return "true" | ||
| } | ||
kadupoornima marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| func getRuntime() string { | ||
| eventEndTime := time.Now() | ||
| return strconv.FormatInt(eventEndTime.Sub(eventStartTime).Milliseconds(), 10) | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,196 @@ | ||
| // Copyright 2026 "Google LLC" | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| package telemetry | ||
|
|
||
| import ( | ||
| "strconv" | ||
| "testing" | ||
| "time" | ||
|
|
||
| "github.com/spf13/cobra" | ||
| ) | ||
|
|
||
| func resetGlobalState() { | ||
| eventStartTime = time.Time{} | ||
| metadata = make(map[string]string) | ||
| } | ||
|
|
||
| func TestCollectPreMetrics(t *testing.T) { | ||
| tests := []struct { | ||
| name string | ||
| cmd *cobra.Command | ||
| args []string | ||
| wantCmdName string | ||
| wantIsTest string | ||
| }{ | ||
| { | ||
| name: "standard command", | ||
| cmd: &cobra.Command{ | ||
| Use: "apply", // cobra.Command.Name() derives from the first word of Use | ||
| }, | ||
| args: []string{}, | ||
| wantCmdName: "apply", | ||
| wantIsTest: "true", | ||
| }, | ||
| { | ||
| name: "command with flags in use string", | ||
| cmd: &cobra.Command{ | ||
| Use: "destroy [flags]", | ||
| }, | ||
| args: []string{"--force"}, | ||
| wantCmdName: "destroy", | ||
| wantIsTest: "true", | ||
| }, | ||
| { | ||
| name: "empty command", | ||
| cmd: &cobra.Command{}, | ||
| args: []string{}, | ||
| wantCmdName: "", | ||
| wantIsTest: "true", | ||
| }, | ||
| } | ||
|
|
||
| for _, tc := range tests { | ||
| t.Run(tc.name, func(t *testing.T) { | ||
| resetGlobalState() | ||
|
|
||
| // Capture bounding times to verify eventStartTime is correctly set to time.Now() | ||
| before := time.Now() | ||
| CollectPreMetrics(tc.cmd, tc.args) | ||
| after := time.Now() | ||
|
|
||
| if eventStartTime.Before(before) || eventStartTime.After(after) { | ||
| t.Errorf("eventStartTime = %v, want between %v and %v", eventStartTime, before, after) | ||
| } | ||
|
|
||
| if got := metadata[COMMAND_NAME]; got != tc.wantCmdName { | ||
| t.Errorf("metadata[%q] = %q, want %q", COMMAND_NAME, got, tc.wantCmdName) | ||
| } | ||
|
|
||
| if got := metadata[IS_TEST_DATA]; got != tc.wantIsTest { | ||
| t.Errorf("metadata[%q] = %q, want %q", IS_TEST_DATA, got, tc.wantIsTest) | ||
| } | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func TestCollectPostMetrics(t *testing.T) { | ||
| tests := []struct { | ||
| name string | ||
| errorCode int | ||
| wantExitCode string | ||
| }{ | ||
| { | ||
| name: "success execution", | ||
| errorCode: 0, | ||
| wantExitCode: "0", | ||
| }, | ||
| { | ||
| name: "standard error", | ||
| errorCode: 1, | ||
| wantExitCode: "1", | ||
| }, | ||
| { | ||
| name: "custom error code", | ||
| errorCode: 127, | ||
| wantExitCode: "127", | ||
| }, | ||
| } | ||
|
|
||
| for _, tc := range tests { | ||
| t.Run(tc.name, func(t *testing.T) { | ||
| resetGlobalState() | ||
|
|
||
| // Set start time to a known duration in the past to simulate runtime | ||
| eventStartTime = time.Now().Add(-50 * time.Millisecond) | ||
|
|
||
| CollectPostMetrics(tc.errorCode) | ||
|
|
||
| if got := metadata[EXIT_CODE]; got != tc.wantExitCode { | ||
| t.Errorf("metadata[%q] = %q, want %q", EXIT_CODE, got, tc.wantExitCode) | ||
| } | ||
|
|
||
| runtimeMsStr, ok := metadata[RUNTIME_MS] | ||
| if !ok { | ||
| t.Fatalf("metadata[%q] missing, want populated", RUNTIME_MS) | ||
| } | ||
|
|
||
| runtimeMs, err := strconv.ParseInt(runtimeMsStr, 10, 64) | ||
| if err != nil { | ||
| t.Fatalf("failed to parse RUNTIME_MS %q: %v", runtimeMsStr, err) | ||
| } | ||
|
|
||
| // Validating that the calculated runtime is at least the 50ms we stubbed | ||
| if runtimeMs < 50 { | ||
| t.Errorf("RUNTIME_MS = %d, want >= 50", runtimeMs) | ||
| } | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func TestGetCommandName(t *testing.T) { | ||
| tests := []struct { | ||
| name string | ||
| cmd *cobra.Command | ||
| want string | ||
| }{ | ||
| { | ||
| name: "simple use", | ||
| cmd: &cobra.Command{Use: "deploy"}, | ||
| want: "deploy", | ||
| }, | ||
| { | ||
| name: "complex use", | ||
| cmd: &cobra.Command{Use: "create cluster"}, | ||
| want: "create", | ||
| }, | ||
| } | ||
|
|
||
| for _, tc := range tests { | ||
| t.Run(tc.name, func(t *testing.T) { | ||
| if got := getCommandName(tc.cmd); got != tc.want { | ||
| t.Errorf("getCommandName() = %q, want %q", got, tc.want) | ||
| } | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func TestGetIsTestData(t *testing.T) { | ||
| t.Run("returns true", func(t *testing.T) { | ||
| if got := getIsTestData(); got != "true" { | ||
| t.Errorf("getIsTestData() = %q, want %q", got, "true") | ||
| } | ||
| }) | ||
| } | ||
|
|
||
| func TestGetRuntime(t *testing.T) { | ||
| t.Run("calculates correct duration", func(t *testing.T) { | ||
| resetGlobalState() | ||
|
|
||
| // Mock start time to exactly 100ms ago | ||
| eventStartTime = time.Now().Add(-100 * time.Millisecond) | ||
|
|
||
| gotStr := getRuntime() | ||
|
|
||
| got, err := strconv.ParseInt(gotStr, 10, 64) | ||
| if err != nil { | ||
| t.Fatalf("getRuntime() returned non-integer %q: %v", gotStr, err) | ||
| } | ||
|
|
||
| if got < 100 { | ||
| t.Errorf("getRuntime() = %d, want >= 100", got) | ||
| } | ||
| }) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| // Copyright 2026 "Google LLC" | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| // The following implementation is done for sending one LogEvent per LogRequest as per the telemetry logic. | ||
|
|
||
| package telemetry | ||
|
|
||
| import ( | ||
| "encoding/json" | ||
| "hpc-toolkit/pkg/config" | ||
| "hpc-toolkit/pkg/logging" | ||
| "time" | ||
|
|
||
| "github.com/spf13/cobra" | ||
| ) | ||
|
|
||
| var ( | ||
| eventMetadata []map[string]string = make([]map[string]string, 0) | ||
kadupoornima marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| ) | ||
|
|
||
| func Initialize(cmd *cobra.Command, args []string) { | ||
| CollectPreMetrics(cmd, args) | ||
| } | ||
|
|
||
| func Finalize(exitCode int) { | ||
| CollectPostMetrics(exitCode) | ||
| payload := ConstructPayload() | ||
| Flush(payload) | ||
| } | ||
|
|
||
| func ConstructPayload() LogRequest { | ||
| sourceExtensionJSON, err := json.Marshal(map[string]any{ | ||
| "event_type": "GCluster CLI", | ||
| "console_type": CLUSTER_TOOLKIT, | ||
| "release_version": config.GetToolkitVersion(), | ||
| "event_metadata": getEventMetadataKVPairs(), | ||
| }) | ||
| if err != nil { | ||
| logging.Error("Error collecting telemetry event metadata: %v", err) | ||
| return LogRequest{} | ||
| } | ||
|
|
||
| logEvent := LogEvent{ | ||
| EventTimeMs: time.Now().UnixMilli(), | ||
| SourceExtensionJson: string(sourceExtensionJSON), | ||
| } | ||
|
|
||
| logRequest := LogRequest{ | ||
| RequestTimeMs: time.Now().UnixMilli(), | ||
| ClientInfo: ClientInfo{ClientType: CLUSTER_TOOLKIT}, | ||
| LogSourceName: CONCORD, | ||
| LogEvent: []LogEvent{logEvent}, | ||
| } | ||
| return logRequest | ||
| } | ||
|
|
||
| func getEventMetadataKVPairs() []map[string]string { | ||
| for k, v := range metadata { | ||
| eventMetadata = append(eventMetadata, map[string]string{ | ||
kadupoornima marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| "key": k, | ||
| "value": v, | ||
| }) | ||
| } | ||
| return eventMetadata | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.