Skip to content

Commit

Permalink
auto handling
Browse files Browse the repository at this point in the history
  • Loading branch information
andresmgot committed Sep 5, 2024
1 parent ad1d39b commit 4511493
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 10 deletions.
15 changes: 12 additions & 3 deletions backend/data_adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@ import (
"fmt"

"github.com/grafana/grafana-plugin-sdk-go/genproto/pluginv2"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)

// dataSDKAdapter adapter between low level plugin protocol and SDK interfaces.
type dataSDKAdapter struct {
queryDataHandler QueryDataHandler
queryConversionHandler QueryConversionHandler // Optional
queryConversionHandler QueryConversionHandler
}

func newDataSDKAdapter(handler QueryDataHandler, queryConversionHandler QueryConversionHandler) *dataSDKAdapter {
Expand Down Expand Up @@ -44,9 +46,16 @@ func (a *dataSDKAdapter) QueryData(ctx context.Context, req *pluginv2.QueryDataR
ctx = withHeaderMiddleware(ctx, parsedReq.GetHTTPHeaders())
var innerErr error
if a.queryConversionHandler != nil && GrafanaConfigFromContext(ctx).FeatureToggles().IsEnabled("dsQueryConvert") {
parsedReq, innerErr = a.ConvertQueryData(ctx, parsedReq)
convertedQuery, innerErr := a.ConvertQueryData(ctx, parsedReq)
if innerErr != nil {
return RequestStatusError, innerErr
if status.Code(innerErr) == codes.Unimplemented {
// The plugin does not implement query migration, disabling it
a.queryConversionHandler = nil
} else {
return RequestStatusError, innerErr
}
} else {
parsedReq = convertedQuery
}
}
resp, innerErr = a.queryDataHandler.QueryData(ctx, parsedReq)
Expand Down
10 changes: 8 additions & 2 deletions backend/data_adapter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"google.golang.org/grpc/metadata"

"github.com/grafana/grafana-plugin-sdk-go/backend/httpclient"
"github.com/grafana/grafana-plugin-sdk-go/experimental/featuretoggles"
"github.com/grafana/grafana-plugin-sdk-go/genproto/pluginv2"
"github.com/grafana/grafana-plugin-sdk-go/internal/tenant"
)
Expand Down Expand Up @@ -246,8 +247,13 @@ func TestQueryData(t *testing.T) {
return &QueryConversionResponse{Queries: req.Queries}, nil
}))
_, err := a.QueryData(context.Background(), &pluginv2.QueryDataRequest{
PluginContext: &pluginv2.PluginContext{},
Queries: []*pluginv2.DataQuery{oldQuery},
PluginContext: &pluginv2.PluginContext{
// Enable feature flag
GrafanaConfig: map[string]string{
featuretoggles.EnabledFeatures: "dsQueryConvert",
},
},
Queries: []*pluginv2.DataQuery{oldQuery},
})
require.NoError(t, err)
})
Expand Down
5 changes: 1 addition & 4 deletions backend/datasource/manage.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,6 @@ type ManageOpts struct {

// Stateless conversion handler
ConversionHandler backend.ConversionHandler

// Stateless query conversion handler
QueryConversionHandler backend.QueryConversionHandler
}

// Manage starts serving the data source over gPRC with automatic instance management.
Expand All @@ -52,9 +49,9 @@ func Manage(pluginID string, instanceFactory InstanceFactoryFunc, opts ManageOpt
CallResourceHandler: handler,
QueryDataHandler: handler,
StreamHandler: handler,
QueryConversionHandler: handler,
AdmissionHandler: opts.AdmissionHandler,
GRPCSettings: opts.GRPCSettings,
ConversionHandler: opts.ConversionHandler,
QueryConversionHandler: opts.QueryConversionHandler,
})
}
2 changes: 1 addition & 1 deletion backend/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ func GRPCServeOpts(opts ServeOpts) grpcplugin.ServeOpts {
pluginOpts.AdmissionServer = newAdmissionSDKAdapter(opts.AdmissionHandler)
}

if opts.ConversionHandler != nil || opts.QueryConversionHandler != nil {
if opts.ConversionHandler != nil {
pluginOpts.ConversionServer = newConversionSDKAdapter(opts.ConversionHandler)
}
return pluginOpts
Expand Down
11 changes: 11 additions & 0 deletions internal/automanagement/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,3 +92,14 @@ func (m *Manager) RunStream(ctx context.Context, req *backend.RunStreamRequest,
}
return status.Error(codes.Unimplemented, "unimplemented")
}

func (m *Manager) ConvertQuery(ctx context.Context, req *backend.QueryConversionRequest) (*backend.QueryConversionResponse, error) {
h, err := m.Get(ctx, req.PluginContext)
if err != nil {
return nil, err
}
if ds, ok := h.(backend.QueryConversionHandler); ok {
return ds.ConvertQuery(ctx, req)
}
return nil, status.Error(codes.Unimplemented, "unimplemented")
}

0 comments on commit 4511493

Please sign in to comment.