2021-11-24 09:56:53 -06:00
|
|
|
package loki
|
|
|
|
|
|
|
|
import (
|
2022-02-25 02:14:17 -06:00
|
|
|
"fmt"
|
2021-11-24 09:56:53 -06:00
|
|
|
"math"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/grafana/grafana-plugin-sdk-go/backend"
|
2024-02-09 04:39:21 -06:00
|
|
|
"github.com/grafana/grafana-plugin-sdk-go/backend/gtime"
|
2023-01-30 02:50:27 -06:00
|
|
|
|
2023-08-03 09:27:23 -05:00
|
|
|
"github.com/grafana/grafana/pkg/tsdb/loki/kinds/dataquery"
|
2021-11-24 09:56:53 -06:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
varInterval = "$__interval"
|
|
|
|
varIntervalMs = "$__interval_ms"
|
|
|
|
varRange = "$__range"
|
|
|
|
varRangeS = "$__range_s"
|
|
|
|
varRangeMs = "$__range_ms"
|
2023-08-03 09:27:23 -05:00
|
|
|
varAuto = "$__auto"
|
2021-11-24 09:56:53 -06:00
|
|
|
)
|
|
|
|
|
2021-12-09 08:16:01 -06:00
|
|
|
const (
|
|
|
|
varIntervalAlt = "${__interval}"
|
|
|
|
varIntervalMsAlt = "${__interval_ms}"
|
|
|
|
varRangeAlt = "${__range}"
|
|
|
|
varRangeSAlt = "${__range_s}"
|
|
|
|
varRangeMsAlt = "${__range_ms}"
|
2023-08-03 09:27:23 -05:00
|
|
|
// $__auto is a new variable and we don't want to support this templating format
|
2021-12-09 08:16:01 -06:00
|
|
|
)
|
|
|
|
|
2023-08-03 09:27:23 -05:00
|
|
|
func interpolateVariables(expr string, interval time.Duration, timeRange time.Duration, queryType dataquery.LokiQueryType, step time.Duration) string {
|
2024-02-09 04:39:21 -06:00
|
|
|
intervalText := gtime.FormatInterval(interval)
|
|
|
|
stepText := gtime.FormatInterval(step)
|
2021-11-24 09:56:53 -06:00
|
|
|
intervalMsText := strconv.FormatInt(int64(interval/time.Millisecond), 10)
|
|
|
|
|
|
|
|
rangeMs := timeRange.Milliseconds()
|
|
|
|
rangeSRounded := int64(math.Round(float64(rangeMs) / 1000.0))
|
|
|
|
rangeMsText := strconv.FormatInt(rangeMs, 10)
|
|
|
|
rangeSText := strconv.FormatInt(rangeSRounded, 10)
|
|
|
|
|
|
|
|
expr = strings.ReplaceAll(expr, varIntervalMs, intervalMsText)
|
|
|
|
expr = strings.ReplaceAll(expr, varInterval, intervalText)
|
|
|
|
expr = strings.ReplaceAll(expr, varRangeMs, rangeMsText)
|
|
|
|
expr = strings.ReplaceAll(expr, varRangeS, rangeSText)
|
|
|
|
expr = strings.ReplaceAll(expr, varRange, rangeSText+"s")
|
2023-08-03 09:27:23 -05:00
|
|
|
if queryType == dataquery.LokiQueryTypeInstant {
|
|
|
|
expr = strings.ReplaceAll(expr, varAuto, rangeSText+"s")
|
|
|
|
}
|
|
|
|
|
|
|
|
if queryType == dataquery.LokiQueryTypeRange {
|
|
|
|
expr = strings.ReplaceAll(expr, varAuto, stepText)
|
|
|
|
}
|
2021-11-24 09:56:53 -06:00
|
|
|
|
2021-12-09 08:16:01 -06:00
|
|
|
// this is duplicated code, hopefully this can be handled in a nicer way when
|
|
|
|
// https://github.com/grafana/grafana/issues/42928 is done.
|
|
|
|
expr = strings.ReplaceAll(expr, varIntervalMsAlt, intervalMsText)
|
|
|
|
expr = strings.ReplaceAll(expr, varIntervalAlt, intervalText)
|
|
|
|
expr = strings.ReplaceAll(expr, varRangeMsAlt, rangeMsText)
|
|
|
|
expr = strings.ReplaceAll(expr, varRangeSAlt, rangeSText)
|
|
|
|
expr = strings.ReplaceAll(expr, varRangeAlt, rangeSText+"s")
|
2021-11-24 09:56:53 -06:00
|
|
|
return expr
|
|
|
|
}
|
|
|
|
|
2023-02-02 10:18:30 -06:00
|
|
|
func parseQueryType(jsonPointerValue *string) (QueryType, error) {
|
|
|
|
if jsonPointerValue == nil {
|
2022-02-25 02:14:17 -06:00
|
|
|
// there are older queries stored in alerting that did not have queryType,
|
|
|
|
// those were range-queries
|
|
|
|
return QueryTypeRange, nil
|
2023-02-02 10:18:30 -06:00
|
|
|
} else {
|
|
|
|
jsonValue := *jsonPointerValue
|
|
|
|
switch jsonValue {
|
|
|
|
case "instant":
|
|
|
|
return QueryTypeInstant, nil
|
|
|
|
case "range":
|
|
|
|
return QueryTypeRange, nil
|
|
|
|
default:
|
|
|
|
return QueryTypeRange, fmt.Errorf("invalid queryType: %s", jsonValue)
|
|
|
|
}
|
2022-02-25 02:14:17 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-02 10:18:30 -06:00
|
|
|
func parseDirection(jsonPointerValue *string) (Direction, error) {
|
|
|
|
if jsonPointerValue == nil {
|
2022-04-20 06:52:15 -05:00
|
|
|
// there are older queries stored in alerting that did not have queryDirection,
|
|
|
|
// we default to "backward"
|
|
|
|
return DirectionBackward, nil
|
2023-02-02 10:18:30 -06:00
|
|
|
} else {
|
|
|
|
jsonValue := *jsonPointerValue
|
|
|
|
switch jsonValue {
|
|
|
|
case "backward":
|
|
|
|
return DirectionBackward, nil
|
|
|
|
case "forward":
|
|
|
|
return DirectionForward, nil
|
2024-10-23 06:21:03 -05:00
|
|
|
case "scan":
|
|
|
|
return DirectionBackward, nil
|
2023-02-02 10:18:30 -06:00
|
|
|
default:
|
|
|
|
return DirectionBackward, fmt.Errorf("invalid queryDirection: %s", jsonValue)
|
|
|
|
}
|
2022-04-20 06:52:15 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-26 06:08:09 -05:00
|
|
|
func parseSupportingQueryType(jsonPointerValue *string) SupportingQueryType {
|
2023-02-02 10:18:30 -06:00
|
|
|
if jsonPointerValue == nil {
|
2024-03-26 06:08:09 -05:00
|
|
|
return SupportingQueryNone
|
|
|
|
}
|
|
|
|
|
|
|
|
jsonValue := *jsonPointerValue
|
|
|
|
switch jsonValue {
|
|
|
|
case "logsVolume":
|
|
|
|
return SupportingQueryLogsVolume
|
|
|
|
case "logsSample":
|
|
|
|
return SupportingQueryLogsSample
|
|
|
|
case "dataSample":
|
|
|
|
return SupportingQueryDataSample
|
|
|
|
case "infiniteScroll":
|
|
|
|
return SupportingQueryInfiniteScroll
|
|
|
|
case "":
|
|
|
|
return SupportingQueryNone
|
|
|
|
default:
|
|
|
|
// `SupportingQueryType` is just a `string` in the schema, so we can just parse this as a string
|
|
|
|
return SupportingQueryType(jsonValue)
|
2023-01-27 09:41:40 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-11-11 05:53:24 -06:00
|
|
|
func parseQuery(queryContext *backend.QueryDataRequest, logqlScopesEnabled bool) ([]*lokiQuery, error) {
|
2021-11-24 09:56:53 -06:00
|
|
|
qs := []*lokiQuery{}
|
|
|
|
for _, query := range queryContext.Queries {
|
2022-03-01 16:46:52 -06:00
|
|
|
model, err := parseQueryModel(query.JSON)
|
2021-11-24 09:56:53 -06:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
start := query.TimeRange.From
|
|
|
|
end := query.TimeRange.To
|
|
|
|
|
|
|
|
var resolution int64 = 1
|
2023-02-02 10:18:30 -06:00
|
|
|
if model.Resolution != nil && (*model.Resolution >= 1 && *model.Resolution <= 5 || *model.Resolution == 10) {
|
|
|
|
resolution = *model.Resolution
|
2021-11-24 09:56:53 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
interval := query.Interval
|
|
|
|
timeRange := query.TimeRange.To.Sub(query.TimeRange.From)
|
|
|
|
|
2023-06-16 11:08:29 -05:00
|
|
|
step, err := calculateStep(interval, timeRange, resolution, model.Step)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-11-24 09:56:53 -06:00
|
|
|
|
2022-02-25 02:14:17 -06:00
|
|
|
queryType, err := parseQueryType(model.QueryType)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2024-03-21 05:11:29 -05:00
|
|
|
expr := interpolateVariables(depointerizer(model.Expr), interval, timeRange, queryType, step)
|
2023-08-03 09:27:23 -05:00
|
|
|
|
2022-04-20 06:52:15 -05:00
|
|
|
direction, err := parseDirection(model.Direction)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-02-02 10:18:30 -06:00
|
|
|
var maxLines int64
|
|
|
|
if model.MaxLines != nil {
|
|
|
|
maxLines = *model.MaxLines
|
|
|
|
}
|
|
|
|
|
|
|
|
var legendFormat string
|
|
|
|
if model.LegendFormat != nil {
|
|
|
|
legendFormat = *model.LegendFormat
|
|
|
|
}
|
|
|
|
|
2024-11-11 05:53:24 -06:00
|
|
|
if logqlScopesEnabled {
|
|
|
|
rewrittenExpr, err := ApplyScopes(expr, model.Scopes)
|
|
|
|
if err == nil {
|
|
|
|
expr = rewrittenExpr
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-26 06:08:09 -05:00
|
|
|
supportingQueryType := parseSupportingQueryType(model.SupportingQueryType)
|
2023-01-27 09:41:40 -06:00
|
|
|
|
2021-11-24 09:56:53 -06:00
|
|
|
qs = append(qs, &lokiQuery{
|
2023-01-27 09:41:40 -06:00
|
|
|
Expr: expr,
|
|
|
|
QueryType: queryType,
|
|
|
|
Direction: direction,
|
|
|
|
Step: step,
|
2023-02-02 10:18:30 -06:00
|
|
|
MaxLines: int(maxLines),
|
|
|
|
LegendFormat: legendFormat,
|
2023-01-27 09:41:40 -06:00
|
|
|
Start: start,
|
|
|
|
End: end,
|
|
|
|
RefID: query.RefID,
|
|
|
|
SupportingQueryType: supportingQueryType,
|
2024-11-11 05:53:24 -06:00
|
|
|
Scopes: model.Scopes,
|
2021-11-24 09:56:53 -06:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return qs, nil
|
|
|
|
}
|
2024-03-21 05:11:29 -05:00
|
|
|
|
|
|
|
func depointerizer[T any](v *T) T {
|
|
|
|
var emptyValue T
|
|
|
|
if v != nil {
|
|
|
|
emptyValue = *v
|
|
|
|
}
|
|
|
|
|
|
|
|
return emptyValue
|
|
|
|
}
|