Skip to content

Commit 7644054

Browse files
committed
Add firetool CLI
This allows to list local blocks
1 parent ceed2d4 commit 7644054

File tree

2 files changed

+106
-0
lines changed

2 files changed

+106
-0
lines changed

cmd/firetool/blocks.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package main
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"os"
7+
"time"
8+
9+
"github.com/dustin/go-humanize"
10+
"github.com/olekukonko/tablewriter"
11+
"github.com/thanos-io/objstore/providers/filesystem"
12+
13+
"github.com/grafana/fire/pkg/firedb"
14+
)
15+
16+
func tableInfo(info firedb.TableInfo) string {
17+
return fmt.Sprintf("%d (%s in %d RGs)", info.Rows, humanize.Bytes(info.Bytes), info.RowGroups)
18+
}
19+
20+
func blocksList(_ context.Context) error {
21+
bucket, err := filesystem.NewBucket(cfg.blocks.path)
22+
if err != nil {
23+
return err
24+
}
25+
26+
q := firedb.NewBlockQuerier(logger, bucket)
27+
if err := q.Open(); err != nil {
28+
return err
29+
}
30+
31+
table := tablewriter.NewWriter(os.Stdout)
32+
table.SetHeader([]string{"Block ID", "MinTime", "MaxTime", "Duration", "Profiles", "Stacktraces", "Locations", "Functions", "Strings"})
33+
for _, blockInfo := range q.BlockInfo() {
34+
table.Append([]string{
35+
blockInfo.ID.String(),
36+
blockInfo.MinTime.Time().Format(time.RFC3339),
37+
blockInfo.MaxTime.Time().Format(time.RFC3339),
38+
blockInfo.MaxTime.Time().Sub(blockInfo.MinTime.Time()).String(),
39+
tableInfo(blockInfo.Profiles),
40+
tableInfo(blockInfo.Stacktraces),
41+
tableInfo(blockInfo.Locations),
42+
tableInfo(blockInfo.Functions),
43+
tableInfo(blockInfo.Strings),
44+
})
45+
}
46+
table.Render()
47+
48+
return nil
49+
}

cmd/firetool/main.go

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package main
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"os"
7+
"path/filepath"
8+
9+
"github.com/go-kit/log"
10+
"github.com/go-kit/log/level"
11+
"github.com/prometheus/common/version"
12+
"gopkg.in/alecthomas/kingpin.v2"
13+
)
14+
15+
var cfg struct {
16+
verbose bool
17+
blocks struct {
18+
path string
19+
}
20+
}
21+
22+
var (
23+
consoleOutput = os.Stderr
24+
logger = log.NewLogfmtLogger(consoleOutput)
25+
)
26+
27+
func main() {
28+
ctx := context.Background()
29+
app := kingpin.New(filepath.Base(os.Args[0]), "Tooling for Grafana Fire, the continuous profiling aggregation system.").UsageWriter(os.Stdout)
30+
app.Version(version.Print("firetool"))
31+
app.HelpFlag.Short('h')
32+
app.Flag("verbose", "Enable verbose logging.").Short('v').Default("0").BoolVar(&cfg.verbose)
33+
34+
blocksCmd := app.Command("blocks", "Operate on Grafana Fire's blocks.")
35+
blocksCmd.Flag("path", "Path to blocks directory").Default("./data/head").StringVar(&cfg.blocks.path)
36+
37+
blocksListCmd := blocksCmd.Command("list", "List blocks.")
38+
39+
parsedCmd := kingpin.MustParse(app.Parse(os.Args[1:]))
40+
41+
if !cfg.verbose {
42+
logger = level.NewFilter(logger, level.AllowWarn())
43+
}
44+
45+
switch parsedCmd {
46+
case blocksListCmd.FullCommand():
47+
os.Exit(checkError(blocksList(ctx)))
48+
}
49+
}
50+
51+
func checkError(err error) int {
52+
if err != nil {
53+
fmt.Fprintf(os.Stderr, "error: %v\n", err)
54+
return 1
55+
}
56+
return 0
57+
}

0 commit comments

Comments
 (0)