-
Notifications
You must be signed in to change notification settings - Fork 498
/
cortex_test.go
411 lines (369 loc) · 12.7 KB
/
cortex_test.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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
// Copyright The OpenTelemetry Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package cortex
import (
"fmt"
"io/ioutil"
"net/http"
"net/http/httptest"
"strconv"
"testing"
"time"
"github.com/golang/snappy"
"github.com/google/go-cmp/cmp"
"github.com/prometheus/prometheus/prompb"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/metric/global"
"go.opentelemetry.io/otel/sdk/export/metric"
export "go.opentelemetry.io/otel/sdk/export/metric"
"go.opentelemetry.io/otel/sdk/export/metric/aggregation"
"go.opentelemetry.io/otel/sdk/resource"
semconv "go.opentelemetry.io/otel/semconv/v1.7.0"
)
var testResource = resource.NewWithAttributes(semconv.SchemaURL, attribute.String("R", "V"))
// ValidConfig is a Config struct that should cause no errors.
var validConfig = Config{
Endpoint: "/api/prom/push",
RemoteTimeout: 30 * time.Second,
Name: "Valid Config Example",
BasicAuth: map[string]string{
"username": "user",
"password": "password",
},
BearerToken: "",
BearerTokenFile: "",
TLSConfig: map[string]string{
"ca_file": "cafile",
"cert_file": "certfile",
"key_file": "keyfile",
"server_name": "server",
"insecure_skip_verify": "1",
},
ProxyURL: nil,
PushInterval: 10 * time.Second,
Headers: map[string]string{
"x-prometheus-remote-write-version": "0.1.0",
"tenant-id": "123",
},
Client: http.DefaultClient,
Quantiles: []float64{0, 0.25, 0.5, 0.75, 1},
}
func TestExportKindFor(t *testing.T) {
exporter := Exporter{}
got := exporter.ExportKindFor(nil, aggregation.Kind(rune(0)))
want := metric.CumulativeExportKind
if got != want {
t.Errorf("ExportKindFor() = %q, want %q", got, want)
}
}
func TestConvertToTimeSeries(t *testing.T) {
// Setup exporter with default quantiles and histogram buckets
exporter := Exporter{
config: Config{
Quantiles: []float64{0.5, 0.9, .99},
},
}
startTime := time.Now()
// Test conversions based on aggregation type
tests := []struct {
name string
input export.InstrumentationLibraryReader
want []*prompb.TimeSeries
wantLength int
}{
{
name: "convertFromSum",
input: getSumReader(t, 1, 2, 3, 4, 5),
want: wantSumTimeSeries,
wantLength: 1,
},
{
name: "convertFromLastValue",
input: getLastValueReader(t, 1, 2, 3, 4, 5),
want: wantLastValueTimeSeries,
wantLength: 1,
},
{
name: "convertFromMinMaxSumCount",
input: getMMSCReader(t, 123.456, 876.543),
want: wantMMSCTimeSeries,
wantLength: 4,
},
{
name: "convertFromHistogram",
input: getHistogramReader(t),
want: wantHistogramTimeSeries,
wantLength: 6,
},
}
endTime := time.Now()
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := exporter.ConvertToTimeSeries(testResource, tt.input)
want := tt.want
// Check for errors and for the correct number of timeseries.
assert.Nil(t, err, "ConvertToTimeSeries error")
assert.Len(t, got, tt.wantLength, "Incorrect number of timeseries")
// The TimeSeries cannot be compared easily using assert.ElementsMatch or
// cmp.Equal since both the ordering of the timeseries and the ordering of the
// attributes inside each timeseries can change. To get around this, all the
// attributes and samples are added to maps first. There aren't many attributes or
// samples, so this nested loop shouldn't be a bottleneck.
gotAttributes := make(map[string]bool)
wantAttributes := make(map[string]bool)
gotSamples := make(map[string]bool)
wantSamples := make(map[string]bool)
for i := 0; i < len(got); i++ {
for _, attribute := range got[i].Labels {
gotAttributes[attribute.String()] = true
}
for _, attribute := range want[i].Labels {
wantAttributes[attribute.String()] = true
}
for _, sample := range got[i].Samples {
gotSamples[fmt.Sprint(sample.Value)] = true
assert.LessOrEqual(t, toMillis(startTime), sample.Timestamp)
assert.GreaterOrEqual(t, toMillis(endTime), sample.Timestamp)
}
for _, sample := range want[i].Samples {
wantSamples[fmt.Sprint(sample.Value)] = true
}
}
assert.Equal(t, wantAttributes, gotAttributes)
assert.Equal(t, wantSamples, gotSamples)
})
}
}
// TestNewRawExporter tests whether NewRawExporter successfully creates an Exporter with
// the same Config struct as the one passed in.
func TestNewRawExporter(t *testing.T) {
exporter, err := NewRawExporter(validConfig)
if err != nil {
t.Fatalf("Failed to create exporter with error %v", err)
}
if !cmp.Equal(validConfig, exporter.config) {
t.Fatalf("Got configuration %v, wanted %v", exporter.config, validConfig)
}
}
// TestNewExportPipeline tests whether a push Controller was successfully created with an
// Exporter from NewRawExporter. Errors in this function will be from calls to push
// controller package and NewRawExport. Both have their own tests.
func TestNewExportPipeline(t *testing.T) {
_, err := NewExportPipeline(validConfig)
if err != nil {
t.Fatalf("Failed to create export pipeline with error %v", err)
}
}
// TestInstallNewPipeline checks whether InstallNewPipeline successfully returns a push
// Controller and whether that controller's MeterProvider is registered globally.
func TestInstallNewPipeline(t *testing.T) {
cont, err := InstallNewPipeline(validConfig)
if err != nil {
t.Fatalf("Failed to create install pipeline with error %v", err)
}
if global.GetMeterProvider() != cont {
t.Fatalf("Failed to register push Controller provider globally")
}
}
// TestAddHeaders tests whether the correct headers are correctly added to a http request.
func TestAddHeaders(t *testing.T) {
testConfig := Config{
Headers: map[string]string{
"TestHeaderOne": "TestFieldTwo",
"TestHeaderTwo": "TestFieldTwo",
},
}
exporter := Exporter{testConfig}
// Create http request to add headers to.
req, err := http.NewRequest("POST", "test.com", nil)
require.NoError(t, err)
err = exporter.addHeaders(req)
require.NoError(t, err)
// Check that all the headers are there.
for name, field := range testConfig.Headers {
require.Equal(t, req.Header.Get(name), field)
}
require.Equal(t, req.Header.Get("Content-Encoding"), "snappy")
require.Equal(t, req.Header.Get("Content-Type"), "application/x-protobuf")
require.Equal(t, req.Header.Get("X-Prometheus-Remote-Write-Version"), "0.1.0")
}
// TestBuildMessage tests whether BuildMessage successfully returns a Snappy-compressed
// protobuf message.
func TestBuildMessage(t *testing.T) {
exporter := Exporter{validConfig}
timeseries := []prompb.TimeSeries{}
// buildMessage returns the error that proto.Marshal() returns. Since the proto
// package has its own tests, buildMessage should work as expected as long as there
// are no errors.
_, err := exporter.buildMessage(timeseries)
require.NoError(t, err)
}
// TestBuildRequest tests whether a http request is a POST request, has the correct body,
// and has the correct headers.
func TestBuildRequest(t *testing.T) {
// Make fake exporter and message for testing.
var testMessage = []byte(`Test Message`)
exporter := Exporter{validConfig}
// Create the http request.
req, err := exporter.buildRequest(testMessage)
require.NoError(t, err)
// Verify the http method, url, and body.
require.Equal(t, req.Method, http.MethodPost)
require.Equal(t, req.URL.String(), validConfig.Endpoint)
reqMessage, err := ioutil.ReadAll(req.Body)
require.NoError(t, err)
require.Equal(t, reqMessage, testMessage)
// Verify headers.
for name, field := range exporter.config.Headers {
require.Equal(t, req.Header.Get(name), field)
}
require.Equal(t, req.Header.Get("Content-Encoding"), "snappy")
require.Equal(t, req.Header.Get("Content-Type"), "application/x-protobuf")
require.Equal(t, req.Header.Get("X-Prometheus-Remote-Write-Version"), "0.1.0")
}
// verifyExporterRequest checks a HTTP request from the export pipeline. It checks whether
// the request contains a correctly formatted remote_write body and the required headers.
func verifyExporterRequest(req *http.Request) error {
// Check for required headers.
if req.Header.Get("X-Prometheus-Remote-Write-Version") != "0.1.0" ||
req.Header.Get("Content-Encoding") != "snappy" ||
req.Header.Get("Content-Type") != "application/x-protobuf" {
return fmt.Errorf("Request does not contain the three required headers")
}
// Check whether request body is in the correct format.
compressed, err := ioutil.ReadAll(req.Body)
if err != nil {
return fmt.Errorf("Failed to read request body")
}
uncompressed, err := snappy.Decode(nil, compressed)
if err != nil {
return fmt.Errorf("Failed to uncompress request body")
}
wr := &prompb.WriteRequest{}
err = wr.Unmarshal(uncompressed)
if err != nil {
return fmt.Errorf("Failed to unmarshal message into WriteRequest struct")
}
// Check whether the request contains the correct data.
expectedWriteRequest := &prompb.WriteRequest{
Timeseries: []prompb.TimeSeries{
{
Samples: []prompb.Sample{
{
Value: float64(123),
Timestamp: int64(time.Nanosecond) * time.Time{}.UnixNano() / int64(time.Millisecond),
},
},
Labels: []prompb.Label{
{
Name: "__name__",
Value: "test_name",
},
},
},
},
}
if !cmp.Equal(wr, expectedWriteRequest) {
return fmt.Errorf("request does not contain the expected contents")
}
return nil
}
// TestSendRequest checks if the Exporter can successfully send a http request with a
// correctly formatted body and the correct headers. A test server returns different
// status codes to test if the Exporter responds to a send failure correctly.
func TestSendRequest(t *testing.T) {
tests := []struct {
testName string
config *Config
expectedError error
isStatusNotFound bool
}{
{
testName: "Successful Export",
config: &validConfig,
expectedError: nil,
isStatusNotFound: false,
},
{
testName: "Export Failure",
config: &Config{},
expectedError: fmt.Errorf("%v", "404 Not Found"),
isStatusNotFound: true,
},
}
// Set up a test server to receive the request. The server responds with a 400 Bad
// Request status code if any headers are missing or if the body does not have the
// correct contents. Additionally, the server can respond with status code 404 Not
// Found to simulate send failures.
handler := func(rw http.ResponseWriter, req *http.Request) {
err := verifyExporterRequest(req)
if err != nil {
rw.WriteHeader(http.StatusBadRequest)
return
}
// Return a status code 400 if header isStatusNotFound is "true". Otherwise,
// return status code 200.
if req.Header.Get("isStatusNotFound") == "true" {
rw.WriteHeader(http.StatusNotFound)
} else {
rw.WriteHeader(http.StatusOK)
}
}
server := httptest.NewServer(http.HandlerFunc(handler))
defer server.Close()
for _, test := range tests {
t.Run(test.testName, func(t *testing.T) {
// Set up an Exporter that uses the test server's endpoint and attaches the
// test's isStatusNotFound header.
test.config.Endpoint = server.URL
test.config.Headers = map[string]string{
"isStatusNotFound": strconv.FormatBool(test.isStatusNotFound),
}
exporter := Exporter{*test.config}
// Create a test TimeSeries struct.
timeSeries := []prompb.TimeSeries{
{
Samples: []prompb.Sample{
{
Value: float64(123),
Timestamp: int64(time.Nanosecond) * time.Time{}.UnixNano() / int64(time.Millisecond),
},
},
Labels: []prompb.Label{
{
Name: "__name__",
Value: "test_name",
},
},
},
}
// Create a Snappy-compressed message.
msg, err := exporter.buildMessage(timeSeries)
require.NoError(t, err)
// Create a http POST request with the compressed message.
req, err := exporter.buildRequest(msg)
require.NoError(t, err)
// Send the request to the test server and verify the error.
err = exporter.sendRequest(req)
if err != nil {
errorString := err.Error()
require.Equal(t, errorString, test.expectedError.Error())
} else {
require.NoError(t, test.expectedError)
}
})
}
}