forked from grafana/xk6-loki
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
256 lines (224 loc) · 6.64 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
package loki
import (
"bytes"
"context"
"fmt"
"math/rand"
"net/http"
"net/url"
"path"
"time"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"go.k6.io/k6/lib"
"go.k6.io/k6/lib/netext/httpext"
)
const (
ContentTypeProtobuf = "application/x-protobuf"
ContentTypeJSON = "application/json"
ContentEncodingSnappy = "snappy"
ContentEncodingGzip = "gzip"
TenantPrefix = "xk6-tenant"
)
type Client struct {
client *http.Client
logger logrus.FieldLogger
cfg *Config
}
type Config struct {
URL url.URL
UserAgent string
Timeout time.Duration
TenantID string
Labels LabelPool
ProtobufRatio float64
}
func (c *Client) InstantQuery(ctx context.Context, logQuery string, limit int) (httpext.Response, error) {
q := &Query{
Type: InstantQuery,
QueryString: logQuery,
Limit: limit,
}
q.SetInstant(time.Now())
return c.sendQuery(ctx, q)
}
func (c *Client) RangeQuery(ctx context.Context, logQuery string, duration string, limit int) (httpext.Response, error) {
now := time.Now()
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,
}
return c.sendQuery(ctx, q)
}
func (c *Client) LabelsQuery(ctx context.Context, duration string) (httpext.Response, error) {
now := time.Now()
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(ctx, q)
}
func (c *Client) LabelValuesQuery(ctx context.Context, label string, duration string) (httpext.Response, error) {
now := time.Now()
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(ctx, q)
}
func (c *Client) SeriesQuery(ctx context.Context, matchers string, duration string) (httpext.Response, error) {
now := time.Now()
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(ctx, 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(ctx context.Context, q *Query) (httpext.Response, error) {
state := lib.GetState(ctx)
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(ctx, state, &httpext.ParsedHTTPRequest{
URL: &url,
Req: r,
Throw: state.Options.Throw.Bool,
Redirects: state.Options.MaxRedirects,
Timeout: c.cfg.Timeout,
ResponseCallback: ResponseCallback,
})
if err != nil {
return *httpResp, err
}
return *response, err
}
func (c *Client) Push(ctx context.Context) (httpext.Response, error) {
// 5 streams per batch
// batch size between 800KB and 1MB
return c.PushParameterized(ctx, 5, 800*1024, 1024*1024)
}
// PushParametrized is deprecated in favor or PushParameterized
func (c *Client) PushParametrized(ctx context.Context, streams, minBatchSize, maxBatchSize int) (httpext.Response, error) {
c.logger.Warn("method pushParametrized() is deprecated and will be removed in future releases; please use pushParameterized() instead")
return c.PushParameterized(ctx, streams, minBatchSize, maxBatchSize)
}
func (c *Client) PushParameterized(ctx context.Context, streams, minBatchSize, maxBatchSize int) (httpext.Response, error) {
batch := newBatch(ctx, c.cfg.Labels, streams, minBatchSize, maxBatchSize)
return c.pushBatch(ctx, batch)
}
func (c *Client) pushBatch(ctx context.Context, batch *Batch) (httpext.Response, error) {
state := lib.GetState(ctx)
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(ctx, 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(ctx context.Context, 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(ctx, 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: ResponseCallback,
})
if err != nil {
return *httpResp, err
}
return *response, err
}
func ResponseCallback(n int) bool {
// report all 2xx respones as successful requests
return n/100 == 2
}