2022-09-19 11:26:18 +00:00
|
|
|
package remotewrite
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2023-02-22 10:26:19 +00:00
|
|
|
"math"
|
2022-09-19 11:26:18 +00:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/prompbmarshal"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestPushWriteRequest(t *testing.T) {
|
2023-02-21 02:38:49 +00:00
|
|
|
rowsCounts := []int{1, 10, 100, 1e3, 1e4}
|
Revert "Exemplar support (#5982)"
This reverts commit 5a3abfa0414ab495cbc34a58146b540aa8289636.
Reason for revert: exemplars aren't in wide use because they have numerous issues which prevent their adoption (see below).
Adding support for examplars into VictoriaMetrics introduces non-trivial code changes. These code changes need to be supported forever
once the release of VictoriaMetrics with exemplar support is published. That's why I don't think this is a good feature despite
that the source code of the reverted commit has an excellent quality. See https://docs.victoriametrics.com/goals/ .
Issues with Prometheus exemplars:
- Prometheus still has only experimental support for exemplars after more than three years since they were introduced.
It stores exemplars in memory, so they are lost after Prometheus restart. This doesn't look like production-ready feature.
See https://github.com/prometheus/docs/blob/0a2f3b37940e2949eefe752ed7b6c768e0b00128/content/docs/instrumenting/exposition_formats.md?plain=1#L153-L159
and https://prometheus.io/docs/prometheus/latest/feature_flags/#exemplars-storage
- It is very non-trivial to expose exemplars alongside metrics in your application, since the official Prometheus SDKs
for metrics' exposition ( https://prometheus.io/docs/instrumenting/clientlibs/ ) either have very hard-to-use API
for exposing histograms or do not have this API at all. For example, try figuring out how to expose exemplars
via https://pkg.go.dev/github.com/prometheus/client_golang@v1.19.1/prometheus .
- It looks like exemplars are supported for Histogram metric types only -
see https://pkg.go.dev/github.com/prometheus/client_golang@v1.19.1/prometheus#Timer.ObserveDurationWithExemplar .
Exemplars aren't supported for Counter, Gauge and Summary metric types.
- Grafana has very poor support for Prometheus exemplars. It looks like it supports exemplars only when the query
contains histogram_quantile() function. It queries exemplars via special Prometheus API -
https://prometheus.io/docs/prometheus/latest/querying/api/#querying-exemplars - (which is still marked as experimental, btw.)
and then displays all the returned exemplars on the graph as special dots. The issue is that this doesn't work
in production in most cases when the histogram_quantile() is calculated over thousands of histogram buckets
exposed by big number of application instances. Every histogram bucket may expose an exemplar on every timestamp shown on the graph.
This makes the graph unusable, since it is litterally filled with thousands of exemplar dots.
Neither Prometheus API nor Grafana doesn't provide the ability to filter out unneeded exemplars.
- Exemplars are usually connected to traces. While traces are good for some
I doubt exemplars will become production-ready in the near future because of the issues outlined above.
Alternative to exemplars:
Exemplars are marketed as a silver bullet for the correlation between metrics, traces and logs -
just click the exemplar dot on some graph in Grafana and instantly see the corresponding trace or log entry!
This doesn't work as expected in production as shown above. Are there better solutions, which work in production?
Yes - just use time-based and label-based correlation between metrics, traces and logs. Assign the same `job`
and `instance` labels to metrics, logs and traces, so you can quickly find the needed trace or log entry
by these labes on the time range with the anomaly on metrics' graph.
2024-07-03 13:30:11 +00:00
|
|
|
expectedBlockLensProm := []int{216, 1848, 16424, 169882, 1757876}
|
|
|
|
expectedBlockLensVM := []int{138, 492, 3927, 34995, 288476}
|
2023-02-21 02:38:49 +00:00
|
|
|
for i, rowsCount := range rowsCounts {
|
|
|
|
expectedBlockLenProm := expectedBlockLensProm[i]
|
|
|
|
expectedBlockLenVM := expectedBlockLensVM[i]
|
2022-09-19 11:26:18 +00:00
|
|
|
t.Run(fmt.Sprintf("%d", rowsCount), func(t *testing.T) {
|
2023-02-21 02:38:49 +00:00
|
|
|
testPushWriteRequest(t, rowsCount, expectedBlockLenProm, expectedBlockLenVM)
|
2022-09-19 11:26:18 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-21 02:38:49 +00:00
|
|
|
func testPushWriteRequest(t *testing.T, rowsCount, expectedBlockLenProm, expectedBlockLenVM int) {
|
2023-02-22 10:26:19 +00:00
|
|
|
f := func(isVMRemoteWrite bool, expectedBlockLen int, tolerancePrc float64) {
|
2023-02-21 02:38:49 +00:00
|
|
|
t.Helper()
|
|
|
|
wr := newTestWriteRequest(rowsCount, 20)
|
|
|
|
pushBlockLen := 0
|
2023-11-24 12:42:11 +00:00
|
|
|
pushBlock := func(block []byte) bool {
|
2023-02-21 02:38:49 +00: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 12:42:11 +00:00
|
|
|
return true
|
2023-02-21 02:38:49 +00:00
|
|
|
}
|
2023-11-25 09:31:30 +00:00
|
|
|
if !tryPushWriteRequest(wr, pushBlock, isVMRemoteWrite) {
|
2024-05-29 12:08:38 +00:00
|
|
|
t.Fatalf("cannot push data to remote storage")
|
2023-11-25 09:31:30 +00:00
|
|
|
}
|
2023-02-22 10:26:19 +00: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 11:26:18 +00:00
|
|
|
}
|
2023-02-21 02:38:49 +00:00
|
|
|
|
|
|
|
// Check Prometheus remote write
|
2024-01-14 21:04:45 +00:00
|
|
|
f(false, expectedBlockLenProm, 3)
|
2023-02-21 02:38:49 +00:00
|
|
|
|
|
|
|
// Check VictoriaMetrics remote write
|
2023-02-22 10:26:19 +00:00
|
|
|
f(true, expectedBlockLenVM, 15)
|
2022-09-19 11:26:18 +00: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 12:13:43 +00:00
|
|
|
Name: fmt.Sprintf("label_%d_%d", i, j),
|
2022-09-19 11:26:18 +00:00
|
|
|
Value: fmt.Sprintf("value_%d_%d", i, j),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
wr.Timeseries = append(wr.Timeseries, prompbmarshal.TimeSeries{
|
|
|
|
Labels: labels,
|
|
|
|
Samples: []prompbmarshal.Sample{
|
|
|
|
{
|
2022-09-19 12:13:43 +00:00
|
|
|
Value: float64(i),
|
|
|
|
Timestamp: 1000 * int64(i),
|
2022-09-19 11:26:18 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
return &wr
|
|
|
|
}
|