/
native_histograms_test.go
420 lines (347 loc) · 16.6 KB
/
native_histograms_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
412
413
414
415
416
417
418
419
420
// Copyright (c) The Thanos Authors.
// Licensed under the Apache License 2.0.
package e2e_test
import (
"context"
"fmt"
"reflect"
"testing"
"time"
"github.com/efficientgo/core/testutil"
"github.com/efficientgo/e2e"
e2emon "github.com/efficientgo/e2e/monitoring"
"github.com/efficientgo/e2e/monitoring/matchers"
"github.com/prometheus/common/model"
"github.com/prometheus/prometheus/model/histogram"
"github.com/prometheus/prometheus/prompb"
"github.com/prometheus/prometheus/storage/remote"
"github.com/prometheus/prometheus/tsdb"
"github.com/thanos-io/thanos/pkg/promclient"
"github.com/thanos-io/thanos/pkg/queryfrontend"
"github.com/thanos-io/thanos/pkg/receive"
"github.com/thanos-io/thanos/test/e2e/e2ethanos"
)
const testHistogramMetricName = "fake_histogram"
func TestQueryNativeHistograms(t *testing.T) {
e, err := e2e.NewDockerEnvironment("nat-hist-query")
testutil.Ok(t, err)
t.Cleanup(e2ethanos.CleanScenario(t, e))
prom1, sidecar1 := e2ethanos.NewPrometheusWithSidecar(e, "ha1", e2ethanos.DefaultPromConfig("prom-ha", 0, "", "", e2ethanos.LocalPrometheusTarget), "", e2ethanos.DefaultPrometheusImage(), "", "native-histograms", "remote-write-receiver")
prom2, sidecar2 := e2ethanos.NewPrometheusWithSidecar(e, "ha2", e2ethanos.DefaultPromConfig("prom-ha", 1, "", "", e2ethanos.LocalPrometheusTarget), "", e2ethanos.DefaultPrometheusImage(), "", "native-histograms", "remote-write-receiver")
testutil.Ok(t, e2e.StartAndWaitReady(prom1, sidecar1, prom2, sidecar2))
querier := e2ethanos.NewQuerierBuilder(e, "querier", sidecar1.InternalEndpoint("grpc"), sidecar2.InternalEndpoint("grpc")).Init()
testutil.Ok(t, e2e.StartAndWaitReady(querier))
rawRemoteWriteURL1 := "http://" + prom1.Endpoint("http") + "/api/v1/write"
rawRemoteWriteURL2 := "http://" + prom2.Endpoint("http") + "/api/v1/write"
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Minute)
t.Cleanup(cancel)
histograms := tsdb.GenerateTestHistograms(4)
now := time.Now()
_, err = writeHistograms(ctx, now, histograms, rawRemoteWriteURL1)
testutil.Ok(t, err)
_, err = writeHistograms(ctx, now, histograms, rawRemoteWriteURL2)
testutil.Ok(t, err)
ts := func() time.Time { return now }
// Make sure we can query histogram from both Prometheus instances.
queryAndAssert(t, ctx, prom1.Endpoint("http"), func() string { return testHistogramMetricName }, ts, promclient.QueryOptions{}, expectedHistogramModelVector(histograms[len(histograms)-1], nil))
queryAndAssert(t, ctx, prom2.Endpoint("http"), func() string { return testHistogramMetricName }, ts, promclient.QueryOptions{}, expectedHistogramModelVector(histograms[len(histograms)-1], nil))
t.Run("query deduplicated histogram", func(t *testing.T) {
queryAndAssert(t, ctx, querier.Endpoint("http"), func() string { return testHistogramMetricName }, ts, promclient.QueryOptions{Deduplicate: true}, expectedHistogramModelVector(histograms[len(histograms)-1], map[string]string{
"prometheus": "prom-ha",
}))
})
t.Run("query histogram using histogram_count fn and deduplication", func(t *testing.T) {
queryAndAssert(t, ctx, querier.Endpoint("http"), func() string { return fmt.Sprintf("histogram_count(%v)", testHistogramMetricName) }, ts, promclient.QueryOptions{Deduplicate: true}, model.Vector{
&model.Sample{
Value: 34,
Metric: model.Metric{
"foo": "bar",
"prometheus": "prom-ha",
},
},
})
})
t.Run("query histogram using group function for testing pushdown", func(t *testing.T) {
queryAndAssert(t, ctx, querier.Endpoint("http"), func() string { return fmt.Sprintf("group(%v)", testHistogramMetricName) }, ts, promclient.QueryOptions{Deduplicate: true}, model.Vector{
&model.Sample{
Value: 1,
Metric: model.Metric{},
},
})
})
t.Run("query histogram rate and compare to Prometheus result", func(t *testing.T) {
query := func() string { return fmt.Sprintf("rate(%v[1m])", testHistogramMetricName) }
expected := instantQuery(t, ctx, prom1.Endpoint("http"), query, ts, promclient.QueryOptions{}, 1)
expected[0].Metric["prometheus"] = "prom-ha"
expected[0].Timestamp = 0
queryAndAssert(t, ctx, querier.Endpoint("http"), query, ts, promclient.QueryOptions{Deduplicate: true}, expected)
})
}
func TestWriteNativeHistograms(t *testing.T) {
e, err := e2e.NewDockerEnvironment("nat-hist-write")
testutil.Ok(t, err)
t.Cleanup(e2ethanos.CleanScenario(t, e))
ingestor0 := e2ethanos.NewReceiveBuilder(e, "ingestor0").WithIngestionEnabled().WithNativeHistograms().Init()
ingestor1 := e2ethanos.NewReceiveBuilder(e, "ingestor1").WithIngestionEnabled().WithNativeHistograms().Init()
h := receive.HashringConfig{
Endpoints: []string{
ingestor0.InternalEndpoint("grpc"),
ingestor1.InternalEndpoint("grpc"),
},
}
router0 := e2ethanos.NewReceiveBuilder(e, "router0").WithRouting(2, h).Init()
testutil.Ok(t, e2e.StartAndWaitReady(ingestor0, ingestor1, router0))
querier := e2ethanos.NewQuerierBuilder(e, "querier", ingestor0.InternalEndpoint("grpc"), ingestor1.InternalEndpoint("grpc")).WithReplicaLabels("receive").Init()
testutil.Ok(t, e2e.StartAndWaitReady(querier))
rawRemoteWriteURL := "http://" + router0.Endpoint("remote-write") + "/api/v1/receive"
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Minute)
t.Cleanup(cancel)
histograms := tsdb.GenerateTestHistograms(1)
now := time.Now()
_, err = writeHistograms(ctx, now, histograms, rawRemoteWriteURL)
testutil.Ok(t, err)
ts := func() time.Time { return now }
queryAndAssert(t, ctx, querier.Endpoint("http"), func() string { return testHistogramMetricName }, ts, promclient.QueryOptions{Deduplicate: true}, expectedHistogramModelVector(histograms[0], map[string]string{
"tenant_id": "default-tenant",
}))
}
func TestQueryFrontendNativeHistograms(t *testing.T) {
e, err := e2e.NewDockerEnvironment("nat-hist-qfe")
testutil.Ok(t, err)
t.Cleanup(e2ethanos.CleanScenario(t, e))
prom1, sidecar1 := e2ethanos.NewPrometheusWithSidecar(e, "ha1", e2ethanos.DefaultPromConfig("prom-ha", 0, "", "", e2ethanos.LocalPrometheusTarget), "", e2ethanos.DefaultPrometheusImage(), "", "native-histograms", "remote-write-receiver")
prom2, sidecar2 := e2ethanos.NewPrometheusWithSidecar(e, "ha2", e2ethanos.DefaultPromConfig("prom-ha", 1, "", "", e2ethanos.LocalPrometheusTarget), "", e2ethanos.DefaultPrometheusImage(), "", "native-histograms", "remote-write-receiver")
testutil.Ok(t, e2e.StartAndWaitReady(prom1, sidecar1, prom2, sidecar2))
querier := e2ethanos.NewQuerierBuilder(e, "querier", sidecar1.InternalEndpoint("grpc"), sidecar2.InternalEndpoint("grpc")).Init()
testutil.Ok(t, e2e.StartAndWaitReady(querier))
inMemoryCacheConfig := queryfrontend.CacheProviderConfig{
Type: queryfrontend.INMEMORY,
Config: queryfrontend.InMemoryResponseCacheConfig{
MaxSizeItems: 1000,
Validity: time.Hour,
},
}
queryFrontend := e2ethanos.NewQueryFrontend(e, "query-frontend", "http://"+querier.InternalEndpoint("http"), queryfrontend.Config{}, inMemoryCacheConfig)
testutil.Ok(t, e2e.StartAndWaitReady(queryFrontend))
rawRemoteWriteURL1 := "http://" + prom1.Endpoint("http") + "/api/v1/write"
rawRemoteWriteURL2 := "http://" + prom2.Endpoint("http") + "/api/v1/write"
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Minute)
t.Cleanup(cancel)
histograms := tsdb.GenerateTestHistograms(4)
now := time.Now()
_, err = writeHistograms(ctx, now, histograms, rawRemoteWriteURL1)
testutil.Ok(t, err)
startTime, err := writeHistograms(ctx, now, histograms, rawRemoteWriteURL2)
testutil.Ok(t, err)
// Ensure we can get the result from Querier first so that it
// doesn't need to retry when we send queries to the frontend later.
queryAndAssertSeries(t, ctx, querier.Endpoint("http"), func() string { return testHistogramMetricName }, time.Now, promclient.QueryOptions{Deduplicate: true}, []model.Metric{
{
"__name__": testHistogramMetricName,
"prometheus": "prom-ha",
"foo": "bar",
},
})
vals, err := querier.SumMetrics([]string{"http_requests_total"})
e2emon.WithLabelMatchers(matchers.MustNewMatcher(matchers.MatchEqual, "handler", "query"))
testutil.Ok(t, err)
testutil.Equals(t, 1, len(vals))
queryTimes := vals[0]
ts := func() time.Time { return now }
t.Run("query frontend works for instant query", func(t *testing.T) {
queryAndAssert(t, ctx, queryFrontend.Endpoint("http"), func() string { return testHistogramMetricName }, ts, promclient.QueryOptions{Deduplicate: true}, expectedHistogramModelVector(histograms[len(histograms)-1], map[string]string{
"prometheus": "prom-ha",
}))
testutil.Ok(t, queryFrontend.WaitSumMetricsWithOptions(
e2emon.Equals(1),
[]string{"thanos_query_frontend_queries_total"},
e2emon.WithLabelMatchers(matchers.MustNewMatcher(matchers.MatchEqual, "op", "query")),
))
testutil.Ok(t, querier.WaitSumMetricsWithOptions(
e2emon.Equals(queryTimes+1),
[]string{"http_requests_total"},
e2emon.WithLabelMatchers(matchers.MustNewMatcher(matchers.MatchEqual, "handler", "query")),
))
})
t.Run("query range query, all but last histogram", func(t *testing.T) {
expectedRes := expectedHistogramModelMatrix(histograms[:len(histograms)-1], startTime, map[string]string{
"prometheus": "prom-ha",
})
// query all but last sample
rangeQuery(t, ctx, queryFrontend.Endpoint("http"), func() string { return testHistogramMetricName },
startTime.UnixMilli(),
// Skip last step, so that news samples is not queried and will be queried in next step.
now.Add(-30*time.Second).UnixMilli(),
30, // Taken from UI.
promclient.QueryOptions{
Deduplicate: true,
}, func(res model.Matrix) error {
if !reflect.DeepEqual(res, expectedRes) {
return fmt.Errorf("unexpected results (got %v but expected %v)", res, expectedRes)
}
return nil
})
testutil.Ok(t, queryFrontend.WaitSumMetrics(e2emon.Equals(1), "cortex_cache_fetched_keys_total"))
testutil.Ok(t, queryFrontend.WaitSumMetrics(e2emon.Equals(0), "cortex_cache_hits_total"))
testutil.Ok(t, queryFrontend.WaitSumMetrics(e2emon.Equals(1), "querier_cache_added_new_total"))
testutil.Ok(t, queryFrontend.WaitSumMetrics(e2emon.Equals(1), "querier_cache_added_total"))
testutil.Ok(t, queryFrontend.WaitSumMetrics(e2emon.Equals(1), "querier_cache_entries"))
testutil.Ok(t, queryFrontend.WaitSumMetrics(e2emon.Equals(1), "querier_cache_gets_total"))
testutil.Ok(t, queryFrontend.WaitSumMetrics(e2emon.Equals(1), "querier_cache_misses_total"))
testutil.Ok(t, querier.WaitSumMetricsWithOptions(
e2emon.Equals(1),
[]string{"http_requests_total"},
e2emon.WithLabelMatchers(matchers.MustNewMatcher(matchers.MatchEqual, "handler", "query_range")),
))
})
t.Run("query range, all histograms", func(t *testing.T) {
expectedRes := expectedHistogramModelMatrix(histograms, startTime, map[string]string{
"prometheus": "prom-ha",
})
rangeQuery(t, ctx, queryFrontend.Endpoint("http"), func() string { return testHistogramMetricName },
startTime.UnixMilli(),
now.UnixMilli(),
30, // Taken from UI.
promclient.QueryOptions{
Deduplicate: true,
}, func(res model.Matrix) error {
if !reflect.DeepEqual(res, expectedRes) {
return fmt.Errorf("unexpected results (got %v but expected %v)", res, expectedRes)
}
return nil
})
testutil.Ok(t, queryFrontend.WaitSumMetrics(e2emon.Equals(2), "cortex_cache_fetched_keys_total"))
testutil.Ok(t, queryFrontend.WaitSumMetrics(e2emon.Equals(1), "cortex_cache_hits_total"))
testutil.Ok(t, queryFrontend.WaitSumMetrics(e2emon.Equals(1), "querier_cache_added_new_total"))
testutil.Ok(t, queryFrontend.WaitSumMetrics(e2emon.Equals(2), "querier_cache_added_total"))
testutil.Ok(t, queryFrontend.WaitSumMetrics(e2emon.Equals(1), "querier_cache_entries"))
testutil.Ok(t, queryFrontend.WaitSumMetrics(e2emon.Equals(2), "querier_cache_gets_total"))
testutil.Ok(t, queryFrontend.WaitSumMetrics(e2emon.Equals(1), "querier_cache_misses_total"))
testutil.Ok(t, querier.WaitSumMetricsWithOptions(
e2emon.Equals(2),
[]string{"http_requests_total"},
e2emon.WithLabelMatchers(matchers.MustNewMatcher(matchers.MatchEqual, "handler", "query_range")),
))
})
t.Run("query range, all histograms again", func(t *testing.T) {
expectedRes := expectedHistogramModelMatrix(histograms, startTime, map[string]string{
"prometheus": "prom-ha",
})
rangeQuery(t, ctx, queryFrontend.Endpoint("http"), func() string { return testHistogramMetricName },
startTime.UnixMilli(),
now.UnixMilli(),
30, // Taken from UI.
promclient.QueryOptions{
Deduplicate: true,
}, func(res model.Matrix) error {
if !reflect.DeepEqual(res, expectedRes) {
return fmt.Errorf("unexpected results (got %v but expected %v)", res, expectedRes)
}
return nil
})
testutil.Ok(t, queryFrontend.WaitSumMetrics(e2emon.Equals(3), "cortex_cache_fetched_keys_total"))
testutil.Ok(t, queryFrontend.WaitSumMetrics(e2emon.Equals(2), "cortex_cache_hits_total"))
testutil.Ok(t, queryFrontend.WaitSumMetrics(e2emon.Equals(1), "querier_cache_added_new_total"))
testutil.Ok(t, queryFrontend.WaitSumMetrics(e2emon.Equals(3), "querier_cache_added_total"))
testutil.Ok(t, queryFrontend.WaitSumMetrics(e2emon.Equals(1), "querier_cache_entries"))
testutil.Ok(t, queryFrontend.WaitSumMetrics(e2emon.Equals(3), "querier_cache_gets_total"))
testutil.Ok(t, queryFrontend.WaitSumMetrics(e2emon.Equals(1), "querier_cache_misses_total"))
testutil.Ok(t, querier.WaitSumMetricsWithOptions(
e2emon.Equals(3),
[]string{"http_requests_total"},
e2emon.WithLabelMatchers(matchers.MustNewMatcher(matchers.MatchEqual, "handler", "query_range")),
))
})
}
func writeHistograms(ctx context.Context, now time.Time, histograms []*histogram.Histogram, rawRemoteWriteURL string) (time.Time, error) {
startTime := now.Add(time.Duration(len(histograms)-1) * -30 * time.Second).Truncate(30 * time.Second)
prompbHistograms := make([]prompb.Histogram, 0, len(histograms))
for i, h := range histograms {
ts := startTime.Add(time.Duration(i) * 30 * time.Second).UnixMilli()
prompbHistograms = append(prompbHistograms, remote.HistogramToHistogramProto(ts, h))
}
timeSeriespb := prompb.TimeSeries{
Labels: []prompb.Label{
{Name: "__name__", Value: testHistogramMetricName},
{Name: "foo", Value: "bar"},
},
Histograms: prompbHistograms,
}
return startTime, storeWriteRequest(ctx, rawRemoteWriteURL, &prompb.WriteRequest{
Timeseries: []prompb.TimeSeries{timeSeriespb},
})
}
func expectedHistogramModelVector(histogram *histogram.Histogram, externalLabels map[string]string) model.Vector {
metrics := model.Metric{
"__name__": testHistogramMetricName,
"foo": "bar",
}
for labelKey, labelValue := range externalLabels {
metrics[model.LabelName(labelKey)] = model.LabelValue(labelValue)
}
return model.Vector{
&model.Sample{
Metric: metrics,
Histogram: histogramToSampleHistogram(histogram),
},
}
}
func expectedHistogramModelMatrix(histograms []*histogram.Histogram, startTime time.Time, externalLabels map[string]string) model.Matrix {
metrics := model.Metric{
"__name__": testHistogramMetricName,
"foo": "bar",
}
for labelKey, labelValue := range externalLabels {
metrics[model.LabelName(labelKey)] = model.LabelValue(labelValue)
}
shp := make([]model.SampleHistogramPair, 0, len(histograms))
for i, h := range histograms {
shp = append(shp, model.SampleHistogramPair{
Timestamp: model.Time(startTime.Add(time.Duration(i) * 30 * time.Second).UnixMilli()),
Histogram: histogramToSampleHistogram(h),
})
}
return model.Matrix{
&model.SampleStream{
Metric: metrics,
Histograms: shp,
},
}
}
func histogramToSampleHistogram(h *histogram.Histogram) *model.SampleHistogram {
var buckets []*model.HistogramBucket
it := h.NegativeBucketIterator()
for it.Next() {
buckets = append([]*model.HistogramBucket{bucketToSampleHistogramBucket(it.At())}, buckets...)
}
buckets = append(buckets, bucketToSampleHistogramBucket(h.ZeroBucket()))
it = h.PositiveBucketIterator()
for it.Next() {
buckets = append(buckets, bucketToSampleHistogramBucket(it.At()))
}
return &model.SampleHistogram{
Count: model.FloatString(h.Count),
Sum: model.FloatString(h.Sum),
Buckets: buckets,
}
}
func bucketToSampleHistogramBucket(bucket histogram.Bucket[uint64]) *model.HistogramBucket {
return &model.HistogramBucket{
Lower: model.FloatString(bucket.Lower),
Upper: model.FloatString(bucket.Upper),
Count: model.FloatString(bucket.Count),
Boundaries: boundaries(bucket),
}
}
func boundaries(bucket histogram.Bucket[uint64]) int32 {
switch {
case bucket.UpperInclusive && !bucket.LowerInclusive:
return 0
case !bucket.UpperInclusive && bucket.LowerInclusive:
return 1
case !bucket.UpperInclusive && !bucket.LowerInclusive:
return 2
default:
return 3
}
}