SQL Expressions: Rework backend errors and error instrumentation (#109633)

* Capture error_type label on metrics/traces
* Make error messages more helpful to user
* Use errutil, categorized errors, and tie them to error_type (category in code)
* Misc trace fixes
* Add metric to track SQL input conversion
This commit is contained in:
Kyle Brandt
2025-08-25 11:13:42 -04:00
committed by GitHub
parent 539b413584
commit 4f0cb47d3c
18 changed files with 858 additions and 299 deletions
-30
View File
@@ -3,8 +3,6 @@ package expr
import (
"errors"
"fmt"
"sort"
"strings"
"github.com/grafana/grafana/pkg/apimachinery/errutil"
)
@@ -95,31 +93,3 @@ func makeUnexpectedNodeTypeError(refID, nodeType string) error {
return UnexpectedNodeTypeError.Build(data)
}
var DuplicateStringColumnError = errutil.NewBase(
errutil.StatusBadRequest, "sse.duplicateStringColumns").MustTemplate(
"your SQL query returned {{ .Public.count }} rows with duplicate values across the string columns, which is not allowed for alerting. Examples: ({{ .Public.examples }}). Hint: use GROUP BY or aggregation (e.g. MAX(), AVG()) to return one row per unique combination.",
errutil.WithPublic("SQL query returned duplicate combinations of string column values. Use GROUP BY or aggregation to return one row per combination."),
)
func makeDuplicateStringColumnError(examples []string) error {
const limit = 5
sort.Strings(examples)
exampleStr := strings.Join(truncateExamples(examples, limit), ", ")
return DuplicateStringColumnError.Build(errutil.TemplateData{
Public: map[string]any{
"examples": exampleStr,
"count": len(examples),
},
})
}
func truncateExamples(examples []string, limit int) []string {
if len(examples) <= limit {
return examples
}
truncated := examples[:limit]
truncated = append(truncated, fmt.Sprintf("... and %d more", len(examples)-limit))
return truncated
}