-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
73 lines (61 loc) · 1.96 KB
/
main.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
package main
import (
"context"
"os"
"path/filepath"
"time"
"github.com/go-kit/log"
"github.com/go-kit/log/level"
"github.com/pkg/errors"
"github.com/prometheus/prometheus/tsdb"
"github.com/prometheus/prometheus/tsdb/chunkenc"
"github.com/thanos-io/thanos/pkg/block/metadata"
"github.com/thanos-io/thanos/pkg/compact/downsample"
"gopkg.in/alecthomas/kingpin.v2"
)
const defaultDBPath = "data/"
var (
blockID string
dbPath string
deleteSource bool
)
func main() {
logger := log.NewLogfmtLogger(log.NewSyncWriter(os.Stderr))
if err := run(logger); err != nil {
level.Error(logger).Log("msg", "failed to run downsample", "error", err)
os.Exit(1)
}
}
func run(logger log.Logger) error {
app := kingpin.New(filepath.Base(os.Args[0]), "A tool to downsample Prometheus or Thanos TSDB block")
app.HelpFlag.Short('h')
app.Flag("id", "Block ID to downsample").StringVar(&blockID)
app.Flag("delete-source-block", "Whether to delete source block or not after downsampling. Default is false.").Default("false").BoolVar(&deleteSource)
app.Arg("db path", "Database path (default is "+defaultDBPath+").").Default(defaultDBPath).StringVar(&dbPath)
kingpin.MustParse(app.Parse(os.Args[1:]))
blockDir := filepath.Join(dbPath, blockID)
meta, err := metadata.ReadFromDir(blockDir)
if err != nil {
return err
}
ctx := context.Background()
pool := chunkenc.NewPool()
block, err := tsdb.OpenBlock(logger, blockDir, pool)
if err != nil {
return err
}
begin := time.Now()
id, err := downsample.Downsample(ctx, logger, meta, block, dbPath, downsample.ResLevel1)
if err != nil {
return err
}
downsampleDuration := time.Since(begin)
level.Info(logger).Log("msg", "downsampled block",
"from", blockID, "to", id, "duration", downsampleDuration, "duration_ms", downsampleDuration.Milliseconds())
if deleteSource {
if err := os.RemoveAll(blockDir); err != nil {
return errors.Wrapf(err, "delete source block after relabeling:%s", id)
}
}
return nil
}