-
Notifications
You must be signed in to change notification settings - Fork 69
/
writer.go
153 lines (131 loc) · 3.36 KB
/
writer.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
package main
import (
"database/sql"
"fmt"
"sort"
"time"
"sync"
"github.com/kshvakov/clickhouse"
"github.com/prometheus/client_golang/prometheus"
)
var insertSQL = `INSERT INTO %s.%s
(date, name, tags, val, ts)
VALUES (?, ?, ?, ?, ?)`
type p2cWriter struct {
conf *config
requests chan *p2cRequest
wg sync.WaitGroup
db *sql.DB
tx prometheus.Counter
ko prometheus.Counter
test prometheus.Counter
timings prometheus.Histogram
}
func NewP2CWriter(conf *config, reqs chan *p2cRequest) (*p2cWriter, error) {
var err error
w := new(p2cWriter)
w.conf = conf
w.requests = reqs
w.db, err = sql.Open("clickhouse", w.conf.ChDSN)
if err != nil {
fmt.Printf("Error connecting to clickhouse: %s\n", err.Error())
return w, err
}
w.tx = prometheus.NewCounter(
prometheus.CounterOpts{
Name: "sent_samples_total",
Help: "Total number of processed samples sent to remote storage.",
},
)
w.ko = prometheus.NewCounter(
prometheus.CounterOpts{
Name: "failed_samples_total",
Help: "Total number of processed samples which failed on send to remote storage.",
},
)
w.test = prometheus.NewCounter(
prometheus.CounterOpts{
Name: "prometheus_remote_storage_sent_batch_duration_seconds_bucket_test",
Help: "Test metric to ensure backfilled metrics are readable via prometheus.",
},
)
w.timings = prometheus.NewHistogram(
prometheus.HistogramOpts{
Name: "sent_batch_duration_seconds",
Help: "Duration of sample batch send calls to the remote storage.",
Buckets: prometheus.DefBuckets,
},
)
prometheus.MustRegister(w.tx)
prometheus.MustRegister(w.ko)
prometheus.MustRegister(w.test)
prometheus.MustRegister(w.timings)
return w, nil
}
func (w *p2cWriter) Start() {
go func() {
w.wg.Add(1)
fmt.Println("Writer starting..")
sql := fmt.Sprintf(insertSQL, w.conf.ChDB, w.conf.ChTable)
ok := true
for ok {
w.test.Add(1)
// get next batch of requests
var reqs []*p2cRequest
tstart := time.Now()
for i := 0; i < w.conf.ChBatch; i++ {
var req *p2cRequest
// get requet and also check if channel is closed
req, ok = <-w.requests
if !ok {
fmt.Println("Writer stopping..")
break
}
reqs = append(reqs, req)
}
// ensure we have something to send..
nmetrics := len(reqs)
if nmetrics < 1 {
continue
}
// post them to db all at once
tx, err := w.db.Begin()
if err != nil {
fmt.Printf("Error: begin transaction: %s\n", err.Error())
w.ko.Add(1.0)
continue
}
// build statements
smt, err := tx.Prepare(sql)
for _, req := range reqs {
if err != nil {
fmt.Printf("Error: prepare statement: %s\n", err.Error())
w.ko.Add(1.0)
continue
}
// ensure tags are inserted in the same order each time
// possibly/probably impacts indexing?
sort.Strings(req.tags)
_, err = smt.Exec(req.ts, req.name, clickhouse.Array(req.tags),
req.val, req.ts)
if err != nil {
fmt.Printf("Error: statement exec: %s\n", err.Error())
w.ko.Add(1.0)
}
}
// commit and record metrics
if err = tx.Commit(); err != nil {
fmt.Printf("Error: commit failed: %s\n", err.Error())
w.ko.Add(1.0)
} else {
w.tx.Add(float64(nmetrics))
w.timings.Observe(float64(time.Since(tstart)))
}
}
fmt.Println("Writer stopped..")
w.wg.Done()
}()
}
func (w *p2cWriter) Wait() {
w.wg.Wait()
}