2025-02-03 03:18:38 +00:00
|
|
|
package telemetry
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/databricks/cli/libs/telemetry/protos"
|
|
|
|
"github.com/databricks/databricks-sdk-go/client"
|
|
|
|
"github.com/databricks/databricks-sdk-go/config"
|
|
|
|
)
|
|
|
|
|
2025-02-04 15:25:59 +00:00
|
|
|
const (
|
2025-02-24 16:09:18 +00:00
|
|
|
// File containing output from the upload process.
|
2025-02-04 15:25:59 +00:00
|
|
|
UploadLogsFileEnvVar = "DATABRICKS_CLI_TELEMETRY_UPLOAD_LOGS_FILE"
|
|
|
|
|
|
|
|
// File containing the PID of the telemetry upload process.
|
|
|
|
PidFileEnvVar = "DATABRICKS_CLI_TELEMETRY_PID_FILE"
|
2025-02-18 15:47:47 +00:00
|
|
|
|
|
|
|
// Environment variable to disable telemetry. If this is set to any value, telemetry
|
|
|
|
// will be disabled.
|
2025-02-24 17:31:43 +00:00
|
|
|
DisableEnvVar = "DATABRICKS_CLI_DISABLE_TELEMETRY"
|
2025-02-24 14:26:31 +00:00
|
|
|
|
|
|
|
// Max time to try and upload the telemetry logs. Useful for testing.
|
2025-02-24 15:10:50 +00:00
|
|
|
UploadTimeoutEnvVar = "DATABRICKS_CLI_TELEMETRY_UPLOAD_TIMEOUT"
|
2025-02-04 15:25:59 +00:00
|
|
|
)
|
2025-02-03 03:18:38 +00:00
|
|
|
|
|
|
|
type UploadConfig struct {
|
|
|
|
Logs []protos.FrontendLog `json:"logs"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// Upload reads telemetry logs from stdin and uploads them to the telemetry endpoint.
|
|
|
|
// This function is always expected to be called in a separate child process from
|
|
|
|
// the main CLI process.
|
2025-02-24 17:05:19 +00:00
|
|
|
func Upload(ctx context.Context) (*ResponseBody, error) {
|
2025-02-03 03:18:38 +00:00
|
|
|
var err error
|
|
|
|
|
|
|
|
b, err := io.ReadAll(os.Stdin)
|
|
|
|
if err != nil {
|
2025-02-04 15:25:59 +00:00
|
|
|
return nil, fmt.Errorf("failed to read from stdin: %s\n", err)
|
2025-02-03 03:18:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
in := UploadConfig{}
|
|
|
|
err = json.Unmarshal(b, &in)
|
|
|
|
if err != nil {
|
2025-02-04 15:25:59 +00:00
|
|
|
return nil, fmt.Errorf("failed to unmarshal input: %s\n", err)
|
2025-02-03 03:18:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(in.Logs) == 0 {
|
2025-02-04 15:25:59 +00:00
|
|
|
return nil, fmt.Errorf("No logs to upload: %s\n", err)
|
2025-02-03 03:18:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
protoLogs := make([]string, len(in.Logs))
|
|
|
|
for i, log := range in.Logs {
|
|
|
|
b, err := json.Marshal(log)
|
|
|
|
if err != nil {
|
2025-02-04 15:25:59 +00:00
|
|
|
return nil, fmt.Errorf("failed to marshal log: %s\n", err)
|
2025-02-03 03:18:38 +00:00
|
|
|
}
|
|
|
|
protoLogs[i] = string(b)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Parent process is responsible for setting environment variables to
|
|
|
|
// configure authentication.
|
|
|
|
apiClient, err := client.New(&config.Config{})
|
|
|
|
if err != nil {
|
2025-02-04 15:25:59 +00:00
|
|
|
return nil, fmt.Errorf("Failed to create API client: %s\n", err)
|
2025-02-03 03:18:38 +00:00
|
|
|
}
|
|
|
|
|
2025-02-24 14:26:31 +00:00
|
|
|
maxUploadTime := 30 * time.Second
|
2025-02-24 15:10:50 +00:00
|
|
|
if v, ok := os.LookupEnv(UploadTimeoutEnvVar); ok {
|
2025-02-24 14:26:31 +00:00
|
|
|
maxUploadTime, err = time.ParseDuration(v)
|
|
|
|
if err != nil {
|
2025-02-24 15:10:50 +00:00
|
|
|
return nil, fmt.Errorf("Failed to parse time limit %s: %s\n", UploadTimeoutEnvVar, err)
|
2025-02-24 14:26:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2025-02-03 03:18:38 +00:00
|
|
|
// Set a maximum total time to try telemetry uploads.
|
2025-02-24 17:05:19 +00:00
|
|
|
ctx, cancel := context.WithTimeout(ctx, maxUploadTime)
|
2025-02-03 03:18:38 +00:00
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
resp := &ResponseBody{}
|
2025-02-28 10:39:01 +00:00
|
|
|
|
2025-03-01 21:57:03 +00:00
|
|
|
// Retry uploading logs a maximum of 3 times incase the uploads are partially successful.
|
|
|
|
for range 3 {
|
2025-02-03 03:18:38 +00:00
|
|
|
// Log the CLI telemetry events.
|
|
|
|
err := apiClient.Do(ctx, http.MethodPost, "/telemetry-ext", nil, nil, RequestBody{
|
|
|
|
UploadTime: time.Now().UnixMilli(),
|
2025-03-01 21:57:03 +00:00
|
|
|
// There is a bug in the `/telemetry-ext` API which requires us to
|
|
|
|
// send an empty array for the `Items` field. Otherwise the API returns
|
|
|
|
// a 500.
|
|
|
|
Items: []string{},
|
|
|
|
ProtoLogs: protoLogs,
|
2025-02-03 03:18:38 +00:00
|
|
|
}, resp)
|
|
|
|
if err != nil {
|
2025-02-04 15:25:59 +00:00
|
|
|
return nil, fmt.Errorf("Failed to upload telemetry logs: %s\n", err)
|
2025-02-03 03:18:38 +00:00
|
|
|
}
|
|
|
|
|
2025-03-01 21:57:03 +00:00
|
|
|
// Skip retrying if the upload fails with an error.
|
2025-02-03 03:18:38 +00:00
|
|
|
if len(resp.Errors) > 0 {
|
2025-02-04 15:25:59 +00:00
|
|
|
return nil, fmt.Errorf("Failed to upload telemetry logs: %s\n", resp.Errors)
|
2025-02-03 03:18:38 +00:00
|
|
|
}
|
|
|
|
|
2025-03-01 21:57:03 +00:00
|
|
|
// All logs were uploaded successfully.
|
2025-02-03 03:18:38 +00:00
|
|
|
if resp.NumProtoSuccess == int64(len(in.Logs)) {
|
2025-02-04 15:25:59 +00:00
|
|
|
return resp, nil
|
2025-02-03 03:18:38 +00:00
|
|
|
}
|
2025-02-28 10:39:01 +00:00
|
|
|
|
|
|
|
// Add a delay of 1 second before retrying. We avoid retrying immediately
|
|
|
|
// to avoid overwhelming the telemetry endpoint.
|
2025-03-01 21:57:03 +00:00
|
|
|
// We only return incase of partial successful uploads. The SDK layer takes
|
|
|
|
// care of retrying in case of retriable status codes.
|
|
|
|
//
|
|
|
|
// TODO: I think I was wrong about the SDKs automatically doing retries.
|
|
|
|
// Look into this more and confirm with ankit what the 5xx status codes are.
|
|
|
|
// TODO: Confirm that the timeout of a request here is indeed one minute.
|
2025-02-28 10:39:01 +00:00
|
|
|
time.Sleep(1 * time.Second)
|
2025-02-03 03:18:38 +00:00
|
|
|
}
|
2025-03-01 21:57:03 +00:00
|
|
|
|
|
|
|
return nil, fmt.Errorf("Failed to upload all telemetry logs after 4 tries. Only %d/%d logs uploaded", resp.NumProtoSuccess, len(in.Logs))
|
2025-02-03 03:18:38 +00:00
|
|
|
}
|