2021-03-02 07:55:16 -06:00
|
|
|
package loki
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2021-08-05 03:25:31 -05:00
|
|
|
"github.com/google/go-cmp/cmp"
|
|
|
|
"github.com/grafana/grafana-plugin-sdk-go/data"
|
2021-03-26 10:47:46 -05:00
|
|
|
"github.com/grafana/loki/pkg/loghttp"
|
2021-03-02 07:55:16 -06:00
|
|
|
p "github.com/prometheus/common/model"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestLoki(t *testing.T) {
|
|
|
|
t.Run("converting metric name", func(t *testing.T) {
|
|
|
|
metric := map[p.LabelName]p.LabelValue{
|
|
|
|
p.LabelName("app"): p.LabelValue("backend"),
|
|
|
|
p.LabelName("device"): p.LabelValue("mobile"),
|
|
|
|
}
|
|
|
|
|
2021-03-08 00:02:49 -06:00
|
|
|
query := &lokiQuery{
|
2021-03-02 07:55:16 -06:00
|
|
|
LegendFormat: "legend {{app}} {{ device }} {{broken}}",
|
|
|
|
}
|
|
|
|
|
|
|
|
require.Equal(t, "legend backend mobile ", formatLegend(metric, query))
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("build full series name", func(t *testing.T) {
|
|
|
|
metric := map[p.LabelName]p.LabelValue{
|
|
|
|
p.LabelName(p.MetricNameLabel): p.LabelValue("http_request_total"),
|
|
|
|
p.LabelName("app"): p.LabelValue("backend"),
|
|
|
|
p.LabelName("device"): p.LabelValue("mobile"),
|
|
|
|
}
|
|
|
|
|
2021-03-08 00:02:49 -06:00
|
|
|
query := &lokiQuery{
|
2021-03-02 07:55:16 -06:00
|
|
|
LegendFormat: "",
|
|
|
|
}
|
|
|
|
|
|
|
|
require.Equal(t, `http_request_total{app="backend", device="mobile"}`, formatLegend(metric, query))
|
|
|
|
})
|
|
|
|
}
|
2021-03-26 10:47:46 -05:00
|
|
|
|
|
|
|
func TestParseResponse(t *testing.T) {
|
|
|
|
t.Run("value is not of type matrix", func(t *testing.T) {
|
2021-08-05 03:25:31 -05:00
|
|
|
queryRes := data.Frames{}
|
2021-03-26 10:47:46 -05:00
|
|
|
value := loghttp.QueryResponse{
|
|
|
|
Data: loghttp.QueryResponseData{
|
|
|
|
Result: loghttp.Vector{},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
res, err := parseResponse(&value, nil)
|
|
|
|
require.Equal(t, queryRes, res)
|
|
|
|
require.Error(t, err)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("response should be parsed normally", func(t *testing.T) {
|
|
|
|
values := []p.SamplePair{
|
|
|
|
{Value: 1, Timestamp: 1000},
|
|
|
|
{Value: 2, Timestamp: 2000},
|
|
|
|
{Value: 3, Timestamp: 3000},
|
|
|
|
{Value: 4, Timestamp: 4000},
|
|
|
|
{Value: 5, Timestamp: 5000},
|
|
|
|
}
|
|
|
|
value := loghttp.QueryResponse{
|
|
|
|
Data: loghttp.QueryResponseData{
|
|
|
|
Result: loghttp.Matrix{
|
|
|
|
p.SampleStream{
|
|
|
|
Metric: p.Metric{"app": "Application", "tag2": "tag2"},
|
|
|
|
Values: values,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
query := &lokiQuery{
|
|
|
|
LegendFormat: "legend {{app}}",
|
|
|
|
}
|
2021-08-05 03:25:31 -05:00
|
|
|
frame, err := parseResponse(&value, query)
|
2021-03-26 10:47:46 -05:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2021-08-05 03:25:31 -05:00
|
|
|
labels, err := data.LabelsFromString("app=Application, tag2=tag2")
|
|
|
|
require.NoError(t, err)
|
|
|
|
field1 := data.NewField("time", nil, []time.Time{
|
|
|
|
time.Date(1970, 1, 1, 0, 0, 1, 0, time.UTC),
|
|
|
|
time.Date(1970, 1, 1, 0, 0, 2, 0, time.UTC),
|
|
|
|
time.Date(1970, 1, 1, 0, 0, 3, 0, time.UTC),
|
|
|
|
time.Date(1970, 1, 1, 0, 0, 4, 0, time.UTC),
|
|
|
|
time.Date(1970, 1, 1, 0, 0, 5, 0, time.UTC),
|
|
|
|
})
|
|
|
|
field2 := data.NewField("value", labels, []float64{1, 2, 3, 4, 5})
|
|
|
|
field2.SetConfig(&data.FieldConfig{DisplayNameFromDS: "legend Application"})
|
|
|
|
testFrame := data.NewFrame("legend Application", field1, field2)
|
|
|
|
|
|
|
|
if diff := cmp.Diff(testFrame, frame[0], data.FrameTestCompareOptions()...); diff != "" {
|
|
|
|
t.Errorf("Result mismatch (-want +got):\n%s", diff)
|
|
|
|
}
|
2021-03-26 10:47:46 -05:00
|
|
|
})
|
|
|
|
}
|