Skip to content

Commit 22c1ce5

Browse files
authored
Merge pull request grafana/phlare#301 from grafana/20221010_merge-into-profilecli
Merge firecli and parquet-tool into profilecli
2 parents 78fbeda + b270e1e commit 22c1ce5

File tree

5 files changed

+47
-16
lines changed

5 files changed

+47
-16
lines changed

Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ go/deps:
6767
go/bin:
6868
mkdir -p ./bin
6969
CGO_ENABLED=0 $(GO) build $(GO_FLAGS) -o bin/ ./cmd/fire
70+
CGO_ENABLED=0 $(GO) build $(GO_FLAGS) -o bin/ ./cmd/profilecli
7071

7172
.PHONY: go/lint
7273
go/lint: $(BIN)/golangci-lint

cmd/fire/Dockerfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ FROM alpine:3.16.2
1515
RUN apk add --no-cache ca-certificates
1616

1717
COPY --from=build /src/fire/bin/fire /usr/bin/fire
18+
COPY --from=build /src/fire/bin/profilecli /usr/bin/profilecli
1819
COPY cmd/fire/fire.yaml /etc/fire/config.yaml
1920

2021
RUN addgroup -g 10001 -S fire && \

cmd/firetool/blocks.go renamed to cmd/profilecli/blocks.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ func blocksList(ctx context.Context) error {
3737
return err
3838
}
3939

40-
table := tablewriter.NewWriter(os.Stdout)
40+
table := tablewriter.NewWriter(output(ctx))
4141
table.SetHeader([]string{"Block ID", "MinTime", "MaxTime", "Duration", "Index", "Profiles", "Stacktraces", "Locations", "Functions", "Strings"})
4242
for _, blockInfo := range metas {
4343
table.Append([]string{

cmd/firetool/main.go renamed to cmd/profilecli/main.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package main
33
import (
44
"context"
55
"fmt"
6+
"io"
67
"os"
78
"path/filepath"
89

@@ -29,6 +30,7 @@ var (
2930

3031
func main() {
3132
ctx := firecontext.WithLogger(context.Background(), logger)
33+
ctx = withOutput(ctx, os.Stdout)
3234

3335
app := kingpin.New(filepath.Base(os.Args[0]), "Tooling for Grafana Fire, the continuous profiling aggregation system.").UsageWriter(os.Stdout)
3436
app.Version(version.Print("firetool"))
@@ -41,6 +43,10 @@ func main() {
4143
blocksListCmd := blocksCmd.Command("list", "List blocks.")
4244
blocksListCmd.Flag("restore-missing-meta", "").Default("false").BoolVar(&cfg.blocks.restoreMissingMeta)
4345

46+
parquetCmd := app.Command("parquet", "Operate on a Parquet file.")
47+
parquetInspectCmd := parquetCmd.Command("inspect", "Inspect a parquet file's structure.")
48+
parquetInspectFiles := parquetInspectCmd.Arg("file", "parquet file path").Required().ExistingFiles()
49+
4450
parsedCmd := kingpin.MustParse(app.Parse(os.Args[1:]))
4551

4652
if !cfg.verbose {
@@ -50,6 +56,12 @@ func main() {
5056
switch parsedCmd {
5157
case blocksListCmd.FullCommand():
5258
os.Exit(checkError(blocksList(ctx)))
59+
case parquetInspectCmd.FullCommand():
60+
for _, file := range *parquetInspectFiles {
61+
if err := parquetInspect(ctx, file); err != nil {
62+
os.Exit(checkError(err))
63+
}
64+
}
5365
}
5466
}
5567

@@ -60,3 +72,20 @@ func checkError(err error) int {
6072
}
6173
return 0
6274
}
75+
76+
type contextKey uint8
77+
78+
const (
79+
contextKeyOutput contextKey = iota
80+
)
81+
82+
func withOutput(ctx context.Context, w io.Writer) context.Context {
83+
return context.WithValue(ctx, contextKeyOutput, w)
84+
}
85+
86+
func output(ctx context.Context) io.Writer {
87+
if w, ok := ctx.Value(contextKeyOutput).(io.Writer); ok {
88+
return w
89+
}
90+
return os.Stdout
91+
}

cmd/parquet-tool/main.go renamed to cmd/profilecli/parquet.go

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package main
22

33
import (
4+
"context"
45
"fmt"
56
"os"
67
"strings"
@@ -10,33 +11,30 @@ import (
1011
"github.com/segmentio/parquet-go"
1112
)
1213

13-
func main() {
14-
if len(os.Args) < 2 {
15-
fmt.Println("Usage: parquet-tool <file>")
16-
os.Exit(1)
17-
}
18-
f, err := os.Open(os.Args[1])
14+
func parquetInspect(ctx context.Context, path string) error {
15+
f, err := os.Open(path)
1916
if err != nil {
20-
panic(err)
17+
return err
2118
}
2219
defer f.Close()
2320
stats, err := f.Stat()
2421
if err != nil {
25-
panic(err)
22+
return err
2623
}
2724
pf, err := parquet.OpenFile(f, stats.Size())
2825
if err != nil {
29-
panic(err)
26+
return err
3027
}
31-
fmt.Println("schema:", pf.Schema())
28+
out := output(ctx)
29+
fmt.Fprintln(out, "schema:", pf.Schema())
3230
meta := pf.Metadata()
3331
fmt.Println("Num Rows:", meta.NumRows)
3432
for i, rg := range meta.RowGroups {
35-
fmt.Println("\t Row group:", i)
36-
fmt.Println("\t\t Row Count:", rg.NumRows)
37-
fmt.Println("\t\t Row size:", humanize.Bytes(uint64(rg.TotalByteSize)))
38-
fmt.Println("\t\t Columns:")
39-
table := tablewriter.NewWriter(os.Stdout)
33+
fmt.Fprintln(out, "\t Row group:", i)
34+
fmt.Fprintln(out, "\t\t Row Count:", rg.NumRows)
35+
fmt.Fprintln(out, "\t\t Row size:", humanize.Bytes(uint64(rg.TotalByteSize)))
36+
fmt.Fprintln(out, "\t\t Columns:")
37+
table := tablewriter.NewWriter(out)
4038
table.SetHeader([]string{
4139
"Col", "Type", "NumVal", "TotalCompressedSize", "TotalUncompressedSize", "Compression", "%", "PageCount", "AvgPageSize",
4240
})
@@ -63,4 +61,6 @@ func main() {
6361
}
6462
table.Render()
6563
}
64+
65+
return nil
6666
}

0 commit comments

Comments
 (0)