Skip to content

Commit

Permalink
Add profilecli command to dump the tsdb index (#3189)
Browse files Browse the repository at this point in the history
This is useful connecting series to the profiles.parquet.

```
❯ go run ./cmd/profilecli admin tsdb series ./my-block/index.tsdb
{"SeriesRef":247850,"SeriesIndex":16992,"Labels":{"__name__":"process_cpu","__period_type__":"cpu","__period_unit__":"nanoseconds","__profile_type__":"process_cpu:cpu:nanoseconds:cpu:nanoseconds", ...}}
[...]
```
  • Loading branch information
simonswine authored Apr 12, 2024
1 parent 032c28b commit 14bcf58
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 0 deletions.
10 changes: 10 additions & 0 deletions cmd/profilecli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@ func main() {
parquetInspectCmd := parquetCmd.Command("inspect", "Inspect a parquet file's structure.")
parquetInspectFiles := parquetInspectCmd.Arg("file", "parquet file path").Required().ExistingFiles()

tsdbCmd := adminCmd.Command("tsdb", "Operate on a TSDB index file.")
tsdbSeriesCmd := tsdbCmd.Command("series", "dump series in an TSDB index file.")
tsdbSeriesFiles := tsdbSeriesCmd.Arg("file", "tsdb file path").Required().ExistingFiles()

queryCmd := app.Command("query", "Query profile store.")
queryMergeCmd := queryCmd.Command("merge", "Request merged profile.")
queryMergeOutput := queryMergeCmd.Flag("output", "How to output the result, examples: console, raw, pprof=./my.pprof").Default("console").String()
Expand Down Expand Up @@ -97,6 +101,12 @@ func main() {
os.Exit(checkError(err))
}
}
case tsdbSeriesCmd.FullCommand():
for _, file := range *tsdbSeriesFiles {
if err := tsdbSeries(ctx, file); err != nil {
os.Exit(checkError(err))
}
}
case queryMergeCmd.FullCommand():
if err := queryMerge(ctx, queryMergeParams, *queryMergeOutput); err != nil {
os.Exit(checkError(err))
Expand Down
64 changes: 64 additions & 0 deletions cmd/profilecli/tsdb.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package main

import (
"context"
"encoding/json"
"fmt"

"github.com/prometheus/prometheus/model/labels"

phlaremodel "github.com/grafana/pyroscope/pkg/model"
"github.com/grafana/pyroscope/pkg/phlaredb"
"github.com/grafana/pyroscope/pkg/phlaredb/tsdb/index"
)

func tsdbSeries(ctx context.Context, path string) error {
r, err := index.NewFileReader(path)
if err != nil {
return err
}

it, err := phlaredb.PostingsForMatchers(r, nil, labels.MustNewMatcher(labels.MatchNotEqual, "__name__", ""))
if err != nil {
return err
}

var (
lbls phlaremodel.Labels
chunkMeta []index.ChunkMeta
)
line := struct {
SeriesRef uint64
SeriesIndex *uint32
Labels json.RawMessage
}{}
enc := json.NewEncoder(output(ctx))

for it.Next() {
if ctx.Err() != nil {
return ctx.Err()
}

_, err = r.Series(it.At(), &lbls, &chunkMeta)
if err != nil {
return fmt.Errorf("error retrieving seriesRef: %w", err)
}

line.Labels, err = lbls.ToPrometheusLabels().MarshalJSON()
if err != nil {
return fmt.Errorf("error marshalling labels: %w", err)
}

if len(chunkMeta) > 0 {
line.SeriesIndex = &chunkMeta[0].SeriesIndex
} else {
line.SeriesIndex = nil
}
line.SeriesRef = uint64(it.At())
if err := enc.Encode(&line); err != nil {
return fmt.Errorf("error writing line: %w", err)
}
}

return nil
}

0 comments on commit 14bcf58

Please sign in to comment.