-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprom2bq.go
135 lines (118 loc) · 3.06 KB
/
prom2bq.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
133
134
135
package main
import (
"context"
"errors"
"fmt"
"os"
"time"
"cloud.google.com/go/bigquery"
"github.com/prometheus/client_golang/api"
v1 "github.com/prometheus/client_golang/api/prometheus/v1"
"github.com/prometheus/common/model"
"google.golang.org/api/option"
)
const ChunkSize = 10000
type Item struct {
Time time.Time
Name string
Value float64
Labels []string
}
func NewAPI(address string) (v1.API, error) {
client, err := api.NewClient(api.Config{
Address: address,
})
if err != nil {
return nil, err
}
return v1.NewAPI(client), nil
}
func Query(ctx context.Context, api v1.API, metric string, start time.Time, end time.Time) (model.Matrix, error) {
seconds := int(end.Sub(start).Seconds())
if seconds < 0 {
return nil, errors.New("start > end")
}
value, warnings, err := api.Query(
ctx,
fmt.Sprintf("%s[%ds]", metric, seconds),
end,
)
if err != nil {
return nil, err
}
if warnings != nil {
return nil, errors.New(fmt.Sprintf("warnings: %+v", warnings))
}
return value.(model.Matrix), nil
}
func MsEpochToTime(ms int64) time.Time {
return time.Unix(ms/int64(1000), (ms%int64(1000))*int64(1000000))
}
func ConvertToItems(data model.Matrix) []*Item {
var items []*Item
for _, sampleStream := range data {
var labels []string
for k, v := range sampleStream.Metric {
if k == "__name__" {
continue
}
labels = append(labels, fmt.Sprintf("%s=\"%s\"", k, v))
}
metricName := string(sampleStream.Metric["__name__"])
for _, sample := range sampleStream.Values {
items = append(items,
&Item{
Time: MsEpochToTime(int64(sample.Timestamp)),
Name: metricName,
Labels: labels,
Value: float64(sample.Value),
},
)
}
}
return items
}
func CreateChunks(items []*Item, chunkSize int) (chunks [][]*Item) {
for chunkSize < len(items) {
items, chunks = items[chunkSize:], append(chunks, items[0:chunkSize:chunkSize])
}
return append(chunks, items)
}
func main() {
config, err := ParseOptions()
if err != nil {
fmt.Fprintf(os.Stderr, "error: %s\n", err)
os.Exit(1)
}
fmt.Printf("Configuration: %+v\n", config)
api, err := NewAPI(config.PrometheusAddress)
if err != nil {
fmt.Fprintf(os.Stderr, "error: %s\n", err)
os.Exit(1)
}
ctx := context.Background()
client, err := bigquery.NewClient(ctx, config.BigQueryProject, option.WithCredentialsFile(config.CredentialFile))
if err != nil {
fmt.Fprintf(os.Stderr, "error: %s\n", err)
os.Exit(1)
}
defer client.Close()
uploader := client.Dataset(config.BigQueryDataset).Table(config.BigQueryTable).Uploader()
for _, metric := range config.Metrics {
fmt.Printf("Processing %s from %s to %s\n", metric, config.Start, config.End)
data, err := Query(ctx, api, metric, config.Start, config.End)
if err != nil {
fmt.Fprintf(os.Stderr, "error: %s\n", err)
os.Exit(1)
}
items := ConvertToItems(data)
fmt.Printf("Obtained %d records\n", len(items))
for _, chunk := range CreateChunks(items, ChunkSize) {
err = uploader.Put(ctx, chunk)
if err != nil {
fmt.Fprintf(os.Stderr, "error: %s\n", err)
os.Exit(1)
}
}
}
}