use daemon

This commit is contained in:
Shreyas Goenka 2025-02-18 16:30:50 +01:00
parent f48fbb950b
commit 520f06905b
No known key found for this signature in database
GPG Key ID: 92A07DF49CCB0622
24 changed files with 39 additions and 102 deletions

View File

@ -1,27 +0,0 @@
#!/bin/bash
wait_file() {
local file_path="$1"
local max_attempts=100
local attempt=0
while [ $attempt -lt $max_attempts ]; do
if [ -e "$file_path" ]; then
echo "File $file_path exists"
return 0
fi
sleep 0.1
attempt=$((attempt + 1))
done
echo "Timeout: File $file_path did not appear within 10 seconds"
return 1
}
if [ $# -eq 0 ]; then
echo "Usage: $0 <file_path>"
exit 1
fi
wait_file "$1"
exit $?

View File

@ -1,5 +0,0 @@
>>> [CLI] telemetry dummy
waiting for telemetry process to finish
File ./telemetry.pid exists
Process has ended

View File

@ -1,5 +0,0 @@
>>> [CLI] telemetry dummy
waiting for telemetry process to finish
File ./telemetry.pid exists
Process has ended

View File

@ -5,8 +5,8 @@
"uploadTime": "UNIX_TIME_MILLIS",
"items": [],
"protoLogs": [
"{\"frontend_log_event_id\":\"[UUID]\",\"entry\":{\"databricks_cli_log\":{\"execution_context\":{\"cmd_exec_id\":\"[UUID]\",\"version\":\"[DEV_VERSION]\",\"command\":\"telemetry_dummy\",\"operating_system\":\"OS\",\"execution_time_ms\":\"SMALL_INT\",\"exit_code\":0},\"cli_test_event\":{\"name\":\"VALUE1\"}}}}",
"{\"frontend_log_event_id\":\"[UUID]\",\"entry\":{\"databricks_cli_log\":{\"execution_context\":{\"cmd_exec_id\":\"[UUID]\",\"version\":\"[DEV_VERSION]\",\"command\":\"telemetry_dummy\",\"operating_system\":\"OS\",\"execution_time_ms\":\"SMALL_INT\",\"exit_code\":0},\"cli_test_event\":{\"name\":\"VALUE2\"}}}}"
"{\"frontend_log_event_id\":\"[UUID]\",\"entry\":{\"databricks_cli_log\":{\"execution_context\":{\"cmd_exec_id\":\"[UUID]\",\"version\":\"[DEV_VERSION]\",\"command\":\"selftest_send-telemetry\",\"operating_system\":\"OS\",\"execution_time_ms\":\"SMALL_INT\",\"exit_code\":0},\"cli_test_event\":{\"name\":\"VALUE1\"}}}}",
"{\"frontend_log_event_id\":\"[UUID]\",\"entry\":{\"databricks_cli_log\":{\"execution_context\":{\"cmd_exec_id\":\"[UUID]\",\"version\":\"[DEV_VERSION]\",\"command\":\"selftest_send-telemetry\",\"operating_system\":\"OS\",\"execution_time_ms\":\"SMALL_INT\",\"exit_code\":0},\"cli_test_event\":{\"name\":\"VALUE2\"}}}}"
]
}
}

View File

@ -0,0 +1,3 @@
>>> [CLI] selftest send-telemetry
[wait_pid] process has ended

View File

@ -3,12 +3,9 @@ export DATABRICKS_CLI_TELEMETRY_UPLOAD_LOGS_FILE=./out.upload.txt
# This test ensures that the main CLI command does not error even if
# telemetry upload fails.
trace $CLI telemetry dummy
echo "waiting for telemetry process to finish"
trace $CLI selftest send-telemetry
# Wait for the child telemetry process to finish
wait_file ./telemetry.pid
wait_pid $(cat ./telemetry.pid)
# cleanup the pid file

View File

