From d7557b2d02f1147819360ae7ab508c2e1c955912 Mon Sep 17 00:00:00 2001 From: "simon.faltum" Date: Thu, 3 Dec 2026 05:55:45 +0000 Subject: [PATCH 1/2] [DECO-29941] Cut the skills telemetry exit wait from 3 s to 500 ms Co-authored-by: Isaac --- cmd/root/telemetry_auth.go | 24 +++++++++++++++------- cmd/root/telemetry_auth_test.go | 34 +++++++++++++++++++++++++++++-- cmd/skills/telemetry.go | 7 +++++++ cmd/skills/telemetry_test.go | 36 +++++++++++++++++++++++++++++++++ 4 files changed, 92 insertions(+), 9 deletions(-) diff --git a/cmd/root/telemetry_auth.go b/cmd/root/telemetry_auth.go index 8c1ae7b1dd0..3f98bebed2f 100644 --- a/cmd/root/telemetry_auth.go +++ b/cmd/root/telemetry_auth.go @@ -15,7 +15,8 @@ import ( // TelemetryAuthBudget bounds how long after its start a command waits for the // background auth resolution its telemetry upload needs. A resolution still // unfinished when the run ends and the budget has passed drops the events. -// Matches the upload's own default timeout. A run waits only once its command +// Matches the upload's own default timeout. A command can set a shorter one +// with TelemetryAuth.Budget. A run waits only once its command // asks for it (TelemetryAuth.WaitAtExit). var TelemetryAuthBudget = 3 * time.Second @@ -30,23 +31,32 @@ type TelemetryAuth struct { pending *PendingAuth // WaitAtExit is whether the end of the run waits for an unfinished - // resolution (within TelemetryAuthBudget). False by default: the + // resolution (within the budget). False by default: the // telemetry uploads only if the resolution has already finished, so a run // that ends early stays instant. RunE sets it once the run has gone far // enough to be worth the wait. WaitAtExit bool + + // Budget is the wait budget of this command, or TelemetryAuthBudget when + // zero. It counts from PreRunE, where the resolution starts, not from the + // end of the run. Set it before the command runs. + Budget time.Duration } // WithTelemetryAuth makes cmd resolve auth best-effort so its telemetry can // upload at exit: PreRunE starts the resolution in the background, and RunE's // wrapper uses it if it has finished, or waits for it within -// TelemetryAuthBudget once RunE sets WaitAtExit on the returned value. Nothing +// its budget (TelemetryAuth.Budget, else TelemetryAuthBudget) once RunE sets WaitAtExit on the returned value. Nothing // is resolved when telemetry is disabled. The command never fails or prompts // because of it. func WithTelemetryAuth(cmd *cobra.Command) *TelemetryAuth { auth := &TelemetryAuth{} cmd.PreRunE = func(cmd *cobra.Command, args []string) error { - auth.pending = startTelemetryAuth(cmd, args) + budget := TelemetryAuthBudget + if auth.Budget > 0 { + budget = auth.Budget + } + auth.pending = startTelemetryAuth(cmd, args, budget) return nil } runE := cmd.RunE @@ -72,7 +82,7 @@ type PendingAuth struct { // output and prompts. It never refreshes an OAuth token (SkipTokenRefresh), // as the command may exit before a refresh finishes; an expired cached token // fails the resolution and drops the events. -func startTelemetryAuth(cmd *cobra.Command, args []string) *PendingAuth { +func startTelemetryAuth(cmd *cobra.Command, args []string, budget time.Duration) *PendingAuth { ctx := cmd.Context() if telemetry.Disabled(ctx) { return nil @@ -82,7 +92,7 @@ func startTelemetryAuth(cmd *cobra.Command, args []string) *PendingAuth { shadow.Flags().AddFlagSet(cmd.Flags()) shadow.SetContext(SkipTokenRefresh(SkipLoadBundle(SkipPrompt(sctx)))) - p := &PendingAuth{Done: make(chan struct{}), Deadline: time.Now().Add(TelemetryAuthBudget)} + p := &PendingAuth{Done: make(chan struct{}), Deadline: time.Now().Add(budget)} resolve := TelemetryAuthResolver go func() { defer close(p.Done) @@ -119,7 +129,7 @@ func (p *PendingAuth) apply(cmd *cobra.Command, wait bool) { return } if wait && !p.Wait() { - log.Debugf(cmd.Context(), "auth for telemetry not resolved within %s; not uploading telemetry", TelemetryAuthBudget) + log.Debugf(cmd.Context(), "auth for telemetry not resolved by %s; not uploading telemetry", p.Deadline.Format(time.RFC3339Nano)) return } if p.Cfg != nil { diff --git a/cmd/root/telemetry_auth_test.go b/cmd/root/telemetry_auth_test.go index 063194103f8..f4cc898a62e 100644 --- a/cmd/root/telemetry_auth_test.go +++ b/cmd/root/telemetry_auth_test.go @@ -51,7 +51,7 @@ func TestTelemetryAuthFinishedResolutionSurvivesAnExpiredBudget(t *testing.T) { }) for range 50 { cmd, _ := authTestCmd(t) - p := startTelemetryAuth(cmd, nil) + p := startTelemetryAuth(cmd, nil, TelemetryAuthBudget) <-p.Done telemetry.Log(cmd.Context(), protos.DatabricksCliLog{AitoolsInstallEvent: &protos.AitoolsInstallEvent{}}) p.apply(cmd, true) @@ -68,10 +68,40 @@ func TestTelemetryAuthNoWaitUsesFinishedResolution(t *testing.T) { }) for range 50 { cmd, _ := authTestCmd(t) - p := startTelemetryAuth(cmd, nil) + p := startTelemetryAuth(cmd, nil, TelemetryAuthBudget) <-p.Done telemetry.Log(cmd.Context(), protos.DatabricksCliLog{AitoolsInstallEvent: &protos.AitoolsInstallEvent{}}) p.apply(cmd, false) require.True(t, cmdctx.HasConfigUsed(cmd.Context())) } } + +// A command's own Budget bounds its wait instead of the global one, and a resolution that outlasts it is dropped. +func TestTelemetryAuthCommandBudgetBoundsTheWait(t *testing.T) { + release := make(chan struct{}) + defer close(release) + setTelemetryAuth(t, time.Minute, func(cmd *cobra.Command, args []string) error { + <-release + return nil + }) + cmd := &cobra.Command{ + Use: "test", + RunE: func(cmd *cobra.Command, args []string) error { + telemetry.Log(cmd.Context(), protos.DatabricksCliLog{AitoolsInstallEvent: &protos.AitoolsInstallEvent{}}) + return nil + }, + } + cmd.Flags().String("profile", "", "") + auth := WithTelemetryAuth(cmd) + auth.WaitAtExit = true + auth.Budget = 100 * time.Millisecond + cmd.SetContext(env.Set(telemetry.WithNewLogger(t.Context()), "DATABRICKS_CLI_DISABLE_TELEMETRY", "")) + require.NoError(t, cmd.PreRunE(cmd, nil)) + + start := time.Now() + require.NoError(t, cmd.RunE(cmd, nil)) + elapsed := time.Since(start) + require.GreaterOrEqual(t, elapsed, 90*time.Millisecond) + require.Less(t, elapsed, 5*time.Second) + require.False(t, cmdctx.HasConfigUsed(cmd.Context())) +} diff --git a/cmd/skills/telemetry.go b/cmd/skills/telemetry.go index 8e29d5ab787..6632d52bd4d 100644 --- a/cmd/skills/telemetry.go +++ b/cmd/skills/telemetry.go @@ -4,6 +4,7 @@ import ( "encoding/json" "errors" "io/fs" + "time" "github.com/databricks/cli/libs/aitools/agents" @@ -15,6 +16,11 @@ import ( "golang.org/x/mod/semver" ) +// telemetryAuthBudget is how long, counted from the start of the run, an exit waits for the upload's auth. Typical auth +// resolves in a few milliseconds, so a longer wait only delays the agent's call. An unfinished resolution drops the +// event. +const telemetryAuthBudget = 500 * time.Millisecond + // recordRuns logs one SkillsCommandEvent per run of each command in the group, and resolves the upload's auth in the // background, as aitools does. It is best effort: it never fails, prompts or changes output or exit codes. func recordRuns(group *cobra.Command) { @@ -37,6 +43,7 @@ func recordRuns(group *cobra.Command) { } // Wrapped last, so its wrapper runs outside ours and sees WaitAtExit after the run. auth = root.WithTelemetryAuth(c) + auth.Budget = telemetryAuthBudget } } diff --git a/cmd/skills/telemetry_test.go b/cmd/skills/telemetry_test.go index 669b1ff263c..eaad7bc14c5 100644 --- a/cmd/skills/telemetry_test.go +++ b/cmd/skills/telemetry_test.go @@ -6,7 +6,9 @@ import ( "fmt" "io/fs" "testing" + "time" + "github.com/databricks/cli/cmd/root" "github.com/databricks/cli/libs/env" libskills "github.com/databricks/cli/libs/skills" "github.com/databricks/cli/libs/telemetry" @@ -111,3 +113,37 @@ func TestRecordRunsDisabledLogsNothing(t *testing.T) { assert.NoError(t, group.ExecuteContext(ctx)) assert.Empty(t, telemetry.BufferedEvents(ctx)) } + +// A run whose source resolved waits for a stuck auth resolution only up to telemetryAuthBudget, not the shared budget. +func TestSkillsRunWaitsAtMostTelemetryAuthBudget(t *testing.T) { + release := make(chan struct{}) + defer close(release) + origResolver, origBudget := root.TelemetryAuthResolver, root.TelemetryAuthBudget + t.Cleanup(func() { root.TelemetryAuthResolver, root.TelemetryAuthBudget = origResolver, origBudget }) + root.TelemetryAuthBudget = time.Minute + root.TelemetryAuthResolver = func(*cobra.Command, []string) error { + <-release + return nil + } + + group := &cobra.Command{Use: "skills"} + group.AddCommand(&cobra.Command{ + Use: "list", + RunE: func(cmd *cobra.Command, args []string) error { + _, err := libskills.Resolve(cmd.Context()) + return err + }, + }) + recordRuns(group) + cmd := group.Commands()[0] + cmd.Flags().String("profile", "", "") + ctx := env.Set(telemetry.WithNewLogger(t.Context()), "DATABRICKS_CLI_DISABLE_TELEMETRY", "") + cmd.SetContext(env.Set(ctx, "DATABRICKS_SKILLS_DIR", "../../libs/skills/testdata/release")) + + require.NoError(t, cmd.PreRunE(cmd, nil)) + start := time.Now() + require.NoError(t, cmd.RunE(cmd, nil)) + elapsed := time.Since(start) + assert.GreaterOrEqual(t, elapsed, telemetryAuthBudget-100*time.Millisecond) + assert.Less(t, elapsed, 10*time.Second) +} From 1d67a16cd37ae6762e1524019d9b98b972526094 Mon Sep 17 00:00:00 2001 From: "simon.faltum" Date: Thu, 3 Dec 2026 11:12:47 +0000 Subject: [PATCH 2/2] Document telemetry auth losses and measure tests from auth start --- cmd/root/telemetry_auth_test.go | 4 ++-- cmd/skills/telemetry.go | 7 ++++--- cmd/skills/telemetry_test.go | 4 ++-- 3 files changed, 8 insertions(+), 7 deletions(-) diff --git a/cmd/root/telemetry_auth_test.go b/cmd/root/telemetry_auth_test.go index f4cc898a62e..76e32e77de6 100644 --- a/cmd/root/telemetry_auth_test.go +++ b/cmd/root/telemetry_auth_test.go @@ -96,12 +96,12 @@ func TestTelemetryAuthCommandBudgetBoundsTheWait(t *testing.T) { auth.WaitAtExit = true auth.Budget = 100 * time.Millisecond cmd.SetContext(env.Set(telemetry.WithNewLogger(t.Context()), "DATABRICKS_CLI_DISABLE_TELEMETRY", "")) + start := time.Now() require.NoError(t, cmd.PreRunE(cmd, nil)) - start := time.Now() require.NoError(t, cmd.RunE(cmd, nil)) elapsed := time.Since(start) - require.GreaterOrEqual(t, elapsed, 90*time.Millisecond) + require.GreaterOrEqual(t, elapsed, auth.Budget) require.Less(t, elapsed, 5*time.Second) require.False(t, cmdctx.HasConfigUsed(cmd.Context())) } diff --git a/cmd/skills/telemetry.go b/cmd/skills/telemetry.go index 6632d52bd4d..938bb84a75c 100644 --- a/cmd/skills/telemetry.go +++ b/cmd/skills/telemetry.go @@ -16,9 +16,10 @@ import ( "golang.org/x/mod/semver" ) -// telemetryAuthBudget is how long, counted from the start of the run, an exit waits for the upload's auth. Typical auth -// resolves in a few milliseconds, so a longer wait only delays the agent's call. An unfinished resolution drops the -// event. +// telemetryAuthBudget favors prompt skills calls over telemetry from slow auth. +// Cold host-metadata lookups or credential helpers that exceed it lose their +// event; a canceled lookup also leaves the metadata cache cold for the next run. +// It bounds only auth, not the subsequent telemetry upload. const telemetryAuthBudget = 500 * time.Millisecond // recordRuns logs one SkillsCommandEvent per run of each command in the group, and resolves the upload's auth in the diff --git a/cmd/skills/telemetry_test.go b/cmd/skills/telemetry_test.go index eaad7bc14c5..7e40400905a 100644 --- a/cmd/skills/telemetry_test.go +++ b/cmd/skills/telemetry_test.go @@ -140,10 +140,10 @@ func TestSkillsRunWaitsAtMostTelemetryAuthBudget(t *testing.T) { ctx := env.Set(telemetry.WithNewLogger(t.Context()), "DATABRICKS_CLI_DISABLE_TELEMETRY", "") cmd.SetContext(env.Set(ctx, "DATABRICKS_SKILLS_DIR", "../../libs/skills/testdata/release")) - require.NoError(t, cmd.PreRunE(cmd, nil)) start := time.Now() + require.NoError(t, cmd.PreRunE(cmd, nil)) require.NoError(t, cmd.RunE(cmd, nil)) elapsed := time.Since(start) - assert.GreaterOrEqual(t, elapsed, telemetryAuthBudget-100*time.Millisecond) + assert.GreaterOrEqual(t, elapsed, telemetryAuthBudget) assert.Less(t, elapsed, 10*time.Second) }