-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ae7ea8b
commit 15c8f9d
Showing
1 changed file
with
102 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) | ||
} | ||
} |