loki: implement schema for loki query (#62114)

* loki: implement schema for loki query

* removed forgotten code

* better types

* adjust cue-maturity-level

Co-authored-by: Alex Khomenko <Clarity-89@users.noreply.github.com>

* sync docs to schema change

* removed forgotten go code

* updated schema docs

Co-authored-by: J Stickler <julie.stickler@grafana.com>

* updated schema docs

Co-authored-by: J Stickler <julie.stickler@grafana.com>

* update schema docs

Co-authored-by: J Stickler <julie.stickler@grafana.com>

* update schema docs

Co-authored-by: J Stickler <julie.stickler@grafana.com>

* updated schema docs

Co-authored-by: J Stickler <julie.stickler@grafana.com>

* updated schema docs

* add documentation to the resolution field

* move direction-field to schema

* loki: cue: moved deprecated fields to the bottom

---------

Co-authored-by: Alex Khomenko <Clarity-89@users.noreply.github.com>
Co-authored-by: J Stickler <julie.stickler@grafana.com>
This commit is contained in:
Gábor Farkas
2023-02-02 16:18:30 +00:00
committed by GitHub
co-authored by Alex Khomenko J Stickler
parent 753c84f825
commit 48a374f50b
9 changed files with 367 additions and 92 deletions
+52 -33
View File
@@ -53,48 +53,57 @@ func interpolateVariables(expr string, interval time.Duration, timeRange time.Du
return expr
}
func parseQueryType(jsonValue string) (QueryType, error) {
switch jsonValue {
case "instant":
return QueryTypeInstant, nil
case "range":
return QueryTypeRange, nil
case "":
func parseQueryType(jsonPointerValue *string) (QueryType, error) {
if jsonPointerValue == nil {
// there are older queries stored in alerting that did not have queryType,
// those were range-queries
return QueryTypeRange, nil
default:
return QueryTypeRange, fmt.Errorf("invalid queryType: %s", jsonValue)
} else {
jsonValue := *jsonPointerValue
switch jsonValue {
case "instant":
return QueryTypeInstant, nil
case "range":
return QueryTypeRange, nil
default:
return QueryTypeRange, fmt.Errorf("invalid queryType: %s", jsonValue)
}
}
}
func parseDirection(jsonValue string) (Direction, error) {
switch jsonValue {
case "backward":
return DirectionBackward, nil
case "forward":
return DirectionForward, nil
case "":
func parseDirection(jsonPointerValue *string) (Direction, error) {
if jsonPointerValue == nil {
// 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)
} else {
jsonValue := *jsonPointerValue
switch jsonValue {
case "backward":
return DirectionBackward, nil
case "forward":
return DirectionForward, nil
default:
return DirectionBackward, fmt.Errorf("invalid queryDirection: %s", jsonValue)
}
}
}
func parseSupportingQueryType(jsonValue string) (SupportingQueryType, error) {
switch jsonValue {
case "logsVolume":
return SupportingQueryLogsVolume, nil
case "logsSample":
return SupportingQueryLogsSample, nil
case "dataSample":
return SupportingQueryDataSample, nil
case "":
func parseSupportingQueryType(jsonPointerValue *string) (SupportingQueryType, error) {
if jsonPointerValue == nil {
return SupportingQueryNone, nil
default:
return SupportingQueryNone, fmt.Errorf("invalid supportingQueryType: %s", jsonValue)
} else {
jsonValue := *jsonPointerValue
switch jsonValue {
case "logsVolume":
return SupportingQueryLogsVolume, nil
case "logsSample":
return SupportingQueryLogsSample, nil
case "dataSample":
return SupportingQueryDataSample, nil
default:
return SupportingQueryNone, fmt.Errorf("invalid supportingQueryType: %s", jsonValue)
}
}
}
@@ -110,8 +119,8 @@ func parseQuery(queryContext *backend.QueryDataRequest) ([]*lokiQuery, error) {
end := query.TimeRange.To
var resolution int64 = 1
if model.Resolution >= 1 && model.Resolution <= 5 || model.Resolution == 10 {
resolution = model.Resolution
if model.Resolution != nil && (*model.Resolution >= 1 && *model.Resolution <= 5 || *model.Resolution == 10) {
resolution = *model.Resolution
}
interval := query.Interval
@@ -131,6 +140,16 @@ func parseQuery(queryContext *backend.QueryDataRequest) ([]*lokiQuery, error) {
return nil, err
}
var maxLines int64
if model.MaxLines != nil {
maxLines = *model.MaxLines
}
var legendFormat string
if model.LegendFormat != nil {
legendFormat = *model.LegendFormat
}
supportingQueryType, err := parseSupportingQueryType(model.SupportingQueryType)
if err != nil {
return nil, err
@@ -141,8 +160,8 @@ func parseQuery(queryContext *backend.QueryDataRequest) ([]*lokiQuery, error) {
QueryType: queryType,
Direction: direction,
Step: step,
MaxLines: model.MaxLines,
LegendFormat: model.LegendFormat,
MaxLines: int(maxLines),
LegendFormat: legendFormat,
Start: start,
End: end,
RefID: query.RefID,