Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
bundle:
name: config-remote-sync-telemetry-host-conflict

workspace:
host: https://other.test

targets:
bad:
variables:
undefined_var: value
own-host:
workspace:
host: $DATABRICKS_HOST
variables:
undefined_var: value
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
bundle:
name: config-remote-sync-telemetry-error-message

variables:
myvar:
description: a required variable

targets:
dev:
default: true
bad:
variables:
undefined_var: value
other:
workspace:
host: https://other.test
variables:
undefined_var: value
auth-error:
workspace:
client_id: x
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
bundle:
name: config-remote-sync-telemetry-default-target

targets:
bad:
default: true
variables:
undefined_var: value
other:
workspace:
host: https://other.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
bundle:
name: config-remote-sync-telemetry-other-workspace

workspace:
host: $DATABRICKS_HOST
workspace_id: "1111"

targets:
bad:
variables:
undefined_var: value
profile-workspace:
workspace:
workspace_id: "2222"
variables:
undefined_var: value
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Cloud = false
GOOS.windows = false
EnvMatrix.DMS = [""]
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@

>>> musterr [CLI] bundle config-remote-sync
Error: no value assigned to required variable myvar. Variables are usually assigned in databricks.yml, and they can be overridden using "--var", the BUNDLE_VAR_myvar environment variable, or .databricks/bundle/<target>/variable-overrides.json


>>> print_requests.py //telemetry-ext
{
"error_message": "no value assigned to required variable myvar. Variables are usually assigned in databricks.yml, and they can be overridden using \"--var\", the BUNDLE_VAR_myvar environment variable, or [REDACTED_REL_PATH](json)",
"error_category": "BUNDLE_LOAD_FAILED"
}

>>> musterr [CLI] bundle config-remote-sync -t bad -p myprofile
Error: failed to perform target override for target=bad: variable undefined_var is not defined but is assigned a value


>>> print_requests.py //telemetry-ext
{
"error_message": "failed to perform target override for target=bad: variable undefined_var is not defined but is assigned a value",
"error_category": "BUNDLE_LOAD_FAILED"
}

>>> musterr [CLI] bundle config-remote-sync -t bad
Error: failed to perform target override for target=bad: variable undefined_var is not defined but is assigned a value


>>> musterr [CLI] bundle config-remote-sync -t bad -p myprofile
Error: failed to perform target override for target=bad: variable undefined_var is not defined but is assigned a value


>>> musterr [CLI] bundle config-remote-sync -t bad -p myprofile
Error: failed to perform target override for target=bad: variable undefined_var is not defined but is assigned a value


>>> musterr [CLI] bundle config-remote-sync -t own-host -p myprofile
Error: failed to perform target override for target=own-host: variable undefined_var is not defined but is assigned a value


>>> musterr [CLI] bundle config-remote-sync -t profile-workspace -p workspace2222
Error: failed to perform target override for target=profile-workspace: variable undefined_var is not defined but is assigned a value


>>> musterr [CLI] bundle config-remote-sync -p myprofile
Error: failed to perform target override for target=bad: variable undefined_var is not defined but is assigned a value


>>> print_requests.py //telemetry-ext
{
"error_message": "failed to perform target override for target=bad: variable undefined_var is not defined but is assigned a value",
"error_category": "BUNDLE_LOAD_FAILED"
}
{
"error_message": "failed to perform target override for target=bad: variable undefined_var is not defined but is assigned a value",
"error_category": "BUNDLE_LOAD_FAILED"
}
{
"error_message": "failed to perform target override for target=bad: variable undefined_var is not defined but is assigned a value",
"error_category": "BUNDLE_LOAD_FAILED"
}
{
"error_message": "failed to perform target override for target=own-host: variable undefined_var is not defined but is assigned a value",
"error_category": "BUNDLE_LOAD_FAILED"
}
{
"error_message": "failed to perform target override for target=profile-workspace: variable undefined_var is not defined but is assigned a value",
"error_category": "BUNDLE_LOAD_FAILED"
}
{
"error_message": "failed to perform target override for target=bad: variable undefined_var is not defined but is assigned a value",
"error_category": "BUNDLE_LOAD_FAILED"
}

