-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
tsdump.go
207 lines (184 loc) · 4.83 KB
/
tsdump.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
// Copyright 2020 The Cockroach Authors.
//
// Use of this software is governed by the Business Source License
// included in the file licenses/BSL.txt.
//
// As of the Change Date specified in that file, in accordance with
// the Business Source License, use of this software will be governed
// by the Apache License, Version 2.0, included in the file
// licenses/APL.txt.
package cli
import (
"context"
"encoding/csv"
"fmt"
"io"
"os"
"time"
"github.com/cockroachdb/cockroach/pkg/cli/clierrorplus"
"github.com/cockroachdb/cockroach/pkg/ts"
"github.com/cockroachdb/cockroach/pkg/ts/tspb"
"github.com/cockroachdb/cockroach/pkg/util/timeutil"
"github.com/cockroachdb/errors"
"github.com/spf13/cobra"
)
// TODO(knz): this struct belongs elsewhere.
// See: https://github.com/cockroachdb/cockroach/issues/49509
var debugTimeSeriesDumpOpts = struct {
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. 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:
// Special case, we don't go through the text output code.
conn, _, finish, err := getClientGRPCConn(ctx, serverCfg)
if err != nil {
return err
}
defer finish()
tsClient := tspb.NewTimeSeriesClient(conn)
stream, err := tsClient.DumpRaw(context.Background(), req)
if err != nil {
return err
}
if err := ts.DumpRawTo(stream, os.Stdout); err != nil {
return err
}
// 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.
_ = os.Stdout.Sync()
return nil
case tsDumpCSV:
w = csvTSWriter{w: csv.NewWriter(os.Stdout)}
case tsDumpTSV:
cw := csvTSWriter{w: csv.NewWriter(os.Stdout)}
cw.w.Comma = '\t'
w = cw
case tsDumpText:
w = defaultTSWriter{w: os.Stdout}
default:
return errors.Newf("unknown output format: %v", debugTimeSeriesDumpOpts.format)
}
conn, _, finish, err := getClientGRPCConn(ctx, serverCfg)
if err != nil {
return err
}
defer finish()
tsClient := tspb.NewTimeSeriesClient(conn)
stream, err := tsClient.Dump(context.Background(), req)
if err != nil {
return err
}
for {
data, err := stream.Recv()
if err == io.EOF {
return w.Flush()
}
if err != nil {
return err
}
if err := w.Emit(data); err != nil {
return err
}
}
}),
}
type tsWriter interface {
Emit(*tspb.TimeSeriesData) error
Flush() error
}
type csvTSWriter struct {
w *csv.Writer
}
func (w csvTSWriter) Emit(data *tspb.TimeSeriesData) error {
for _, d := range data.Datapoints {
if err := w.w.Write(
[]string{data.Name, timeutil.Unix(0, d.TimestampNanos).In(time.UTC).Format(time.RFC3339), data.Source, fmt.Sprint(d.Value)},
); err != nil {
return err
}
}
return nil
}
func (w csvTSWriter) Flush() error {
w.w.Flush()
return w.w.Error()
}
type defaultTSWriter struct {
last struct {
name, source string
}
w io.Writer
}
func (w defaultTSWriter) Flush() error { return nil }
func (w defaultTSWriter) Emit(data *tspb.TimeSeriesData) error {
if w.last.name != data.Name || w.last.source != data.Source {
w.last.name, w.last.source = data.Name, data.Source
fmt.Fprintf(w.w, "%s %s\n", data.Name, data.Source)
}
for _, d := range data.Datapoints {
fmt.Fprintf(w.w, "%v %v\n", d.TimestampNanos, d.Value)
}
return nil
}
type tsDumpFormat int
const (
tsDumpText tsDumpFormat = iota
tsDumpCSV
tsDumpTSV
tsDumpRaw
)
// Type implements the pflag.Value interface.
func (m *tsDumpFormat) Type() string { return "string" }
// String implements the pflag.Value interface.
func (m *tsDumpFormat) String() string {
switch *m {
case tsDumpCSV:
return "csv"
case tsDumpTSV:
return "tsv"
case tsDumpText:
return "text"
case tsDumpRaw:
return "raw"
}
return ""
}
// Set implements the pflag.Value interface.
func (m *tsDumpFormat) Set(s string) error {
switch s {
case "text":
*m = tsDumpText
case "csv":
*m = tsDumpCSV
case "tsv":
*m = tsDumpTSV
case "raw":
*m = tsDumpRaw
default:
return fmt.Errorf("invalid value for --format: %s", s)
}
return nil
}