Skip to content

Commit

Permalink
Fix error source coming from getFrames function (#142)
Browse files Browse the repository at this point in the history
* Fix error source coming from getFrames function

* Fix lint

* Trigger
  • Loading branch information
ivanahuckova authored Nov 8, 2024
1 parent f2dcaa2 commit aacfeb3
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 5 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ require (
github.com/go-sql-driver/mysql v1.8.1
github.com/google/go-cmp v0.6.0
github.com/grafana/dataplane/sdata v0.0.9
github.com/grafana/grafana-plugin-sdk-go v0.259.0
github.com/grafana/grafana-plugin-sdk-go v0.259.1
github.com/mithrandie/csvq-driver v1.7.0
github.com/prometheus/client_golang v1.20.5
github.com/stretchr/testify v1.9.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,8 @@ github.com/gorilla/mux v1.8.1 h1:TuBL49tXwgrFYWhqrNgrUNEY92u81SPhu7sTdzQEiWY=
github.com/gorilla/mux v1.8.1/go.mod h1:AKf9I4AEqPTmMytcMc0KkNouC66V3BtZ4qD5fmWSiMQ=
github.com/grafana/dataplane/sdata v0.0.9 h1:AGL1LZnCUG4MnQtnWpBPbQ8ZpptaZs14w6kE/MWfg7s=
github.com/grafana/dataplane/sdata v0.0.9/go.mod h1:Jvs5ddpGmn6vcxT7tCTWAZ1mgi4sbcdFt9utQx5uMAU=
github.com/grafana/grafana-plugin-sdk-go v0.259.0 h1:r/IbTzE89nqewLOpTVJUUnRaZUEu/3an+yIyBztUEd8=
github.com/grafana/grafana-plugin-sdk-go v0.259.0/go.mod h1:W0oyXqr67j7VuFqGRz6bwYnIVO/O0pscPNOzpa/oeUA=
github.com/grafana/grafana-plugin-sdk-go v0.259.1 h1:wrwblEszlWtHW90zz2Cm00cfRAdaY9iXDqdk/czJGEQ=
github.com/grafana/grafana-plugin-sdk-go v0.259.1/go.mod h1:W0oyXqr67j7VuFqGRz6bwYnIVO/O0pscPNOzpa/oeUA=
github.com/grafana/otel-profiling-go v0.5.1 h1:stVPKAFZSa7eGiqbYuG25VcqYksR6iWvF3YH66t4qL8=
github.com/grafana/otel-profiling-go v0.5.1/go.mod h1:ftN/t5A/4gQI19/8MoWurBEtC6gFw8Dns1sJZ9W4Tls=
github.com/grafana/pyroscope-go/godeltaprof v0.1.8 h1:iwOtYXeeVSAeYefJNaxDytgjKtUuKQbJqgAIjlnicKg=
Expand Down
25 changes: 23 additions & 2 deletions query.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import (
"github.com/grafana/grafana-plugin-sdk-go/backend"
"github.com/grafana/grafana-plugin-sdk-go/data"
"github.com/grafana/grafana-plugin-sdk-go/data/sqlutil"
"github.com/grafana/grafana-plugin-sdk-go/experimental/errorsource"
"github.com/grafana/grafana-plugin-sdk-go/experimental/status"
)

// FormatQueryOption defines how the user has chosen to represent the data
Expand Down Expand Up @@ -107,8 +109,13 @@ func (q *DBQuery) Run(ctx context.Context, query *Query, args ...interface{}) (d
// Convert the response to frames
res, err := getFrames(rows, -1, q.converters, q.fillMode, query)
if err != nil {
errWithSource := PluginError(fmt.Errorf("%w: %s", err, "Could not process SQL results"))
q.metrics.CollectDuration(SourcePlugin, StatusError, time.Since(start).Seconds())
// We default to plugin error source
errSource := status.SourcePlugin
if backend.IsDownstreamHTTPError(err) || isProcessingDownstreamError(err) {
errSource = status.SourceDownstream
}
errWithSource := errorsource.SourceError(errSource, fmt.Errorf("%w: %s", err, "Could not process SQL results"), false)
q.metrics.CollectDuration(Source(errSource), StatusError, time.Since(start).Seconds())
return sqlutil.ErrorFrameFromQuery(query), errWithSource
}

Expand Down Expand Up @@ -235,3 +242,17 @@ func applyHeaders(query *Query, headers http.Header) *Query {

return query
}

func isProcessingDownstreamError(err error) bool {
downstreamErrors := []error{
data.ErrorInputFieldsWithoutRows,
data.ErrorSeriesUnsorted,
data.ErrorNullTimeValues,
}
for _, e := range downstreamErrors {
if errors.Is(err, e) {
return true
}
}
return false
}
43 changes: 43 additions & 0 deletions query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,3 +188,46 @@ func TestLabelNameSanitization(t *testing.T) {
}
}
}

func TestIsProcessingDownstreamError(t *testing.T) {
tests := []struct {
name string
err error
expected bool
}{
{
name: "ErrorInputFieldsWithoutRows returns true",
err: data.ErrorInputFieldsWithoutRows,
expected: true,
},
{
name: "ErrorSeriesUnsorted returns true",
err: data.ErrorSeriesUnsorted,
expected: true,
},
{
name: "ErrorNullTimeValues returns true",
err: data.ErrorNullTimeValues,
expected: true,
},
{
name: "Different error returns false",
err: errors.New("some other error"),
expected: false,
},
{
name: "Wrapped downstream error returns true",
err: fmt.Errorf("wrapped: %w", data.ErrorInputFieldsWithoutRows),
expected: true,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := isProcessingDownstreamError(tt.err)
if result != tt.expected {
t.Errorf("isProcessingDownstreamError(%v) = %v; want %v", tt.err, result, tt.expected)
}
})
}
}

0 comments on commit aacfeb3

Please sign in to comment.