mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
Loki: add backend-forward mode to queries, update log-row-context (#47726)
* loki: add helper function to sort dataframe by time * loki: add direction-attribute to queries * loki: make log-row-context code backward-compatible * better comment Co-authored-by: Ivana Huckova <30407135+ivanahuckova@users.noreply.github.com> * fixed test * simplified code Co-authored-by: Ivana Huckova <30407135+ivanahuckova@users.noreply.github.com>
This commit is contained in:
@@ -29,6 +29,8 @@ func makeRequest(ctx context.Context, lokiDsUrl string, query lokiQuery) (*http.
|
||||
qs := url.Values{}
|
||||
qs.Set("query", query.Expr)
|
||||
|
||||
qs.Set("direction", string(query.Direction))
|
||||
|
||||
// MaxLines defaults to zero when not received,
|
||||
// and Loki does not like limit=0, even when it is not needed
|
||||
// (for example for metric queries), so we
|
||||
|
||||
@@ -22,9 +22,9 @@ import (
|
||||
// but i wanted to test for all of them, to be sure.
|
||||
|
||||
func TestSuccessResponse(t *testing.T) {
|
||||
matrixQuery := lokiQuery{Expr: "up(ALERTS)", Step: time.Second * 42, QueryType: QueryTypeRange}
|
||||
vectorQuery := lokiQuery{Expr: "query1", QueryType: QueryTypeInstant}
|
||||
streamsQuery := lokiQuery{Expr: "query1", QueryType: QueryTypeRange}
|
||||
matrixQuery := lokiQuery{Expr: "up(ALERTS)", Step: time.Second * 42, QueryType: QueryTypeRange, Direction: DirectionBackward}
|
||||
vectorQuery := lokiQuery{Expr: "query1", QueryType: QueryTypeInstant, Direction: DirectionBackward}
|
||||
streamsQuery := lokiQuery{Expr: "query1", QueryType: QueryTypeRange, Direction: DirectionBackward}
|
||||
|
||||
tt := []struct {
|
||||
name string
|
||||
@@ -119,7 +119,7 @@ func TestErrorResponse(t *testing.T) {
|
||||
|
||||
for _, test := range tt {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
frames, err := runQuery(context.Background(), makeMockedAPI(400, test.contentType, test.body), &lokiQuery{QueryType: QueryTypeRange})
|
||||
frames, err := runQuery(context.Background(), makeMockedAPI(400, test.contentType, test.body), &lokiQuery{QueryType: QueryTypeRange, Direction: DirectionBackward})
|
||||
|
||||
require.Len(t, frames, 0)
|
||||
require.Error(t, err)
|
||||
|
||||
@@ -53,6 +53,7 @@ type datasourceInfo struct {
|
||||
type QueryJSONModel struct {
|
||||
QueryType string `json:"queryType"`
|
||||
Expr string `json:"expr"`
|
||||
Direction string `json:"direction"`
|
||||
LegendFormat string `json:"legendFormat"`
|
||||
Interval string `json:"interval"`
|
||||
IntervalMS int `json:"intervalMS"`
|
||||
|
||||
@@ -67,6 +67,21 @@ func parseQueryType(jsonValue string) (QueryType, error) {
|
||||
}
|
||||
}
|
||||
|
||||
func parseDirection(jsonValue string) (Direction, error) {
|
||||
switch jsonValue {
|
||||
case "backward":
|
||||
return DirectionBackward, nil
|
||||
case "forward":
|
||||
return DirectionForward, nil
|
||||
case "":
|
||||
// there are older queries stored in alerting that did not have queryDirection,
|
||||
// we default to "backward"
|
||||
return DirectionBackward, nil
|
||||
default:
|
||||
return DirectionBackward, fmt.Errorf("invalid queryDirection: %s", jsonValue)
|
||||
}
|
||||
}
|
||||
|
||||
func parseQuery(queryContext *backend.QueryDataRequest) ([]*lokiQuery, error) {
|
||||
qs := []*lokiQuery{}
|
||||
for _, query := range queryContext.Queries {
|
||||
@@ -95,9 +110,15 @@ func parseQuery(queryContext *backend.QueryDataRequest) ([]*lokiQuery, error) {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
direction, err := parseDirection(model.Direction)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
qs = append(qs, &lokiQuery{
|
||||
Expr: expr,
|
||||
QueryType: queryType,
|
||||
Direction: direction,
|
||||
Step: step,
|
||||
MaxLines: model.MaxLines,
|
||||
LegendFormat: model.LegendFormat,
|
||||
|
||||
@@ -9,9 +9,17 @@ const (
|
||||
QueryTypeInstant QueryType = "instant"
|
||||
)
|
||||
|
||||
type Direction string
|
||||
|
||||
const (
|
||||
DirectionBackward Direction = "backward"
|
||||
DirectionForward Direction = "forward"
|
||||
)
|
||||
|
||||
type lokiQuery struct {
|
||||
Expr string
|
||||
QueryType QueryType
|
||||
Direction Direction
|
||||
Step time.Duration
|
||||
MaxLines int
|
||||
LegendFormat string
|
||||
|
||||
Reference in New Issue
Block a user