-
Notifications
You must be signed in to change notification settings - Fork 10
/
main.go
132 lines (111 loc) · 3.42 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
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package main
import (
"encoding/json"
"flag"
"fmt"
"log"
"math"
"os"
"strings"
"github.com/ryotarai/prometheus-tsdb-dump/pkg/writer"
gokitlog "github.com/go-kit/kit/log"
"github.com/pkg/errors"
"github.com/prometheus/prometheus/pkg/labels"
"github.com/prometheus/prometheus/tsdb"
"github.com/prometheus/prometheus/tsdb/chunkenc"
"github.com/prometheus/prometheus/tsdb/chunks"
)
func main() {
blockPath := flag.String("block", "", "Path to block directory")
labelKey := flag.String("label-key", "", "")
labelValue := flag.String("label-value", "", "")
externalLabels := flag.String("external-labels", "{}", "Labels to be added to dumped result in JSON")
minTimestamp := flag.Int64("min-timestamp", 0, "min of timestamp of datapoints to be dumped; unix time in msec")
maxTimestamp := flag.Int64("max-timestamp", math.MaxInt64, "min of timestamp of datapoints to be dumped; unix time in msec")
format := flag.String("format", "victoriametrics", "")
flag.Parse()
if *blockPath == "" {
log.Fatal("-block argument is required")
}
if err := run(*blockPath, *labelKey, *labelValue, *format, *minTimestamp, *maxTimestamp, *externalLabels); err != nil {
log.Fatalf("error: %s", err)
}
}
func run(blockPath string, labelKey string, labelValue string, outFormat string, minTimestamp int64, maxTimestamp int64, externalLabelsJSON string) error {
externalLabelsMap := map[string]string{}
if err := json.NewDecoder(strings.NewReader(externalLabelsJSON)).Decode(&externalLabelsMap); err != nil {
return errors.Wrap(err, "decode external labels")
}
var externalLabels labels.Labels
for k, v := range externalLabelsMap {
externalLabels = append(externalLabels, labels.Label{Name: k, Value: v})
}
wr, err := writer.NewWriter(outFormat)
logger := gokitlog.NewLogfmtLogger(os.Stderr)
block, err := tsdb.OpenBlock(logger, blockPath, chunkenc.NewPool())
if err != nil {
return errors.Wrap(err, "tsdb.OpenBlock")
}
indexr, err := block.Index()
if err != nil {
return errors.Wrap(err, "block.Index")
}
defer indexr.Close()
chunkr, err := block.Chunks()
if err != nil {
return errors.Wrap(err, "block.Chunks")
}
defer chunkr.Close()
postings, err := indexr.Postings(labelKey, labelValue)
if err != nil {
return errors.Wrap(err, "indexr.Postings")
}
var it chunkenc.Iterator
for postings.Next() {
ref := postings.At()
lset := labels.Labels{}
chks := []chunks.Meta{}
if err := indexr.Series(ref, &lset, &chks); err != nil {
return errors.Wrap(err, "indexr.Series")
}
if len(externalLabels) > 0 {
lset = append(lset, externalLabels...)
}
for _, meta := range chks {
chunk, err := chunkr.Chunk(meta.Ref)
if err != nil {
return errors.Wrap(err, "chunkr.Chunk")
}
var timestamps []int64
var values []float64
it := chunk.Iterator(it)
for it.Next() {
t, v := it.At()
if math.IsNaN(v) {
continue
}
if math.IsInf(v, -1) || math.IsInf(v, 1) {
continue
}
if t < minTimestamp || maxTimestamp < t {
continue
}
timestamps = append(timestamps, t)
values = append(values, v)
}
if it.Err() != nil {
return errors.Wrap(err, "iterator.Err")
}
if len(timestamps) == 0 {
continue
}
if err := wr.Write(&lset, timestamps, values); err != nil {
return errors.Wrap(err, fmt.Sprintf("Writer.Write(%v, %v, %v)", lset, timestamps, values))
}
}
}
if postings.Err() != nil {
return errors.Wrap(err, "postings.Err")
}
return nil
}