>>> musterr [CLI] bundle config-remote-sync -p myprofile
Error: Invalid bundle root DATABRICKS_BUNDLE_ROOT="missing": stat missing: no such file or directory

The DATABRICKS_BUNDLE_ROOT environment variable must point to an existing directory that contains a bundle configuration file.


>>> print_requests.py //telemetry-ext
{
"error_message": "Invalid bundle root DATABRICKS_BUNDLE_ROOT=\"missing\": stat missing: no such file or directory",
"error_category": "BUNDLE_LOAD_FAILED"
}

>>> musterr [CLI] bundle config-remote-sync -t bad
Error: failed to perform target override for target=bad: variable undefined_var is not defined but is assigned a value


>>> musterr [CLI] bundle config-remote-sync -t bad -p missing
Error: failed to perform target override for target=bad: variable undefined_var is not defined but is assigned a value


>>> musterr [CLI] bundle config-remote-sync -t bad -p myprofile
Error: failed to perform target override for target=bad: variable undefined_var is not defined but is assigned a value


>>> musterr [CLI] bundle config-remote-sync -t bad -p workspace2222
Error: failed to perform target override for target=bad: variable undefined_var is not defined but is assigned a value


>>> musterr [CLI] bundle config-remote-sync -t other -p myprofile
Error: failed to perform target override for target=other: variable undefined_var is not defined but is assigned a value


>>> musterr [CLI] bundle config-remote-sync -p myprofile
Error: failed to perform target override for target=other: variable undefined_var is not defined but is assigned a value


>>> musterr [CLI] bundle config-remote-sync -t auth-error -p myprofile
Error: cannot resolve bundle auth configuration: validate: more than one authorization method configured: oauth and pat. Config: host=[DATABRICKS_URL], token=***, profile=myprofile, config_file=[TEST_TMP_DIR]/.databrickscfg, client_id=x, databricks_cli_path=[CLI]. Env: DATABRICKS_HOST, DATABRICKS_TOKEN, DATABRICKS_CONFIG_FILE, DATABRICKS_CLI_PATH


>>> errcode print_requests.py //telemetry-ext
File [OUTPUT_DIR]/out.requests.txt not found

Exit code: 1
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
bundle:
name: config-remote-sync-telemetry-same-host

workspace:
host: $DATABRICKS_HOST

targets:
bad:
variables:
undefined_var: value
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# The diagnostic is already printed, so telemetry records it instead of the AlreadyPrinted sentinel.
trace musterr $CLI bundle config-remote-sync

trace print_requests.py //telemetry-ext | jq '.body.protoLogs[] | fromjson | .entry.databricks_cli_log.bundle_config_remote_sync_event | {error_message, error_category}'

cat > .databrickscfg <<EOT
[myprofile]
host = $DATABRICKS_HOST
token = $DATABRICKS_TOKEN

[workspace2222]
host = $DATABRICKS_HOST
workspace_id = 2222
token = $DATABRICKS_TOKEN
EOT
export DATABRICKS_CONFIG_FILE=$PWD/.databrickscfg

# Selecting the target fails before the auth config is set; with an explicit profile the failure is still reported.
trace musterr $CLI bundle config-remote-sync -t bad -p myprofile

trace print_requests.py //telemetry-ext | jq '.body.protoLogs[] | fromjson | .entry.databricks_cli_log.bundle_config_remote_sync_event | {error_message, error_category}'

# So is one with the profile from DATABRICKS_CONFIG_PROFILE, one where the bundle's or the target's host is the
# profile's (also to the profile's host when DATABRICKS_HOST names another), one where the target's workspace on the
# same host is the profile's, and a default target next to a target on another host.
DATABRICKS_CONFIG_PROFILE=myprofile trace musterr $CLI bundle config-remote-sync -t bad
envsubst < same-host/databricks.yml > out && mv out same-host/databricks.yml
withdir same-host trace musterr $CLI bundle config-remote-sync -t bad -p myprofile
DATABRICKS_HOST=https://other.test withdir same-host trace musterr $CLI bundle config-remote-sync -t bad -p myprofile
envsubst < conflict/databricks.yml > out && mv out conflict/databricks.yml
withdir conflict trace musterr $CLI bundle config-remote-sync -t own-host -p myprofile
envsubst < other-workspace/databricks.yml > out && mv out other-workspace/databricks.yml
withdir other-workspace trace musterr $CLI bundle config-remote-sync -t profile-workspace -p workspace2222
withdir default-target trace musterr $CLI bundle config-remote-sync -p myprofile

