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

Add library to store cmd-exec-id in context #2439

Open
wants to merge 7 commits into
base: main
Choose a base branch
from

Conversation

shreyas-goenka
Copy link
Contributor

@shreyas-goenka shreyas-goenka commented Mar 6, 2025

Why

The command execution ID is a UUID that should be initialized once, and then have a consistent read value for the duration of the command.

As we start emitting telemetry events, we also want to emit the command execution ID in the telemetry payload. This library helps consolidate all reads to the cmd-exec-id value, and ensures there's only one write for the UUID.

relevant comment: #2432 (comment)

Tests

Unit tests.

@shreyas-goenka shreyas-goenka marked this pull request as ready for review March 6, 2025 11:47
@shreyas-goenka shreyas-goenka marked this pull request as draft March 6, 2025 11:47
@@ -106,7 +107,7 @@ func getCobraCmdForTest(f fixtures.HTTPFixture) (*cobra.Command, *bytes.Buffer)
func TestTokenCmdWithProfilePrintsHelpfulLoginMessageOnRefreshFailure(t *testing.T) {
cmd, output := getCobraCmdForTest(refreshFailureTokenResponse)
cmd.SetArgs([]string{"auth", "token", "--profile", "expired"})
err := cmd.Execute()
err := root.Execute(cmd.Context(), cmd)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These unrelated tests were modified to use the root.Execute execute function, which ensures that cmd-exec-id is set in the context since, otherwise, these tests fail.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are there other side effects of doing so?

@shreyas-goenka shreyas-goenka marked this pull request as ready for review March 6, 2025 12:39
@shreyas-goenka shreyas-goenka enabled auto-merge March 7, 2025 11:39
@@ -106,7 +107,7 @@ func getCobraCmdForTest(f fixtures.HTTPFixture) (*cobra.Command, *bytes.Buffer)
func TestTokenCmdWithProfilePrintsHelpfulLoginMessageOnRefreshFailure(t *testing.T) {
cmd, output := getCobraCmdForTest(refreshFailureTokenResponse)
cmd.SetArgs([]string{"auth", "token", "--profile", "expired"})
err := cmd.Execute()
err := root.Execute(cmd.Context(), cmd)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are there other side effects of doing so?

@@ -124,6 +125,9 @@ Stack Trace:
%s`, version, r, string(trace))
}()

// Set a cmd-exec-id value in the context
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That it is used as cmd-exec-id in the UA is not important here.

I suggest also not abbreviating here.

// execIDKey is the context key for the execution ID.
// The value of 1 is arbitrary and can be any number.
// Other keys in the same package must have different values.
execIDKey = key(1)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mix of initialism and not, please use one.

execIDKey = key(1)
)

func SetExecId(ctx context.Context) context.Context {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Set implies (to me) that the user specifies a value. That's not the case here. Maybe GenerateExecutionID?

require.Len(t, matches, 1)
_, err := uuid.Parse(matches[0][1])
assert.NoError(t, err)
assert.Contains(t, ua, "cmd-exec-id/some-exec-id")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the mock function is only included to make this test not use a regexp, it can be omitted IMO.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants