Skip to content

Commit

Permalink
cli: add commands for managing statement diagnostics
Browse files Browse the repository at this point in the history
This change adds a `statement-diag` command, with the following subcommands:
```
  list        list available bundles and outstanding activation requests
  download    download statement diagnostics bundle into a zip file
  delete      delete statement diagnostics bundles
  cancel      cancel outstanding activation requests
```

Fixes #48597.

Release note (cli change): A new set of `statement-diag` CLI commands that can
be used to manage statement diagnostics.
  • Loading branch information
RaduBerinde committed Jul 9, 2020
1 parent 3a03f38 commit 49e6d39
Show file tree
Hide file tree
Showing 9 changed files with 550 additions and 2 deletions.
1 change: 1 addition & 0 deletions pkg/cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,7 @@ func init() {
quitCmd,

sqlShellCmd,
stmtDiagCmd,
authCmd,
nodeCmd,
dumpCmd,
Expand Down
3 changes: 2 additions & 1 deletion pkg/cli/cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@ func isSQLCommand(args []string) bool {
return false
}
switch args[0] {
case "sql", "dump", "workload", "nodelocal":
case "sql", "dump", "workload", "nodelocal", "statement-diag":
return true
case "node":
if len(args) == 0 {
Expand Down Expand Up @@ -1403,6 +1403,7 @@ Available Commands:
init initialize a cluster
cert create ca, node, and client certs
sql open a sql shell
statement-diag commands for managing statement diagnostics bundles
auth-session log in and out of HTTP sessions
node list, inspect, drain or remove nodes
dump dump sql tables
Expand Down
10 changes: 10 additions & 0 deletions pkg/cli/cliflags/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -1188,4 +1188,14 @@ other items retrieved by the zip command may still consider
confidential data or PII.
`,
}

StmtDiagDeleteAll = FlagInfo{
Name: "all",
Description: `Delete all bundles.`,
}

StmtDiagCancelAll = FlagInfo{
Name: "all",
Description: `Cancel all outstanding requests.`,
}
)
11 changes: 11 additions & 0 deletions pkg/cli/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ func initCLIDefaults() {
setNetworkBenchContextDefaults()
setSqlfmtContextDefaults()
setDemoContextDefaults()
setStmtDiagContextDefaults()
setAuthContextDefaults()

initPreFlagsDefaults()
Expand Down Expand Up @@ -506,6 +507,16 @@ func setDemoContextDefaults() {
demoCtx.insecure = false
}

// stmtDiagCtx captures the command-line parameters of the 'statement-diag'
// command.
var stmtDiagCtx struct {
all bool
}

func setStmtDiagContextDefaults() {
stmtDiagCtx.all = false
}

// GetServerCfgStores provides direct public access to the StoreSpecList inside
// serverCfg. This is used by CCL code to populate some fields.
//
Expand Down
8 changes: 8 additions & 0 deletions pkg/cli/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -535,6 +535,7 @@ func init() {
clientCmds = append(clientCmds, nodeCmds...)
clientCmds = append(clientCmds, systemBenchCmds...)
clientCmds = append(clientCmds, nodeLocalCmds...)
clientCmds = append(clientCmds, stmtDiagCmds...)
for _, cmd := range clientCmds {
f := cmd.PersistentFlags()
varFlag(f, addrSetter{&cliCtx.clientConnHost, &cliCtx.clientConnPort}, cliflags.ClientHost)
Expand Down Expand Up @@ -645,6 +646,7 @@ func init() {
sqlCmds := []*cobra.Command{sqlShellCmd, dumpCmd, demoCmd}
sqlCmds = append(sqlCmds, authCmds...)
sqlCmds = append(sqlCmds, demoCmd.Commands()...)
sqlCmds = append(sqlCmds, stmtDiagCmds...)
sqlCmds = append(sqlCmds, nodeLocalCmds...)
for _, cmd := range sqlCmds {
f := cmd.Flags()
Expand Down Expand Up @@ -732,6 +734,12 @@ func init() {
stringFlag(f, &startCtx.geoLibsDir, cliflags.GeoLibsDir)
}

// statement-diag command.
{
boolFlag(stmtDiagDeleteCmd.Flags(), &stmtDiagCtx.all, cliflags.StmtDiagDeleteAll)
boolFlag(stmtDiagCancelCmd.Flags(), &stmtDiagCtx.all, cliflags.StmtDiagCancelAll)
}

// sqlfmt command.
{
f := sqlfmtCmd.Flags()
Expand Down
5 changes: 5 additions & 0 deletions pkg/cli/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ package cli_test
import (
"os"
"testing"
"time"

"github.com/cockroachdb/cockroach/pkg/build"
"github.com/cockroachdb/cockroach/pkg/security"
Expand All @@ -30,6 +31,10 @@ func TestMain(m *testing.M) {
// a version injected. Pretend to be a very up-to-date version.
defer build.TestingOverrideTag("v999.0.0")()

// The times for Example_statement_diag are reported in the local timezone.
// Fix it to UTC so the output is always the same.
time.Local = time.UTC

serverutils.InitTestServerFactory(server.TestServerFactory)
os.Exit(m.Run())
}
Expand Down
Loading

0 comments on commit 49e6d39

Please sign in to comment.