-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathremote_write.go
208 lines (180 loc) · 4.84 KB
/
remote_write.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
package remotewrite
import (
"bytes"
"context"
"encoding/binary"
"fmt"
"log"
"net/http"
"time"
"github.com/golang/protobuf/proto"
"github.com/golang/snappy"
"github.com/pkg/errors"
"github.com/prometheus/prometheus/prompb"
"github.com/xhit/go-str2duration/v2"
"go.k6.io/k6/js/common"
"go.k6.io/k6/js/modules"
"go.k6.io/k6/lib"
"go.k6.io/k6/lib/metrics"
"go.k6.io/k6/lib/netext"
"go.k6.io/k6/stats"
)
// Register the extension on module initialization, available to
// import from JS as "k6/x/remotewrite".
func init() {
modules.Register("k6/x/remotewrite", new(RemoteWrite))
}
// RemoteWrite is the k6 extension for interacting with Kubernetes jobs.
type RemoteWrite struct {
}
// Client is the client wrapper.
type Client struct {
client *http.Client
cfg *Config
}
type Config struct {
Url string `json:"url"`
UserAgent string `json:"user_agent"`
Timeout string `json:"timeout"`
TenantName string `json:"tenant_name"`
}
// XClient represents
func (r *RemoteWrite) XClient(ctxPtr *context.Context, config Config) interface{} {
if config.Url == "" {
log.Fatal(fmt.Errorf("url is required"))
}
if config.UserAgent == "" {
config.UserAgent = "k6-remote-write/0.0.1"
}
if config.Timeout == "" {
config.Timeout = "10s"
}
rt := common.GetRuntime(*ctxPtr)
return common.Bind(rt, &Client{
client: &http.Client{},
cfg: &config,
}, ctxPtr)
}
type Timeseries struct {
Labels []struct {
Name string `json:"name"`
Value string `json:"value"`
} `json:"labels"`
Samples []struct {
Value float64 `json:"value"`
Timestamp int64 `json:"timestamp"`
} `json:"samples"`
}
func (c *Client) Store(ctx context.Context, ts []Timeseries) (http.Response, error) {
var batch []prompb.TimeSeries
for _, t := range ts {
batch = append(batch, FromTimeseriesToPrometheusTimeseries(t))
}
// Required for k6 metrics
state := lib.GetState(ctx)
if state == nil {
return http.Response{}, errors.New("State is nil")
}
now := time.Now()
stats.PushIfNotDone(ctx, state.Samples, stats.Sample{
Metric: RemoteWriteNumSeries,
Time: now,
Value: float64(len(batch)),
})
req := prompb.WriteRequest{
Timeseries: batch,
}
data, err := proto.Marshal(&req)
if err != nil {
return http.Response{}, errors.Wrap(err, "failed to marshal remote-write request")
}
compressed := snappy.Encode(nil, data)
res, err := c.send(ctx, state, compressed)
if err != nil {
return http.Response{}, errors.Wrap(err, "remote-write request failed")
}
return res, nil
}
// send sends a batch of samples to the HTTP endpoint, the request is the proto marshalled
// and encoded bytes
func (c *Client) send(ctx context.Context, state *lib.State, req []byte) (http.Response, error) {
httpReq, err := http.NewRequest("POST", c.cfg.Url, bytes.NewReader(req))
if err != nil {
return http.Response{}, err
}
httpReq.Header.Add("Content-Encoding", "snappy")
httpReq.Header.Set("Content-Type", "application/x-protobuf")
httpReq.Header.Set("User-Agent", c.cfg.UserAgent)
httpReq.Header.Set("X-Prometheus-Remote-Write-Version", "0.1.0")
if c.cfg.TenantName != "" {
httpReq.Header.Set("X-Scope-OrgID", c.cfg.TenantName)
}
duration, err := str2duration.ParseDuration(c.cfg.Timeout)
if err != nil {
return http.Response{}, err
}
ctx, cancel := context.WithTimeout(ctx, duration)
defer cancel()
httpReq = httpReq.WithContext(ctx)
now := time.Now()
stats.PushIfNotDone(ctx, state.Samples, stats.Sample{
Metric: RemoteWriteReqs,
Time: now,
Value: float64(1),
})
simpleNetTrail := netext.NetTrail{
BytesWritten: int64(binary.Size(req)),
StartTime: now.Add(-time.Minute),
EndTime: now,
Samples: []stats.Sample{
{
Time: now,
Metric: metrics.DataSent,
Value: float64(binary.Size(req)),
},
},
}
stats.PushIfNotDone(ctx, state.Samples, &simpleNetTrail)
start := time.Now()
httpResp, err := c.client.Do(httpReq)
elapsed := time.Since(start)
if err != nil {
return http.Response{}, err
}
stats.PushIfNotDone(ctx, state.Samples, stats.Sample{
Metric: RemoteWriteReqDuration,
Time: now,
Value: float64(elapsed.Milliseconds()),
})
if httpResp.StatusCode != http.StatusOK {
stats.PushIfNotDone(ctx, state.Samples, stats.Sample{
Metric: RemoteWriteReqFailed,
Time: now,
Value: float64(1),
})
}
return *httpResp, err
}
func FromTimeseriesToPrometheusTimeseries(ts Timeseries) prompb.TimeSeries {
var labels []prompb.Label
var samples []prompb.Sample
for _, label := range ts.Labels {
labels = append(labels, prompb.Label{
Name: label.Name,
Value: label.Value,
})
}
for _, sample := range ts.Samples {
if sample.Timestamp == 0 {
sample.Timestamp = time.Now().UnixNano() / int64(time.Millisecond)
}
samples = append(samples, prompb.Sample{
Value: sample.Value,
Timestamp: sample.Timestamp,
})
}
return prompb.TimeSeries{
Labels: labels,
Samples: samples,
}
}