@ -5,8 +5,8 @@
"uploadTime": "UNIX_TIME_MILLIS",
"items": [],
"protoLogs": [
"{\"frontend_log_event_id\":\"[UUID]\",\"entry\":{\"databricks_cli_log\":{\"execution_context\":{\"cmd_exec_id\":\"[UUID]\",\"version\":\"[DEV_VERSION]\",\"command\":\"telemetry_dummy\",\"operating_system\":\"OS\",\"execution_time_ms\":\"SMALL_INT\",\"exit_code\":0},\"cli_test_event\":{\"name\":\"VALUE1\"}}}}",
"{\"frontend_log_event_id\":\"[UUID]\",\"entry\":{\"databricks_cli_log\":{\"execution_context\":{\"cmd_exec_id\":\"[UUID]\",\"version\":\"[DEV_VERSION]\",\"command\":\"telemetry_dummy\",\"operating_system\":\"OS\",\"execution_time_ms\":\"SMALL_INT\",\"exit_code\":0},\"cli_test_event\":{\"name\":\"VALUE2\"}}}}"
"{\"frontend_log_event_id\":\"[UUID]\",\"entry\":{\"databricks_cli_log\":{\"execution_context\":{\"cmd_exec_id\":\"[UUID]\",\"version\":\"[DEV_VERSION]\",\"command\":\"selftest_send-telemetry\",\"operating_system\":\"OS\",\"execution_time_ms\":\"SMALL_INT\",\"exit_code\":0},\"cli_test_event\":{\"name\":\"VALUE1\"}}}}",
"{\"frontend_log_event_id\":\"[UUID]\",\"entry\":{\"databricks_cli_log\":{\"execution_context\":{\"cmd_exec_id\":\"[UUID]\",\"version\":\"[DEV_VERSION]\",\"command\":\"selftest_send-telemetry\",\"operating_system\":\"OS\",\"execution_time_ms\":\"SMALL_INT\",\"exit_code\":0},\"cli_test_event\":{\"name\":\"VALUE2\"}}}}"
]
}
}

View File

@ -0,0 +1,4 @@
>>> [CLI] selftest send-telemetry
waiting for telemetry process to finish
[wait_pid] process has ended

View File

@ -1,12 +1,11 @@
export DATABRICKS_CLI_TELEMETRY_PID_FILE=./telemetry.pid
export DATABRICKS_CLI_TELEMETRY_UPLOAD_LOGS_FILE=./out.upload.txt
export DATABRICKS_CLI_TELEMETRY_UPLOAD_LOGS_FILE=./out.send_telemetry.txt
trace $CLI telemetry dummy
trace $CLI selftest send-telemetry
echo "waiting for telemetry process to finish"
# Wait for the child telemetry process to finish
wait_file ./telemetry.pid
wait_pid $(cat ./telemetry.pid)
# cleanup the pid file

View File

