Skip to content

Commit

Permalink
add cmd/walreader
Browse files Browse the repository at this point in the history
  • Loading branch information
simonpasquier committed Mar 19, 2021
1 parent ae7ea8b commit 15c8f9d
Showing 1 changed file with 102 additions and 0 deletions.
102 changes: 102 additions & 0 deletions cmd/walreader/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
package main

import (
"flag"
"fmt"
"io"
"os"

"github.com/prometheus/prometheus/pkg/labels"
"github.com/prometheus/prometheus/tsdb/record"
"github.com/prometheus/prometheus/tsdb/wal"
)

var (
path string
matchers string
help bool
)

func init() {
flag.BoolVar(&help, "help", false, "Show help")
flag.StringVar(&matchers, "matchers", "{__name__=~\".+\"", "Label matchers")
}

func main() {
flag.Parse()

if help {
fmt.Fprintln(os.Stderr, "Analyzes a WAL directory or file")
flag.PrintDefaults()
return
}

args := flag.Args()
if len(args) != 1 {
fmt.Fprintln(os.Stderr, "expecting one argument")
os.Exit(1)
}

path := args[0]

fi, err := os.Stat(path)
if err != nil {
panic(err)
}

var rc io.ReadCloser
if fi.IsDir() {
rc, err = wal.NewSegmentsReader(path)
} else {
rc, err = wal.OpenReadSegment(path)
}

if err != nil {
panic(err)
}

r := wal.NewReader(rc)

var (
dec record.Decoder
series []record.RefSeries
samples []record.RefSample
lbls = make(map[uint64]labels.Labels)
)

for r.Next() {
rec := r.Record()
switch dec.Type(rec) {
case record.Series:
series, err = dec.Series(rec, series)
if err != nil {
fmt.Printf("error while decoding series: %v\n", err)
break
}
for _, s := range series {
lbls[s.Ref] = s.Labels
//fmt.Printf("series 0x%X (%d): %v\n", s.Ref, s.Ref, s.Labels.String())
}
series = series[:0]
case record.Samples:
samples, err = dec.Samples(rec, samples)
if err != nil {
fmt.Printf("error while decoding samples: %v\n", err)
break
}
for _, s := range samples {
lbl, found := lbls[s.Ref]
if !found {
continue
}
fmt.Printf("%s (ref: 0x%X): %f@%d\n", lbl.String(), s.Ref, s.V, s.T)
}
samples = samples[:0]

}
}

if r.Err() != nil {
fmt.Printf("error while reading WAL: %v\n", r.Err())
}
}

0 comments on commit 15c8f9d

Please sign in to comment.