mirror of
https://github.com/grafana/grafana.git
synced 2025-09-25 19:03:53 +08:00
Plugins: Add state logs for plugin client retrieval (#93630)
* add state to grpc plugin * tidy * fix lint * fix issues * return true * use defer * update err message
This commit is contained in:
@ -14,16 +14,6 @@ import (
|
||||
"github.com/grafana/grafana/pkg/plugins/log"
|
||||
)
|
||||
|
||||
type pluginClient interface {
|
||||
backend.CollectMetricsHandler
|
||||
backend.CheckHealthHandler
|
||||
backend.QueryDataHandler
|
||||
backend.CallResourceHandler
|
||||
backend.AdmissionHandler
|
||||
backend.ConversionHandler
|
||||
backend.StreamHandler
|
||||
}
|
||||
|
||||
type grpcPlugin struct {
|
||||
descriptor PluginDescriptor
|
||||
clientFactory func() *plugin.Client
|
||||
@ -32,8 +22,19 @@ type grpcPlugin struct {
|
||||
logger log.Logger
|
||||
mutex sync.RWMutex
|
||||
decommissioned bool
|
||||
state pluginState
|
||||
}
|
||||
|
||||
type pluginState int
|
||||
|
||||
const (
|
||||
pluginStateNotStarted pluginState = iota
|
||||
pluginStateStartInit
|
||||
pluginStateStartSuccess
|
||||
pluginStateStartFail
|
||||
pluginStateStopped
|
||||
)
|
||||
|
||||
// newPlugin allocates and returns a new gRPC (external) backendplugin.Plugin.
|
||||
func newPlugin(descriptor PluginDescriptor) backendplugin.PluginFactoryFunc {
|
||||
return func(pluginID string, logger log.Logger, env func() []string) (backendplugin.Plugin, error) {
|
||||
@ -48,6 +49,7 @@ func newGrpcPlugin(descriptor PluginDescriptor, logger log.Logger, env func() []
|
||||
clientFactory: func() *plugin.Client {
|
||||
return plugin.NewClient(newClientConfig(descriptor.executablePath, descriptor.executableArgs, env(), descriptor.skipHostEnvVars, logger, descriptor.versionedPlugins))
|
||||
},
|
||||
state: pluginStateNotStarted,
|
||||
}
|
||||
}
|
||||
|
||||
@ -63,21 +65,27 @@ func (p *grpcPlugin) Start(_ context.Context) error {
|
||||
p.mutex.Lock()
|
||||
defer p.mutex.Unlock()
|
||||
|
||||
p.state = pluginStateStartInit
|
||||
|
||||
p.client = p.clientFactory()
|
||||
rpcClient, err := p.client.Client()
|
||||
if err != nil {
|
||||
p.state = pluginStateStartFail
|
||||
return err
|
||||
}
|
||||
|
||||
if p.client.NegotiatedVersion() < 2 {
|
||||
p.state = pluginStateStartFail
|
||||
return errors.New("plugin protocol version not supported")
|
||||
}
|
||||
p.pluginClient, err = newClientV2(p.descriptor, p.logger, rpcClient)
|
||||
if err != nil {
|
||||
p.state = pluginStateStartFail
|
||||
return err
|
||||
}
|
||||
|
||||
if p.pluginClient == nil {
|
||||
p.state = pluginStateStartFail
|
||||
return errors.New("no compatible plugin implementation found")
|
||||
}
|
||||
|
||||
@ -89,6 +97,7 @@ func (p *grpcPlugin) Start(_ context.Context) error {
|
||||
p.logger.Warn("Plugin process is running with elevated privileges. This is not recommended")
|
||||
}
|
||||
|
||||
p.state = pluginStateStartSuccess
|
||||
return nil
|
||||
}
|
||||
|
||||
@ -99,6 +108,7 @@ func (p *grpcPlugin) Stop(_ context.Context) error {
|
||||
if p.client != nil {
|
||||
p.client.Kill()
|
||||
}
|
||||
p.state = pluginStateStopped
|
||||
return nil
|
||||
}
|
||||
|
||||
@ -134,94 +144,110 @@ func (p *grpcPlugin) Target() backendplugin.Target {
|
||||
return backendplugin.TargetLocal
|
||||
}
|
||||
|
||||
func (p *grpcPlugin) getPluginClient() (pluginClient, bool) {
|
||||
func (p *grpcPlugin) getPluginClient(ctx context.Context) (*ClientV2, bool) {
|
||||
p.mutex.RLock()
|
||||
if p.client == nil || p.client.Exited() || p.pluginClient == nil {
|
||||
p.mutex.RUnlock()
|
||||
return nil, false
|
||||
defer p.mutex.RUnlock()
|
||||
if p.client != nil && !p.client.Exited() && p.pluginClient != nil {
|
||||
return p.pluginClient, true
|
||||
}
|
||||
pluginClient := p.pluginClient
|
||||
p.mutex.RUnlock()
|
||||
return pluginClient, true
|
||||
|
||||
logger := p.Logger().FromContext(ctx)
|
||||
if p.state == pluginStateNotStarted {
|
||||
logger.Debug("Plugin client has not been started yet")
|
||||
}
|
||||
|
||||
if p.state == pluginStateStartInit {
|
||||
logger.Debug("Plugin client is starting")
|
||||
}
|
||||
|
||||
if p.state == pluginStateStartFail {
|
||||
logger.Debug("Plugin client failed to start")
|
||||
}
|
||||
|
||||
if p.state == pluginStateStopped {
|
||||
logger.Debug("Plugin client has stopped")
|
||||
}
|
||||
|
||||
return nil, false
|
||||
}
|
||||
|
||||
func (p *grpcPlugin) CollectMetrics(ctx context.Context, req *backend.CollectMetricsRequest) (*backend.CollectMetricsResult, error) {
|
||||
pluginClient, ok := p.getPluginClient()
|
||||
pc, ok := p.getPluginClient(ctx)
|
||||
if !ok {
|
||||
return nil, plugins.ErrPluginUnavailable
|
||||
}
|
||||
return pluginClient.CollectMetrics(ctx, req)
|
||||
return pc.CollectMetrics(ctx, req)
|
||||
}
|
||||
|
||||
func (p *grpcPlugin) CheckHealth(ctx context.Context, req *backend.CheckHealthRequest) (*backend.CheckHealthResult, error) {
|
||||
pluginClient, ok := p.getPluginClient()
|
||||
pc, ok := p.getPluginClient(ctx)
|
||||
if !ok {
|
||||
return nil, plugins.ErrPluginUnavailable
|
||||
}
|
||||
return pluginClient.CheckHealth(ctx, req)
|
||||
return pc.CheckHealth(ctx, req)
|
||||
}
|
||||
|
||||
func (p *grpcPlugin) QueryData(ctx context.Context, req *backend.QueryDataRequest) (*backend.QueryDataResponse, error) {
|
||||
pluginClient, ok := p.getPluginClient()
|
||||
pc, ok := p.getPluginClient(ctx)
|
||||
if !ok {
|
||||
return nil, plugins.ErrPluginUnavailable
|
||||
}
|
||||
|
||||
return pluginClient.QueryData(ctx, req)
|
||||
return pc.QueryData(ctx, req)
|
||||
}
|
||||
|
||||
func (p *grpcPlugin) CallResource(ctx context.Context, req *backend.CallResourceRequest, sender backend.CallResourceResponseSender) error {
|
||||
pluginClient, ok := p.getPluginClient()
|
||||
pc, ok := p.getPluginClient(ctx)
|
||||
if !ok {
|
||||
return plugins.ErrPluginUnavailable
|
||||
}
|
||||
return pluginClient.CallResource(ctx, req, sender)
|
||||
return pc.CallResource(ctx, req, sender)
|
||||
}
|
||||
|
||||
func (p *grpcPlugin) SubscribeStream(ctx context.Context, request *backend.SubscribeStreamRequest) (*backend.SubscribeStreamResponse, error) {
|
||||
pluginClient, ok := p.getPluginClient()
|
||||
pc, ok := p.getPluginClient(ctx)
|
||||
if !ok {
|
||||
return nil, plugins.ErrPluginUnavailable
|
||||
}
|
||||
return pluginClient.SubscribeStream(ctx, request)
|
||||
return pc.SubscribeStream(ctx, request)
|
||||
}
|
||||
|
||||
func (p *grpcPlugin) PublishStream(ctx context.Context, request *backend.PublishStreamRequest) (*backend.PublishStreamResponse, error) {
|
||||
pluginClient, ok := p.getPluginClient()
|
||||
pc, ok := p.getPluginClient(ctx)
|
||||
if !ok {
|
||||
return nil, plugins.ErrPluginUnavailable
|
||||
}
|
||||
return pluginClient.PublishStream(ctx, request)
|
||||
return pc.PublishStream(ctx, request)
|
||||
}
|
||||
|
||||
func (p *grpcPlugin) RunStream(ctx context.Context, req *backend.RunStreamRequest, sender *backend.StreamSender) error {
|
||||
pluginClient, ok := p.getPluginClient()
|
||||
pc, ok := p.getPluginClient(ctx)
|
||||
if !ok {
|
||||
return plugins.ErrPluginUnavailable
|
||||
}
|
||||
return pluginClient.RunStream(ctx, req, sender)
|
||||
return pc.RunStream(ctx, req, sender)
|
||||
}
|
||||
|
||||
func (p *grpcPlugin) ValidateAdmission(ctx context.Context, request *backend.AdmissionRequest) (*backend.ValidationResponse, error) {
|
||||
pluginClient, ok := p.getPluginClient()
|
||||
pc, ok := p.getPluginClient(ctx)
|
||||
if !ok {
|
||||
return nil, plugins.ErrPluginUnavailable
|
||||
}
|
||||
return pluginClient.ValidateAdmission(ctx, request)
|
||||
return pc.ValidateAdmission(ctx, request)
|
||||
}
|
||||
|
||||
func (p *grpcPlugin) MutateAdmission(ctx context.Context, request *backend.AdmissionRequest) (*backend.MutationResponse, error) {
|
||||
pluginClient, ok := p.getPluginClient()
|
||||
pc, ok := p.getPluginClient(ctx)
|
||||
if !ok {
|
||||
return nil, plugins.ErrPluginUnavailable
|
||||
}
|
||||
return pluginClient.MutateAdmission(ctx, request)
|
||||
return pc.MutateAdmission(ctx, request)
|
||||
}
|
||||
|
||||
func (p *grpcPlugin) ConvertObjects(ctx context.Context, request *backend.ConversionRequest) (*backend.ConversionResponse, error) {
|
||||
pluginClient, ok := p.getPluginClient()
|
||||
pc, ok := p.getPluginClient(ctx)
|
||||
if !ok {
|
||||
return nil, plugins.ErrPluginUnavailable
|
||||
}
|
||||
return pluginClient.ConvertObjects(ctx, request)
|
||||
return pc.ConvertObjects(ctx, request)
|
||||
}
|
||||
|
Reference in New Issue
Block a user