-
Notifications
You must be signed in to change notification settings - Fork 195
Add telemetry for databricks aitools install
#5862
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
Merged
Merged
Changes from all commits
Commits
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
Some comments aren't visible on the classic Files Changed page.
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
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,99 @@ | ||
| package aitools | ||
|
|
||
| import ( | ||
| "context" | ||
| "slices" | ||
|
|
||
| "github.com/databricks/cli/cmd/root" | ||
| "github.com/databricks/cli/libs/aitools/agents" | ||
| "github.com/databricks/cli/libs/aitools/installer" | ||
| "github.com/databricks/cli/libs/telemetry" | ||
| "github.com/databricks/cli/libs/telemetry/protos" | ||
| "github.com/spf13/cobra" | ||
| ) | ||
|
|
||
| // tryConfigureAuth resolves auth config onto the context so telemetry can | ||
| // upload at command exit. It does not fail if auth is not configured. | ||
| func tryConfigureAuth(cmd *cobra.Command, args []string) error { | ||
| ctx := root.SkipPrompt(cmd.Context()) | ||
| ctx = root.SkipLoadBundle(ctx) | ||
| cmd.SetContext(ctx) | ||
| _, _ = root.MustAnyClient(cmd, args) | ||
| return nil | ||
| } | ||
|
|
||
| // installOpts is the subset of install options telemetry records, kept narrow | ||
| // so every field that leaves the machine is visible here. | ||
| type installOpts struct { | ||
| Scope string | ||
| Experimental bool | ||
| } | ||
|
|
||
| // logInstallEvent buffers an install event; cmd/root uploads it at exit. | ||
| func logInstallEvent(ctx context.Context, plan []agentPlanItem, opts installOpts) { | ||
| telemetry.Log(ctx, protos.DatabricksCliLog{ | ||
| AitoolsInstallEvent: &protos.AitoolsInstallEvent{ | ||
| Agents: agentsField(plan), | ||
| Scope: scopeType(opts.Scope), | ||
| Experimental: opts.Experimental, | ||
| }, | ||
| }) | ||
| } | ||
|
|
||
| // agentsField returns the deduped agent enums from the plan, sorted so the | ||
| // same set of agents always produces the same array on the analytics side. | ||
| func agentsField(plan []agentPlanItem) []protos.AitoolsAgentType { | ||
| if len(plan) == 0 { | ||
| return nil | ||
| } | ||
| out := make([]protos.AitoolsAgentType, 0, len(plan)) | ||
| seen := make(map[protos.AitoolsAgentType]struct{}, len(plan)) | ||
| for _, it := range plan { | ||
| if it.agent == nil { | ||
| continue | ||
| } | ||
| t := agentType(it.agent.Name) | ||
| if _, ok := seen[t]; ok { | ||
| continue | ||
| } | ||
| seen[t] = struct{}{} | ||
| out = append(out, t) | ||
| } | ||
| slices.Sort(out) | ||
| return out | ||
| } | ||
|
|
||
| // agentType maps a CLI agent registry name to its telemetry enum. Every agent | ||
| // in agents.Registry must have a case here; TestAgentTypeCoversRegistry fails | ||
| // if one is missing. The default maps genuinely unknown names to Unspecified | ||
| // so an older proto never drops an install. | ||
| func agentType(name string) protos.AitoolsAgentType { | ||
| switch name { | ||
| case agents.NameClaudeCode: | ||
| return protos.AitoolsAgentTypeClaudeCode | ||
| case agents.NameCursor: | ||
| return protos.AitoolsAgentTypeCursor | ||
| case agents.NameCodex: | ||
| return protos.AitoolsAgentTypeCodex | ||
| case agents.NameOpenCode: | ||
| return protos.AitoolsAgentTypeOpenCode | ||
| case agents.NameCopilot: | ||
| return protos.AitoolsAgentTypeCopilot | ||
| case agents.NameAntigravity: | ||
| return protos.AitoolsAgentTypeAntigravity | ||
| default: | ||
| return protos.AitoolsAgentTypeUnspecified | ||
| } | ||
| } | ||
|
|
||
| // scopeType maps a resolved install scope to its telemetry enum. | ||
| func scopeType(scope string) protos.AitoolsInstallScope { | ||
| switch scope { | ||
| case installer.ScopeGlobal: | ||
| return protos.AitoolsInstallScopeGlobal | ||
| case installer.ScopeProject: | ||
| return protos.AitoolsInstallScopeProject | ||
| default: | ||
| return protos.AitoolsInstallScopeUnspecified | ||
| } | ||
| } |
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,86 @@ | ||
| package aitools | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "github.com/databricks/cli/libs/aitools/agents" | ||
| "github.com/databricks/cli/libs/aitools/installer" | ||
| "github.com/databricks/cli/libs/telemetry/protos" | ||
| "github.com/stretchr/testify/assert" | ||
| ) | ||
|
|
||
| func TestAgentsField(t *testing.T) { | ||
| cases := []struct { | ||
| name string | ||
| plan []agentPlanItem | ||
| want []protos.AitoolsAgentType | ||
| }{ | ||
| { | ||
| name: "empty plan", | ||
| plan: nil, | ||
| want: nil, | ||
| }, | ||
| { | ||
| name: "single agent", | ||
| plan: []agentPlanItem{{agent: &agents.Agent{Name: agents.NameClaudeCode}}}, | ||
| want: []protos.AitoolsAgentType{protos.AitoolsAgentTypeClaudeCode}, | ||
| }, | ||
| { | ||
| name: "multiple agents are sorted", | ||
| plan: []agentPlanItem{ | ||
| {agent: &agents.Agent{Name: agents.NameCursor}}, | ||
| {agent: &agents.Agent{Name: agents.NameClaudeCode}}, | ||
| {agent: &agents.Agent{Name: agents.NameCodex}}, | ||
| }, | ||
| want: []protos.AitoolsAgentType{ | ||
| protos.AitoolsAgentTypeClaudeCode, | ||
| protos.AitoolsAgentTypeCodex, | ||
| protos.AitoolsAgentTypeCursor, | ||
| }, | ||
| }, | ||
| { | ||
| name: "duplicates are deduplicated", | ||
| plan: []agentPlanItem{ | ||
| {agent: &agents.Agent{Name: agents.NameClaudeCode}}, | ||
| {agent: &agents.Agent{Name: agents.NameClaudeCode}}, | ||
| }, | ||
| want: []protos.AitoolsAgentType{protos.AitoolsAgentTypeClaudeCode}, | ||
| }, | ||
| { | ||
| name: "nil agent entries are skipped", | ||
| plan: []agentPlanItem{ | ||
| {agent: nil}, | ||
| {agent: &agents.Agent{Name: agents.NameCursor}}, | ||
| }, | ||
| want: []protos.AitoolsAgentType{protos.AitoolsAgentTypeCursor}, | ||
| }, | ||
| { | ||
| name: "unknown agent maps to unspecified", | ||
| plan: []agentPlanItem{{agent: &agents.Agent{Name: "future-agent"}}}, | ||
| want: []protos.AitoolsAgentType{protos.AitoolsAgentTypeUnspecified}, | ||
| }, | ||
| } | ||
| for _, tc := range cases { | ||
| t.Run(tc.name, func(t *testing.T) { | ||
| got := agentsField(tc.plan) | ||
| assert.Equal(t, tc.want, got) | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| // TestAgentTypeCoversRegistry fails when an agent is added to agents.Registry | ||
| // without a matching telemetry enum in agentType. This is the guard against | ||
| // silently logging a new agent as UNSPECIFIED. | ||
| func TestAgentTypeCoversRegistry(t *testing.T) { | ||
| for _, a := range agents.Registry { | ||
| assert.NotEqualf(t, protos.AitoolsAgentTypeUnspecified, agentType(a.Name), | ||
| "agent %q has no telemetry enum: add a case to agentType and a value to "+ | ||
| "AitoolsAgentType in enum.proto (Universe) + aitools_install.go (CLI)", a.Name) | ||
| } | ||
| } | ||
|
|
||
| func TestScopeType(t *testing.T) { | ||
| assert.Equal(t, protos.AitoolsInstallScopeGlobal, scopeType(installer.ScopeGlobal)) | ||
| assert.Equal(t, protos.AitoolsInstallScopeProject, scopeType(installer.ScopeProject)) | ||
| assert.Equal(t, protos.AitoolsInstallScopeUnspecified, scopeType("")) | ||
| } |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We need the logger for the telemetry because otherwise, it will panic. A logger is always present in the production path through the root command.