-
Notifications
You must be signed in to change notification settings - Fork 3
/
dump.go
99 lines (91 loc) · 2.66 KB
/
dump.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package main
import (
"context"
"fmt"
"github.com/go-kit/log"
"github.com/go-kit/log/level"
"github.com/oklog/ulid"
"github.com/pkg/errors"
"github.com/prometheus/prometheus/promql/parser"
"github.com/prometheus/prometheus/tsdb"
"github.com/prometheus/prometheus/tsdb/chunkenc"
tsdb_errors "github.com/prometheus/prometheus/tsdb/errors"
"github.com/thanos-io/objstore"
"github.com/thanos-io/thanos/pkg/block"
"io"
"os"
"path/filepath"
"time"
)
func dump(bkt objstore.Bucket, out io.Writer, ids *[]string, dir *string, mint, maxt *int64, match *string, logger log.Logger) error {
ctx := context.Background()
for _, id := range *ids {
if _, err := ulid.Parse(id); err != nil {
return errors.Wrapf(err, `invalid ULID "%s"`, id)
}
if err := downloadBlock(ctx, *dir, id, bkt, logger); err != nil {
return err
}
}
os.Mkdir(filepath.Join(*dir, "wal"), 0777)
return dumpSamples(out, *dir, *mint, *maxt, *match)
}
// https://github.com/prometheus/prometheus/blob/main/cmd/promtool/tsdb.go#L703
func dumpSamples(out io.Writer, path string, mint, maxt int64, match string) (err error) {
db, err := tsdb.OpenDBReadOnly(path, nil)
if err != nil {
return err
}
defer func() {
err = tsdb_errors.NewMulti(err, db.Close()).Err()
}()
q, err := db.Querier(context.Background(), mint, maxt)
if err != nil {
return err
}
defer q.Close()
matchers, err := parser.ParseMetricSelector(match)
if err != nil {
return err
}
ss := q.Select(false, nil, matchers...)
for ss.Next() {
series := ss.At()
name := series.Labels().Get("__name__")
lbs := series.Labels().MatchLabels(false, "__name__")
// todo: add thanos labels?
it := series.Iterator(nil)
for it.Next() == chunkenc.ValFloat {
ts, val := it.At()
fmt.Fprintf(out, "%s%s %g %d\n", name, lbs, val, ts)
}
for it.Next() == chunkenc.ValFloatHistogram {
ts, fh := it.AtFloatHistogram()
fmt.Fprintf(out, "%s%s %s %d\n", name, lbs, fh.String(), ts)
}
for it.Next() == chunkenc.ValHistogram {
ts, h := it.AtHistogram()
fmt.Fprintf(out, "%s%s %s %d\n", name, lbs, h.String(), ts)
}
if it.Err() != nil {
return ss.Err()
}
}
if ws := ss.Warnings(); len(ws) > 0 {
return tsdb_errors.NewMulti(ws...).Err()
}
if ss.Err() != nil {
return ss.Err()
}
return nil
}
// download block id to dir
func downloadBlock(ctx context.Context, dir, id string, bkt objstore.Bucket, logger log.Logger) error {
dest := filepath.Join(dir, id)
begin := time.Now()
err := block.Download(ctx, logger, bkt, ulid.MustParse(id), dest)
if err != nil {
return errors.Wrapf(err, "download block %s", id)
}
return level.Info(logger).Log("msg", "downloaded block", "id", id, "duration", time.Since(begin))
}