Skip to content

Commit

Permalink
telemetry: add basic telemetry for tiup (#1341)
Browse files Browse the repository at this point in the history
  • Loading branch information
AstroProfundis authored May 7, 2021
1 parent 4403b08 commit a254fbc
Show file tree
Hide file tree
Showing 21 changed files with 601 additions and 113 deletions.
1 change: 1 addition & 0 deletions cmd/clean.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ func newCleanCmd() *cobra.Command {
Use: "clean <name>",
Short: "Clean the data of instantiated components",
RunE: func(cmd *cobra.Command, args []string) error {
teleCommand = cmd.CommandPath()
env := environment.GlobalEnv()
if len(args) == 0 && !all {
return cmd.Help()
Expand Down
1 change: 1 addition & 0 deletions cmd/completion.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ func newCompletionCmd() *cobra.Command {
SilenceUsage: true,
SilenceErrors: true,
RunE: func(cmd *cobra.Command, args []string) error {
teleCommand = cmd.CommandPath()
if len(args) == 1 {
switch args[0] {
case "bash":
Expand Down
1 change: 1 addition & 0 deletions cmd/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ func newEnvCmd() *cobra.Command {
Use: "env [name1...N]",
Short: "Show the list of system environment variable that related to TiUP",
RunE: func(cmd *cobra.Command, args []string) error {
teleCommand = cmd.CommandPath()
if len(args) == 0 {
showEnvList(true, envList...)
return nil
Expand Down
1 change: 1 addition & 0 deletions cmd/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ of the same component:
tiup install tidb:v3.0.5 tikv pd
tiup install tidb:v3.0.5 tidb:v3.0.8 tikv:v3.0.9`,
RunE: func(cmd *cobra.Command, args []string) error {
teleCommand = cmd.CommandPath()
env := environment.GlobalEnv()
if len(args) == 0 {
return cmd.Help()
Expand Down
1 change: 1 addition & 0 deletions cmd/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ components or versions which have not been installed.
SilenceUsage: true,
SilenceErrors: true,
RunE: func(cmd *cobra.Command, args []string) error {
teleCommand = cmd.CommandPath()
env := environment.GlobalEnv()
switch len(args) {
case 0:
Expand Down
12 changes: 12 additions & 0 deletions cmd/mirror.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ The repository can be used either online or offline.
It also provides some useful utilities to help managing keys, users and versions
of components or the repository itself.`,
RunE: func(cmd *cobra.Command, args []string) error {
teleCommand = cmd.CommandPath()
if len(args) == 0 {
return cmd.Help()
}
Expand Down Expand Up @@ -87,6 +88,7 @@ func newMirrorSignCmd() *cobra.Command {
Short: "Add signatures to a manifest file",
Long: fmt.Sprintf("Add signatures to a manifest file; if no key file is specified, ~/.tiup/keys/%s will be used", localdata.DefaultPrivateKeyName),
RunE: func(cmd *cobra.Command, args []string) error {
teleCommand = cmd.CommandPath()
env := environment.GlobalEnv()
if len(args) < 1 {
return cmd.Help()
Expand Down Expand Up @@ -148,6 +150,7 @@ func newMirrorShowCmd() *cobra.Command {
Short: "Show mirror address",
Long: `Show current mirror address`,
RunE: func(cmd *cobra.Command, args []string) error {
teleCommand = cmd.CommandPath()
fmt.Println(environment.Mirror())
return nil
},
Expand All @@ -169,6 +172,7 @@ func newMirrorSetCmd() *cobra.Command {
directory. Relative paths will not be expanded, so absolute paths are recommended.
The root manifest in $TIUP_HOME will be replaced with the one in given repository automatically.`,
RunE: func(cmd *cobra.Command, args []string) error {
teleCommand = cmd.CommandPath()
if !reset && len(args) != 1 {
return cmd.Help()
}
Expand Down Expand Up @@ -213,6 +217,7 @@ func newMirrorGrantCmd() *cobra.Command {
Short: "grant a new owner",
Long: "grant a new owner to current mirror",
RunE: func(cmd *cobra.Command, args []string) error {
teleCommand = cmd.CommandPath()
if len(args) < 1 {
return cmd.Help()
}
Expand Down Expand Up @@ -265,6 +270,7 @@ func newMirrorModifyCmd() *cobra.Command {
Short: "Modify published component",
Long: "Modify component attributes (hidden, standalone, yanked)",
RunE: func(cmd *cobra.Command, args []string) error {
teleCommand = cmd.CommandPath()
if len(args) != 1 {
return cmd.Help()
}
Expand Down Expand Up @@ -340,6 +346,7 @@ func newMirrorRotateCmd() *cobra.Command {
Short: "Rotate root.json",
Long: "Rotate root.json make it possible to modify root.json",
RunE: func(cmd *cobra.Command, args []string) error {
teleCommand = cmd.CommandPath()
root, err := editLatestRootManifest()
if err != nil {
return err
Expand Down Expand Up @@ -473,6 +480,7 @@ func newMirrorPublishCmd() *cobra.Command {
Short: "Publish a component",
Long: "Publish a component to the repository",
RunE: func(cmd *cobra.Command, args []string) error {
teleCommand = cmd.CommandPath()
if len(args) != 4 {
return cmd.Help()
}
Expand Down Expand Up @@ -612,6 +620,7 @@ func newMirrorGenkeyCmd() *cobra.Command {
Short: "Generate a new key pair",
Long: `Generate a new key pair that can be used to sign components.`,
RunE: func(cmd *cobra.Command, args []string) error {
teleCommand = cmd.CommandPath()
env := environment.GlobalEnv()
privPath := env.Profile().Path(localdata.KeyInfoParentDir, name+".json")
keyDir := filepath.Dir(privPath)
Expand Down Expand Up @@ -705,6 +714,7 @@ func newMirrorInitCmd() *cobra.Command {
The specified path must be an empty directory.
If the path does not exist, a new directory will be created.`,
RunE: func(cmd *cobra.Command, args []string) error {
teleCommand = cmd.CommandPath()
if len(args) != 1 {
return cmd.Help()
}
Expand Down Expand Up @@ -759,6 +769,7 @@ func newMirrorMergeCmd() *cobra.Command {
tiup mirror merge tidb-community-v4.0.1 tidb-community-v4.0.2 # merge v4.0.1 and v4.0.2 into current mirror`,
Short: "Merge two or more offline mirror",
RunE: func(cmd *cobra.Command, args []string) error {
teleCommand = cmd.CommandPath()
if len(args) < 1 {
return cmd.Help()
}
Expand Down Expand Up @@ -837,6 +848,7 @@ func newMirrorCloneCmd() *cobra.Command {
return initMirrorCloneExtraArgs(cmd)
},
RunE: func(cmd *cobra.Command, args []string) error {
teleCommand = cmd.CommandPath()
cmd.DisableFlagParsing = false
err := cmd.ParseFlags(args)
if err != nil {
Expand Down
88 changes: 84 additions & 4 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,27 +14,39 @@
package cmd

import (
"context"
"encoding/json"
"errors"
"fmt"
"os"
"time"

"github.com/fatih/color"
"github.com/google/uuid"
perrs "github.com/pingcap/errors"
"github.com/pingcap/tiup/pkg/environment"
"github.com/pingcap/tiup/pkg/exec"
"github.com/pingcap/tiup/pkg/flags"
"github.com/pingcap/tiup/pkg/localdata"
"github.com/pingcap/tiup/pkg/logger/log"
"github.com/pingcap/tiup/pkg/repository"
"github.com/pingcap/tiup/pkg/repository/v1manifest"
"github.com/pingcap/tiup/pkg/telemetry"
"github.com/pingcap/tiup/pkg/version"
"github.com/spf13/cobra"
)

var rootCmd *cobra.Command
var repoOpts repository.Options
var (
rootCmd *cobra.Command
repoOpts repository.Options
reportEnabled bool // is telemetry report enabled
eventUUID = uuid.New().String()
teleCommand string
)

func init() {
cobra.EnableCommandSorting = false
_ = os.Setenv(localdata.EnvNameTelemetryEventUUID, eventUUID)

var (
binary string
Expand All @@ -59,6 +71,7 @@ the latest stable version will be downloaded from the repository.`,
return nil
},
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
teleCommand = cmd.CommandPath()
if printVersion && len(args) == 0 {
return nil
}
Expand Down Expand Up @@ -116,6 +129,7 @@ the latest stable version will be downloaded from the repository.`,
break
}
}
teleCommand = fmt.Sprintf("%s %s", cmd.CommandPath(), componentSpec)
return exec.RunComponent(env, tag, componentSpec, binPath, transparentParams)
}
return cmd.Help()
Expand Down Expand Up @@ -177,8 +191,74 @@ the latest stable version will be downloaded from the repository.`,

// Execute parses the command line arguments and calls proper functions
func Execute() {
if err := rootCmd.Execute(); err != nil {
start := time.Now()
code := 0

err := rootCmd.Execute()
if err != nil {
fmt.Println(color.RedString("Error: %v", err))
os.Exit(1)
code = 1
}

teleReport := new(telemetry.Report)
tiupReport := new(telemetry.TiUPReport)
teleReport.EventDetail = &telemetry.Report_Tiup{Tiup: tiupReport}

env := environment.GlobalEnv()
if env == nil {
// if the env is not initialized, skip telemetry upload
// as many info are read from the env.
// TODO: split pure meta information from env object and
// us a dedicated package for that
reportEnabled = false
} else {
teleMeta, _, err := telemetry.GetMeta(env)
if err == nil {
reportEnabled = teleMeta.Status == telemetry.EnableStatus
teleReport.InstallationUUID = teleMeta.UUID
} // default to false on errors
}

if reportEnabled {
teleReport.EventUUID = eventUUID
teleReport.EventUnixTimestamp = start.Unix()
teleReport.Version = telemetry.TiUPMeta()
teleReport.Version.TiUPVersion = version.NewTiUPVersion().SemVer()
tiupReport.Command = teleCommand
tiupReport.CustomMirror = env.Profile().Config.Mirror != repository.DefaultMirror

f := func() {
defer func() {
if r := recover(); r != nil {
if flags.DebugMode {
log.Debugf("Recovered in telemetry report: %v", r)
}
}
}()

tiupReport.ExitCode = int32(code)
tiupReport.TakeMilliseconds = uint64(time.Since(start).Milliseconds())
tele := telemetry.NewTelemetry()
ctx, cancel := context.WithTimeout(context.Background(), time.Second*2)
err := tele.Report(ctx, teleReport)
if flags.DebugMode {
if err != nil {
log.Infof("report failed: %v", err)
}
fmt.Printf("report: %s\n", teleReport.String())
if data, err := json.Marshal(teleReport); err == nil {
log.Debugf("report: %s\n", string(data))
}
}
cancel()
}

f()
}

color.Unset()

if code != 0 {
os.Exit(code)
}
}
1 change: 1 addition & 0 deletions cmd/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ func newStatusCmd() *cobra.Command {
Use: "status",
Short: "List the status of instantiated components",
RunE: func(cmd *cobra.Command, args []string) error {
teleCommand = cmd.CommandPath()
env := environment.GlobalEnv()
if len(args) > 0 {
return cmd.Help()
Expand Down
4 changes: 4 additions & 0 deletions cmd/telemetry.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ func newTelemetryCmd() *cobra.Command {
Use: "reset",
Short: "Reset the uuid used for telemetry",
RunE: func(cmd *cobra.Command, args []string) error {
teleCommand = cmd.CommandPath()
env := environment.GlobalEnv()
teleMeta, fname, err := telemetry.GetMeta(env)
if err != nil {
Expand All @@ -39,6 +40,7 @@ func newTelemetryCmd() *cobra.Command {
Use: "enable",
Short: "Enable telemetry of tiup",
RunE: func(cmd *cobra.Command, args []string) error {
teleCommand = cmd.CommandPath()
env := environment.GlobalEnv()
teleMeta, fname, err := telemetry.GetMeta(env)
if err != nil {
Expand All @@ -60,6 +62,7 @@ func newTelemetryCmd() *cobra.Command {
Use: "disable",
Short: "Disable telemetry of tiup",
RunE: func(cmd *cobra.Command, args []string) error {
teleCommand = cmd.CommandPath()
env := environment.GlobalEnv()
teleMeta, fname, err := telemetry.GetMeta(env)
if err != nil {
Expand All @@ -81,6 +84,7 @@ func newTelemetryCmd() *cobra.Command {
Use: "status",
Short: "Display the current status of tiup telemetry",
RunE: func(cmd *cobra.Command, args []string) error {
teleCommand = cmd.CommandPath()
env := environment.GlobalEnv()
teleMeta, _, err := telemetry.GetMeta(env)
if err != nil {
Expand Down
1 change: 1 addition & 0 deletions cmd/uninstall.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ which is used to uninstall tiup.
# Uninstall all installed components
tiup uninstall --all`,
RunE: func(cmd *cobra.Command, args []string) error {
teleCommand = cmd.CommandPath()
env := environment.GlobalEnv()
if self {
deletable := []string{"bin", "manifest", "manifests", "components", "storage/cluster/packages"}
Expand Down
1 change: 1 addition & 0 deletions cmd/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ latest version. All other flags will be ignored if the flag --self is given.
$ tiup update playground:v0.0.3 --force # Overwrite an existing local installation
$ tiup update --self # Update TiUP to the latest version`,
RunE: func(cmd *cobra.Command, components []string) error {
teleCommand = cmd.CommandPath()
if (len(components) == 0 && !all && !force && !self) || (len(components) > 0 && all) {
return cmd.Help()
}
Expand Down
11 changes: 5 additions & 6 deletions components/cluster/command/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,18 @@ import (
"github.com/pingcap/tiup/pkg/cluster/executor"
"github.com/pingcap/tiup/pkg/cluster/manager"
operator "github.com/pingcap/tiup/pkg/cluster/operation"
"github.com/pingcap/tiup/pkg/cluster/report"
"github.com/pingcap/tiup/pkg/cluster/spec"
"github.com/pingcap/tiup/pkg/cluster/task"
"github.com/pingcap/tiup/pkg/errutil"
telemetry2 "github.com/pingcap/tiup/pkg/telemetry"
"github.com/pingcap/tiup/pkg/telemetry"
"github.com/pingcap/tiup/pkg/utils"
"github.com/spf13/cobra"
)

var (
teleReport *telemetry2.Report
clusterReport *telemetry2.ClusterReport
teleNodeInfos []*telemetry2.NodeInfo
teleReport *telemetry.Report
clusterReport *telemetry.ClusterReport
teleNodeInfos []*telemetry.NodeInfo
teleTopology string
teleCommand []string
)
Expand Down Expand Up @@ -104,7 +103,7 @@ func postDeployHook(builder *task.Builder, topo spec.Topology) {
return nil
}).BuildAsStep("Check status").SetHidden(true)

if report.Enabled() {
if telemetry.Enabled() {
builder.ParallelStep("+ Check status", false, nodeInfoTask)
}

Expand Down
Loading

0 comments on commit a254fbc

Please sign in to comment.