-
Notifications
You must be signed in to change notification settings - Fork 15
/
output.go
214 lines (180 loc) · 5.6 KB
/
output.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
// Package timescaledb provides the xk6-output-timescaledb extension
package timescaledb
import (
"context"
"fmt"
"time"
"github.com/jackc/pgx/v5"
"github.com/jackc/pgx/v5/pgxpool"
"github.com/sirupsen/logrus"
"go.k6.io/k6/metrics"
"go.k6.io/k6/output"
)
func init() {
output.RegisterExtension("timescaledb", newOutput)
}
var _ interface{ output.WithThresholds } = &Output{}
// Output is a k6 output that sends metrics to a TimescaleDB instance.
type Output struct {
output.SampleBuffer
periodicFlusher *output.PeriodicFlusher
Pool *pgxpool.Pool
Config config
thresholds map[string][]*dbThreshold
logger logrus.FieldLogger
}
// Description returns a short human-readable description of the output.
func (o *Output) Description() string {
return fmt.Sprintf("TimescaleDB (%s)", o.Config.addr.String)
}
func newOutput(params output.Params) (output.Output, error) {
configs, err := getConsolidatedConfig(params.JSONConfig, params.Environment, params.ConfigArgument)
if err != nil {
return nil, fmt.Errorf("problem parsing config: %w", err)
}
pconf, err := pgxpool.ParseConfig(configs.URL.String)
if err != nil {
return nil, fmt.Errorf("TimescaleDB: Unable to parse config: %w", err)
}
pool, err := pgxpool.NewWithConfig(context.Background(), pconf)
if err != nil {
return nil, fmt.Errorf("TimescaleDB: Unable to create connection pool: %w", err)
}
o := Output{
Pool: pool,
Config: configs,
logger: params.Logger.WithFields(logrus.Fields{
"output": "TimescaleDB",
}),
}
return &o, nil
}
// SetThresholds receives threshold data to be output.
func (o *Output) SetThresholds(thresholds map[string]metrics.Thresholds) {
ths := make(map[string][]*dbThreshold)
for metric, fullTh := range thresholds {
for _, t := range fullTh.Thresholds {
ths[metric] = append(ths[metric], &dbThreshold{
id: -1,
threshold: t,
})
}
}
o.thresholds = ths
}
type dbThreshold struct {
id int
threshold *metrics.Threshold
}
const schema = `
CREATE TABLE IF NOT EXISTS samples (
ts timestamptz NOT NULL DEFAULT current_timestamp,
metric varchar(128) NOT NULL,
tags jsonb,
value real
);
CREATE TABLE IF NOT EXISTS thresholds (
id serial,
ts timestamptz NOT NULL DEFAULT current_timestamp,
metric varchar(128) NOT NULL,
tags jsonb,
threshold varchar(128) NOT NULL,
abort_on_fail boolean DEFAULT FALSE,
delay_abort_eval varchar(128),
last_failed boolean DEFAULT FALSE
);
SELECT create_hypertable('samples', 'ts');
CREATE INDEX IF NOT EXISTS idx_samples_ts ON samples (ts DESC);
CREATE INDEX IF NOT EXISTS idx_thresholds_ts ON thresholds (ts DESC);`
// Start initializes the output.
func (o *Output) Start() error {
conn, err := o.Pool.Acquire(context.Background())
if err != nil {
o.logger.WithError(err).Error("Start: Couldn't acquire connection")
}
defer conn.Release()
_, err = conn.Exec(context.Background(), "CREATE DATABASE "+o.Config.dbName.String)
if err != nil {
o.logger.WithError(err).Debug("Start: Couldn't create database; most likely harmless")
}
_, err = conn.Exec(context.Background(), schema)
if err != nil {
o.logger.WithError(err).Debug("Start: Couldn't create database schema; most likely harmless")
}
for metric, thresholds := range o.thresholds {
for _, t := range thresholds {
err = conn.QueryRow(context.Background(), `
INSERT INTO thresholds (metric, threshold, abort_on_fail, delay_abort_eval, last_failed)
VALUES ($1, $2, $3, $4, $5)
RETURNING id`,
metric, t.threshold.Source, t.threshold.AbortOnFail, t.threshold.AbortGracePeriod.String(), t.threshold.LastFailed).
Scan(&t.id)
if err != nil {
o.logger.WithError(err).Debug("Start: Failed to insert threshold")
}
}
}
pf, err := output.NewPeriodicFlusher(time.Duration(o.Config.PushInterval.Duration), o.flushMetrics)
if err != nil {
return err
}
o.logger.Debug("Start: Running!")
o.periodicFlusher = pf
return nil
}
func (o *Output) flushMetrics() {
sampleContainers := o.GetBufferedSamples()
start := time.Now()
o.logger.WithField("sample-containers", len(sampleContainers)).Debug("flushMetrics: Collecting...")
rows := [][]interface{}{}
for _, sc := range sampleContainers {
samples := sc.GetSamples()
o.logger.WithField("samples", len(samples)).Debug("flushMetrics: Writing...")
for _, s := range samples {
tags := s.Tags.Map()
row := []interface{}{s.Time, s.Metric.Name, s.Value, tags}
rows = append(rows, row)
}
}
var batch pgx.Batch
for _, t := range o.thresholds {
for _, threshold := range t {
batch.Queue(`UPDATE thresholds SET last_failed = $1 WHERE id = $2`,
threshold.threshold.LastFailed, threshold.id)
}
}
br := o.Pool.SendBatch(context.Background(), &batch)
defer func() {
if err := br.Close(); err != nil {
o.logger.WithError(err).Warn("flushMetrics: Couldn't close batch results")
}
}()
_, err := o.Pool.CopyFrom(context.Background(),
pgx.Identifier{"samples"},
[]string{"ts", "metric", "value", "tags"},
pgx.CopyFromRows(rows))
if err != nil {
o.logger.WithError(err).Warn("copyMetrics: Couldn't commit samples")
}
for i := 0; i < batch.Len(); i++ {
ct, err := br.Exec()
if err != nil {
o.logger.WithError(err).Error("flushMetrics: Couldn't exec batch")
return
}
if ct.RowsAffected() != 1 {
o.logger.WithError(err).Error("flushMetrics: Batch did not insert")
return
}
}
t := time.Since(start)
o.logger.WithField("time_since_start", t).Debug("flushMetrics: Samples committed!")
}
// Stop stops the output.
func (o *Output) Stop() error {
o.logger.Debug("Stopping...")
defer o.logger.Debug("Stopped!")
o.periodicFlusher.Stop()
o.Pool.Close()
return nil
}