@ -1,7 +1,7 @@
{
"logs": [
{
"frontend_log_event_id": "BB79BB52-96F6-42C5-9E44-E63EEA84888D",
"frontend_log_event_id": "AAAAAAAA-AAAA-AAAA-AAAA-AAAAAAAAAAAA",
"entry": {
"databricks_cli_log": {
"cli_test_event": {
@ -11,7 +11,7 @@
}
},
{
"frontend_log_event_id": "A7F597B0-66D1-462D-824C-C5C706F232E8",
"frontend_log_event_id": "BBBBBBBB-BBBB-BBBB-BBBB-BBBBBBBBBBBB",
"entry": {
"databricks_cli_log": {
"cli_test_event": {

View File

@ -14,7 +14,6 @@ import (
"github.com/databricks/cli/cmd/root"
"github.com/databricks/cli/cmd/selftest"
"github.com/databricks/cli/cmd/sync"
"github.com/databricks/cli/cmd/telemetry"
"github.com/databricks/cli/cmd/version"
"github.com/databricks/cli/cmd/workspace"
"github.com/spf13/cobra"
@ -76,7 +75,6 @@ func New(ctx context.Context) *cobra.Command {
cli.AddCommand(labs.New(ctx))
cli.AddCommand(sync.New())
cli.AddCommand(version.New())
cli.AddCommand(telemetry.New())
cli.AddCommand(selftest.New())
return cli

View File

@ -7,16 +7,15 @@ import (
"fmt"
"log/slog"
"os"
"os/exec"
"runtime"
"slices"
"strconv"
"strings"
"time"
"github.com/databricks/cli/internal/build"
"github.com/databricks/cli/libs/auth"
"github.com/databricks/cli/libs/cmdio"
"github.com/databricks/cli/libs/daemon"
"github.com/databricks/cli/libs/dbr"
"github.com/databricks/cli/libs/env"
"github.com/databricks/cli/libs/log"
@ -198,47 +197,38 @@ func uploadTelemetry(ctx context.Context, cmdStr string, start, end time.Time, e
Logs: logs,
}
execPath, err := os.Executable()
if err != nil {
log.Debugf(ctx, "failed to get executable path: %s", err)
}
telemetryCmd := exec.Command(execPath, "telemetry", "upload")
telemetryCmd.Env = inheritEnvVars()
// Compute environment variables with the appropriate auth configuration.
env := inheritEnvVars()
for k, v := range auth.Env(ConfigUsed(ctx)) {
telemetryCmd.Env = append(telemetryCmd.Env, fmt.Sprintf("%s=%s", k, v))
env = append(env, fmt.Sprintf("%s=%s", k, v))
}
b, err := json.Marshal(in)
if err != nil {
log.Debugf(ctx, "failed to marshal telemetry logs: %s", err)
return
d := daemon.Daemon{
Args: []string{"telemetry", "upload"},
Env: env,
PidFilePath: os.Getenv(telemetry.PidFileEnvVar),
LogFile: os.Getenv(telemetry.UploadLogsFileEnvVar),
}
stdin, err := telemetryCmd.StdinPipe()
if err != nil {
log.Debugf(ctx, "failed to create stdin pipe for telemetry worker: %s", err)
}
err = telemetryCmd.Start()
err := d.Start()
if err != nil {
log.Debugf(ctx, "failed to start telemetry worker: %s", err)
return
}
if pidFilePath := env.Get(ctx, telemetry.PidFileEnvVar); pidFilePath != "" {
err = os.WriteFile(pidFilePath, []byte(strconv.Itoa(telemetryCmd.Process.Pid)), 0o644)
if err != nil {
log.Debugf(ctx, "failed to write telemetry worker PID file: %s", err)
}
// If the telemetry worker is started successfully, we write the logs to its stdin.
b, err := json.Marshal(in)
if err != nil {
log.Debugf(ctx, "failed to marshal telemetry logs: %s", err)
return
}
_, err = stdin.Write(b)
err = d.WriteInput(b)
if err != nil {
log.Debugf(ctx, "failed to write to telemetry worker: %s", err)
}
err = stdin.Close()
err = d.Release()
if err != nil {
log.Debugf(ctx, "failed to close stdin for telemetry worker: %s", err)
log.Debugf(ctx, "failed to release telemetry worker: %s", err)
}
}

View File

@ -13,5 +13,6 @@ func New() *cobra.Command {
cmd.AddCommand(newChildCommand())
cmd.AddCommand(newParentCommand())
cmd.AddCommand(newSendTelemetry())
return cmd
}

View File

@ -1,4 +1,4 @@
package telemetry
package selftest
import (
"github.com/databricks/cli/cmd/root"
@ -7,12 +7,10 @@ import (
"github.com/spf13/cobra"
)
func newDummyCommand() *cobra.Command {
func newSendTelemetry() *cobra.Command {
cmd := &cobra.Command{
Use: "dummy",
Short: "log dummy telemetry events",
Long: "Fire a test telemetry event against the configured Databricks workspace.",
Hidden: true,
Use: "send-telemetry",
Short: "log some test telemetry events",
PreRunE: root.MustWorkspaceClient,
}

View File

@ -1,16 +0,0 @@
package telemetry
import (
"github.com/spf13/cobra"
)
func New() *cobra.Command {
cmd := &cobra.Command{
Use: "telemetry",
Short: "",
Hidden: true,
}
cmd.AddCommand(newDummyCommand())
return cmd
}