trace print_requests.py //telemetry-ext | jq '.body.protoLogs[] | fromjson | .entry.databricks_cli_log.bundle_config_remote_sync_event | {error_message, error_category}'

# So is a bundle that is not found at all.
DATABRICKS_BUNDLE_ROOT=missing trace musterr $CLI bundle config-remote-sync -p myprofile

trace print_requests.py //telemetry-ext | jq '.body.protoLogs[] | fromjson | .entry.databricks_cli_log.bundle_config_remote_sync_event | {error_message, error_category}'

# Without a profile, with one that does not resolve, with one for another host than the bundle's or the target's
# or for another workspace on the bundle's host, or when the bundle's own auth fails, nothing is sent.
trace musterr $CLI bundle config-remote-sync -t bad
trace musterr $CLI bundle config-remote-sync -t bad -p missing
withdir conflict trace musterr $CLI bundle config-remote-sync -t bad -p myprofile
Comment thread
Sankalp-Mittal marked this conversation as resolved.
Resolved
withdir other-workspace trace musterr $CLI bundle config-remote-sync -t bad -p workspace2222
trace musterr $CLI bundle config-remote-sync -t other -p myprofile
DATABRICKS_BUNDLE_TARGET=other trace musterr $CLI bundle config-remote-sync -p myprofile
trace musterr $CLI bundle config-remote-sync -t auth-error -p myprofile

trace errcode print_requests.py //telemetry-ext
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# config-remote-sync is not supported on Windows.
GOOS.windows = false

