mirror of
https://github.com/grafana/grafana.git
synced 2025-08-01 18:26:52 +08:00
Chore: Refactor backend plugin errors (#74928)
This commit is contained in:

committed by
GitHub

parent
38d2357bb8
commit
1714fa598c
@ -5,6 +5,7 @@ import (
|
||||
|
||||
"github.com/grafana/grafana-plugin-sdk-go/backend"
|
||||
|
||||
"github.com/grafana/grafana/pkg/plugins"
|
||||
"github.com/grafana/grafana/pkg/plugins/backendplugin"
|
||||
"github.com/grafana/grafana/pkg/plugins/log"
|
||||
)
|
||||
@ -70,7 +71,7 @@ func (cp *corePlugin) Target() backendplugin.Target {
|
||||
}
|
||||
|
||||
func (cp *corePlugin) CollectMetrics(_ context.Context, _ *backend.CollectMetricsRequest) (*backend.CollectMetricsResult, error) {
|
||||
return nil, backendplugin.ErrMethodNotImplemented
|
||||
return nil, plugins.ErrMethodNotImplemented
|
||||
}
|
||||
|
||||
func (cp *corePlugin) CheckHealth(ctx context.Context, req *backend.CheckHealthRequest) (*backend.CheckHealthResult, error) {
|
||||
@ -78,7 +79,7 @@ func (cp *corePlugin) CheckHealth(ctx context.Context, req *backend.CheckHealthR
|
||||
return cp.CheckHealthHandler.CheckHealth(ctx, req)
|
||||
}
|
||||
|
||||
return nil, backendplugin.ErrMethodNotImplemented
|
||||
return nil, plugins.ErrMethodNotImplemented
|
||||
}
|
||||
|
||||
func (cp *corePlugin) QueryData(ctx context.Context, req *backend.QueryDataRequest) (*backend.QueryDataResponse, error) {
|
||||
@ -86,7 +87,7 @@ func (cp *corePlugin) QueryData(ctx context.Context, req *backend.QueryDataReque
|
||||
return cp.QueryDataHandler.QueryData(ctx, req)
|
||||
}
|
||||
|
||||
return nil, backendplugin.ErrMethodNotImplemented
|
||||
return nil, plugins.ErrMethodNotImplemented
|
||||
}
|
||||
|
||||
func (cp *corePlugin) CallResource(ctx context.Context, req *backend.CallResourceRequest, sender backend.CallResourceResponseSender) error {
|
||||
@ -94,26 +95,26 @@ func (cp *corePlugin) CallResource(ctx context.Context, req *backend.CallResourc
|
||||
return cp.CallResourceHandler.CallResource(ctx, req, sender)
|
||||
}
|
||||
|
||||
return backendplugin.ErrMethodNotImplemented
|
||||
return plugins.ErrMethodNotImplemented
|
||||
}
|
||||
|
||||
func (cp *corePlugin) SubscribeStream(ctx context.Context, req *backend.SubscribeStreamRequest) (*backend.SubscribeStreamResponse, error) {
|
||||
if cp.StreamHandler != nil {
|
||||
return cp.StreamHandler.SubscribeStream(ctx, req)
|
||||
}
|
||||
return nil, backendplugin.ErrMethodNotImplemented
|
||||
return nil, plugins.ErrMethodNotImplemented
|
||||
}
|
||||
|
||||
func (cp *corePlugin) PublishStream(ctx context.Context, req *backend.PublishStreamRequest) (*backend.PublishStreamResponse, error) {
|
||||
if cp.StreamHandler != nil {
|
||||
return cp.StreamHandler.PublishStream(ctx, req)
|
||||
}
|
||||
return nil, backendplugin.ErrMethodNotImplemented
|
||||
return nil, plugins.ErrMethodNotImplemented
|
||||
}
|
||||
|
||||
func (cp *corePlugin) RunStream(ctx context.Context, req *backend.RunStreamRequest, sender *backend.StreamSender) error {
|
||||
if cp.StreamHandler != nil {
|
||||
return cp.StreamHandler.RunStream(ctx, req, sender)
|
||||
}
|
||||
return backendplugin.ErrMethodNotImplemented
|
||||
return plugins.ErrMethodNotImplemented
|
||||
}
|
||||
|
@ -5,7 +5,7 @@ import (
|
||||
"testing"
|
||||
|
||||
"github.com/grafana/grafana-plugin-sdk-go/backend"
|
||||
"github.com/grafana/grafana/pkg/plugins/backendplugin"
|
||||
"github.com/grafana/grafana/pkg/plugins"
|
||||
"github.com/grafana/grafana/pkg/plugins/backendplugin/coreplugin"
|
||||
"github.com/grafana/grafana/pkg/plugins/log"
|
||||
"github.com/stretchr/testify/require"
|
||||
@ -23,13 +23,13 @@ func TestCorePlugin(t *testing.T) {
|
||||
require.False(t, p.Exited())
|
||||
|
||||
_, err = p.CollectMetrics(context.Background(), &backend.CollectMetricsRequest{})
|
||||
require.Equal(t, backendplugin.ErrMethodNotImplemented, err)
|
||||
require.Equal(t, plugins.ErrMethodNotImplemented, err)
|
||||
|
||||
_, err = p.CheckHealth(context.Background(), nil)
|
||||
require.Equal(t, backendplugin.ErrMethodNotImplemented, err)
|
||||
require.Equal(t, plugins.ErrMethodNotImplemented, err)
|
||||
|
||||
err = p.CallResource(context.Background(), nil, nil)
|
||||
require.Equal(t, backendplugin.ErrMethodNotImplemented, err)
|
||||
require.Equal(t, plugins.ErrMethodNotImplemented, err)
|
||||
})
|
||||
|
||||
t.Run("New core plugin with handlers set in opts should return expected values", func(t *testing.T) {
|
||||
@ -56,7 +56,7 @@ func TestCorePlugin(t *testing.T) {
|
||||
require.False(t, p.Exited())
|
||||
|
||||
_, err = p.CollectMetrics(context.Background(), &backend.CollectMetricsRequest{})
|
||||
require.Equal(t, backendplugin.ErrMethodNotImplemented, err)
|
||||
require.Equal(t, plugins.ErrMethodNotImplemented, err)
|
||||
|
||||
_, err = p.CheckHealth(context.Background(), &backend.CheckHealthRequest{})
|
||||
require.NoError(t, err)
|
||||
|
@ -1,12 +0,0 @@
|
||||
package backendplugin
|
||||
|
||||
import "errors"
|
||||
|
||||
var (
|
||||
// ErrPluginNotRegistered error returned when plugin is not registered.
|
||||
ErrPluginNotRegistered = errors.New("plugin not registered")
|
||||
// ErrPluginUnavailable error returned when plugin is unavailable.
|
||||
ErrPluginUnavailable = errors.New("plugin unavailable")
|
||||
// ErrMethodNotImplemented error returned when plugin method not implemented.
|
||||
ErrMethodNotImplemented = errors.New("method not implemented")
|
||||
)
|
@ -13,7 +13,7 @@ import (
|
||||
"google.golang.org/grpc/codes"
|
||||
"google.golang.org/grpc/status"
|
||||
|
||||
"github.com/grafana/grafana/pkg/plugins/backendplugin"
|
||||
"github.com/grafana/grafana/pkg/plugins"
|
||||
"github.com/grafana/grafana/pkg/plugins/backendplugin/pluginextensionv2"
|
||||
"github.com/grafana/grafana/pkg/plugins/backendplugin/secretsmanagerplugin"
|
||||
"github.com/grafana/grafana/pkg/plugins/log"
|
||||
@ -130,7 +130,7 @@ func (c *ClientV2) CollectMetrics(ctx context.Context, req *backend.CollectMetri
|
||||
|
||||
func (c *ClientV2) CheckHealth(ctx context.Context, req *backend.CheckHealthRequest) (*backend.CheckHealthResult, error) {
|
||||
if c.DiagnosticsClient == nil {
|
||||
return nil, backendplugin.ErrMethodNotImplemented
|
||||
return nil, plugins.ErrMethodNotImplemented
|
||||
}
|
||||
|
||||
protoContext := backend.ToProto().PluginContext(req.PluginContext)
|
||||
@ -151,7 +151,7 @@ func (c *ClientV2) CheckHealth(ctx context.Context, req *backend.CheckHealthRequ
|
||||
|
||||
func (c *ClientV2) QueryData(ctx context.Context, req *backend.QueryDataRequest) (*backend.QueryDataResponse, error) {
|
||||
if c.DataClient == nil {
|
||||
return nil, backendplugin.ErrMethodNotImplemented
|
||||
return nil, plugins.ErrMethodNotImplemented
|
||||
}
|
||||
|
||||
protoReq := backend.ToProto().QueryDataRequest(req)
|
||||
@ -159,7 +159,7 @@ func (c *ClientV2) QueryData(ctx context.Context, req *backend.QueryDataRequest)
|
||||
|
||||
if err != nil {
|
||||
if status.Code(err) == codes.Unimplemented {
|
||||
return nil, backendplugin.ErrMethodNotImplemented
|
||||
return nil, plugins.ErrMethodNotImplemented
|
||||
}
|
||||
|
||||
return nil, fmt.Errorf("%v: %w", "Failed to query data", err)
|
||||
@ -170,14 +170,14 @@ func (c *ClientV2) QueryData(ctx context.Context, req *backend.QueryDataRequest)
|
||||
|
||||
func (c *ClientV2) CallResource(ctx context.Context, req *backend.CallResourceRequest, sender backend.CallResourceResponseSender) error {
|
||||
if c.ResourceClient == nil {
|
||||
return backendplugin.ErrMethodNotImplemented
|
||||
return plugins.ErrMethodNotImplemented
|
||||
}
|
||||
|
||||
protoReq := backend.ToProto().CallResourceRequest(req)
|
||||
protoStream, err := c.ResourceClient.CallResource(ctx, protoReq)
|
||||
if err != nil {
|
||||
if status.Code(err) == codes.Unimplemented {
|
||||
return backendplugin.ErrMethodNotImplemented
|
||||
return plugins.ErrMethodNotImplemented
|
||||
}
|
||||
|
||||
return fmt.Errorf("%v: %w", "Failed to call resource", err)
|
||||
@ -187,7 +187,7 @@ func (c *ClientV2) CallResource(ctx context.Context, req *backend.CallResourceRe
|
||||
protoResp, err := protoStream.Recv()
|
||||
if err != nil {
|
||||
if status.Code(err) == codes.Unimplemented {
|
||||
return backendplugin.ErrMethodNotImplemented
|
||||
return plugins.ErrMethodNotImplemented
|
||||
}
|
||||
|
||||
if errors.Is(err, io.EOF) {
|
||||
@ -205,7 +205,7 @@ func (c *ClientV2) CallResource(ctx context.Context, req *backend.CallResourceRe
|
||||
|
||||
func (c *ClientV2) SubscribeStream(ctx context.Context, req *backend.SubscribeStreamRequest) (*backend.SubscribeStreamResponse, error) {
|
||||
if c.StreamClient == nil {
|
||||
return nil, backendplugin.ErrMethodNotImplemented
|
||||
return nil, plugins.ErrMethodNotImplemented
|
||||
}
|
||||
protoResp, err := c.StreamClient.SubscribeStream(ctx, backend.ToProto().SubscribeStreamRequest(req))
|
||||
if err != nil {
|
||||
@ -216,7 +216,7 @@ func (c *ClientV2) SubscribeStream(ctx context.Context, req *backend.SubscribeSt
|
||||
|
||||
func (c *ClientV2) PublishStream(ctx context.Context, req *backend.PublishStreamRequest) (*backend.PublishStreamResponse, error) {
|
||||
if c.StreamClient == nil {
|
||||
return nil, backendplugin.ErrMethodNotImplemented
|
||||
return nil, plugins.ErrMethodNotImplemented
|
||||
}
|
||||
protoResp, err := c.StreamClient.PublishStream(ctx, backend.ToProto().PublishStreamRequest(req))
|
||||
if err != nil {
|
||||
@ -227,14 +227,14 @@ func (c *ClientV2) PublishStream(ctx context.Context, req *backend.PublishStream
|
||||
|
||||
func (c *ClientV2) RunStream(ctx context.Context, req *backend.RunStreamRequest, sender *backend.StreamSender) error {
|
||||
if c.StreamClient == nil {
|
||||
return backendplugin.ErrMethodNotImplemented
|
||||
return plugins.ErrMethodNotImplemented
|
||||
}
|
||||
|
||||
protoReq := backend.ToProto().RunStreamRequest(req)
|
||||
protoStream, err := c.StreamClient.RunStream(ctx, protoReq)
|
||||
if err != nil {
|
||||
if status.Code(err) == codes.Unimplemented {
|
||||
return backendplugin.ErrMethodNotImplemented
|
||||
return plugins.ErrMethodNotImplemented
|
||||
}
|
||||
return fmt.Errorf("%v: %w", "Failed to call resource", err)
|
||||
}
|
||||
@ -243,7 +243,7 @@ func (c *ClientV2) RunStream(ctx context.Context, req *backend.RunStreamRequest,
|
||||
p, err := protoStream.Recv()
|
||||
if err != nil {
|
||||
if status.Code(err) == codes.Unimplemented {
|
||||
return backendplugin.ErrMethodNotImplemented
|
||||
return plugins.ErrMethodNotImplemented
|
||||
}
|
||||
if errors.Is(err, io.EOF) {
|
||||
return nil
|
||||
|
@ -9,6 +9,7 @@ import (
|
||||
"github.com/hashicorp/go-plugin"
|
||||
|
||||
"github.com/grafana/grafana/pkg/infra/process"
|
||||
"github.com/grafana/grafana/pkg/plugins"
|
||||
"github.com/grafana/grafana/pkg/plugins/backendplugin"
|
||||
"github.com/grafana/grafana/pkg/plugins/log"
|
||||
)
|
||||
@ -141,7 +142,7 @@ func (p *grpcPlugin) getPluginClient() (pluginClient, bool) {
|
||||
func (p *grpcPlugin) CollectMetrics(ctx context.Context, req *backend.CollectMetricsRequest) (*backend.CollectMetricsResult, error) {
|
||||
pluginClient, ok := p.getPluginClient()
|
||||
if !ok {
|
||||
return nil, backendplugin.ErrPluginUnavailable
|
||||
return nil, plugins.ErrPluginUnavailable
|
||||
}
|
||||
return pluginClient.CollectMetrics(ctx, req)
|
||||
}
|
||||
@ -149,7 +150,7 @@ func (p *grpcPlugin) CollectMetrics(ctx context.Context, req *backend.CollectMet
|
||||
func (p *grpcPlugin) CheckHealth(ctx context.Context, req *backend.CheckHealthRequest) (*backend.CheckHealthResult, error) {
|
||||
pluginClient, ok := p.getPluginClient()
|
||||
if !ok {
|
||||
return nil, backendplugin.ErrPluginUnavailable
|
||||
return nil, plugins.ErrPluginUnavailable
|
||||
}
|
||||
return pluginClient.CheckHealth(ctx, req)
|
||||
}
|
||||
@ -157,7 +158,7 @@ func (p *grpcPlugin) CheckHealth(ctx context.Context, req *backend.CheckHealthRe
|
||||
func (p *grpcPlugin) QueryData(ctx context.Context, req *backend.QueryDataRequest) (*backend.QueryDataResponse, error) {
|
||||
pluginClient, ok := p.getPluginClient()
|
||||
if !ok {
|
||||
return nil, backendplugin.ErrPluginUnavailable
|
||||
return nil, plugins.ErrPluginUnavailable
|
||||
}
|
||||
|
||||
return pluginClient.QueryData(ctx, req)
|
||||
@ -166,7 +167,7 @@ func (p *grpcPlugin) QueryData(ctx context.Context, req *backend.QueryDataReques
|
||||
func (p *grpcPlugin) CallResource(ctx context.Context, req *backend.CallResourceRequest, sender backend.CallResourceResponseSender) error {
|
||||
pluginClient, ok := p.getPluginClient()
|
||||
if !ok {
|
||||
return backendplugin.ErrPluginUnavailable
|
||||
return plugins.ErrPluginUnavailable
|
||||
}
|
||||
return pluginClient.CallResource(ctx, req, sender)
|
||||
}
|
||||
@ -174,7 +175,7 @@ func (p *grpcPlugin) CallResource(ctx context.Context, req *backend.CallResource
|
||||
func (p *grpcPlugin) SubscribeStream(ctx context.Context, request *backend.SubscribeStreamRequest) (*backend.SubscribeStreamResponse, error) {
|
||||
pluginClient, ok := p.getPluginClient()
|
||||
if !ok {
|
||||
return nil, backendplugin.ErrPluginUnavailable
|
||||
return nil, plugins.ErrPluginUnavailable
|
||||
}
|
||||
return pluginClient.SubscribeStream(ctx, request)
|
||||
}
|
||||
@ -182,7 +183,7 @@ func (p *grpcPlugin) SubscribeStream(ctx context.Context, request *backend.Subsc
|
||||
func (p *grpcPlugin) PublishStream(ctx context.Context, request *backend.PublishStreamRequest) (*backend.PublishStreamResponse, error) {
|
||||
pluginClient, ok := p.getPluginClient()
|
||||
if !ok {
|
||||
return nil, backendplugin.ErrPluginUnavailable
|
||||
return nil, plugins.ErrPluginUnavailable
|
||||
}
|
||||
return pluginClient.PublishStream(ctx, request)
|
||||
}
|
||||
@ -190,7 +191,7 @@ func (p *grpcPlugin) PublishStream(ctx context.Context, request *backend.Publish
|
||||
func (p *grpcPlugin) RunStream(ctx context.Context, req *backend.RunStreamRequest, sender *backend.StreamSender) error {
|
||||
pluginClient, ok := p.getPluginClient()
|
||||
if !ok {
|
||||
return backendplugin.ErrPluginUnavailable
|
||||
return plugins.ErrPluginUnavailable
|
||||
}
|
||||
return pluginClient.RunStream(ctx, req, sender)
|
||||
}
|
||||
|
Reference in New Issue
Block a user