Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cli: Enable cockroach debug tsdump to automatically create tsdump.yaml #114046

Merged
merged 1 commit into from
Dec 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions pkg/cli/debug.go
Original file line number Diff line number Diff line change
Expand Up @@ -1543,6 +1543,7 @@ func init() {
f.Var(&debugTimeSeriesDumpOpts.to, "to", "newest timestamp to include (inclusive)")
f.StringVar(&debugTimeSeriesDumpOpts.clusterLabel, "cluster-label",
"", "prometheus label for cluster name")
f.StringVar(&debugTimeSeriesDumpOpts.yaml, "yaml", debugTimeSeriesDumpOpts.yaml, "full path to create the tsdump.yaml with storeID: nodeID mappings (raw format only). This file is required when loading the raw tsdump for troubleshooting.")

f = debugSendKVBatchCmd.Flags()
f.StringVar(&debugSendKVBatchContext.traceFormat, "trace", debugSendKVBatchContext.traceFormat,
Expand Down
44 changes: 44 additions & 0 deletions pkg/cli/tsdump.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"time"

"github.com/cockroachdb/cockroach/pkg/cli/clierrorplus"
"github.com/cockroachdb/cockroach/pkg/cli/clisqlclient"
"github.com/cockroachdb/cockroach/pkg/roachpb"
"github.com/cockroachdb/cockroach/pkg/server/serverpb"
"github.com/cockroachdb/cockroach/pkg/ts"
Expand All @@ -39,11 +40,13 @@ var debugTimeSeriesDumpOpts = struct {
format tsDumpFormat
from, to timestampValue
clusterLabel string
yaml string
}{
format: tsDumpText,
from: timestampValue{},
to: timestampValue(timeutil.Now().Add(24 * time.Hour)),
clusterLabel: "",
yaml: "/tmp/tsdump.yaml",
}

var debugTimeSeriesDumpCmd = &cobra.Command{
Expand Down Expand Up @@ -75,6 +78,11 @@ will then convert it to the --format requested in the current invocation.
if convertFile != "" {
return errors.Errorf("input file is already in raw format")
}
err := createYAML(ctx)
if err != nil {
return err
}

// Special case, we don't go through the text output code.
case tsDumpCSV:
w = csvTSWriter{w: csv.NewWriter(os.Stdout)}
Expand Down Expand Up @@ -202,6 +210,42 @@ type openMetricsWriter struct {
labels map[string]string
}

// createYAML generates and writes tsdump.yaml to default /tmp or to a specified path
func createYAML(ctx context.Context) (resErr error) {
file, err := os.OpenFile(debugTimeSeriesDumpOpts.yaml, os.O_TRUNC|os.O_CREATE|os.O_WRONLY, 0666)
if err != nil {
return err
}
defer file.Close()

sqlConn, err := makeSQLClient(ctx, "cockroach tsdump", useSystemDb)
if err != nil {
return err
}
defer func() { resErr = errors.CombineErrors(resErr, sqlConn.Close()) }()

_, rows, err := sqlExecCtx.RunQuery(
ctx,
sqlConn,
clisqlclient.MakeQuery(`SELECT store_id || ': ' || node_id FROM crdb_internal.kv_store_status`), false)

if err != nil {
return err
}

var strStoreNodeID string
for _, row := range rows {
storeNodeID := row
strStoreNodeID = strings.Join(storeNodeID, " ")
strStoreNodeID += "\n"
_, err := file.WriteString(strStoreNodeID)
if err != nil {
return err
}
}
return nil
}

func makeOpenMetricsWriter(out io.Writer) *openMetricsWriter {
// construct labels
labelMap := make(map[string]string)
Expand Down