forked from grafana/xk6-loki
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
365 lines (319 loc) · 10 KB
/
client.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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
package loki
import (
"bytes"
"encoding/json"
"fmt"
"math/rand"
"net/http"
"net/url"
"path"
"time"
"github.com/grafana/loki/pkg/logql/stats"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"go.k6.io/k6/js/modules"
"go.k6.io/k6/lib"
"go.k6.io/k6/lib/netext/httpext"
"go.k6.io/k6/metrics"
)
const (
ContentTypeProtobuf = "application/x-protobuf"
ContentTypeJSON = "application/json"
ContentEncodingSnappy = "snappy"
ContentEncodingGzip = "gzip"
TenantPrefix = "xk6-tenant"
)
type Client struct {
vu modules.VU
client *http.Client
logger logrus.FieldLogger
cfg *Config
metrics lokiMetrics
}
type Config struct {
URL url.URL
UserAgent string
Timeout time.Duration
TenantID string
Labels LabelPool
ProtobufRatio float64
}
func (c *Client) InstantQuery(logQuery string, limit int) (httpext.Response, error) {
return c.instantQuery(logQuery, limit, time.Now())
}
func (c *Client) InstantQueryAt(logQuery string, limit int, instant int64) (httpext.Response, error) {
return c.instantQuery(logQuery, limit, time.Unix(instant, 0))
}
func (c *Client) instantQuery(logQuery string, limit int, now time.Time) (httpext.Response, error) {
q := &Query{
Type: InstantQuery,
QueryString: logQuery,
Limit: limit,
}
q.SetInstant(now)
response, err := c.sendQuery(q)
if err == nil && IsSuccessfulResponse(response.Status) {
err = c.reportMetricsFromStats(response, InstantQuery)
}
return response, err
}
func (c *Client) RangeQuery(logQuery string, duration string, limit int) (httpext.Response, error) {
return c.rangeQuery(logQuery, duration, limit, time.Now())
}
func (c *Client) RangeQueryAt(logQuery string, duration string, limit int, instant int64) (httpext.Response, error) {
return c.rangeQuery(logQuery, duration, limit, time.Unix(instant, 0))
}
func (c *Client) rangeQuery(logQuery string, duration string, limit int, now time.Time) (httpext.Response, error) {
dur, err := time.ParseDuration(duration)
if err != nil {
return httpext.Response{}, err
}
q := &Query{
Type: RangeQuery,
QueryString: logQuery,
Start: now.Add(-dur),
End: now,
Limit: limit,
}
response, err := c.sendQuery(q)
if err == nil && IsSuccessfulResponse(response.Status) {
err = c.reportMetricsFromStats(response, RangeQuery)
}
return response, err
}
func (c *Client) LabelsQuery(duration string) (httpext.Response, error) {
return c.labelsQuery(duration, time.Now())
}
func (c *Client) LabelsQueryAt(duration string, instant int64) (httpext.Response, error) {
return c.labelsQuery(duration, time.Unix(instant, 0))
}
func (c *Client) labelsQuery(duration string, now time.Time) (httpext.Response, error) {
dur, err := time.ParseDuration(duration)
if err != nil {
return httpext.Response{}, err
}
q := &Query{
Type: LabelsQuery,
Start: now.Add(-dur),
End: now,
}
return c.sendQuery(q)
}
func (c *Client) LabelValuesQuery(label string, duration string) (httpext.Response, error) {
return c.labelValuesQuery(label, duration, time.Now())
}
func (c *Client) LabelValuesQueryAt(label string, duration string, instant int64) (httpext.Response, error) {
return c.labelValuesQuery(label, duration, time.Unix(instant, 0))
}
func (c *Client) labelValuesQuery(label string, duration string, now time.Time) (httpext.Response, error) {
dur, err := time.ParseDuration(duration)
if err != nil {
return httpext.Response{}, err
}
q := &Query{
Type: LabelValuesQuery,
Start: now.Add(-dur),
End: now,
PathParams: []interface{}{label},
}
return c.sendQuery(q)
}
func (c *Client) SeriesQuery(matchers string, duration string) (httpext.Response, error) {
return c.seriesQuery(matchers, duration, time.Now())
}
func (c *Client) SeriesQueryAt(matchers string, duration string, instant int64) (httpext.Response, error) {
return c.seriesQuery(matchers, duration, time.Unix(instant, 0))
}
func (c *Client) seriesQuery(matchers string, duration string, now time.Time) (httpext.Response, error) {
dur, err := time.ParseDuration(duration)
if err != nil {
return httpext.Response{}, err
}
q := &Query{
Type: SeriesQuery,
QueryString: matchers,
Start: now.Add(-dur),
End: now,
}
return c.sendQuery(q)
}
// buildURL concatinates a URL `http://foo/bar` with a path `/buzz` and a query string `?query=...`.
func buildURL(u, p, qs string) (string, error) {
url, err := url.Parse(u)
if err != nil {
return "", err
}
url.Path = path.Join(url.Path, p)
url.RawQuery = qs
return url.String(), nil
}
func (c *Client) sendQuery(q *Query) (httpext.Response, error) {
state := c.vu.State()
if state == nil {
return *httpext.NewResponse(), errors.New("state is nil")
}
httpResp := httpext.NewResponse()
path := q.Endpoint()
urlString, err := buildURL(c.cfg.URL.String(), path, q.Values().Encode())
if err != nil {
return *httpext.NewResponse(), err
}
r, err := http.NewRequest(http.MethodGet, urlString, nil)
if err != nil {
return *httpResp, err
}
r.Header.Set("User-Agent", c.cfg.UserAgent)
r.Header.Set("Accept", ContentTypeJSON)
if c.cfg.TenantID != "" {
r.Header.Set("X-Scope-OrgID", c.cfg.TenantID)
} else {
r.Header.Set("X-Scope-OrgID", fmt.Sprintf("%s-%d", TenantPrefix, state.VUID))
}
url, _ := httpext.NewURL(urlString, path)
response, err := httpext.MakeRequest(c.vu.Context(), state, &httpext.ParsedHTTPRequest{
URL: &url,
Req: r,
Throw: state.Options.Throw.Bool,
Redirects: state.Options.MaxRedirects,
Timeout: c.cfg.Timeout,
ResponseCallback: IsSuccessfulResponse,
})
if err != nil {
return *httpResp, err
}
return *response, err
}
func (c *Client) Push() (httpext.Response, error) {
// 5 streams per batch
// batch size between 800KB and 1MB
return c.PushParameterized(5, 800*1024, 1024*1024)
}
// PushParametrized is deprecated in favor or PushParameterized
func (c *Client) PushParametrized(streams, minBatchSize, maxBatchSize int) (httpext.Response, error) {
if state := c.vu.State(); state == nil {
return *httpext.NewResponse(), errors.New("state is nil")
} else {
state.Logger.Warn("method pushParametrized() is deprecated and will be removed in future releases; please use pushParameterized() instead")
}
return c.PushParameterized(streams, minBatchSize, maxBatchSize)
}
func (c *Client) PushParameterized(streams, minBatchSize, maxBatchSize int) (httpext.Response, error) {
state := c.vu.State()
if state == nil {
return *httpext.NewResponse(), errors.New("state is nil")
}
batch := c.newBatch(c.cfg.Labels, streams, minBatchSize, maxBatchSize)
return c.pushBatch(batch)
}
func (c *Client) pushBatch(batch *Batch) (httpext.Response, error) {
state := c.vu.State()
if state == nil {
return *httpext.NewResponse(), errors.New("state is nil")
}
var buf []byte
var err error
// Use snappy encoded Protobuf for 90% of the requests
// Use JSON encoding for 10% of the requests
encodeSnappy := rand.Float64() < c.cfg.ProtobufRatio
if encodeSnappy {
buf, _, err = batch.encodeSnappy()
} else {
buf, _, err = batch.encodeJSON()
}
if err != nil {
return *httpext.NewResponse(), errors.Wrap(err, "failed to encode payload")
}
res, err := c.send(state, buf, encodeSnappy)
if err != nil {
return *httpext.NewResponse(), errors.Wrap(err, "push request failed")
}
res.Request.Body = ""
return res, nil
}
func (c *Client) send(state *lib.State, buf []byte, useProtobuf bool) (httpext.Response, error) {
httpResp := httpext.NewResponse()
path := "/loki/api/v1/push"
r, err := http.NewRequest(http.MethodPost, c.cfg.URL.String()+path, nil)
if err != nil {
return *httpResp, err
}
r.Header.Set("User-Agent", c.cfg.UserAgent)
r.Header.Set("Accept", ContentTypeJSON)
if c.cfg.TenantID != "" {
r.Header.Set("X-Scope-OrgID", c.cfg.TenantID)
} else {
r.Header.Set("X-Scope-OrgID", fmt.Sprintf("%s-%d", TenantPrefix, state.VUID))
}
if useProtobuf {
r.Header.Set("Content-Type", ContentTypeProtobuf)
r.Header.Add("Content-Encoding", ContentEncodingSnappy)
} else {
r.Header.Set("Content-Type", ContentTypeJSON)
}
url, _ := httpext.NewURL(c.cfg.URL.String()+path, path)
response, err := httpext.MakeRequest(c.vu.Context(), state, &httpext.ParsedHTTPRequest{
URL: &url,
Req: r,
Body: bytes.NewBuffer(buf),
Throw: state.Options.Throw.Bool,
Redirects: state.Options.MaxRedirects,
Timeout: c.cfg.Timeout,
ResponseCallback: IsSuccessfulResponse,
})
if err != nil {
return *httpResp, err
}
return *response, err
}
func IsSuccessfulResponse(n int) bool {
// report all 2xx respones as successful requests
return n/100 == 2
}
type responseWithStats struct {
Data struct {
Stats stats.Result
}
}
func (c *Client) reportMetricsFromStats(response httpext.Response, queryType QueryType) error {
responseBody, ok := response.Body.(string)
if !ok {
return errors.New("response body is not a string")
}
responseWithStats := responseWithStats{}
err := json.Unmarshal([]byte(responseBody), &responseWithStats)
if err != nil {
return errors.Wrap(err, "error unmarshalling response body to response with stats")
}
now := time.Now()
tags := metrics.NewSampleTags(map[string]string{"endpoint": queryType.Endpoint()})
ctx := c.vu.Context()
metrics.PushIfNotDone(ctx, c.vu.State().Samples, metrics.ConnectedSamples{
Samples: []metrics.Sample{
{
Metric: c.metrics.BytesProcessedTotal,
Tags: tags,
Value: float64(responseWithStats.Data.Stats.Summary.TotalBytesProcessed),
Time: now,
},
{
Metric: c.metrics.BytesProcessedPerSeconds,
Tags: tags,
Value: float64(responseWithStats.Data.Stats.Summary.BytesProcessedPerSecond),
Time: now,
},
{
Metric: c.metrics.LinesProcessedTotal,
Tags: tags,
Value: float64(responseWithStats.Data.Stats.Summary.TotalLinesProcessed),
Time: now,
},
{
Metric: c.metrics.LinesProcessedPerSeconds,
Tags: tags,
Value: float64(responseWithStats.Data.Stats.Summary.LinesProcessedPerSecond),
Time: now,
},
},
})
return nil
}