From 92d04149938994dfe0ab30c06cd5fba7c5875291 Mon Sep 17 00:00:00 2001 From: Krasi Georgiev Date: Sat, 9 Sep 2017 14:11:12 +0300 Subject: [PATCH 1/2] replaced cobra with kingpin Signed-off-by: Krasi Georgiev --- cmd/tsdb/README.md | 3 ++ cmd/tsdb/main.go | 68 ++++++++++++++++------------------------------ 2 files changed, 26 insertions(+), 45 deletions(-) create mode 100644 cmd/tsdb/README.md diff --git a/cmd/tsdb/README.md b/cmd/tsdb/README.md new file mode 100644 index 000000000..61fce5f9e --- /dev/null +++ b/cmd/tsdb/README.md @@ -0,0 +1,3 @@ +TODO: +- [ ] add tabular output +- [ ] break commands in separate files diff --git a/cmd/tsdb/main.go b/cmd/tsdb/main.go index 491c2ac35..26c9658c4 100644 --- a/cmd/tsdb/main.go +++ b/cmd/tsdb/main.go @@ -18,8 +18,6 @@ import ( "fmt" "io" "io/ioutil" - "net/http" - _ "net/http/pprof" "os" "path/filepath" "runtime" @@ -33,41 +31,36 @@ import ( "github.com/prometheus/prometheus/pkg/textparse" "github.com/prometheus/tsdb" "github.com/prometheus/tsdb/labels" - "github.com/spf13/cobra" + "gopkg.in/alecthomas/kingpin.v2" ) func main() { - // Start HTTP server for pprof endpoint. - go http.ListenAndServe(":9999", nil) - - root := &cobra.Command{ - Use: "tsdb", - Short: "CLI tool for tsdb", - } - - root.AddCommand( - NewBenchCommand(), + var ( + cli = kingpin.New(filepath.Base(os.Args[0]), "CLI tool for tsdb") + benchCmd = cli.Command("bench", "run benchmarks") + benchWriteCmd = benchCmd.Command("write", "run a write performance benchmark") + benchWriteOutPath = benchWriteCmd.Flag("out", "set the output path").Default("benchout/").String() + benchWriteNumMetrics = benchWriteCmd.Flag("metrics", "number of metrics to read").Default("10000").Int() + benchSamplesFile = benchWriteCmd.Arg("file", "input file with samples data, default is (../../testdata/20k.series)").Default("../../testdata/20k.series").String() ) - flag.CommandLine.Set("log.level", "debug") - - root.Execute() -} - -func NewBenchCommand() *cobra.Command { - c := &cobra.Command{ - Use: "bench", - Short: "run benchmarks", + switch kingpin.MustParse(cli.Parse(os.Args[1:])) { + case benchWriteCmd.FullCommand(): + wb := &writeBenchmark{ + outPath: *benchWriteOutPath, + numMetrics: *benchWriteNumMetrics, + samplesFile: *benchSamplesFile, + } + wb.run() } - c.AddCommand(NewBenchWriteCommand()) - - return c + flag.CommandLine.Set("log.level", "debug") } type writeBenchmark struct { - outPath string - cleanup bool - numMetrics int + outPath string + samplesFile string + cleanup bool + numMetrics int storage *tsdb.DB @@ -77,22 +70,7 @@ type writeBenchmark struct { mtxprof *os.File } -func NewBenchWriteCommand() *cobra.Command { - var wb writeBenchmark - c := &cobra.Command{ - Use: "write ", - Short: "run a write performance benchmark", - Run: wb.run, - } - c.PersistentFlags().StringVar(&wb.outPath, "out", "benchout/", "set the output path") - c.PersistentFlags().IntVar(&wb.numMetrics, "metrics", 10000, "number of metrics to read") - return c -} - -func (b *writeBenchmark) run(cmd *cobra.Command, args []string) { - if len(args) != 1 { - exitWithError(fmt.Errorf("missing file argument")) - } +func (b *writeBenchmark) run() { if b.outPath == "" { dir, err := ioutil.TempDir("", "tsdb_bench") if err != nil { @@ -123,7 +101,7 @@ func (b *writeBenchmark) run(cmd *cobra.Command, args []string) { var metrics []labels.Labels measureTime("readData", func() { - f, err := os.Open(args[0]) + f, err := os.Open(b.samplesFile) if err != nil { exitWithError(err) } From 8919baef037dbad69ec719334b68dbc186ba26c5 Mon Sep 17 00:00:00 2001 From: Goutham Veeramachaneni Date: Wed, 13 Sep 2017 13:47:20 +0530 Subject: [PATCH 2/2] Expose NewIndexReader() and cleanups Signed-off-by: Goutham Veeramachaneni --- block.go | 5 ----- db.go | 3 +-- index.go | 3 +++ 3 files changed, 4 insertions(+), 7 deletions(-) diff --git a/block.go b/block.go index 67cd57491..232e64e67 100644 --- a/block.go +++ b/block.go @@ -64,11 +64,6 @@ type Appendable interface { Appender() Appender } -// Queryable defines an entity which provides a Querier. -type Queryable interface { - Querier(mint, maxt int64) Querier -} - // BlockMeta provides meta information about a block. type BlockMeta struct { // Unique identifier for the block and its contents. Changes on compaction. diff --git a/db.go b/db.go index c9745cfc6..87b5ed253 100644 --- a/db.go +++ b/db.go @@ -165,8 +165,7 @@ func Open(dir string, l log.Logger, r prometheus.Registerer, opts *Options) (db return nil, err } if l == nil { - l = log.NewLogfmtLogger(os.Stdout) - l = log.With(l, "ts", log.DefaultTimestampUTC, "caller", log.DefaultCaller) + l = log.NewNopLogger() } if opts == nil { opts = DefaultOptions diff --git a/index.go b/index.go index fd9b25162..3cdaad74d 100644 --- a/index.go +++ b/index.go @@ -570,6 +570,9 @@ var ( errInvalidFlag = fmt.Errorf("invalid flag") ) +// NewIndexReader returns a new IndexReader on the given directory. +func NewIndexReader(dir string) (IndexReader, error) { return newIndexReader(dir) } + // newIndexReader returns a new indexReader on the given directory. func newIndexReader(dir string) (*indexReader, error) { f, err := openMmapFile(filepath.Join(dir, "index"))