mirror of https://github.com/grafana/loki
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
253 lines
5.4 KiB
253 lines
5.4 KiB
|
2 years ago
|
package queryrange
|
||
|
|
|
||
|
|
import (
|
||
|
|
"testing"
|
||
|
|
"time"
|
||
|
|
|
||
|
|
"github.com/stretchr/testify/require"
|
||
|
|
|
||
|
|
"github.com/grafana/loki/pkg/loghttp"
|
||
|
|
"github.com/grafana/loki/pkg/logproto"
|
||
|
|
"github.com/grafana/loki/pkg/push"
|
||
|
|
"github.com/grafana/loki/pkg/querier/queryrange/queryrangebase"
|
||
|
|
)
|
||
|
|
|
||
|
|
const forRangeQuery = false
|
||
|
|
const forInstantQuery = true
|
||
|
|
const aggregateBySeries = true
|
||
|
|
const aggregateByLabels = false
|
||
|
|
|
||
|
|
func Test_toPrometheusResponse(t *testing.T) {
|
||
|
|
t2 := time.Now()
|
||
|
|
t1 := t2.Add(-1 * time.Minute)
|
||
|
|
|
||
|
|
setup := func(instant bool, nameOne, nameTwo string) chan *bucketedVolumeResponse {
|
||
|
|
collector := make(chan *bucketedVolumeResponse, 2)
|
||
|
|
defer close(collector)
|
||
|
|
|
||
|
|
collector <- &bucketedVolumeResponse{
|
||
|
|
t1, &VolumeResponse{
|
||
|
|
Response: &logproto.VolumeResponse{
|
||
|
|
Volumes: []logproto.Volume{
|
||
|
|
{
|
||
|
|
Name: nameOne,
|
||
|
|
Volume: 100,
|
||
|
|
},
|
||
|
|
{
|
||
|
|
Name: nameTwo,
|
||
|
|
Volume: 50,
|
||
|
|
},
|
||
|
|
},
|
||
|
|
Limit: 10,
|
||
|
|
},
|
||
|
|
},
|
||
|
|
}
|
||
|
|
|
||
|
|
if !instant {
|
||
|
|
collector <- &bucketedVolumeResponse{
|
||
|
|
t2, &VolumeResponse{
|
||
|
|
Response: &logproto.VolumeResponse{
|
||
|
|
Volumes: []logproto.Volume{
|
||
|
|
{
|
||
|
|
Name: nameOne,
|
||
|
|
Volume: 50,
|
||
|
|
},
|
||
|
|
{
|
||
|
|
Name: nameTwo,
|
||
|
|
Volume: 25,
|
||
|
|
},
|
||
|
|
},
|
||
|
|
Limit: 10,
|
||
|
|
},
|
||
|
|
},
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
return collector
|
||
|
|
}
|
||
|
|
|
||
|
|
setupSeries := func(instant bool) chan *bucketedVolumeResponse {
|
||
|
2 years ago
|
return setup(instant, `{foo="baz"}`, `{foo="bar", fizz="buzz"}`)
|
||
|
2 years ago
|
}
|
||
|
|
|
||
|
|
setupLabels := func(instant bool) chan *bucketedVolumeResponse {
|
||
|
|
return setup(instant, `foo`, `fizz`)
|
||
|
|
}
|
||
|
|
|
||
|
|
t.Run("it converts series volumes with multiple timestamps into a prometheus timeseries matix response", func(t *testing.T) {
|
||
|
|
collector := setupSeries(forRangeQuery)
|
||
|
|
promResp := ToPrometheusResponse(collector, aggregateBySeries)
|
||
|
|
require.Equal(t, queryrangebase.PrometheusData{
|
||
|
|
ResultType: loghttp.ResultTypeMatrix,
|
||
|
|
Result: []queryrangebase.SampleStream{
|
||
|
|
{
|
||
|
|
Labels: []push.LabelAdapter{
|
||
|
|
{
|
||
|
|
Name: "foo",
|
||
|
2 years ago
|
Value: "baz",
|
||
|
2 years ago
|
},
|
||
|
|
},
|
||
|
|
Samples: []logproto.LegacySample{
|
||
|
|
{
|
||
|
|
Value: 100,
|
||
|
|
TimestampMs: t1.UnixNano() / 1e6,
|
||
|
|
},
|
||
|
|
{
|
||
|
|
Value: 50,
|
||
|
|
TimestampMs: t2.UnixNano() / 1e6,
|
||
|
|
},
|
||
|
|
},
|
||
|
|
},
|
||
|
|
{
|
||
|
|
Labels: []push.LabelAdapter{
|
||
|
2 years ago
|
{
|
||
|
|
Name: "fizz",
|
||
|
|
Value: "buzz",
|
||
|
|
},
|
||
|
2 years ago
|
{
|
||
|
|
Name: "foo",
|
||
|
2 years ago
|
Value: "bar",
|
||
|
2 years ago
|
},
|
||
|
|
},
|
||
|
|
Samples: []logproto.LegacySample{
|
||
|
|
{
|
||
|
|
Value: 50,
|
||
|
|
TimestampMs: t1.UnixNano() / 1e6,
|
||
|
|
},
|
||
|
|
{
|
||
|
|
Value: 25,
|
||
|
|
TimestampMs: t2.UnixNano() / 1e6,
|
||
|
|
},
|
||
|
|
},
|
||
|
|
},
|
||
|
|
},
|
||
|
|
}, promResp.Response.Data)
|
||
|
|
})
|
||
|
|
|
||
|
|
t.Run("it converts series volumes with a single timestamp into a prometheus timeseries vector response", func(t *testing.T) {
|
||
|
|
collector := setupSeries(forInstantQuery)
|
||
|
|
promResp := ToPrometheusResponse(collector, aggregateBySeries)
|
||
|
|
require.Equal(t, queryrangebase.PrometheusData{
|
||
|
|
ResultType: loghttp.ResultTypeVector,
|
||
|
|
Result: []queryrangebase.SampleStream{
|
||
|
|
{
|
||
|
|
Labels: []push.LabelAdapter{
|
||
|
|
{
|
||
|
|
Name: "foo",
|
||
|
2 years ago
|
Value: "baz",
|
||
|
2 years ago
|
},
|
||
|
|
},
|
||
|
|
Samples: []logproto.LegacySample{
|
||
|
|
{
|
||
|
|
Value: 100,
|
||
|
|
TimestampMs: t1.UnixNano() / 1e6,
|
||
|
|
},
|
||
|
|
},
|
||
|
|
},
|
||
|
|
{
|
||
|
|
Labels: []push.LabelAdapter{
|
||
|
2 years ago
|
{
|
||
|
|
Name: "fizz",
|
||
|
|
Value: "buzz",
|
||
|
|
},
|
||
|
2 years ago
|
{
|
||
|
|
Name: "foo",
|
||
|
2 years ago
|
Value: "bar",
|
||
|
2 years ago
|
},
|
||
|
|
},
|
||
|
|
Samples: []logproto.LegacySample{
|
||
|
|
{
|
||
|
|
Value: 50,
|
||
|
|
TimestampMs: t1.UnixNano() / 1e6,
|
||
|
|
},
|
||
|
|
},
|
||
|
|
},
|
||
|
|
},
|
||
|
|
}, promResp.Response.Data)
|
||
|
|
})
|
||
|
|
|
||
|
|
t.Run("it converts label volumes with multiple timestamps into a prometheus timeseries matrix response", func(t *testing.T) {
|
||
|
|
collector := setupLabels(forRangeQuery)
|
||
|
|
promResp := ToPrometheusResponse(collector, aggregateByLabels)
|
||
|
|
require.Equal(t, queryrangebase.PrometheusData{
|
||
|
|
ResultType: loghttp.ResultTypeMatrix,
|
||
|
|
Result: []queryrangebase.SampleStream{
|
||
|
|
{
|
||
|
|
Labels: []push.LabelAdapter{
|
||
|
|
{
|
||
|
2 years ago
|
Name: "foo",
|
||
|
2 years ago
|
Value: "",
|
||
|
|
},
|
||
|
|
},
|
||
|
|
Samples: []logproto.LegacySample{
|
||
|
|
{
|
||
|
2 years ago
|
Value: 100,
|
||
|
2 years ago
|
TimestampMs: t1.UnixNano() / 1e6,
|
||
|
|
},
|
||
|
|
{
|
||
|
2 years ago
|
Value: 50,
|
||
|
2 years ago
|
TimestampMs: t2.UnixNano() / 1e6,
|
||
|
|
},
|
||
|
|
},
|
||
|
|
},
|
||
|
|
{
|
||
|
|
Labels: []push.LabelAdapter{
|
||
|
|
{
|
||
|
2 years ago
|
Name: "fizz",
|
||
|
2 years ago
|
Value: "",
|
||
|
|
},
|
||
|
|
},
|
||
|
|
Samples: []logproto.LegacySample{
|
||
|
|
{
|
||
|
2 years ago
|
Value: 50,
|
||
|
2 years ago
|
TimestampMs: t1.UnixNano() / 1e6,
|
||
|
|
},
|
||
|
|
{
|
||
|
2 years ago
|
Value: 25,
|
||
|
2 years ago
|
TimestampMs: t2.UnixNano() / 1e6,
|
||
|
|
},
|
||
|
|
},
|
||
|
|
},
|
||
|
|
},
|
||
|
|
}, promResp.Response.Data)
|
||
|
|
})
|
||
|
|
|
||
|
|
t.Run("it converts label volumes with a single timestamp into a prometheus timeseries vector response", func(t *testing.T) {
|
||
|
|
collector := setupLabels(forInstantQuery)
|
||
|
|
promResp := ToPrometheusResponse(collector, aggregateByLabels)
|
||
|
|
require.Equal(t, queryrangebase.PrometheusData{
|
||
|
|
ResultType: loghttp.ResultTypeVector,
|
||
|
|
Result: []queryrangebase.SampleStream{
|
||
|
|
{
|
||
|
|
Labels: []push.LabelAdapter{
|
||
|
|
{
|
||
|
2 years ago
|
Name: "foo",
|
||
|
2 years ago
|
Value: "",
|
||
|
|
},
|
||
|
|
},
|
||
|
|
Samples: []logproto.LegacySample{
|
||
|
|
{
|
||
|
2 years ago
|
Value: 100,
|
||
|
2 years ago
|
TimestampMs: t1.UnixNano() / 1e6,
|
||
|
|
},
|
||
|
|
},
|
||
|
|
},
|
||
|
|
{
|
||
|
|
Labels: []push.LabelAdapter{
|
||
|
|
{
|
||
|
2 years ago
|
Name: "fizz",
|
||
|
2 years ago
|
Value: "",
|
||
|
|
},
|
||
|
|
},
|
||
|
|
Samples: []logproto.LegacySample{
|
||
|
|
{
|
||
|
2 years ago
|
Value: 50,
|
||
|
2 years ago
|
TimestampMs: t1.UnixNano() / 1e6,
|
||
|
|
},
|
||
|
|
},
|
||
|
|
},
|
||
|
|
},
|
||
|
|
}, promResp.Response.Data)
|
||
|
|
})
|
||
|
|
}
|