Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

error source #103

Merged
merged 6 commits into from
Oct 27, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .drone.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ services:

steps:
- name: "test"
image: golang:1.20
image: golang:1.21
commands:
- go test ./...
- name: "integraiton_tests"
image: golang:1.20
image: golang:1.21
environment:
INTEGRATION_TESTS: "true"
MYSQL_URL: "mysql:mysql@tcp(mysql:3306)/mysql"
Expand Down
8 changes: 4 additions & 4 deletions datasource.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,9 +135,11 @@ func (ds *SQLDatasource) QueryData(ctx context.Context, req *backend.QueryDataRe
}
}
}

response.Set(query.RefID, backend.DataResponse{
Frames: frames,
Error: err,
Frames: frames,
Error: err,
ErrorSource: ErrorSource(err),
})

wg.Done()
Expand Down Expand Up @@ -241,8 +243,6 @@ func (ds *SQLDatasource) handleQuery(ctx context.Context, req backend.DataQuery,
return res, nil
}

// err = Unwrap(err)

if errors.Is(err, ErrorNoResults) {
return res, nil
}
Expand Down
54 changes: 54 additions & 0 deletions datasource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,60 @@ func Test_error_retries(t *testing.T) {

}

func Test_error_source(t *testing.T) {
testCounter = 0
dsUID := "error"
settings := backend.DataSourceInstanceSettings{UID: dsUID}

handler := &testSqlHandler{
error: errors.New("foo"),
}
mockDriver := "sqlmock-error-source"
mock.RegisterDriver(mockDriver, handler)

errorDriver := fakeDriver{
openDBfn: func(msg json.RawMessage) (*sql.DB, error) {
db, err := sql.Open(mockDriver, "")
if err != nil {
t.Errorf("failed to connect to mock driver: %v", err)
}
return db, errors.New("foo")
},
}
retries := 0
max := time.Duration(10) * time.Second
driverSettings := DriverSettings{Retries: retries, Timeout: max, Pause: 1, RetryOn: []string{"foo"}}
ds := &SQLDatasource{c: errorDriver, driverSettings: driverSettings}

key := defaultKey(dsUID)
// Add the mandatory default db
db, _ := errorDriver.Connect(context.Background(), settings, nil)
ds.storeDBConnection(key, dbConnection{db, settings})
ctx := context.Background()

qry := `{ "rawSql": "foo" }`

req := &backend.QueryDataRequest{
PluginContext: backend.PluginContext{
DataSourceInstanceSettings: &settings,
},
Queries: []backend.DataQuery{
{
RefID: "foo",
JSON: []byte(qry),
},
},
}

data, err := ds.QueryData(ctx, req)
assert.Nil(t, err)
assert.NotNil(t, data.Responses)

res := data.Responses["foo"]
assert.NotNil(t, res.Error)
assert.Equal(t, backend.ErrorSourceDownstream, res.ErrorSource)
}

func Test_query_apply_headers(t *testing.T) {
testCounter = 0
dsUID := "headers"
Expand Down
11 changes: 11 additions & 0 deletions errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package sqlds
import (
"errors"

"github.com/grafana/grafana-plugin-sdk-go/backend"
es "github.com/grafana/grafana-plugin-sdk-go/experimental/errorsource"
)

Expand All @@ -26,3 +27,13 @@ func PluginError(err error, override ...bool) error {
func DownstreamError(err error, override ...bool) error {
return es.DownstreamError(err, len(override) > 0)
}

type Error es.Error

func ErrorSource(err error) backend.ErrorSource {
var se es.Error
if errors.As(err, &se) {
return se.Source()
}
return backend.ErrorSourcePlugin
}
43 changes: 22 additions & 21 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
module github.com/grafana/sqlds/v3

go 1.20
go 1.21

toolchain go1.21.1

require (
github.com/go-sql-driver/mysql v1.4.0
github.com/google/go-cmp v0.5.9
github.com/grafana/grafana-plugin-sdk-go v0.184.0
github.com/grafana/grafana-plugin-sdk-go v0.188.3
github.com/mithrandie/csvq-driver v1.6.8
github.com/stretchr/testify v1.8.3
github.com/stretchr/testify v1.8.4
)

require (
cloud.google.com/go/compute/metadata v0.2.3 // indirect
github.com/BurntSushi/toml v1.2.1 // indirect
github.com/BurntSushi/toml v1.3.2 // indirect
github.com/apache/arrow/go/v13 v13.0.0 // indirect
github.com/cenkalti/backoff/v4 v4.2.1 // indirect
github.com/chromedp/cdproto v0.0.0-20220208224320-6efb837e6bc2 // indirect
Expand All @@ -38,18 +40,17 @@ require (
github.com/unknwon/bra v0.0.0-20200517080246-1e3013ecaff8 // indirect
github.com/unknwon/com v1.0.1 // indirect
github.com/unknwon/log v0.0.0-20150304194804-e617c87089d3 // indirect
github.com/urfave/cli v1.22.12 // indirect
github.com/urfave/cli v1.22.14 // indirect
github.com/zeebo/xxh3 v1.0.2 // indirect
go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc v0.42.0 // indirect
go.opentelemetry.io/contrib/instrumentation/net/http/httptrace/otelhttptrace v0.42.0 // indirect
go.opentelemetry.io/contrib/propagators/jaeger v1.17.0 // indirect
go.opentelemetry.io/otel v1.16.0 // indirect
go.opentelemetry.io/otel/exporters/otlp/internal/retry v1.16.0 // indirect
go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.16.0 // indirect
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc v1.16.0 // indirect
go.opentelemetry.io/otel/metric v1.16.0 // indirect
go.opentelemetry.io/otel/sdk v1.16.0 // indirect
go.opentelemetry.io/otel/trace v1.16.0 // indirect
go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc v0.45.0 // indirect
go.opentelemetry.io/contrib/instrumentation/net/http/httptrace/otelhttptrace v0.45.0 // indirect
go.opentelemetry.io/contrib/propagators/jaeger v1.20.0 // indirect
go.opentelemetry.io/otel v1.19.0 // indirect
go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.19.0 // indirect
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc v1.19.0 // indirect
go.opentelemetry.io/otel/metric v1.19.0 // indirect
go.opentelemetry.io/otel/sdk v1.19.0 // indirect
go.opentelemetry.io/otel/trace v1.19.0 // indirect
go.opentelemetry.io/proto/otlp v1.0.0 // indirect
golang.org/x/mod v0.9.0 // indirect
golang.org/x/tools v0.6.0 // indirect
Expand Down Expand Up @@ -94,15 +95,15 @@ require (
github.com/prometheus/client_model v0.3.0 // indirect
github.com/prometheus/common v0.42.0 // indirect
github.com/prometheus/procfs v0.8.0 // indirect
golang.org/x/crypto v0.11.0 // indirect
golang.org/x/crypto v0.14.0 // indirect
golang.org/x/exp v0.0.0-20230307190834-24139beb5833
golang.org/x/net v0.12.0 // indirect
golang.org/x/sys v0.12.0 // indirect
golang.org/x/term v0.10.0 // indirect
golang.org/x/text v0.11.0 // indirect
golang.org/x/net v0.17.0 // indirect
golang.org/x/sys v0.13.0 // indirect
golang.org/x/term v0.13.0 // indirect
golang.org/x/text v0.13.0 // indirect
golang.org/x/xerrors v0.0.0-20220907171357-04be3eba64a2 // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/grpc v1.57.0 // indirect
google.golang.org/grpc v1.58.2 // indirect
google.golang.org/protobuf v1.31.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
Loading