From 83016f4838dbde755f241a6d7ee56c9272a8325a Mon Sep 17 00:00:00 2001 From: Anton Nekipelov <226657+anton-107@users.noreply.github.com> Date: Fri, 6 Nov 2026 15:00:25 +0100 Subject: [PATCH] root: log host-metadata resolution through the command's logger The SDK's Config.EnsureResolved hands the host-metadata resolver context.Background(), so libs/hostmetadata logs through the process default logger and ignores any logger the command set on its context. MustWorkspaceClient and MustAccountClient now wrap the per-config resolver: it gets the SDK's context plus the command's logger and nothing else, so the request keeps the SDK's user agent and is not tied to the command's cancellation. The resolver is still built only when the SDK asks for it, through the registered factory, so the disk cache and test overrides still apply. When the factory returns nil, the wrapper falls back to the SDK's own fetch, as the SDK does. No output change for existing commands: their context logger is the one root installs as the process default (cmd/root/logger.go). A command that sets its own logger, such as a background auth resolution that should log only at debug level, now keeps host-metadata warnings out of its output. Co-authored-by: Isaac --- cmd/root/auth.go | 29 +++++++++++ cmd/root/auth_test.go | 112 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 141 insertions(+) diff --git a/cmd/root/auth.go b/cmd/root/auth.go index 5c03c8cfa8c..df9a454ba21 100644 --- a/cmd/root/auth.go +++ b/cmd/root/auth.go @@ -13,6 +13,7 @@ import ( "github.com/databricks/cli/libs/databrickscfg/profile" envlib "github.com/databricks/cli/libs/env" "github.com/databricks/cli/libs/liteswap" + "github.com/databricks/cli/libs/log" "github.com/databricks/cli/libs/logdiag" "github.com/databricks/databricks-sdk-go" "github.com/databricks/databricks-sdk-go/config" @@ -205,10 +206,37 @@ func MustAnyClient(cmd *cobra.Command, args []string) (bool, error) { return true, aerr } +// bindHostMetadataLogger makes cfg's host-metadata resolution log through +// ctx's logger. The SDK's EnsureResolved hands the resolver +// context.Background(), so without this its log lines bypass the command's +// logger, including the one a background resolution (aitools telemetry auth) +// sets to keep its warnings out of the command's output. Only the logger is +// carried over: the request keeps the SDK's context (user agent, no +// cancellation), as before. The resolver is built when the SDK asks for it, +// as the SDK itself does, so configs that never resolve a host build none. +func bindHostMetadataLogger(ctx context.Context, cfg *config.Config) { + // Only main.go registers the factory (blank import of libs/hostmetadata), + // so it is nil in test binaries and other programs that use this package. + factory := config.DefaultHostMetadataResolverFactory + if factory == nil { + return + } + logger := log.GetLogger(ctx) + cfg.HostMetadataResolver = func(sdkCtx context.Context, host string) (*config.HostMetadata, error) { + resolve := factory(cfg) + if resolve == nil { + // The factory contract allows nil to fall through to the SDK's fetch. + resolve = cfg.DefaultHostMetadataResolver() + } + return resolve(log.NewContext(sdkCtx, logger), host) + } +} + func MustAccountClient(cmd *cobra.Command, args []string) error { cfg := &config.Config{} ctx := cmd.Context() liteswap.Apply(ctx, cfg) + bindHostMetadataLogger(ctx, cfg) // The command-line profile flag takes precedence over DATABRICKS_CONFIG_PROFILE. pr, hasProfileFlag := profileFlagValue(cmd) @@ -338,6 +366,7 @@ func MustWorkspaceClient(cmd *cobra.Command, args []string) error { cfg := &config.Config{} liteswap.Apply(ctx, cfg) + bindHostMetadataLogger(ctx, cfg) // The command-line profile flag takes precedence over DATABRICKS_CONFIG_PROFILE. profile, hasProfileFlag := profileFlagValue(cmd) diff --git a/cmd/root/auth_test.go b/cmd/root/auth_test.go index b455dd0e382..afc022efce1 100644 --- a/cmd/root/auth_test.go +++ b/cmd/root/auth_test.go @@ -3,6 +3,7 @@ package root import ( "context" "fmt" + "log/slog" "net/http" "net/http/httptest" "os" @@ -14,8 +15,10 @@ import ( "github.com/databricks/cli/libs/cmdctx" "github.com/databricks/cli/libs/cmdio" "github.com/databricks/cli/libs/databrickscfg/profile" + "github.com/databricks/cli/libs/log" "github.com/databricks/databricks-sdk-go" "github.com/databricks/databricks-sdk-go/config" + "github.com/spf13/cobra" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) @@ -992,3 +995,112 @@ password = pass assert.Equal(t, "https://env.test", w.Config.Host) assert.Equal(t, "env-token", w.Config.Token) } + +// hostMetadataResolution is what a host-metadata resolution saw on its context. +type hostMetadataResolution struct { + logger *slog.Logger + err error +} + +// recordHostMetadataResolutions replaces the host-metadata resolver factory +// with one that records the logger and the error on the context each +// resolution receives. +func recordHostMetadataResolutions(t *testing.T) *[]hostMetadataResolution { + var seen []hostMetadataResolution + orig := config.DefaultHostMetadataResolverFactory + t.Cleanup(func() { config.DefaultHostMetadataResolverFactory = orig }) + config.DefaultHostMetadataResolverFactory = func(*config.Config) config.HostMetadataResolver { + return func(ctx context.Context, host string) (*config.HostMetadata, error) { + seen = append(seen, hostMetadataResolution{log.GetLogger(ctx), ctx.Err()}) + return nil, nil + } + } + return &seen +} + +// The resolution logs through the command's logger but otherwise keeps the +// SDK's context, so the request is not tied to the command's cancellation: +// the command's context is canceled up front and the resolver must not see it. +func TestMustClientResolvesHostMetadataWithCommandLogger(t *testing.T) { + for _, tc := range []struct { + name string + env map[string]string + mustAny func(cmd *cobra.Command, args []string) error + }{ + { + name: "workspace", + env: map[string]string{"DATABRICKS_HOST": "https://ws.test", "DATABRICKS_TOKEN": "t"}, + mustAny: MustWorkspaceClient, + }, + { + name: "account", + env: map[string]string{"DATABRICKS_HOST": "https://accounts.cloud.databricks.test", "DATABRICKS_TOKEN": "t", "DATABRICKS_ACCOUNT_ID": "1111"}, + mustAny: MustAccountClient, + }, + } { + t.Run(tc.name, func(t *testing.T) { + testutil.CleanupEnvironment(t) + for k, v := range tc.env { + t.Setenv(k, v) + } + seen := recordHostMetadataResolutions(t) + + logger := slog.New(slog.DiscardHandler) + ctx, cancel := context.WithCancel(log.NewContext(cmdio.MockDiscard(t.Context()), logger)) + cancel() + cmd := New(SkipLoadBundle(ctx)) + require.NoError(t, tc.mustAny(cmd, nil)) + require.NotEmpty(t, *seen) + for _, r := range *seen { + assert.Same(t, logger, r.logger) + assert.NoError(t, r.err) + } + }) + } +} + +// The SDK already skips the factory when there is no host; this guards the +// wrapper against calling it eagerly at bind time, which would create the +// host-metadata cache directories on every client attempt. +func TestBindHostMetadataLoggerDefersFactoryCall(t *testing.T) { + testutil.CleanupEnvironment(t) + t.Setenv("DATABRICKS_CONFIG_FILE", filepath.Join(t.TempDir(), "missing.databrickscfg")) + factoryCalls := 0 + orig := config.DefaultHostMetadataResolverFactory + t.Cleanup(func() { config.DefaultHostMetadataResolverFactory = orig }) + config.DefaultHostMetadataResolverFactory = func(*config.Config) config.HostMetadataResolver { + factoryCalls++ + return nil + } + + cmd := New(SkipLoadBundle(cmdio.MockDiscard(t.Context()))) + require.Error(t, MustWorkspaceClient(cmd, nil)) + assert.Zero(t, factoryCalls) +} + +// The SDK's factory contract lets a factory return nil to fall through to the +// SDK's own fetch; the wrapper must honor that rather than call a nil resolver. +func TestBindHostMetadataLoggerFallsBackToSDKFetch(t *testing.T) { + fetches := 0 + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if r.URL.Path == "/.well-known/databricks-config" { + fetches++ + } + w.Header().Set("Content-Type", "application/json") + _, _ = w.Write([]byte("{}")) + })) + t.Cleanup(server.Close) + + testutil.CleanupEnvironment(t) + t.Setenv("DATABRICKS_HOST", server.URL) + t.Setenv("DATABRICKS_TOKEN", "t") + orig := config.DefaultHostMetadataResolverFactory + t.Cleanup(func() { config.DefaultHostMetadataResolverFactory = orig }) + config.DefaultHostMetadataResolverFactory = func(*config.Config) config.HostMetadataResolver { + return nil + } + + cmd := New(SkipLoadBundle(cmdio.MockDiscard(t.Context()))) + require.NoError(t, MustWorkspaceClient(cmd, nil)) + assert.Equal(t, 1, fetches) +}