# The script writes .databrickscfg.
Ignore = [".databrickscfg"]
4 changes: 3 additions & 1 deletion bundle/configsync/output.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,9 @@ func WriteResult(out io.Writer, jsonOutput bool, stats *Stats, files []FileChang
if stats.ErrorCategory == "" {
stats.ErrorCategory = protos.BundleConfigRemoteSyncErrorCategoryBundleLoadFailed
}
stats.ErrorMessage = telemetry.ScrubErrorMessage(err.Error())
if stats.ErrorMessage == "" {
stats.ErrorMessage = telemetry.ScrubErrorMessage(err.Error())
}
// A selector that matched nothing: nothing to sync, not a failure.
if errors.Is(err, ErrNoMatchingSelector) {
status = StatusSkipped
Expand Down
93 changes: 93 additions & 0 deletions bundle/configsync/telemetry.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,27 @@
package configsync

import (
"cmp"
"context"
"slices"
"strings"

"github.com/databricks/cli/bundle"
"github.com/databricks/cli/bundle/config"
"github.com/databricks/cli/bundle/config/engine"
"github.com/databricks/cli/bundle/direct/dresources"
bundleenv "github.com/databricks/cli/bundle/env"
"github.com/databricks/cli/bundle/statemgmt"
"github.com/databricks/cli/libs/auth"
"github.com/databricks/cli/libs/cmdctx"
"github.com/databricks/cli/libs/databrickscfg"
"github.com/databricks/cli/libs/databrickscfg/profile"
"github.com/databricks/cli/libs/env"
"github.com/databricks/cli/libs/log"
"github.com/databricks/cli/libs/structs/structpath"
"github.com/databricks/cli/libs/telemetry"
"github.com/databricks/cli/libs/telemetry/protos"
sdkconfig "github.com/databricks/databricks-sdk-go/config"
)

// recoverTelemetry swallows any panic raised while collecting or emitting
Expand Down Expand Up @@ -283,6 +293,89 @@ func (s *Stats) CollectStateIDs(typeAndIDs []string) {
s.StateResourceIDs = collectResourceIDs(typeAndIDs)
}

// WithProfileAuth sets the auth config from an explicitly selected profile when
// loading the bundle or its target failed before the bundle set its own, so the
// failure can still be reported. Without the target its workspace is unknown, so
// nothing is set without a profile or when the profile is not found.
// Only the profile is used: the bundle config can name other hosts, and
// telemetry must not be sent there. The config is left for the upload to
// resolve within its own timeout, and as the upload may be cut off at exit, its
// auth never refreshes an OAuth token.
func WithProfileAuth(ctx context.Context, b *bundle.Bundle, profileName, target string) (out context.Context) {
out = ctx
defer recoverTelemetry(ctx)
if cmdctx.HasConfigUsed(ctx) || telemetry.Disabled(ctx) {
return ctx
}
// With the target selected, only the bundle's own auth can have failed, and
// auth errors are not reported.
if b != nil && b.Config.Bundle.Target != "" {
log.Debugf(ctx, "config-remote-sync telemetry skipped: bundle auth failed")
return ctx
}
profileName = cmp.Or(profileName, env.Get(ctx, "DATABRICKS_CONFIG_PROFILE"))
if profileName == "" {
log.Debugf(ctx, "config-remote-sync telemetry skipped: no profile selected")
return ctx
}
profiles, err := profile.GetProfiler(ctx).LoadProfiles(ctx, profile.WithName(profileName))
if err != nil || len(profiles) == 0 {
log.Debugf(ctx, "config-remote-sync telemetry skipped: profile %s not found", profileName)
return ctx
}
// As in bundle auth, a profile for another host than the bundle's is rejected,
// and so is one for another workspace on the same unified host.
if b != nil {
host, id := b.Config.Workspace.Host, b.Config.Workspace.WorkspaceID
// The target was not merged, so its own workspace is not in the root config yet.
if t := b.Config.Targets[selectedTarget(ctx, b, target)]; t != nil && t.Workspace != nil {
host, id = cmp.Or(t.Workspace.Host, host), cmp.Or(t.Workspace.WorkspaceID, id)
}
p := profiles[0]
bundleID, profileID := workspaceID(host, id), workspaceID(p.Host, p.WorkspaceID)
if host != "" && canonicalHost(host) != canonicalHost(p.Host) || bundleID != "" && profileID != "" && bundleID != profileID {
log.Debugf(ctx, "config-remote-sync telemetry skipped: profile %s is for another workspace than the bundle's %s", profileName, host)
return ctx
}
}
cfg := (&config.Workspace{Profile: profileName}).Config(ctx)
// As for a bundle's own profile, the profile wins over auth env vars, so the
// upload goes to the host checked above.
cfg.Loaders = databrickscfg.ProfileAuthLoaders
cfg.Credentials = auth.CredentialsWithoutRefresh()
return cmdctx.SetConfigUsed(ctx, cfg)
}

// workspaceID returns the explicit workspace ID, else the one in the host URL.
func workspaceID(host, id string) string {
return cmp.Or(id, auth.ExtractHostQueryParams(host).WorkspaceID)
}

func canonicalHost(host string) string {
return (&sdkconfig.Config{Host: host}).CanonicalHostName()
}

// selectedTarget returns the target loading the bundle selects: the named one,
// else DATABRICKS_BUNDLE_TARGET, else the only target or the one marked default.
func selectedTarget(ctx context.Context, b *bundle.Bundle, name string) string {
if name == "" {
name, _ = bundleenv.Target(ctx)
}
if name != "" {
return name
}
var defaults []string
for n, t := range b.Config.Targets {
if len(b.Config.Targets) == 1 || (t != nil && config.IsExplicitlyEnabled(t.Default)) {
defaults = append(defaults, n)
}
}
if len(defaults) != 1 {
return ""
}
return defaults[0]
}

// LogTelemetry emits the BundleConfigRemoteSyncEvent for this run.
func (s *Stats) LogTelemetry(ctx context.Context) {
defer recoverTelemetry(ctx)
Expand Down
Loading
Loading