Skip to content

Commit aacfeb3

Browse files
authored
Fix error source coming from getFrames function (#142)
* Fix error source coming from getFrames function * Fix lint * Trigger
1 parent f2dcaa2 commit aacfeb3

File tree

4 files changed

+69
-5
lines changed

4 files changed

+69
-5
lines changed

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ require (
88
github.com/go-sql-driver/mysql v1.8.1
99
github.com/google/go-cmp v0.6.0
1010
github.com/grafana/dataplane/sdata v0.0.9
11-
github.com/grafana/grafana-plugin-sdk-go v0.259.0
11+
github.com/grafana/grafana-plugin-sdk-go v0.259.1
1212
github.com/mithrandie/csvq-driver v1.7.0
1313
github.com/prometheus/client_golang v1.20.5
1414
github.com/stretchr/testify v1.9.0

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,8 @@ github.com/gorilla/mux v1.8.1 h1:TuBL49tXwgrFYWhqrNgrUNEY92u81SPhu7sTdzQEiWY=
7171
github.com/gorilla/mux v1.8.1/go.mod h1:AKf9I4AEqPTmMytcMc0KkNouC66V3BtZ4qD5fmWSiMQ=
7272
github.com/grafana/dataplane/sdata v0.0.9 h1:AGL1LZnCUG4MnQtnWpBPbQ8ZpptaZs14w6kE/MWfg7s=
7373
github.com/grafana/dataplane/sdata v0.0.9/go.mod h1:Jvs5ddpGmn6vcxT7tCTWAZ1mgi4sbcdFt9utQx5uMAU=
74-
github.com/grafana/grafana-plugin-sdk-go v0.259.0 h1:r/IbTzE89nqewLOpTVJUUnRaZUEu/3an+yIyBztUEd8=
75-
github.com/grafana/grafana-plugin-sdk-go v0.259.0/go.mod h1:W0oyXqr67j7VuFqGRz6bwYnIVO/O0pscPNOzpa/oeUA=
74+
github.com/grafana/grafana-plugin-sdk-go v0.259.1 h1:wrwblEszlWtHW90zz2Cm00cfRAdaY9iXDqdk/czJGEQ=
75+
github.com/grafana/grafana-plugin-sdk-go v0.259.1/go.mod h1:W0oyXqr67j7VuFqGRz6bwYnIVO/O0pscPNOzpa/oeUA=
7676
github.com/grafana/otel-profiling-go v0.5.1 h1:stVPKAFZSa7eGiqbYuG25VcqYksR6iWvF3YH66t4qL8=
7777
github.com/grafana/otel-profiling-go v0.5.1/go.mod h1:ftN/t5A/4gQI19/8MoWurBEtC6gFw8Dns1sJZ9W4Tls=
7878
github.com/grafana/pyroscope-go/godeltaprof v0.1.8 h1:iwOtYXeeVSAeYefJNaxDytgjKtUuKQbJqgAIjlnicKg=

query.go

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ import (
1313
"github.com/grafana/grafana-plugin-sdk-go/backend"
1414
"github.com/grafana/grafana-plugin-sdk-go/data"
1515
"github.com/grafana/grafana-plugin-sdk-go/data/sqlutil"
16+
"github.com/grafana/grafana-plugin-sdk-go/experimental/errorsource"
17+
"github.com/grafana/grafana-plugin-sdk-go/experimental/status"
1618
)
1719

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

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

236243
return query
237244
}
245+
246+
func isProcessingDownstreamError(err error) bool {
247+
downstreamErrors := []error{
248+
data.ErrorInputFieldsWithoutRows,
249+
data.ErrorSeriesUnsorted,
250+
data.ErrorNullTimeValues,
251+
}
252+
for _, e := range downstreamErrors {
253+
if errors.Is(err, e) {
254+
return true
255+
}
256+
}
257+
return false
258+
}

query_test.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,3 +188,46 @@ func TestLabelNameSanitization(t *testing.T) {
188188
}
189189
}
190190
}
191+
192+
func TestIsProcessingDownstreamError(t *testing.T) {
193+
tests := []struct {
194+
name string
195+
err error
196+
expected bool
197+
}{
198+
{
199+
name: "ErrorInputFieldsWithoutRows returns true",
200+
err: data.ErrorInputFieldsWithoutRows,
201+
expected: true,
202+
},
203+
{
204+
name: "ErrorSeriesUnsorted returns true",
205+
err: data.ErrorSeriesUnsorted,
206+
expected: true,
207+
},
208+
{
209+
name: "ErrorNullTimeValues returns true",
210+
err: data.ErrorNullTimeValues,
211+
expected: true,
212+
},
213+
{
214+
name: "Different error returns false",
215+
err: errors.New("some other error"),
216+
expected: false,
217+
},
218+
{
219+
name: "Wrapped downstream error returns true",
220+
err: fmt.Errorf("wrapped: %w", data.ErrorInputFieldsWithoutRows),
221+
expected: true,
222+
},
223+
}
224+
225+
for _, tt := range tests {
226+
t.Run(tt.name, func(t *testing.T) {
227+
result := isProcessingDownstreamError(tt.err)
228+
if result != tt.expected {
229+
t.Errorf("isProcessingDownstreamError(%v) = %v; want %v", tt.err, result, tt.expected)
230+
}
231+
})
232+
}
233+
}

0 commit comments

Comments
 (0)