2022-09-19 13:26:18 +02:00
|
|
|
package remotewrite
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2023-02-22 11:26:19 +01:00
|
|
|
"math"
|
2022-09-19 13:26:18 +02:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/prompbmarshal"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestPushWriteRequest(t *testing.T) {
|
2023-02-21 03:38:49 +01:00
|
|
|
rowsCounts := []int{1, 10, 100, 1e3, 1e4}
|
2024-05-07 12:09:44 +02:00
|
|
|
expectedBlockLensProm := []int{248, 1952, 17433, 180381, 1861994}
|
|
|
|
expectedBlockLensVM := []int{170, 575, 4748, 44936, 367096}
|
2023-02-21 03:38:49 +01:00
|
|
|
for i, rowsCount := range rowsCounts {
|
|
|
|
expectedBlockLenProm := expectedBlockLensProm[i]
|
|
|
|
expectedBlockLenVM := expectedBlockLensVM[i]
|
2022-09-19 13:26:18 +02:00
|
|
|
t.Run(fmt.Sprintf("%d", rowsCount), func(t *testing.T) {
|
2023-02-21 03:38:49 +01:00
|
|
|
testPushWriteRequest(t, rowsCount, expectedBlockLenProm, expectedBlockLenVM)
|
2022-09-19 13:26:18 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-21 03:38:49 +01:00
|
|
|
func testPushWriteRequest(t *testing.T, rowsCount, expectedBlockLenProm, expectedBlockLenVM int) {
|
2023-02-22 11:26:19 +01:00
|
|
|
f := func(isVMRemoteWrite bool, expectedBlockLen int, tolerancePrc float64) {
|
2023-02-21 03:38:49 +01:00
|
|
|
t.Helper()
|
|
|
|
wr := newTestWriteRequest(rowsCount, 20)
|
|
|
|
pushBlockLen := 0
|
2023-11-24 13:42:11 +01:00
|
|
|
pushBlock := func(block []byte) bool {
|
2023-02-21 03:38:49 +01:00
|
|
|
if pushBlockLen > 0 {
|
|
|
|
panic(fmt.Errorf("BUG: pushBlock called multiple times; pushBlockLen=%d at first call, len(block)=%d at second call", pushBlockLen, len(block)))
|
|
|
|
}
|
|
|
|
pushBlockLen = len(block)
|
2023-11-24 13:42:11 +01:00
|
|
|
return true
|
2023-02-21 03:38:49 +01:00
|
|
|
}
|
2023-11-25 10:31:30 +01:00
|
|
|
if !tryPushWriteRequest(wr, pushBlock, isVMRemoteWrite) {
|
|
|
|
t.Fatalf("cannot push data to to remote storage")
|
|
|
|
}
|
2023-02-22 11:26:19 +01:00
|
|
|
if math.Abs(float64(pushBlockLen-expectedBlockLen)/float64(expectedBlockLen)*100) > tolerancePrc {
|
|
|
|
t.Fatalf("unexpected block len for rowsCount=%d, isVMRemoteWrite=%v; got %d bytes; expecting %d bytes +- %.0f%%",
|
|
|
|
rowsCount, isVMRemoteWrite, pushBlockLen, expectedBlockLen, tolerancePrc)
|
|
|
|
}
|
2022-09-19 13:26:18 +02:00
|
|
|
}
|
2023-02-21 03:38:49 +01:00
|
|
|
|
|
|
|
// Check Prometheus remote write
|
2024-01-14 22:04:45 +01:00
|
|
|
f(false, expectedBlockLenProm, 3)
|
2023-02-21 03:38:49 +01:00
|
|
|
|
|
|
|
// Check VictoriaMetrics remote write
|
2023-02-22 11:26:19 +01:00
|
|
|
f(true, expectedBlockLenVM, 15)
|
2022-09-19 13:26:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func newTestWriteRequest(seriesCount, labelsCount int) *prompbmarshal.WriteRequest {
|
|
|
|
var wr prompbmarshal.WriteRequest
|
|
|
|
for i := 0; i < seriesCount; i++ {
|
|
|
|
var labels []prompbmarshal.Label
|
|
|
|
for j := 0; j < labelsCount; j++ {
|
|
|
|
labels = append(labels, prompbmarshal.Label{
|
2022-09-19 14:13:43 +02:00
|
|
|
Name: fmt.Sprintf("label_%d_%d", i, j),
|
2022-09-19 13:26:18 +02:00
|
|
|
Value: fmt.Sprintf("value_%d_%d", i, j),
|
|
|
|
})
|
|
|
|
}
|
2024-05-07 12:09:44 +02:00
|
|
|
exemplar := prompbmarshal.Exemplar{
|
|
|
|
Labels: []prompbmarshal.Label{
|
|
|
|
{
|
|
|
|
Name: "trace_id",
|
|
|
|
Value: "123456",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "log_id",
|
|
|
|
Value: "987654",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Value: float64(i),
|
|
|
|
Timestamp: 1000 * int64(i),
|
|
|
|
}
|
2022-09-19 13:26:18 +02:00
|
|
|
wr.Timeseries = append(wr.Timeseries, prompbmarshal.TimeSeries{
|
|
|
|
Labels: labels,
|
|
|
|
Samples: []prompbmarshal.Sample{
|
|
|
|
{
|
2022-09-19 14:13:43 +02:00
|
|
|
Value: float64(i),
|
|
|
|
Timestamp: 1000 * int64(i),
|
2022-09-19 13:26:18 +02:00
|
|
|
},
|
|
|
|
},
|
2024-05-07 12:09:44 +02:00
|
|
|
|
|
|
|
Exemplars: []prompbmarshal.Exemplar{
|
|
|
|
exemplar,
|
|
|
|
},
|
2022-09-19 13:26:18 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
return &wr
|
|
|
|
}
|