Skip to content

Commit

Permalink
cli: add --{from,to} to tsdump command
Browse files Browse the repository at this point in the history
This allows restricting which range of datapoints is pulled by
`./cockroach debug tsdump` via the `--from` and `--to` flags.

This command also touches up the `tsdump` command a little bit.

Release justification: low-risk observability change
Release note (cli change): The `debug tsdump` command now accepts
`--from` and `--to` flags that limit for which dates timeseries
are exported.
  • Loading branch information
tbg committed Aug 28, 2021
1 parent 2a16eed commit 640e6b2
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 6 deletions.
2 changes: 2 additions & 0 deletions pkg/cli/debug.go
Original file line number Diff line number Diff line change
Expand Up @@ -1565,6 +1565,8 @@ func init() {

f = debugTimeSeriesDumpCmd.Flags()
f.Var(&debugTimeSeriesDumpOpts.format, "format", "output format (text, csv, tsv, raw)")
f.Var(&debugTimeSeriesDumpOpts.from, "from", "oldest timestamp to include (inclusive)")
f.Var(&debugTimeSeriesDumpOpts.to, "to", "newest timestamp to include (inclusive)")
}

func initPebbleCmds(cmd *cobra.Command) {
Expand Down
27 changes: 21 additions & 6 deletions pkg/cli/tsdump.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,21 +29,31 @@ import (
// TODO(knz): this struct belongs elsewhere.
// See: https://github.com/cockroachdb/cockroach/issues/49509
var debugTimeSeriesDumpOpts = struct {
format tsDumpFormat
format tsDumpFormat
from, to timestampValue
}{
format: tsDumpText,
from: timestampValue{},
to: timestampValue(timeutil.Now().Add(24 * time.Hour)),
}

var debugTimeSeriesDumpCmd = &cobra.Command{
Use: "tsdump",
Short: "dump all the raw timeseries values in a cluster",
Long: `
Dumps all of the raw timeseries values in a cluster.
Dumps all of the raw timeseries values in a cluster. Only the default resolution
is retrieved, i.e. typically datapoints older than the value of the
'timeseries.storage.resolution_10s.ttl' cluster setting will be absent from the
output.
`,
RunE: clierrorplus.MaybeDecorateError(func(cmd *cobra.Command, args []string) error {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()

req := &tspb.DumpRequest{
StartNanos: time.Time(debugTimeSeriesDumpOpts.from).UnixNano(),
EndNanos: time.Time(debugTimeSeriesDumpOpts.to).UnixNano(),
}
var w tsWriter
switch debugTimeSeriesDumpOpts.format {
case tsDumpRaw:
Expand All @@ -55,16 +65,21 @@ Dumps all of the raw timeseries values in a cluster.
defer finish()

tsClient := tspb.NewTimeSeriesClient(conn)
stream, err := tsClient.DumpRaw(context.Background(), &tspb.DumpRequest{})
stream, err := tsClient.DumpRaw(context.Background(), req)
if err != nil {
return err
}

if err := ts.DumpRawTo(stream, os.Stdout); err != nil {
return err
}
return os.Stdout.Sync()

// Stdout does not support Sync in all situations, for example when piping
// out from roachprod via `roachprod ssh foo:1 -- [...] tsdump > foo.bar`,
// so ignore the error. See:
//
// https://github.com/uber-go/zap/issues/328#issuecomment-284337436
_ = os.Stdout.Sync()
return nil
case tsDumpCSV:
w = csvTSWriter{w: csv.NewWriter(os.Stdout)}
case tsDumpTSV:
Expand All @@ -84,7 +99,7 @@ Dumps all of the raw timeseries values in a cluster.
defer finish()

tsClient := tspb.NewTimeSeriesClient(conn)
stream, err := tsClient.Dump(context.Background(), &tspb.DumpRequest{})
stream, err := tsClient.Dump(context.Background(), req)
if err != nil {
return err
}
Expand Down

0 comments on commit 640e6b2

Please sign in to comment.